blob: a48df1449ece66248b147ec5efdbb95fc3a13980 [file] [log] [blame]
Chuck Levera246b012005-08-11 16:25:23 -04001/*
2 * linux/net/sunrpc/xprtsock.c
3 *
4 * Client-side transport implementation for sockets.
5 *
Alan Cox113aa832008-10-13 19:01:08 -07006 * TCP callback races fixes (C) 1998 Red Hat
7 * TCP send fixes (C) 1998 Red Hat
Chuck Levera246b012005-08-11 16:25:23 -04008 * TCP NFS related read + write fixes
9 * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
10 *
11 * Rewrite of larges part of the code in order to stabilize TCP stuff.
12 * Fix behaviour when socket buffer is full.
13 * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
Chuck Lever55aa4f52005-08-11 16:25:47 -040014 *
15 * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
Chuck Lever8f9d5b12007-08-06 11:57:53 -040016 *
17 * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
18 * <gilles.quillard@bull.net>
Chuck Levera246b012005-08-11 16:25:23 -040019 */
20
21#include <linux/types.h>
22#include <linux/slab.h>
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -040023#include <linux/module.h>
Chuck Levera246b012005-08-11 16:25:23 -040024#include <linux/capability.h>
Chuck Levera246b012005-08-11 16:25:23 -040025#include <linux/pagemap.h>
26#include <linux/errno.h>
27#include <linux/socket.h>
28#include <linux/in.h>
29#include <linux/net.h>
30#include <linux/mm.h>
31#include <linux/udp.h>
32#include <linux/tcp.h>
33#include <linux/sunrpc/clnt.h>
Chuck Lever02107142006-01-03 09:55:49 +010034#include <linux/sunrpc/sched.h>
\"Talpey, Thomas\49c36fc2007-09-10 13:47:31 -040035#include <linux/sunrpc/xprtsock.h>
Chuck Levera246b012005-08-11 16:25:23 -040036#include <linux/file.h>
37
38#include <net/sock.h>
39#include <net/checksum.h>
40#include <net/udp.h>
41#include <net/tcp.h>
42
Chuck Lever9903cd12005-08-11 16:25:26 -040043/*
Chuck Leverc556b752005-11-01 12:24:48 -050044 * xprtsock tunables
45 */
46unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
47unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
48
49unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
50unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
51
Trond Myklebust7d1e8252009-03-11 14:38:03 -040052#define XS_TCP_LINGER_TO (15U * HZ)
Trond Myklebust25fe6142009-03-11 14:38:03 -040053static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
Trond Myklebust7d1e8252009-03-11 14:38:03 -040054
Chuck Leverc556b752005-11-01 12:24:48 -050055/*
Chuck Leverfbf76682006-12-05 16:35:54 -050056 * We can register our own files under /proc/sys/sunrpc by
57 * calling register_sysctl_table() again. The files in that
58 * directory become the union of all files registered there.
59 *
60 * We simply need to make sure that we don't collide with
61 * someone else's file names!
62 */
63
64#ifdef RPC_DEBUG
65
66static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
67static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
68static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
69static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
70
71static struct ctl_table_header *sunrpc_table_header;
72
73/*
74 * FIXME: changing the UDP slot table size should also resize the UDP
75 * socket buffers for existing UDP transports
76 */
77static ctl_table xs_tunables_table[] = {
78 {
79 .ctl_name = CTL_SLOTTABLE_UDP,
80 .procname = "udp_slot_table_entries",
81 .data = &xprt_udp_slot_table_entries,
82 .maxlen = sizeof(unsigned int),
83 .mode = 0644,
84 .proc_handler = &proc_dointvec_minmax,
85 .strategy = &sysctl_intvec,
86 .extra1 = &min_slot_table_size,
87 .extra2 = &max_slot_table_size
88 },
89 {
90 .ctl_name = CTL_SLOTTABLE_TCP,
91 .procname = "tcp_slot_table_entries",
92 .data = &xprt_tcp_slot_table_entries,
93 .maxlen = sizeof(unsigned int),
94 .mode = 0644,
95 .proc_handler = &proc_dointvec_minmax,
96 .strategy = &sysctl_intvec,
97 .extra1 = &min_slot_table_size,
98 .extra2 = &max_slot_table_size
99 },
100 {
101 .ctl_name = CTL_MIN_RESVPORT,
102 .procname = "min_resvport",
103 .data = &xprt_min_resvport,
104 .maxlen = sizeof(unsigned int),
105 .mode = 0644,
106 .proc_handler = &proc_dointvec_minmax,
107 .strategy = &sysctl_intvec,
108 .extra1 = &xprt_min_resvport_limit,
109 .extra2 = &xprt_max_resvport_limit
110 },
111 {
112 .ctl_name = CTL_MAX_RESVPORT,
113 .procname = "max_resvport",
114 .data = &xprt_max_resvport,
115 .maxlen = sizeof(unsigned int),
116 .mode = 0644,
117 .proc_handler = &proc_dointvec_minmax,
118 .strategy = &sysctl_intvec,
119 .extra1 = &xprt_min_resvport_limit,
120 .extra2 = &xprt_max_resvport_limit
121 },
122 {
Trond Myklebust25fe6142009-03-11 14:38:03 -0400123 .procname = "tcp_fin_timeout",
124 .data = &xs_tcp_fin_timeout,
125 .maxlen = sizeof(xs_tcp_fin_timeout),
126 .mode = 0644,
127 .proc_handler = &proc_dointvec_jiffies,
128 .strategy = sysctl_jiffies
129 },
130 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500131 .ctl_name = 0,
132 },
133};
134
135static ctl_table sunrpc_table[] = {
136 {
137 .ctl_name = CTL_SUNRPC,
138 .procname = "sunrpc",
139 .mode = 0555,
140 .child = xs_tunables_table
141 },
142 {
143 .ctl_name = 0,
144 },
145};
146
147#endif
148
149/*
Chuck Lever03bf4b72005-08-25 16:25:55 -0700150 * Time out for an RPC UDP socket connect. UDP socket connects are
151 * synchronous, but we set a timeout anyway in case of resource
152 * exhaustion on the local host.
153 */
154#define XS_UDP_CONN_TO (5U * HZ)
155
156/*
157 * Wait duration for an RPC TCP connection to be established. Solaris
158 * NFS over TCP uses 60 seconds, for example, which is in line with how
159 * long a server takes to reboot.
160 */
161#define XS_TCP_CONN_TO (60U * HZ)
162
163/*
164 * Wait duration for a reply from the RPC portmapper.
165 */
166#define XS_BIND_TO (60U * HZ)
167
168/*
169 * Delay if a UDP socket connect error occurs. This is most likely some
170 * kind of resource problem on the local host.
171 */
172#define XS_UDP_REEST_TO (2U * HZ)
173
174/*
175 * The reestablish timeout allows clients to delay for a bit before attempting
176 * to reconnect to a server that just dropped our connection.
177 *
178 * We implement an exponential backoff when trying to reestablish a TCP
179 * transport connection with the server. Some servers like to drop a TCP
180 * connection when they are overworked, so we start with a short timeout and
181 * increase over time if the server is down or not responding.
182 */
183#define XS_TCP_INIT_REEST_TO (3U * HZ)
184#define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
185
186/*
187 * TCP idle timeout; client drops the transport socket if it is idle
188 * for this long. Note that we also timeout UDP sockets to prevent
189 * holding port numbers when there is no RPC traffic.
190 */
191#define XS_IDLE_DISC_TO (5U * 60 * HZ)
192
Chuck Levera246b012005-08-11 16:25:23 -0400193#ifdef RPC_DEBUG
194# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400195# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400196#endif
197
Chuck Levera246b012005-08-11 16:25:23 -0400198#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400199static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400200{
Chuck Lever9903cd12005-08-11 16:25:26 -0400201 u8 *buf = (u8 *) packet;
202 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400203
Chuck Lever46121cf2007-01-31 12:14:08 -0500204 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400205 for (j = 0; j < count && j < 128; j += 4) {
206 if (!(j & 31)) {
207 if (j)
208 dprintk("\n");
209 dprintk("0x%04x ", j);
210 }
211 dprintk("%02x%02x%02x%02x ",
212 buf[j], buf[j+1], buf[j+2], buf[j+3]);
213 }
214 dprintk("\n");
215}
216#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400217static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400218{
219 /* NOP */
220}
221#endif
222
Chuck Leverffc2e512006-12-05 16:35:11 -0500223struct sock_xprt {
224 struct rpc_xprt xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500225
226 /*
227 * Network layer
228 */
229 struct socket * sock;
230 struct sock * inet;
Chuck Lever51971132006-12-05 16:35:19 -0500231
232 /*
233 * State of TCP reply receive
234 */
235 __be32 tcp_fraghdr,
236 tcp_xid;
237
238 u32 tcp_offset,
239 tcp_reclen;
240
241 unsigned long tcp_copied,
242 tcp_flags;
Chuck Leverc8475462006-12-05 16:35:26 -0500243
244 /*
245 * Connection of transports
246 */
Trond Myklebust34161db2006-12-07 15:48:15 -0500247 struct delayed_work connect_worker;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +0200248 struct sockaddr_storage addr;
Chuck Leverc8475462006-12-05 16:35:26 -0500249 unsigned short port;
Chuck Lever7c6e0662006-12-05 16:35:30 -0500250
251 /*
252 * UDP socket buffer size parameters
253 */
254 size_t rcvsize,
255 sndsize;
Chuck Lever314dfd72006-12-05 16:35:34 -0500256
257 /*
258 * Saved socket callback addresses
259 */
260 void (*old_data_ready)(struct sock *, int);
261 void (*old_state_change)(struct sock *);
262 void (*old_write_space)(struct sock *);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400263 void (*old_error_report)(struct sock *);
Chuck Leverffc2e512006-12-05 16:35:11 -0500264};
265
Chuck Levere136d092006-12-05 16:35:23 -0500266/*
267 * TCP receive state flags
268 */
269#define TCP_RCV_LAST_FRAG (1UL << 0)
270#define TCP_RCV_COPY_FRAGHDR (1UL << 1)
271#define TCP_RCV_COPY_XID (1UL << 2)
272#define TCP_RCV_COPY_DATA (1UL << 3)
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400273#define TCP_RCV_READ_CALLDIR (1UL << 4)
274#define TCP_RCV_COPY_CALLDIR (1UL << 5)
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400275
276/*
277 * TCP RPC flags
278 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400279#define TCP_RPC_REPLY (1UL << 6)
Chuck Levere136d092006-12-05 16:35:23 -0500280
Chuck Lever95392c52007-08-06 11:57:58 -0400281static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400282{
Chuck Lever95392c52007-08-06 11:57:58 -0400283 return (struct sockaddr *) &xprt->addr;
284}
285
286static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
287{
288 return (struct sockaddr_in *) &xprt->addr;
289}
290
291static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
292{
293 return (struct sockaddr_in6 *) &xprt->addr;
294}
295
Chuck Leverb454ae92008-01-07 18:34:48 -0500296static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt,
297 const char *protocol,
298 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400299{
Chuck Lever95392c52007-08-06 11:57:58 -0400300 struct sockaddr_in *addr = xs_addr_in(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400301 char *buf;
302
303 buf = kzalloc(20, GFP_KERNEL);
304 if (buf) {
David S. Millere0db4a72008-11-02 23:57:06 -0800305 snprintf(buf, 20, "%pI4", &addr->sin_addr.s_addr);
Chuck Leveredb267a2006-08-22 20:06:18 -0400306 }
307 xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
308
309 buf = kzalloc(8, GFP_KERNEL);
310 if (buf) {
311 snprintf(buf, 8, "%u",
312 ntohs(addr->sin_port));
313 }
314 xprt->address_strings[RPC_DISPLAY_PORT] = buf;
315
Chuck Leverb454ae92008-01-07 18:34:48 -0500316 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leveredb267a2006-08-22 20:06:18 -0400317
318 buf = kzalloc(48, GFP_KERNEL);
319 if (buf) {
Harvey Harrison21454aa2008-10-31 00:54:56 -0700320 snprintf(buf, 48, "addr=%pI4 port=%u proto=%s",
321 &addr->sin_addr.s_addr,
Chuck Leveredb267a2006-08-22 20:06:18 -0400322 ntohs(addr->sin_port),
Chuck Leverb454ae92008-01-07 18:34:48 -0500323 protocol);
Chuck Leveredb267a2006-08-22 20:06:18 -0400324 }
325 xprt->address_strings[RPC_DISPLAY_ALL] = buf;
Chuck Leverfbfe3cc2007-08-06 11:57:02 -0400326
327 buf = kzalloc(10, GFP_KERNEL);
328 if (buf) {
329 snprintf(buf, 10, "%02x%02x%02x%02x",
330 NIPQUAD(addr->sin_addr.s_addr));
331 }
332 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
333
334 buf = kzalloc(8, GFP_KERNEL);
335 if (buf) {
336 snprintf(buf, 8, "%4hx",
337 ntohs(addr->sin_port));
338 }
339 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
Chuck Lever756805e2007-08-16 16:03:26 -0400340
341 buf = kzalloc(30, GFP_KERNEL);
342 if (buf) {
Harvey Harrison21454aa2008-10-31 00:54:56 -0700343 snprintf(buf, 30, "%pI4.%u.%u",
344 &addr->sin_addr.s_addr,
Chuck Lever756805e2007-08-16 16:03:26 -0400345 ntohs(addr->sin_port) >> 8,
346 ntohs(addr->sin_port) & 0xff);
347 }
348 xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
\"Talpey, Thomas\4417c8c2007-09-10 13:43:05 -0400349
Chuck Leverb454ae92008-01-07 18:34:48 -0500350 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leveredb267a2006-08-22 20:06:18 -0400351}
352
Chuck Leverb454ae92008-01-07 18:34:48 -0500353static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt,
354 const char *protocol,
355 const char *netid)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400356{
Chuck Lever95392c52007-08-06 11:57:58 -0400357 struct sockaddr_in6 *addr = xs_addr_in6(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400358 char *buf;
359
360 buf = kzalloc(40, GFP_KERNEL);
361 if (buf) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700362 snprintf(buf, 40, "%pI6",&addr->sin6_addr);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400363 }
364 xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
365
366 buf = kzalloc(8, GFP_KERNEL);
367 if (buf) {
368 snprintf(buf, 8, "%u",
369 ntohs(addr->sin6_port));
370 }
371 xprt->address_strings[RPC_DISPLAY_PORT] = buf;
372
Chuck Leverb454ae92008-01-07 18:34:48 -0500373 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Lever4b6473f2007-08-06 11:57:12 -0400374
375 buf = kzalloc(64, GFP_KERNEL);
376 if (buf) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700377 snprintf(buf, 64, "addr=%pI6 port=%u proto=%s",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -0700378 &addr->sin6_addr,
Chuck Lever4b6473f2007-08-06 11:57:12 -0400379 ntohs(addr->sin6_port),
Chuck Leverb454ae92008-01-07 18:34:48 -0500380 protocol);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400381 }
382 xprt->address_strings[RPC_DISPLAY_ALL] = buf;
383
384 buf = kzalloc(36, GFP_KERNEL);
Harvey Harrisonb0711952008-10-28 16:05:40 -0700385 if (buf)
Harvey Harrison4b7a4272008-10-29 12:50:24 -0700386 snprintf(buf, 36, "%pi6", &addr->sin6_addr);
Harvey Harrisonb0711952008-10-28 16:05:40 -0700387
Chuck Lever4b6473f2007-08-06 11:57:12 -0400388 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
389
390 buf = kzalloc(8, GFP_KERNEL);
391 if (buf) {
392 snprintf(buf, 8, "%4hx",
393 ntohs(addr->sin6_port));
394 }
395 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
Chuck Lever756805e2007-08-16 16:03:26 -0400396
397 buf = kzalloc(50, GFP_KERNEL);
398 if (buf) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700399 snprintf(buf, 50, "%pI6.%u.%u",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -0700400 &addr->sin6_addr,
401 ntohs(addr->sin6_port) >> 8,
402 ntohs(addr->sin6_port) & 0xff);
Chuck Lever756805e2007-08-16 16:03:26 -0400403 }
404 xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
\"Talpey, Thomas\4417c8c2007-09-10 13:43:05 -0400405
Chuck Leverb454ae92008-01-07 18:34:48 -0500406 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leveredb267a2006-08-22 20:06:18 -0400407}
408
409static void xs_free_peer_addresses(struct rpc_xprt *xprt)
410{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500411 unsigned int i;
412
413 for (i = 0; i < RPC_DISPLAY_MAX; i++)
414 switch (i) {
415 case RPC_DISPLAY_PROTO:
416 case RPC_DISPLAY_NETID:
417 continue;
418 default:
419 kfree(xprt->address_strings[i]);
420 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400421}
422
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400423#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
424
Trond Myklebust24c56842006-10-17 15:06:22 -0400425static 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 -0400426{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400427 struct msghdr msg = {
428 .msg_name = addr,
429 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400430 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
431 };
432 struct kvec iov = {
433 .iov_base = vec->iov_base + base,
434 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400435 };
436
Trond Myklebust24c56842006-10-17 15:06:22 -0400437 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400438 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
439 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
440}
441
Trond Myklebust24c56842006-10-17 15:06:22 -0400442static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400443{
Trond Myklebust24c56842006-10-17 15:06:22 -0400444 struct page **ppage;
445 unsigned int remainder;
446 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400447
Trond Myklebust24c56842006-10-17 15:06:22 -0400448 remainder = xdr->page_len - base;
449 base += xdr->page_base;
450 ppage = xdr->pages + (base >> PAGE_SHIFT);
451 base &= ~PAGE_MASK;
452 for(;;) {
453 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
454 int flags = XS_SENDMSG_FLAGS;
455
456 remainder -= len;
457 if (remainder != 0 || more)
458 flags |= MSG_MORE;
459 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
460 if (remainder == 0 || err != len)
461 break;
462 sent += err;
463 ppage++;
464 base = 0;
465 }
466 if (sent == 0)
467 return err;
468 if (err > 0)
469 sent += err;
470 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400471}
472
Chuck Lever9903cd12005-08-11 16:25:26 -0400473/**
474 * xs_sendpages - write pages directly to a socket
475 * @sock: socket to send on
476 * @addr: UDP only -- address of destination
477 * @addrlen: UDP only -- length of destination address
478 * @xdr: buffer containing this request
479 * @base: starting position in the buffer
480 *
Chuck Levera246b012005-08-11 16:25:23 -0400481 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400482static 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 -0400483{
Trond Myklebust24c56842006-10-17 15:06:22 -0400484 unsigned int remainder = xdr->len - base;
485 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400486
Chuck Lever262965f2005-08-11 16:25:56 -0400487 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400488 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400489
490 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400491 if (base != 0) {
492 addr = NULL;
493 addrlen = 0;
494 }
Chuck Lever262965f2005-08-11 16:25:56 -0400495
Trond Myklebust24c56842006-10-17 15:06:22 -0400496 if (base < xdr->head[0].iov_len || addr != NULL) {
497 unsigned int len = xdr->head[0].iov_len - base;
498 remainder -= len;
499 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
500 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400501 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400502 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400503 base = 0;
504 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400505 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400506
Trond Myklebust24c56842006-10-17 15:06:22 -0400507 if (base < xdr->page_len) {
508 unsigned int len = xdr->page_len - base;
509 remainder -= len;
510 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
511 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400512 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400513 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400514 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400515 } else
516 base -= xdr->page_len;
517
518 if (base >= xdr->tail[0].iov_len)
519 return sent;
520 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400521out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400522 if (sent == 0)
523 return err;
524 if (err > 0)
525 sent += err;
526 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400527}
528
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400529static void xs_nospace_callback(struct rpc_task *task)
530{
531 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
532
533 transport->inet->sk_write_pending--;
534 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
535}
536
Chuck Lever9903cd12005-08-11 16:25:26 -0400537/**
Chuck Lever262965f2005-08-11 16:25:56 -0400538 * xs_nospace - place task on wait queue if transmit was incomplete
539 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400540 *
Chuck Levera246b012005-08-11 16:25:23 -0400541 */
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400542static int xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400543{
Chuck Lever262965f2005-08-11 16:25:56 -0400544 struct rpc_rqst *req = task->tk_rqstp;
545 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500546 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400547 int ret = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400548
Chuck Lever46121cf2007-01-31 12:14:08 -0500549 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400550 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
551 req->rq_slen);
552
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400553 /* Protect against races with write_space */
554 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400555
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400556 /* Don't race with disconnect */
557 if (xprt_connected(xprt)) {
558 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400559 ret = -EAGAIN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400560 /*
561 * Notify TCP that we're limited by the application
562 * window size
563 */
564 set_bit(SOCK_NOSPACE, &transport->sock->flags);
565 transport->inet->sk_write_pending++;
566 /* ...and wait for more buffer space */
567 xprt_wait_for_buffer_space(task, xs_nospace_callback);
568 }
569 } else {
570 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400571 ret = -ENOTCONN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400572 }
Chuck Lever262965f2005-08-11 16:25:56 -0400573
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400574 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400575 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400576}
577
578/**
579 * xs_udp_send_request - write an RPC request to a UDP socket
580 * @task: address of RPC task that manages the state of an RPC request
581 *
582 * Return values:
583 * 0: The request has been sent
584 * EAGAIN: The socket was blocked, please call again later to
585 * complete the request
586 * ENOTCONN: Caller needs to invoke connect logic then call again
587 * other: Some other error occured, the request was not sent
588 */
589static int xs_udp_send_request(struct rpc_task *task)
590{
591 struct rpc_rqst *req = task->tk_rqstp;
592 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500593 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400594 struct xdr_buf *xdr = &req->rq_snd_buf;
595 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400596
Chuck Lever9903cd12005-08-11 16:25:26 -0400597 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400598 req->rq_svec->iov_base,
599 req->rq_svec->iov_len);
600
Trond Myklebust01d37c42009-03-11 14:09:39 -0400601 if (!xprt_bound(xprt))
602 return -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500603 status = xs_sendpages(transport->sock,
Chuck Lever95392c52007-08-06 11:57:58 -0400604 xs_addr(xprt),
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500605 xprt->addrlen, xdr,
606 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400607
Chuck Lever46121cf2007-01-31 12:14:08 -0500608 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400609 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400610
Trond Myklebust21997002007-10-01 11:43:37 -0400611 if (status >= 0) {
612 task->tk_bytes_sent += status;
613 if (status >= req->rq_slen)
614 return 0;
615 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400616 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400617 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400618 if (!transport->sock)
619 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400620
Chuck Lever262965f2005-08-11 16:25:56 -0400621 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400622 case -ENOTSOCK:
623 status = -ENOTCONN;
624 /* Should we call xs_close() here? */
625 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400626 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400627 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400628 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400629 default:
630 dprintk("RPC: sendmsg returned unrecognized error %d\n",
631 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400632 case -ENETUNREACH:
633 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400634 case -ECONNREFUSED:
635 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400636 * prompts ECONNREFUSED. */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400637 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400638 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400639out:
Chuck Lever262965f2005-08-11 16:25:56 -0400640 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400641}
642
Trond Myklebuste06799f2007-11-05 15:44:12 -0500643/**
644 * xs_tcp_shutdown - gracefully shut down a TCP socket
645 * @xprt: transport
646 *
647 * Initiates a graceful shutdown of the TCP socket by calling the
648 * equivalent of shutdown(SHUT_WR);
649 */
650static void xs_tcp_shutdown(struct rpc_xprt *xprt)
651{
652 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
653 struct socket *sock = transport->sock;
654
655 if (sock != NULL)
656 kernel_sock_shutdown(sock, SHUT_WR);
657}
658
Chuck Lever808012f2005-08-25 16:25:49 -0700659static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
660{
661 u32 reclen = buf->len - sizeof(rpc_fraghdr);
662 rpc_fraghdr *base = buf->head[0].iov_base;
663 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
664}
665
Chuck Lever9903cd12005-08-11 16:25:26 -0400666/**
Chuck Lever262965f2005-08-11 16:25:56 -0400667 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400668 * @task: address of RPC task that manages the state of an RPC request
669 *
670 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400671 * 0: The request has been sent
672 * EAGAIN: The socket was blocked, please call again later to
673 * complete the request
674 * ENOTCONN: Caller needs to invoke connect logic then call again
675 * other: Some other error occured, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400676 *
677 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400678 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400679 */
Chuck Lever262965f2005-08-11 16:25:56 -0400680static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400681{
682 struct rpc_rqst *req = task->tk_rqstp;
683 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500684 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400685 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Leverb595bb12007-08-06 11:56:42 -0400686 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400687
Chuck Lever808012f2005-08-25 16:25:49 -0700688 xs_encode_tcp_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400689
Chuck Lever262965f2005-08-11 16:25:56 -0400690 xs_pktdump("packet data:",
691 req->rq_svec->iov_base,
692 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400693
694 /* Continue transmitting the packet/record. We must be careful
695 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400696 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400697 while (1) {
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500698 status = xs_sendpages(transport->sock,
699 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400700
Chuck Lever46121cf2007-01-31 12:14:08 -0500701 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400702 xdr->len - req->rq_bytes_sent, status);
703
704 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400705 break;
706
Chuck Lever262965f2005-08-11 16:25:56 -0400707 /* If we've sent the entire packet, immediately
708 * reset the count of bytes sent. */
709 req->rq_bytes_sent += status;
Chuck Leveref759a22006-03-20 13:44:17 -0500710 task->tk_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400711 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
712 req->rq_bytes_sent = 0;
713 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400714 }
715
Trond Myklebust06b4b682008-04-16 16:51:38 -0400716 if (status != 0)
717 continue;
Chuck Levera246b012005-08-11 16:25:23 -0400718 status = -EAGAIN;
Trond Myklebust06b4b682008-04-16 16:51:38 -0400719 break;
Chuck Levera246b012005-08-11 16:25:23 -0400720 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400721 if (!transport->sock)
722 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400723
Chuck Lever262965f2005-08-11 16:25:56 -0400724 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400725 case -ENOTSOCK:
726 status = -ENOTCONN;
727 /* Should we call xs_close() here? */
728 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400729 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400730 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400731 break;
732 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500733 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400734 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400735 case -ECONNRESET:
Trond Myklebust55420c22009-03-11 15:29:24 -0400736 case -EPIPE:
Trond Myklebuste06799f2007-11-05 15:44:12 -0500737 xs_tcp_shutdown(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400738 case -ECONNREFUSED:
739 case -ENOTCONN:
Chuck Lever262965f2005-08-11 16:25:56 -0400740 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400741 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400742out:
Chuck Levera246b012005-08-11 16:25:23 -0400743 return status;
744}
745
Chuck Lever9903cd12005-08-11 16:25:26 -0400746/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400747 * xs_tcp_release_xprt - clean up after a tcp transmission
748 * @xprt: transport
749 * @task: rpc task
750 *
751 * This cleans up if an error causes us to abort the transmission of a request.
752 * In this case, the socket may need to be reset in order to avoid confusing
753 * the server.
754 */
755static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
756{
757 struct rpc_rqst *req;
758
759 if (task != xprt->snd_task)
760 return;
761 if (task == NULL)
762 goto out_release;
763 req = task->tk_rqstp;
764 if (req->rq_bytes_sent == 0)
765 goto out_release;
766 if (req->rq_bytes_sent == req->rq_snd_buf.len)
767 goto out_release;
768 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
769out_release:
770 xprt_release_xprt(xprt, task);
771}
772
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400773static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
774{
775 transport->old_data_ready = sk->sk_data_ready;
776 transport->old_state_change = sk->sk_state_change;
777 transport->old_write_space = sk->sk_write_space;
778 transport->old_error_report = sk->sk_error_report;
779}
780
781static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
782{
783 sk->sk_data_ready = transport->old_data_ready;
784 sk->sk_state_change = transport->old_state_change;
785 sk->sk_write_space = transport->old_write_space;
786 sk->sk_error_report = transport->old_error_report;
787}
788
Chuck Leverfe315e72009-03-11 14:10:21 -0400789static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400790{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500791 struct socket *sock = transport->sock;
792 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400793
Chuck Leverfe315e72009-03-11 14:10:21 -0400794 if (sk == NULL)
795 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400796
Chuck Levera246b012005-08-11 16:25:23 -0400797 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500798 transport->inet = NULL;
799 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400800
Chuck Lever9903cd12005-08-11 16:25:26 -0400801 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400802
803 xs_restore_old_callbacks(transport, sk);
Chuck Levera246b012005-08-11 16:25:23 -0400804 write_unlock_bh(&sk->sk_callback_lock);
805
Chuck Lever9903cd12005-08-11 16:25:26 -0400806 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400807
808 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -0400809}
810
811/**
812 * xs_close - close a socket
813 * @xprt: transport
814 *
815 * This is used when all requests are complete; ie, no DRC state remains
816 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400817 *
818 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
819 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e72009-03-11 14:10:21 -0400820 */
821static void xs_close(struct rpc_xprt *xprt)
822{
823 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
824
825 dprintk("RPC: xs_close xprt %p\n", xprt);
826
827 xs_reset_transport(transport);
828
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100829 smp_mb__before_clear_bit();
Trond Myklebust7d1e8252009-03-11 14:38:03 -0400830 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100831 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -0500832 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100833 smp_mb__after_clear_bit();
Trond Myklebust62da3b22007-11-06 18:44:20 -0500834 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400835}
836
Trond Myklebustf75e6742009-04-21 17:18:20 -0400837static void xs_tcp_close(struct rpc_xprt *xprt)
838{
839 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
840 xs_close(xprt);
841 else
842 xs_tcp_shutdown(xprt);
843}
844
Chuck Lever9903cd12005-08-11 16:25:26 -0400845/**
846 * xs_destroy - prepare to shutdown a transport
847 * @xprt: doomed transport
848 *
849 */
850static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400851{
Chuck Leverc8475462006-12-05 16:35:26 -0500852 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
853
Chuck Lever46121cf2007-01-31 12:14:08 -0500854 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400855
Trond Myklebustc1384c92007-06-14 18:00:42 -0400856 cancel_rearming_delayed_work(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400857
Chuck Lever9903cd12005-08-11 16:25:26 -0400858 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400859 xs_free_peer_addresses(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400860 kfree(xprt->slot);
Chuck Leverc8541ec2006-10-17 14:44:27 -0400861 kfree(xprt);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -0400862 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400863}
864
Chuck Lever9903cd12005-08-11 16:25:26 -0400865static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400866{
Chuck Lever9903cd12005-08-11 16:25:26 -0400867 return (struct rpc_xprt *) sk->sk_user_data;
868}
869
870/**
871 * xs_udp_data_ready - "data ready" callback for UDP sockets
872 * @sk: socket with data to read
873 * @len: how much data to read
874 *
875 */
876static void xs_udp_data_ready(struct sock *sk, int len)
877{
878 struct rpc_task *task;
879 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400880 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400881 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400882 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700883 u32 _xid;
884 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400885
886 read_lock(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500887 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400888 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400889 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400890
891 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
892 goto out;
893
894 if (xprt->shutdown)
895 goto dropit;
896
897 repsize = skb->len - sizeof(struct udphdr);
898 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500899 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400900 goto dropit;
901 }
902
903 /* Copy the XID from the skb... */
904 xp = skb_header_pointer(skb, sizeof(struct udphdr),
905 sizeof(_xid), &_xid);
906 if (xp == NULL)
907 goto dropit;
908
909 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400910 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400911 rovr = xprt_lookup_rqst(xprt, *xp);
912 if (!rovr)
913 goto out_unlock;
914 task = rovr->rq_task;
915
Chuck Levera246b012005-08-11 16:25:23 -0400916 if ((copied = rovr->rq_private_buf.buflen) > repsize)
917 copied = repsize;
918
919 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -0800920 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
921 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -0400922 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -0800923 }
924
925 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -0400926
927 /* Something worked... */
928 dst_confirm(skb->dst);
929
Chuck Lever1570c1e2005-08-25 16:25:52 -0700930 xprt_adjust_cwnd(task, copied);
931 xprt_update_rtt(task);
932 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -0400933
934 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400935 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400936 dropit:
937 skb_free_datagram(sk, skb);
938 out:
939 read_unlock(&sk->sk_callback_lock);
940}
941
Chuck Leverdd456472006-12-05 16:35:44 -0500942static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400943{
Chuck Lever51971132006-12-05 16:35:19 -0500944 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400945 size_t len, used;
946 char *p;
947
Chuck Lever51971132006-12-05 16:35:19 -0500948 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
949 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500950 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500951 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400952 if (used != len)
953 return;
Chuck Lever808012f2005-08-25 16:25:49 -0700954
Chuck Lever51971132006-12-05 16:35:19 -0500955 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
956 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -0500957 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -0400958 else
Chuck Levere136d092006-12-05 16:35:23 -0500959 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -0500960 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -0700961
Chuck Levere136d092006-12-05 16:35:23 -0500962 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500963 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -0700964
Chuck Levera246b012005-08-11 16:25:23 -0400965 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400966 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500967 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebust3ebb0672007-11-06 18:40:12 -0500968 xprt_force_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400969 return;
Chuck Levera246b012005-08-11 16:25:23 -0400970 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500971 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -0500972 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400973}
974
Chuck Lever51971132006-12-05 16:35:19 -0500975static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400976{
Chuck Lever51971132006-12-05 16:35:19 -0500977 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -0500978 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500979 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -0500980 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
981 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
982 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -0500983 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400984 }
985 }
986}
987
Chuck Leverdd456472006-12-05 16:35:44 -0500988static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400989{
990 size_t len, used;
991 char *p;
992
Chuck Lever51971132006-12-05 16:35:19 -0500993 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -0500994 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -0500995 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500996 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500997 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400998 if (used != len)
999 return;
Chuck Levere136d092006-12-05 16:35:23 -05001000 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001001 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -05001002 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001003 dprintk("RPC: reading %s XID %08x\n",
1004 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1005 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -05001006 ntohl(transport->tcp_xid));
1007 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001008}
1009
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001010static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1011 struct xdr_skb_reader *desc)
1012{
1013 size_t len, used;
1014 u32 offset;
1015 __be32 calldir;
1016
1017 /*
1018 * We want transport->tcp_offset to be 8 at the end of this routine
1019 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1020 * When this function is called for the first time,
1021 * transport->tcp_offset is 4 (after having already read the xid).
1022 */
1023 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
1024 len = sizeof(calldir) - offset;
1025 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
1026 used = xdr_skb_read_bits(desc, &calldir, len);
1027 transport->tcp_offset += used;
1028 if (used != len)
1029 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001030 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
1031 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001032 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001033 /*
1034 * We don't yet have the XDR buffer, so we will write the calldir
1035 * out after we get the buffer from the 'struct rpc_rqst'
1036 */
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001037 if (ntohl(calldir) == RPC_REPLY)
1038 transport->tcp_flags |= TCP_RPC_REPLY;
1039 else
1040 transport->tcp_flags &= ~TCP_RPC_REPLY;
1041 dprintk("RPC: reading %s CALL/REPLY flag %08x\n",
1042 (transport->tcp_flags & TCP_RPC_REPLY) ?
1043 "reply for" : "request with", calldir);
1044 xs_tcp_check_fraghdr(transport);
1045}
1046
Chuck Leverdd456472006-12-05 16:35:44 -05001047static inline void xs_tcp_read_request(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001048{
Chuck Lever51971132006-12-05 16:35:19 -05001049 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001050 struct rpc_rqst *req;
1051 struct xdr_buf *rcvbuf;
1052 size_t len;
1053 ssize_t r;
1054
1055 /* Find and lock the request corresponding to this xid */
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001056 spin_lock(&xprt->transport_lock);
Chuck Lever51971132006-12-05 16:35:19 -05001057 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
Chuck Levera246b012005-08-11 16:25:23 -04001058 if (!req) {
Chuck Levere136d092006-12-05 16:35:23 -05001059 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001060 dprintk("RPC: XID %08x request not found!\n",
Chuck Lever51971132006-12-05 16:35:19 -05001061 ntohl(transport->tcp_xid));
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001062 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001063 return;
1064 }
1065
1066 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001067
1068 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1069 /*
1070 * Save the RPC direction in the XDR buffer
1071 */
1072 __be32 calldir = transport->tcp_flags & TCP_RPC_REPLY ?
1073 htonl(RPC_REPLY) : 0;
1074
1075 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
1076 &calldir, sizeof(calldir));
1077 transport->tcp_copied += sizeof(calldir);
1078 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
1079 }
1080
Chuck Levera246b012005-08-11 16:25:23 -04001081 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -05001082 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -05001083 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -04001084
Chuck Lever51971132006-12-05 16:35:19 -05001085 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001086 memcpy(&my_desc, desc, sizeof(my_desc));
1087 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -05001088 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001089 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001090 desc->count -= r;
1091 desc->offset += r;
1092 } else
Chuck Lever51971132006-12-05 16:35:19 -05001093 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001094 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001095
1096 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001097 transport->tcp_copied += r;
1098 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001099 }
1100 if (r != len) {
1101 /* Error when copying to the receive buffer,
1102 * usually because we weren't able to allocate
1103 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001104 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001105 * will not receive any additional updates,
1106 * and time out.
1107 * Any remaining data from this record will
1108 * be discarded.
1109 */
Chuck Levere136d092006-12-05 16:35:23 -05001110 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001111 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001112 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001113 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1114 "tcp_offset = %u, tcp_reclen = %u\n",
1115 xprt, transport->tcp_copied,
1116 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001117 goto out;
1118 }
1119
Chuck Lever46121cf2007-01-31 12:14:08 -05001120 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001121 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001122 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1123 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1124 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001125
Chuck Lever51971132006-12-05 16:35:19 -05001126 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001127 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001128 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001129 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1130 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001131 }
1132
1133out:
Chuck Levere136d092006-12-05 16:35:23 -05001134 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001135 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001136 spin_unlock(&xprt->transport_lock);
Chuck Lever51971132006-12-05 16:35:19 -05001137 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001138}
1139
Chuck Leverdd456472006-12-05 16:35:44 -05001140static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001141{
1142 size_t len;
1143
Chuck Lever51971132006-12-05 16:35:19 -05001144 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001145 if (len > desc->count)
1146 len = desc->count;
1147 desc->count -= len;
1148 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001149 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001150 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001151 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001152}
1153
Chuck Lever9903cd12005-08-11 16:25:26 -04001154static 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 -04001155{
1156 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001157 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001158 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001159 .skb = skb,
1160 .offset = offset,
1161 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001162 };
Chuck Levera246b012005-08-11 16:25:23 -04001163
Chuck Lever46121cf2007-01-31 12:14:08 -05001164 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001165 do {
1166 /* Read in a new fragment marker if necessary */
1167 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001168 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001169 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001170 continue;
1171 }
1172 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001173 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001174 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001175 continue;
1176 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001177 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001178 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001179 xs_tcp_read_calldir(transport, &desc);
1180 continue;
1181 }
Chuck Levera246b012005-08-11 16:25:23 -04001182 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001183 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001184 xs_tcp_read_request(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001185 continue;
1186 }
1187 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001188 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001189 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001190 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001191 return len - desc.count;
1192}
1193
Chuck Lever9903cd12005-08-11 16:25:26 -04001194/**
1195 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1196 * @sk: socket with data to read
1197 * @bytes: how much data to read
1198 *
1199 */
1200static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001201{
1202 struct rpc_xprt *xprt;
1203 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001204 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001205
Chuck Lever46121cf2007-01-31 12:14:08 -05001206 dprintk("RPC: xs_tcp_data_ready...\n");
1207
Chuck Levera246b012005-08-11 16:25:23 -04001208 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001209 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001210 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001211 if (xprt->shutdown)
1212 goto out;
1213
Chuck Lever9903cd12005-08-11 16:25:26 -04001214 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001215 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001216 do {
1217 rd_desc.count = 65536;
1218 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1219 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001220out:
1221 read_unlock(&sk->sk_callback_lock);
1222}
1223
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001224/*
1225 * Do the equivalent of linger/linger2 handling for dealing with
1226 * broken servers that don't close the socket in a timely
1227 * fashion
1228 */
1229static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1230 unsigned long timeout)
1231{
1232 struct sock_xprt *transport;
1233
1234 if (xprt_test_and_set_connecting(xprt))
1235 return;
1236 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1237 transport = container_of(xprt, struct sock_xprt, xprt);
1238 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1239 timeout);
1240}
1241
1242static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1243{
1244 struct sock_xprt *transport;
1245
1246 transport = container_of(xprt, struct sock_xprt, xprt);
1247
1248 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1249 !cancel_delayed_work(&transport->connect_worker))
1250 return;
1251 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1252 xprt_clear_connecting(xprt);
1253}
1254
1255static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1256{
1257 smp_mb__before_clear_bit();
1258 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1259 clear_bit(XPRT_CLOSING, &xprt->state);
1260 smp_mb__after_clear_bit();
1261 /* Mark transport as closed and wake up all pending tasks */
1262 xprt_disconnect_done(xprt);
1263}
1264
Chuck Lever9903cd12005-08-11 16:25:26 -04001265/**
1266 * xs_tcp_state_change - callback to handle TCP socket state changes
1267 * @sk: socket whose state has changed
1268 *
1269 */
1270static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001271{
Chuck Lever9903cd12005-08-11 16:25:26 -04001272 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001273
1274 read_lock(&sk->sk_callback_lock);
1275 if (!(xprt = xprt_from_sock(sk)))
1276 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001277 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
1278 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
1279 sk->sk_state, xprt_connected(xprt),
1280 sock_flag(sk, SOCK_DEAD),
1281 sock_flag(sk, SOCK_ZAPPED));
Chuck Levera246b012005-08-11 16:25:23 -04001282
1283 switch (sk->sk_state) {
1284 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001285 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001286 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001287 struct sock_xprt *transport = container_of(xprt,
1288 struct sock_xprt, xprt);
1289
Chuck Levera246b012005-08-11 16:25:23 -04001290 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001291 transport->tcp_offset = 0;
1292 transport->tcp_reclen = 0;
1293 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001294 transport->tcp_flags =
1295 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001296
Trond Myklebust2a491992009-03-11 14:38:00 -04001297 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001298 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001299 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001300 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001301 case TCP_FIN_WAIT1:
1302 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001303 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001304 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001305 set_bit(XPRT_CLOSING, &xprt->state);
1306 smp_mb__before_clear_bit();
1307 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001308 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001309 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001310 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001311 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001312 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001313 /* The server initiated a shutdown of the socket */
Trond Myklebust66af1e52007-11-06 10:18:36 -05001314 xprt_force_disconnect(xprt);
Trond Myklebust663b8852008-01-01 18:42:12 -05001315 case TCP_SYN_SENT:
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001316 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001317 case TCP_CLOSING:
1318 /*
1319 * If the server closed down the connection, make sure that
1320 * we back off before reconnecting
1321 */
1322 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1323 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001324 break;
1325 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001326 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001327 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001328 smp_mb__before_clear_bit();
1329 clear_bit(XPRT_CONNECTED, &xprt->state);
1330 smp_mb__after_clear_bit();
1331 break;
1332 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001333 xs_tcp_cancel_linger_timeout(xprt);
1334 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001335 }
1336 out:
1337 read_unlock(&sk->sk_callback_lock);
1338}
1339
Chuck Lever9903cd12005-08-11 16:25:26 -04001340/**
Trond Myklebust482f32e2009-03-11 14:38:00 -04001341 * xs_error_report - callback mainly for catching socket errors
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001342 * @sk: socket
1343 */
Trond Myklebust482f32e2009-03-11 14:38:00 -04001344static void xs_error_report(struct sock *sk)
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001345{
1346 struct rpc_xprt *xprt;
1347
1348 read_lock(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001349 if (!(xprt = xprt_from_sock(sk)))
1350 goto out;
1351 dprintk("RPC: %s client %p...\n"
1352 "RPC: error %d\n",
1353 __func__, xprt, sk->sk_err);
Trond Myklebust482f32e2009-03-11 14:38:00 -04001354 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001355out:
1356 read_unlock(&sk->sk_callback_lock);
1357}
1358
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001359static void xs_write_space(struct sock *sk)
1360{
1361 struct socket *sock;
1362 struct rpc_xprt *xprt;
1363
1364 if (unlikely(!(sock = sk->sk_socket)))
1365 return;
1366 clear_bit(SOCK_NOSPACE, &sock->flags);
1367
1368 if (unlikely(!(xprt = xprt_from_sock(sk))))
1369 return;
1370 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1371 return;
1372
1373 xprt_write_space(xprt);
1374}
1375
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001376/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001377 * xs_udp_write_space - callback invoked when socket buffer space
1378 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001379 * @sk: socket whose state has changed
1380 *
Chuck Levera246b012005-08-11 16:25:23 -04001381 * Called when more output buffer space is available for this socket.
1382 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001383 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001384 * with a bunch of small requests.
1385 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001386static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001387{
Chuck Levera246b012005-08-11 16:25:23 -04001388 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001389
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001390 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001391 if (sock_writeable(sk))
1392 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001393
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001394 read_unlock(&sk->sk_callback_lock);
1395}
Chuck Levera246b012005-08-11 16:25:23 -04001396
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001397/**
1398 * xs_tcp_write_space - callback invoked when socket buffer space
1399 * becomes available
1400 * @sk: socket whose state has changed
1401 *
1402 * Called when more output buffer space is available for this socket.
1403 * We try not to wake our writers until they can make "significant"
1404 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1405 * with a bunch of small requests.
1406 */
1407static void xs_tcp_write_space(struct sock *sk)
1408{
1409 read_lock(&sk->sk_callback_lock);
1410
1411 /* from net/core/stream.c:sk_stream_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001412 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1413 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001414
Chuck Levera246b012005-08-11 16:25:23 -04001415 read_unlock(&sk->sk_callback_lock);
1416}
1417
Chuck Lever470056c2005-08-25 16:25:56 -07001418static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001419{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001420 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1421 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001422
Chuck Lever7c6e0662006-12-05 16:35:30 -05001423 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001424 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001425 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001426 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001427 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001428 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001429 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001430 sk->sk_write_space(sk);
1431 }
1432}
1433
Chuck Lever43118c22005-08-25 16:25:49 -07001434/**
Chuck Lever470056c2005-08-25 16:25:56 -07001435 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001436 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001437 * @sndsize: requested size of send buffer, in bytes
1438 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001439 *
Chuck Lever470056c2005-08-25 16:25:56 -07001440 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001441 */
Chuck Lever470056c2005-08-25 16:25:56 -07001442static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001443{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001444 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1445
1446 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001447 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001448 transport->sndsize = sndsize + 1024;
1449 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001450 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001451 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001452
1453 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001454}
1455
Chuck Lever46c0ee82005-08-25 16:25:52 -07001456/**
1457 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1458 * @task: task that timed out
1459 *
1460 * Adjust the congestion window after a retransmit timeout has occurred.
1461 */
1462static void xs_udp_timer(struct rpc_task *task)
1463{
1464 xprt_adjust_cwnd(task, -ETIMEDOUT);
1465}
1466
Chuck Leverb85d8802006-05-25 01:40:49 -04001467static unsigned short xs_get_random_port(void)
1468{
1469 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1470 unsigned short rand = (unsigned short) net_random() % range;
1471 return rand + xprt_min_resvport;
1472}
1473
Chuck Lever92200412006-01-03 09:55:51 +01001474/**
1475 * xs_set_port - reset the port number in the remote endpoint address
1476 * @xprt: generic transport
1477 * @port: new port number
1478 *
1479 */
1480static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1481{
Chuck Lever95392c52007-08-06 11:57:58 -04001482 struct sockaddr *addr = xs_addr(xprt);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001483
Chuck Lever46121cf2007-01-31 12:14:08 -05001484 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001485
Chuck Lever20612002007-08-06 11:57:23 -04001486 switch (addr->sa_family) {
1487 case AF_INET:
1488 ((struct sockaddr_in *)addr)->sin_port = htons(port);
1489 break;
1490 case AF_INET6:
1491 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
1492 break;
1493 default:
1494 BUG();
1495 }
Chuck Lever92200412006-01-03 09:55:51 +01001496}
1497
Trond Myklebust67a391d2007-11-05 17:40:58 -05001498static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
1499{
1500 unsigned short port = transport->port;
1501
1502 if (port == 0 && transport->xprt.resvport)
1503 port = xs_get_random_port();
1504 return port;
1505}
1506
1507static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
1508{
1509 if (transport->port != 0)
1510 transport->port = 0;
1511 if (!transport->xprt.resvport)
1512 return 0;
1513 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1514 return xprt_max_resvport;
1515 return --port;
1516}
1517
Chuck Lever7dc753f2007-08-06 11:57:28 -04001518static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001519{
1520 struct sockaddr_in myaddr = {
1521 .sin_family = AF_INET,
1522 };
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001523 struct sockaddr_in *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001524 int err, nloop = 0;
1525 unsigned short port = xs_get_srcport(transport, sock);
1526 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001527
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001528 sa = (struct sockaddr_in *)&transport->addr;
1529 myaddr.sin_addr = sa->sin_addr;
Chuck Levera246b012005-08-11 16:25:23 -04001530 do {
1531 myaddr.sin_port = htons(port);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001532 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
Chuck Levera246b012005-08-11 16:25:23 -04001533 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001534 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001535 break;
Chuck Levera246b012005-08-11 16:25:23 -04001536 if (err == 0) {
Chuck Leverc8475462006-12-05 16:35:26 -05001537 transport->port = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001538 break;
Chuck Levera246b012005-08-11 16:25:23 -04001539 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001540 last = port;
1541 port = xs_next_srcport(transport, sock, port);
1542 if (port > last)
1543 nloop++;
1544 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison21454aa2008-10-31 00:54:56 -07001545 dprintk("RPC: %s %pI4:%u: %s (%d)\n",
1546 __func__, &myaddr.sin_addr,
Chuck Lever7dc753f2007-08-06 11:57:28 -04001547 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001548 return err;
1549}
1550
Chuck Lever90058d32007-08-06 11:57:33 -04001551static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
1552{
1553 struct sockaddr_in6 myaddr = {
1554 .sin6_family = AF_INET6,
1555 };
1556 struct sockaddr_in6 *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001557 int err, nloop = 0;
1558 unsigned short port = xs_get_srcport(transport, sock);
1559 unsigned short last;
Chuck Lever90058d32007-08-06 11:57:33 -04001560
Chuck Lever90058d32007-08-06 11:57:33 -04001561 sa = (struct sockaddr_in6 *)&transport->addr;
1562 myaddr.sin6_addr = sa->sin6_addr;
1563 do {
1564 myaddr.sin6_port = htons(port);
1565 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1566 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001567 if (port == 0)
Chuck Lever90058d32007-08-06 11:57:33 -04001568 break;
1569 if (err == 0) {
1570 transport->port = port;
1571 break;
1572 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001573 last = port;
1574 port = xs_next_srcport(transport, sock, port);
1575 if (port > last)
1576 nloop++;
1577 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001578 dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07001579 &myaddr.sin6_addr, port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001580 return err;
1581}
1582
Peter Zijlstraed075362006-12-06 20:35:24 -08001583#ifdef CONFIG_DEBUG_LOCK_ALLOC
1584static struct lock_class_key xs_key[2];
1585static struct lock_class_key xs_slock_key[2];
1586
Chuck Lever8945ee52007-08-06 11:58:04 -04001587static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001588{
1589 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001590
John Heffner02b3d342007-09-12 10:42:12 +02001591 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001592 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1593 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1594}
Peter Zijlstraed075362006-12-06 20:35:24 -08001595
Chuck Lever8945ee52007-08-06 11:58:04 -04001596static inline void xs_reclassify_socket6(struct socket *sock)
1597{
1598 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001599
Linus Torvaldsf4921af2007-10-15 10:46:05 -07001600 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001601 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1602 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001603}
1604#else
Chuck Lever8945ee52007-08-06 11:58:04 -04001605static inline void xs_reclassify_socket4(struct socket *sock)
1606{
1607}
1608
1609static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001610{
1611}
1612#endif
1613
Chuck Lever16be2d22007-08-06 11:57:38 -04001614static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001615{
Chuck Lever16be2d22007-08-06 11:57:38 -04001616 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001617
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001618 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001619 struct sock *sk = sock->sk;
1620
1621 write_lock_bh(&sk->sk_callback_lock);
1622
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001623 xs_save_old_callbacks(transport, sk);
1624
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001625 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001626 sk->sk_data_ready = xs_udp_data_ready;
1627 sk->sk_write_space = xs_udp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001628 sk->sk_error_report = xs_error_report;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001629 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa72005-12-13 16:13:52 -05001630 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001631
1632 xprt_set_connected(xprt);
1633
1634 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001635 transport->sock = sock;
1636 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001637
1638 write_unlock_bh(&sk->sk_callback_lock);
1639 }
Chuck Lever470056c2005-08-25 16:25:56 -07001640 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04001641}
1642
Chuck Levera246b012005-08-11 16:25:23 -04001643/**
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001644 * xs_udp_connect_worker4 - set up a UDP socket
Chuck Levera246b012005-08-11 16:25:23 -04001645 * @work: RPC transport to connect
1646 *
1647 * Invoked by a work queue tasklet.
1648 */
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001649static void xs_udp_connect_worker4(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04001650{
1651 struct sock_xprt *transport =
1652 container_of(work, struct sock_xprt, connect_worker.work);
1653 struct rpc_xprt *xprt = &transport->xprt;
1654 struct socket *sock = transport->sock;
1655 int err, status = -EIO;
1656
Trond Myklebust01d37c42009-03-11 14:09:39 -04001657 if (xprt->shutdown)
Chuck Lever9903cd12005-08-11 16:25:26 -04001658 goto out;
1659
Chuck Levera246b012005-08-11 16:25:23 -04001660 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001661 xs_reset_transport(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001662
Chuck Leverfe315e72009-03-11 14:10:21 -04001663 err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1664 if (err < 0) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001665 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001666 goto out;
1667 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001668 xs_reclassify_socket4(sock);
Chuck Levera246b012005-08-11 16:25:23 -04001669
Chuck Lever7dc753f2007-08-06 11:57:28 -04001670 if (xs_bind4(transport, sock)) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001671 sock_release(sock);
1672 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001673 }
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001674
1675 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1676 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1677
Chuck Lever16be2d22007-08-06 11:57:38 -04001678 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001679 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001680out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001681 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001682 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001683}
1684
Chuck Lever68e220b2007-08-06 11:57:48 -04001685/**
1686 * xs_udp_connect_worker6 - set up a UDP socket
1687 * @work: RPC transport to connect
1688 *
1689 * Invoked by a work queue tasklet.
1690 */
1691static void xs_udp_connect_worker6(struct work_struct *work)
1692{
1693 struct sock_xprt *transport =
1694 container_of(work, struct sock_xprt, connect_worker.work);
1695 struct rpc_xprt *xprt = &transport->xprt;
1696 struct socket *sock = transport->sock;
1697 int err, status = -EIO;
1698
Trond Myklebust01d37c42009-03-11 14:09:39 -04001699 if (xprt->shutdown)
Chuck Lever68e220b2007-08-06 11:57:48 -04001700 goto out;
1701
1702 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001703 xs_reset_transport(transport);
Chuck Lever68e220b2007-08-06 11:57:48 -04001704
Chuck Leverfe315e72009-03-11 14:10:21 -04001705 err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
1706 if (err < 0) {
Chuck Lever68e220b2007-08-06 11:57:48 -04001707 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1708 goto out;
1709 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001710 xs_reclassify_socket6(sock);
Chuck Lever68e220b2007-08-06 11:57:48 -04001711
1712 if (xs_bind6(transport, sock) < 0) {
1713 sock_release(sock);
1714 goto out;
1715 }
1716
1717 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1718 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1719
1720 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001721 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001722out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001723 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001724 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001725}
1726
Chuck Lever3167e122005-08-25 16:25:55 -07001727/*
1728 * We need to preserve the port number so the reply cache on the server can
1729 * find our cached RPC replies when we get around to reconnecting.
1730 */
Trond Myklebust40d25492009-03-11 14:37:58 -04001731static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07001732{
1733 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07001734 struct sockaddr any;
1735
Chuck Lever46121cf2007-01-31 12:14:08 -05001736 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
Chuck Lever3167e122005-08-25 16:25:55 -07001737
1738 /*
1739 * Disconnect the transport socket by doing a connect operation
1740 * with AF_UNSPEC. This should return immediately...
1741 */
1742 memset(&any, 0, sizeof(any));
1743 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001744 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001745 if (!result)
1746 xs_sock_mark_closed(xprt);
1747 else
Chuck Lever46121cf2007-01-31 12:14:08 -05001748 dprintk("RPC: AF_UNSPEC connect return code %d\n",
Chuck Lever3167e122005-08-25 16:25:55 -07001749 result);
1750}
1751
Trond Myklebust40d25492009-03-11 14:37:58 -04001752static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
1753{
1754 unsigned int state = transport->inet->sk_state;
1755
1756 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED)
1757 return;
1758 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT))
1759 return;
1760 xs_abort_connection(xprt, transport);
1761}
1762
Chuck Lever16be2d22007-08-06 11:57:38 -04001763static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001764{
Chuck Lever16be2d22007-08-06 11:57:38 -04001765 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001766
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001767 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001768 struct sock *sk = sock->sk;
1769
1770 write_lock_bh(&sk->sk_callback_lock);
1771
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001772 xs_save_old_callbacks(transport, sk);
1773
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001774 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001775 sk->sk_data_ready = xs_tcp_data_ready;
1776 sk->sk_state_change = xs_tcp_state_change;
1777 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001778 sk->sk_error_report = xs_error_report;
Trond Myklebustb079fa72005-12-13 16:13:52 -05001779 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07001780
1781 /* socket options */
1782 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1783 sock_reset_flag(sk, SOCK_LINGER);
1784 tcp_sk(sk)->linger2 = 0;
1785 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001786
1787 xprt_clear_connected(xprt);
1788
1789 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001790 transport->sock = sock;
1791 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001792
1793 write_unlock_bh(&sk->sk_callback_lock);
1794 }
1795
Trond Myklebust01d37c42009-03-11 14:09:39 -04001796 if (!xprt_bound(xprt))
1797 return -ENOTCONN;
1798
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001799 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05001800 xprt->stat.connect_count++;
1801 xprt->stat.connect_start = jiffies;
Chuck Lever95392c52007-08-06 11:57:58 -04001802 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
Chuck Lever16be2d22007-08-06 11:57:38 -04001803}
1804
1805/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001806 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1807 * @xprt: RPC transport to connect
1808 * @transport: socket transport to connect
1809 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04001810 *
1811 * Invoked by a work queue tasklet.
1812 */
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001813static void xs_tcp_setup_socket(struct rpc_xprt *xprt,
1814 struct sock_xprt *transport,
1815 struct socket *(*create_sock)(struct rpc_xprt *,
1816 struct sock_xprt *))
Chuck Lever16be2d22007-08-06 11:57:38 -04001817{
Chuck Lever16be2d22007-08-06 11:57:38 -04001818 struct socket *sock = transport->sock;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001819 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04001820
Trond Myklebust01d37c42009-03-11 14:09:39 -04001821 if (xprt->shutdown)
Chuck Lever16be2d22007-08-06 11:57:38 -04001822 goto out;
1823
1824 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001825 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001826 sock = create_sock(xprt, transport);
1827 if (IS_ERR(sock)) {
1828 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04001829 goto out;
1830 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001831 } else {
1832 int abort_and_exit;
1833
1834 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
1835 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04001836 /* "close" the socket, preserving the local port */
Trond Myklebust40d25492009-03-11 14:37:58 -04001837 xs_tcp_reuse_connection(xprt, transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04001838
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001839 if (abort_and_exit)
1840 goto out_eagain;
1841 }
1842
Chuck Lever16be2d22007-08-06 11:57:38 -04001843 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1844 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1845
1846 status = xs_tcp_finish_connecting(xprt, sock);
Chuck Lever46121cf2007-01-31 12:14:08 -05001847 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1848 xprt, -status, xprt_connected(xprt),
1849 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04001850 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04001851 default:
1852 printk("%s: connect returned unhandled error %d\n",
1853 __func__, status);
1854 case -EADDRNOTAVAIL:
1855 /* We're probably in TIME_WAIT. Get rid of existing socket,
1856 * and retry
1857 */
1858 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1859 xprt_force_disconnect(xprt);
Trond Myklebust8a2cec22009-03-11 14:38:01 -04001860 case -ECONNREFUSED:
1861 case -ECONNRESET:
1862 case -ENETUNREACH:
1863 /* retry with existing socket, after a delay */
Trond Myklebust2a491992009-03-11 14:38:00 -04001864 case 0:
1865 case -EINPROGRESS:
1866 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001867 xprt_clear_connecting(xprt);
1868 return;
Chuck Levera246b012005-08-11 16:25:23 -04001869 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001870out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04001871 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04001872out:
Chuck Lever2226feb2005-08-11 16:25:38 -04001873 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001874 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001875}
1876
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001877static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt,
1878 struct sock_xprt *transport)
1879{
1880 struct socket *sock;
1881 int err;
1882
1883 /* start from scratch */
1884 err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1885 if (err < 0) {
1886 dprintk("RPC: can't create TCP transport socket (%d).\n",
1887 -err);
1888 goto out_err;
1889 }
1890 xs_reclassify_socket4(sock);
1891
1892 if (xs_bind4(transport, sock) < 0) {
1893 sock_release(sock);
1894 goto out_err;
1895 }
1896 return sock;
1897out_err:
1898 return ERR_PTR(-EIO);
1899}
1900
1901/**
Chuck Levera246b012005-08-11 16:25:23 -04001902 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1903 * @work: RPC transport to connect
1904 *
1905 * Invoked by a work queue tasklet.
1906 */
1907static void xs_tcp_connect_worker4(struct work_struct *work)
1908{
1909 struct sock_xprt *transport =
1910 container_of(work, struct sock_xprt, connect_worker.work);
1911 struct rpc_xprt *xprt = &transport->xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001912
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001913 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4);
1914}
Chuck Levera246b012005-08-11 16:25:23 -04001915
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001916static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt,
1917 struct sock_xprt *transport)
1918{
1919 struct socket *sock;
1920 int err;
Chuck Lever9903cd12005-08-11 16:25:26 -04001921
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001922 /* start from scratch */
1923 err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock);
1924 if (err < 0) {
1925 dprintk("RPC: can't create TCP transport socket (%d).\n",
1926 -err);
1927 goto out_err;
Chuck Levera246b012005-08-11 16:25:23 -04001928 }
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001929 xs_reclassify_socket6(sock);
1930
1931 if (xs_bind6(transport, sock) < 0) {
1932 sock_release(sock);
1933 goto out_err;
1934 }
1935 return sock;
1936out_err:
1937 return ERR_PTR(-EIO);
Chuck Levera246b012005-08-11 16:25:23 -04001938}
1939
Chuck Lever9903cd12005-08-11 16:25:26 -04001940/**
Chuck Lever68e220b2007-08-06 11:57:48 -04001941 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
1942 * @work: RPC transport to connect
1943 *
1944 * Invoked by a work queue tasklet.
1945 */
1946static void xs_tcp_connect_worker6(struct work_struct *work)
1947{
1948 struct sock_xprt *transport =
1949 container_of(work, struct sock_xprt, connect_worker.work);
1950 struct rpc_xprt *xprt = &transport->xprt;
Chuck Lever68e220b2007-08-06 11:57:48 -04001951
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001952 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6);
Chuck Lever68e220b2007-08-06 11:57:48 -04001953}
1954
1955/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001956 * xs_connect - connect a socket to a remote endpoint
1957 * @task: address of RPC task that manages state of connect request
1958 *
1959 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07001960 *
1961 * UDP socket connects are synchronous, but we use a work queue anyway
1962 * to guarantee that even unprivileged user processes can set up a
1963 * socket on a privileged port.
1964 *
1965 * If a UDP socket connect fails, the delay behavior here prevents
1966 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04001967 */
1968static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04001969{
1970 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001971 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001972
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001973 if (xprt_test_and_set_connecting(xprt))
1974 return;
1975
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001976 if (transport->sock != NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001977 dprintk("RPC: xs_connect delayed xprt %p for %lu "
1978 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07001979 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04001980 queue_delayed_work(rpciod_workqueue,
1981 &transport->connect_worker,
1982 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07001983 xprt->reestablish_timeout <<= 1;
1984 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
1985 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001986 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05001987 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04001988 queue_delayed_work(rpciod_workqueue,
1989 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04001990 }
1991}
1992
Trond Myklebuste06799f2007-11-05 15:44:12 -05001993static void xs_tcp_connect(struct rpc_task *task)
1994{
1995 struct rpc_xprt *xprt = task->tk_xprt;
1996
Trond Myklebuste06799f2007-11-05 15:44:12 -05001997 /* Exit if we need to wait for socket shutdown to complete */
1998 if (test_bit(XPRT_CLOSING, &xprt->state))
1999 return;
2000 xs_connect(task);
2001}
2002
Chuck Lever262ca072006-03-20 13:44:16 -05002003/**
2004 * xs_udp_print_stats - display UDP socket-specifc stats
2005 * @xprt: rpc_xprt struct containing statistics
2006 * @seq: output file
2007 *
2008 */
2009static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2010{
Chuck Leverc8475462006-12-05 16:35:26 -05002011 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2012
Chuck Lever262ca072006-03-20 13:44:16 -05002013 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
Chuck Leverc8475462006-12-05 16:35:26 -05002014 transport->port,
Chuck Lever262ca072006-03-20 13:44:16 -05002015 xprt->stat.bind_count,
2016 xprt->stat.sends,
2017 xprt->stat.recvs,
2018 xprt->stat.bad_xids,
2019 xprt->stat.req_u,
2020 xprt->stat.bklog_u);
2021}
2022
2023/**
2024 * xs_tcp_print_stats - display TCP socket-specifc stats
2025 * @xprt: rpc_xprt struct containing statistics
2026 * @seq: output file
2027 *
2028 */
2029static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2030{
Chuck Leverc8475462006-12-05 16:35:26 -05002031 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002032 long idle_time = 0;
2033
2034 if (xprt_connected(xprt))
2035 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2036
2037 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
Chuck Leverc8475462006-12-05 16:35:26 -05002038 transport->port,
Chuck Lever262ca072006-03-20 13:44:16 -05002039 xprt->stat.bind_count,
2040 xprt->stat.connect_count,
2041 xprt->stat.connect_time,
2042 idle_time,
2043 xprt->stat.sends,
2044 xprt->stat.recvs,
2045 xprt->stat.bad_xids,
2046 xprt->stat.req_u,
2047 xprt->stat.bklog_u);
2048}
2049
Chuck Lever262965f2005-08-11 16:25:56 -04002050static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002051 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002052 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002053 .release_xprt = xprt_release_xprt_cong,
Chuck Lever45160d62007-07-01 12:13:17 -04002054 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002055 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002056 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002057 .buf_alloc = rpc_malloc,
2058 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002059 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002060 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002061 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002062 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002063 .close = xs_close,
2064 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002065 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04002066};
2067
2068static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002069 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002070 .release_xprt = xs_tcp_release_xprt,
Chuck Lever45160d62007-07-01 12:13:17 -04002071 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002072 .set_port = xs_set_port,
Trond Myklebuste06799f2007-11-05 15:44:12 -05002073 .connect = xs_tcp_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002074 .buf_alloc = rpc_malloc,
2075 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002076 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002077 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustf75e6742009-04-21 17:18:20 -04002078 .close = xs_tcp_close,
Chuck Lever9903cd12005-08-11 16:25:26 -04002079 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002080 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002081};
2082
\"Talpey, Thomas\3c341b02007-09-10 13:47:07 -04002083static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002084 unsigned int slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002085{
2086 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002087 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002088
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002089 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002090 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002091 return ERR_PTR(-EBADF);
2092 }
2093
Chuck Leverffc2e512006-12-05 16:35:11 -05002094 new = kzalloc(sizeof(*new), GFP_KERNEL);
2095 if (new == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002096 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2097 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002098 return ERR_PTR(-ENOMEM);
2099 }
Chuck Leverffc2e512006-12-05 16:35:11 -05002100 xprt = &new->xprt;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002101
2102 xprt->max_reqs = slot_table_size;
2103 xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
2104 if (xprt->slot == NULL) {
2105 kfree(xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05002106 dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
2107 "table\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002108 return ERR_PTR(-ENOMEM);
2109 }
2110
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002111 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2112 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002113 if (args->srcaddr)
2114 memcpy(&new->addr, args->srcaddr, args->addrlen);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002115
2116 return xprt;
2117}
2118
Trond Myklebust2881ae72007-12-20 16:03:54 -05002119static const struct rpc_timeout xs_udp_default_timeout = {
2120 .to_initval = 5 * HZ,
2121 .to_maxval = 30 * HZ,
2122 .to_increment = 5 * HZ,
2123 .to_retries = 5,
2124};
2125
Chuck Lever9903cd12005-08-11 16:25:26 -04002126/**
2127 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002128 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002129 *
2130 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002131static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002132{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002133 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002134 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002135 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04002136
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002137 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002138 if (IS_ERR(xprt))
2139 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002140 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002141
Chuck Leverec739ef2006-08-22 20:06:15 -04002142 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002143 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002144 /* XXX: header size can vary due to auth type, IPv6, etc. */
2145 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2146
Chuck Lever03bf4b72005-08-25 16:25:55 -07002147 xprt->bind_timeout = XS_BIND_TO;
2148 xprt->connect_timeout = XS_UDP_CONN_TO;
2149 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2150 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002151
Chuck Lever262965f2005-08-11 16:25:56 -04002152 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002153
Trond Myklebustba7392b2007-12-20 16:03:55 -05002154 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002155
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002156 switch (addr->sa_family) {
2157 case AF_INET:
2158 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2159 xprt_set_bound(xprt);
2160
2161 INIT_DELAYED_WORK(&transport->connect_worker,
2162 xs_udp_connect_worker4);
Chuck Leverb454ae92008-01-07 18:34:48 -05002163 xs_format_ipv4_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002164 break;
2165 case AF_INET6:
2166 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2167 xprt_set_bound(xprt);
2168
2169 INIT_DELAYED_WORK(&transport->connect_worker,
2170 xs_udp_connect_worker6);
Chuck Leverb454ae92008-01-07 18:34:48 -05002171 xs_format_ipv6_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002172 break;
2173 default:
2174 kfree(xprt);
2175 return ERR_PTR(-EAFNOSUPPORT);
2176 }
2177
Chuck Lever46121cf2007-01-31 12:14:08 -05002178 dprintk("RPC: set up transport to address %s\n",
Chuck Lever7559c7a2006-12-05 16:35:37 -05002179 xprt->address_strings[RPC_DISPLAY_ALL]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002180
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002181 if (try_module_get(THIS_MODULE))
2182 return xprt;
2183
2184 kfree(xprt->slot);
2185 kfree(xprt);
2186 return ERR_PTR(-EINVAL);
Chuck Levera246b012005-08-11 16:25:23 -04002187}
2188
Trond Myklebust2881ae72007-12-20 16:03:54 -05002189static const struct rpc_timeout xs_tcp_default_timeout = {
2190 .to_initval = 60 * HZ,
2191 .to_maxval = 60 * HZ,
2192 .to_retries = 2,
2193};
2194
Chuck Lever9903cd12005-08-11 16:25:26 -04002195/**
2196 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002197 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002198 *
2199 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002200static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002201{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002202 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002203 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002204 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04002205
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002206 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002207 if (IS_ERR(xprt))
2208 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002209 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002210
Chuck Leverec739ef2006-08-22 20:06:15 -04002211 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002212 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002213 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002214
Chuck Lever03bf4b72005-08-25 16:25:55 -07002215 xprt->bind_timeout = XS_BIND_TO;
2216 xprt->connect_timeout = XS_TCP_CONN_TO;
2217 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2218 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002219
Chuck Lever262965f2005-08-11 16:25:56 -04002220 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002221 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002222
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002223 switch (addr->sa_family) {
2224 case AF_INET:
2225 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2226 xprt_set_bound(xprt);
2227
2228 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4);
Chuck Leverb454ae92008-01-07 18:34:48 -05002229 xs_format_ipv4_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002230 break;
2231 case AF_INET6:
2232 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2233 xprt_set_bound(xprt);
2234
2235 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker6);
Chuck Leverb454ae92008-01-07 18:34:48 -05002236 xs_format_ipv6_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002237 break;
2238 default:
2239 kfree(xprt);
2240 return ERR_PTR(-EAFNOSUPPORT);
2241 }
2242
Chuck Lever46121cf2007-01-31 12:14:08 -05002243 dprintk("RPC: set up transport to address %s\n",
Chuck Lever7559c7a2006-12-05 16:35:37 -05002244 xprt->address_strings[RPC_DISPLAY_ALL]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002245
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002246 if (try_module_get(THIS_MODULE))
2247 return xprt;
2248
2249 kfree(xprt->slot);
2250 kfree(xprt);
2251 return ERR_PTR(-EINVAL);
Chuck Levera246b012005-08-11 16:25:23 -04002252}
Chuck Lever282b32e2006-12-05 16:35:51 -05002253
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002254static struct xprt_class xs_udp_transport = {
2255 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2256 .name = "udp",
2257 .owner = THIS_MODULE,
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -04002258 .ident = IPPROTO_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002259 .setup = xs_setup_udp,
2260};
2261
2262static struct xprt_class xs_tcp_transport = {
2263 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2264 .name = "tcp",
2265 .owner = THIS_MODULE,
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -04002266 .ident = IPPROTO_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002267 .setup = xs_setup_tcp,
2268};
2269
Chuck Lever282b32e2006-12-05 16:35:51 -05002270/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002271 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002272 *
2273 */
2274int init_socket_xprt(void)
2275{
Chuck Leverfbf76682006-12-05 16:35:54 -05002276#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002277 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002278 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002279#endif
2280
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002281 xprt_register_transport(&xs_udp_transport);
2282 xprt_register_transport(&xs_tcp_transport);
2283
Chuck Lever282b32e2006-12-05 16:35:51 -05002284 return 0;
2285}
2286
2287/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002288 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05002289 *
2290 */
2291void cleanup_socket_xprt(void)
2292{
Chuck Leverfbf76682006-12-05 16:35:54 -05002293#ifdef RPC_DEBUG
2294 if (sunrpc_table_header) {
2295 unregister_sysctl_table(sunrpc_table_header);
2296 sunrpc_table_header = NULL;
2297 }
2298#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002299
2300 xprt_unregister_transport(&xs_udp_transport);
2301 xprt_unregister_transport(&xs_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05002302}