blob: d40ff50887aa468544c500de540d7b6a29108b67 [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)
273
Chuck Lever95392c52007-08-06 11:57:58 -0400274static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400275{
Chuck Lever95392c52007-08-06 11:57:58 -0400276 return (struct sockaddr *) &xprt->addr;
277}
278
279static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
280{
281 return (struct sockaddr_in *) &xprt->addr;
282}
283
284static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
285{
286 return (struct sockaddr_in6 *) &xprt->addr;
287}
288
Chuck Leverb454ae92008-01-07 18:34:48 -0500289static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt,
290 const char *protocol,
291 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400292{
Chuck Lever95392c52007-08-06 11:57:58 -0400293 struct sockaddr_in *addr = xs_addr_in(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400294 char *buf;
295
296 buf = kzalloc(20, GFP_KERNEL);
297 if (buf) {
David S. Millere0db4a72008-11-02 23:57:06 -0800298 snprintf(buf, 20, "%pI4", &addr->sin_addr.s_addr);
Chuck Leveredb267a2006-08-22 20:06:18 -0400299 }
300 xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
301
302 buf = kzalloc(8, GFP_KERNEL);
303 if (buf) {
304 snprintf(buf, 8, "%u",
305 ntohs(addr->sin_port));
306 }
307 xprt->address_strings[RPC_DISPLAY_PORT] = buf;
308
Chuck Leverb454ae92008-01-07 18:34:48 -0500309 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leveredb267a2006-08-22 20:06:18 -0400310
311 buf = kzalloc(48, GFP_KERNEL);
312 if (buf) {
Harvey Harrison21454aa2008-10-31 00:54:56 -0700313 snprintf(buf, 48, "addr=%pI4 port=%u proto=%s",
314 &addr->sin_addr.s_addr,
Chuck Leveredb267a2006-08-22 20:06:18 -0400315 ntohs(addr->sin_port),
Chuck Leverb454ae92008-01-07 18:34:48 -0500316 protocol);
Chuck Leveredb267a2006-08-22 20:06:18 -0400317 }
318 xprt->address_strings[RPC_DISPLAY_ALL] = buf;
Chuck Leverfbfe3cc2007-08-06 11:57:02 -0400319
320 buf = kzalloc(10, GFP_KERNEL);
321 if (buf) {
322 snprintf(buf, 10, "%02x%02x%02x%02x",
323 NIPQUAD(addr->sin_addr.s_addr));
324 }
325 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
326
327 buf = kzalloc(8, GFP_KERNEL);
328 if (buf) {
329 snprintf(buf, 8, "%4hx",
330 ntohs(addr->sin_port));
331 }
332 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
Chuck Lever756805e2007-08-16 16:03:26 -0400333
334 buf = kzalloc(30, GFP_KERNEL);
335 if (buf) {
Harvey Harrison21454aa2008-10-31 00:54:56 -0700336 snprintf(buf, 30, "%pI4.%u.%u",
337 &addr->sin_addr.s_addr,
Chuck Lever756805e2007-08-16 16:03:26 -0400338 ntohs(addr->sin_port) >> 8,
339 ntohs(addr->sin_port) & 0xff);
340 }
341 xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
\"Talpey, Thomas\4417c8c2007-09-10 13:43:05 -0400342
Chuck Leverb454ae92008-01-07 18:34:48 -0500343 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leveredb267a2006-08-22 20:06:18 -0400344}
345
Chuck Leverb454ae92008-01-07 18:34:48 -0500346static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt,
347 const char *protocol,
348 const char *netid)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400349{
Chuck Lever95392c52007-08-06 11:57:58 -0400350 struct sockaddr_in6 *addr = xs_addr_in6(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400351 char *buf;
352
353 buf = kzalloc(40, GFP_KERNEL);
354 if (buf) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700355 snprintf(buf, 40, "%pI6",&addr->sin6_addr);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400356 }
357 xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
358
359 buf = kzalloc(8, GFP_KERNEL);
360 if (buf) {
361 snprintf(buf, 8, "%u",
362 ntohs(addr->sin6_port));
363 }
364 xprt->address_strings[RPC_DISPLAY_PORT] = buf;
365
Chuck Leverb454ae92008-01-07 18:34:48 -0500366 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Lever4b6473f2007-08-06 11:57:12 -0400367
368 buf = kzalloc(64, GFP_KERNEL);
369 if (buf) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700370 snprintf(buf, 64, "addr=%pI6 port=%u proto=%s",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -0700371 &addr->sin6_addr,
Chuck Lever4b6473f2007-08-06 11:57:12 -0400372 ntohs(addr->sin6_port),
Chuck Leverb454ae92008-01-07 18:34:48 -0500373 protocol);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400374 }
375 xprt->address_strings[RPC_DISPLAY_ALL] = buf;
376
377 buf = kzalloc(36, GFP_KERNEL);
Harvey Harrisonb0711952008-10-28 16:05:40 -0700378 if (buf)
Harvey Harrison4b7a4272008-10-29 12:50:24 -0700379 snprintf(buf, 36, "%pi6", &addr->sin6_addr);
Harvey Harrisonb0711952008-10-28 16:05:40 -0700380
Chuck Lever4b6473f2007-08-06 11:57:12 -0400381 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
382
383 buf = kzalloc(8, GFP_KERNEL);
384 if (buf) {
385 snprintf(buf, 8, "%4hx",
386 ntohs(addr->sin6_port));
387 }
388 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
Chuck Lever756805e2007-08-16 16:03:26 -0400389
390 buf = kzalloc(50, GFP_KERNEL);
391 if (buf) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700392 snprintf(buf, 50, "%pI6.%u.%u",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -0700393 &addr->sin6_addr,
394 ntohs(addr->sin6_port) >> 8,
395 ntohs(addr->sin6_port) & 0xff);
Chuck Lever756805e2007-08-16 16:03:26 -0400396 }
397 xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
\"Talpey, Thomas\4417c8c2007-09-10 13:43:05 -0400398
Chuck Leverb454ae92008-01-07 18:34:48 -0500399 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leveredb267a2006-08-22 20:06:18 -0400400}
401
402static void xs_free_peer_addresses(struct rpc_xprt *xprt)
403{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500404 unsigned int i;
405
406 for (i = 0; i < RPC_DISPLAY_MAX; i++)
407 switch (i) {
408 case RPC_DISPLAY_PROTO:
409 case RPC_DISPLAY_NETID:
410 continue;
411 default:
412 kfree(xprt->address_strings[i]);
413 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400414}
415
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400416#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
417
Trond Myklebust24c56842006-10-17 15:06:22 -0400418static 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 -0400419{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400420 struct msghdr msg = {
421 .msg_name = addr,
422 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400423 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
424 };
425 struct kvec iov = {
426 .iov_base = vec->iov_base + base,
427 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400428 };
429
Trond Myklebust24c56842006-10-17 15:06:22 -0400430 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400431 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
432 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
433}
434
Trond Myklebust24c56842006-10-17 15:06:22 -0400435static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400436{
Trond Myklebust24c56842006-10-17 15:06:22 -0400437 struct page **ppage;
438 unsigned int remainder;
439 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400440
Trond Myklebust24c56842006-10-17 15:06:22 -0400441 remainder = xdr->page_len - base;
442 base += xdr->page_base;
443 ppage = xdr->pages + (base >> PAGE_SHIFT);
444 base &= ~PAGE_MASK;
445 for(;;) {
446 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
447 int flags = XS_SENDMSG_FLAGS;
448
449 remainder -= len;
450 if (remainder != 0 || more)
451 flags |= MSG_MORE;
452 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
453 if (remainder == 0 || err != len)
454 break;
455 sent += err;
456 ppage++;
457 base = 0;
458 }
459 if (sent == 0)
460 return err;
461 if (err > 0)
462 sent += err;
463 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400464}
465
Chuck Lever9903cd12005-08-11 16:25:26 -0400466/**
467 * xs_sendpages - write pages directly to a socket
468 * @sock: socket to send on
469 * @addr: UDP only -- address of destination
470 * @addrlen: UDP only -- length of destination address
471 * @xdr: buffer containing this request
472 * @base: starting position in the buffer
473 *
Chuck Levera246b012005-08-11 16:25:23 -0400474 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400475static 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 -0400476{
Trond Myklebust24c56842006-10-17 15:06:22 -0400477 unsigned int remainder = xdr->len - base;
478 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400479
Chuck Lever262965f2005-08-11 16:25:56 -0400480 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400481 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400482
483 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400484 if (base != 0) {
485 addr = NULL;
486 addrlen = 0;
487 }
Chuck Lever262965f2005-08-11 16:25:56 -0400488
Trond Myklebust24c56842006-10-17 15:06:22 -0400489 if (base < xdr->head[0].iov_len || addr != NULL) {
490 unsigned int len = xdr->head[0].iov_len - base;
491 remainder -= len;
492 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
493 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400494 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400495 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400496 base = 0;
497 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400498 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400499
Trond Myklebust24c56842006-10-17 15:06:22 -0400500 if (base < xdr->page_len) {
501 unsigned int len = xdr->page_len - base;
502 remainder -= len;
503 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
504 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400505 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400506 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400507 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400508 } else
509 base -= xdr->page_len;
510
511 if (base >= xdr->tail[0].iov_len)
512 return sent;
513 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400514out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400515 if (sent == 0)
516 return err;
517 if (err > 0)
518 sent += err;
519 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400520}
521
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400522static void xs_nospace_callback(struct rpc_task *task)
523{
524 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
525
526 transport->inet->sk_write_pending--;
527 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
528}
529
Chuck Lever9903cd12005-08-11 16:25:26 -0400530/**
Chuck Lever262965f2005-08-11 16:25:56 -0400531 * xs_nospace - place task on wait queue if transmit was incomplete
532 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400533 *
Chuck Levera246b012005-08-11 16:25:23 -0400534 */
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400535static int xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400536{
Chuck Lever262965f2005-08-11 16:25:56 -0400537 struct rpc_rqst *req = task->tk_rqstp;
538 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500539 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400540 int ret = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400541
Chuck Lever46121cf2007-01-31 12:14:08 -0500542 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400543 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
544 req->rq_slen);
545
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400546 /* Protect against races with write_space */
547 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400548
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400549 /* Don't race with disconnect */
550 if (xprt_connected(xprt)) {
551 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400552 ret = -EAGAIN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400553 /*
554 * Notify TCP that we're limited by the application
555 * window size
556 */
557 set_bit(SOCK_NOSPACE, &transport->sock->flags);
558 transport->inet->sk_write_pending++;
559 /* ...and wait for more buffer space */
560 xprt_wait_for_buffer_space(task, xs_nospace_callback);
561 }
562 } else {
563 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400564 ret = -ENOTCONN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400565 }
Chuck Lever262965f2005-08-11 16:25:56 -0400566
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400567 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400568 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400569}
570
571/**
572 * xs_udp_send_request - write an RPC request to a UDP socket
573 * @task: address of RPC task that manages the state of an RPC request
574 *
575 * Return values:
576 * 0: The request has been sent
577 * EAGAIN: The socket was blocked, please call again later to
578 * complete the request
579 * ENOTCONN: Caller needs to invoke connect logic then call again
580 * other: Some other error occured, the request was not sent
581 */
582static int xs_udp_send_request(struct rpc_task *task)
583{
584 struct rpc_rqst *req = task->tk_rqstp;
585 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500586 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400587 struct xdr_buf *xdr = &req->rq_snd_buf;
588 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400589
Chuck Lever9903cd12005-08-11 16:25:26 -0400590 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400591 req->rq_svec->iov_base,
592 req->rq_svec->iov_len);
593
Trond Myklebust01d37c42009-03-11 14:09:39 -0400594 if (!xprt_bound(xprt))
595 return -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500596 status = xs_sendpages(transport->sock,
Chuck Lever95392c52007-08-06 11:57:58 -0400597 xs_addr(xprt),
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500598 xprt->addrlen, xdr,
599 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400600
Chuck Lever46121cf2007-01-31 12:14:08 -0500601 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400602 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400603
Trond Myklebust21997002007-10-01 11:43:37 -0400604 if (status >= 0) {
605 task->tk_bytes_sent += status;
606 if (status >= req->rq_slen)
607 return 0;
608 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400609 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400610 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400611 if (!transport->sock)
612 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400613
Chuck Lever262965f2005-08-11 16:25:56 -0400614 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400615 case -ENOTSOCK:
616 status = -ENOTCONN;
617 /* Should we call xs_close() here? */
618 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400619 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400620 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400621 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400622 default:
623 dprintk("RPC: sendmsg returned unrecognized error %d\n",
624 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400625 case -ENETUNREACH:
626 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400627 case -ECONNREFUSED:
628 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400629 * prompts ECONNREFUSED. */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400630 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400631 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400632out:
Chuck Lever262965f2005-08-11 16:25:56 -0400633 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400634}
635
Trond Myklebuste06799f2007-11-05 15:44:12 -0500636/**
637 * xs_tcp_shutdown - gracefully shut down a TCP socket
638 * @xprt: transport
639 *
640 * Initiates a graceful shutdown of the TCP socket by calling the
641 * equivalent of shutdown(SHUT_WR);
642 */
643static void xs_tcp_shutdown(struct rpc_xprt *xprt)
644{
645 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
646 struct socket *sock = transport->sock;
647
648 if (sock != NULL)
649 kernel_sock_shutdown(sock, SHUT_WR);
650}
651
Chuck Lever808012f2005-08-25 16:25:49 -0700652static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
653{
654 u32 reclen = buf->len - sizeof(rpc_fraghdr);
655 rpc_fraghdr *base = buf->head[0].iov_base;
656 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
657}
658
Chuck Lever9903cd12005-08-11 16:25:26 -0400659/**
Chuck Lever262965f2005-08-11 16:25:56 -0400660 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400661 * @task: address of RPC task that manages the state of an RPC request
662 *
663 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400664 * 0: The request has been sent
665 * EAGAIN: The socket was blocked, please call again later to
666 * complete the request
667 * ENOTCONN: Caller needs to invoke connect logic then call again
668 * other: Some other error occured, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400669 *
670 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400671 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400672 */
Chuck Lever262965f2005-08-11 16:25:56 -0400673static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400674{
675 struct rpc_rqst *req = task->tk_rqstp;
676 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500677 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400678 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Leverb595bb12007-08-06 11:56:42 -0400679 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400680
Chuck Lever808012f2005-08-25 16:25:49 -0700681 xs_encode_tcp_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400682
Chuck Lever262965f2005-08-11 16:25:56 -0400683 xs_pktdump("packet data:",
684 req->rq_svec->iov_base,
685 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400686
687 /* Continue transmitting the packet/record. We must be careful
688 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400689 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400690 while (1) {
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500691 status = xs_sendpages(transport->sock,
692 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400693
Chuck Lever46121cf2007-01-31 12:14:08 -0500694 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400695 xdr->len - req->rq_bytes_sent, status);
696
697 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400698 break;
699
Chuck Lever262965f2005-08-11 16:25:56 -0400700 /* If we've sent the entire packet, immediately
701 * reset the count of bytes sent. */
702 req->rq_bytes_sent += status;
Chuck Leveref759a22006-03-20 13:44:17 -0500703 task->tk_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400704 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
705 req->rq_bytes_sent = 0;
706 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400707 }
708
Trond Myklebust06b4b682008-04-16 16:51:38 -0400709 if (status != 0)
710 continue;
Chuck Levera246b012005-08-11 16:25:23 -0400711 status = -EAGAIN;
Trond Myklebust06b4b682008-04-16 16:51:38 -0400712 break;
Chuck Levera246b012005-08-11 16:25:23 -0400713 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400714 if (!transport->sock)
715 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400716
Chuck Lever262965f2005-08-11 16:25:56 -0400717 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400718 case -ENOTSOCK:
719 status = -ENOTCONN;
720 /* Should we call xs_close() here? */
721 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400722 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400723 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400724 break;
725 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500726 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400727 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400728 case -ECONNRESET:
Trond Myklebust55420c22009-03-11 15:29:24 -0400729 case -EPIPE:
Trond Myklebuste06799f2007-11-05 15:44:12 -0500730 xs_tcp_shutdown(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400731 case -ECONNREFUSED:
732 case -ENOTCONN:
Chuck Lever262965f2005-08-11 16:25:56 -0400733 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400734 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400735out:
Chuck Levera246b012005-08-11 16:25:23 -0400736 return status;
737}
738
Chuck Lever9903cd12005-08-11 16:25:26 -0400739/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400740 * xs_tcp_release_xprt - clean up after a tcp transmission
741 * @xprt: transport
742 * @task: rpc task
743 *
744 * This cleans up if an error causes us to abort the transmission of a request.
745 * In this case, the socket may need to be reset in order to avoid confusing
746 * the server.
747 */
748static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
749{
750 struct rpc_rqst *req;
751
752 if (task != xprt->snd_task)
753 return;
754 if (task == NULL)
755 goto out_release;
756 req = task->tk_rqstp;
757 if (req->rq_bytes_sent == 0)
758 goto out_release;
759 if (req->rq_bytes_sent == req->rq_snd_buf.len)
760 goto out_release;
761 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
762out_release:
763 xprt_release_xprt(xprt, task);
764}
765
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400766static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
767{
768 transport->old_data_ready = sk->sk_data_ready;
769 transport->old_state_change = sk->sk_state_change;
770 transport->old_write_space = sk->sk_write_space;
771 transport->old_error_report = sk->sk_error_report;
772}
773
774static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
775{
776 sk->sk_data_ready = transport->old_data_ready;
777 sk->sk_state_change = transport->old_state_change;
778 sk->sk_write_space = transport->old_write_space;
779 sk->sk_error_report = transport->old_error_report;
780}
781
Chuck Leverfe315e72009-03-11 14:10:21 -0400782static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400783{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500784 struct socket *sock = transport->sock;
785 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400786
Chuck Leverfe315e72009-03-11 14:10:21 -0400787 if (sk == NULL)
788 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400789
Chuck Levera246b012005-08-11 16:25:23 -0400790 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500791 transport->inet = NULL;
792 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400793
Chuck Lever9903cd12005-08-11 16:25:26 -0400794 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400795
796 xs_restore_old_callbacks(transport, sk);
Chuck Levera246b012005-08-11 16:25:23 -0400797 write_unlock_bh(&sk->sk_callback_lock);
798
Chuck Lever9903cd12005-08-11 16:25:26 -0400799 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400800
801 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -0400802}
803
804/**
805 * xs_close - close a socket
806 * @xprt: transport
807 *
808 * This is used when all requests are complete; ie, no DRC state remains
809 * on the server we want to save.
810 */
811static void xs_close(struct rpc_xprt *xprt)
812{
813 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
814
815 dprintk("RPC: xs_close xprt %p\n", xprt);
816
817 xs_reset_transport(transport);
818
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100819 smp_mb__before_clear_bit();
Trond Myklebust7d1e8252009-03-11 14:38:03 -0400820 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100821 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -0500822 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100823 smp_mb__after_clear_bit();
Trond Myklebust62da3b22007-11-06 18:44:20 -0500824 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400825}
826
Chuck Lever9903cd12005-08-11 16:25:26 -0400827/**
828 * xs_destroy - prepare to shutdown a transport
829 * @xprt: doomed transport
830 *
831 */
832static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400833{
Chuck Leverc8475462006-12-05 16:35:26 -0500834 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
835
Chuck Lever46121cf2007-01-31 12:14:08 -0500836 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400837
Trond Myklebustc1384c92007-06-14 18:00:42 -0400838 cancel_rearming_delayed_work(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400839
Chuck Lever9903cd12005-08-11 16:25:26 -0400840 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400841 xs_free_peer_addresses(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400842 kfree(xprt->slot);
Chuck Leverc8541ec2006-10-17 14:44:27 -0400843 kfree(xprt);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -0400844 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400845}
846
Chuck Lever9903cd12005-08-11 16:25:26 -0400847static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400848{
Chuck Lever9903cd12005-08-11 16:25:26 -0400849 return (struct rpc_xprt *) sk->sk_user_data;
850}
851
852/**
853 * xs_udp_data_ready - "data ready" callback for UDP sockets
854 * @sk: socket with data to read
855 * @len: how much data to read
856 *
857 */
858static void xs_udp_data_ready(struct sock *sk, int len)
859{
860 struct rpc_task *task;
861 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400862 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400863 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400864 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700865 u32 _xid;
866 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400867
868 read_lock(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500869 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400870 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400871 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400872
873 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
874 goto out;
875
876 if (xprt->shutdown)
877 goto dropit;
878
879 repsize = skb->len - sizeof(struct udphdr);
880 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500881 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400882 goto dropit;
883 }
884
885 /* Copy the XID from the skb... */
886 xp = skb_header_pointer(skb, sizeof(struct udphdr),
887 sizeof(_xid), &_xid);
888 if (xp == NULL)
889 goto dropit;
890
891 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400892 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400893 rovr = xprt_lookup_rqst(xprt, *xp);
894 if (!rovr)
895 goto out_unlock;
896 task = rovr->rq_task;
897
Chuck Levera246b012005-08-11 16:25:23 -0400898 if ((copied = rovr->rq_private_buf.buflen) > repsize)
899 copied = repsize;
900
901 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -0800902 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
903 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -0400904 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -0800905 }
906
907 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -0400908
909 /* Something worked... */
910 dst_confirm(skb->dst);
911
Chuck Lever1570c1e2005-08-25 16:25:52 -0700912 xprt_adjust_cwnd(task, copied);
913 xprt_update_rtt(task);
914 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -0400915
916 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400917 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400918 dropit:
919 skb_free_datagram(sk, skb);
920 out:
921 read_unlock(&sk->sk_callback_lock);
922}
923
Chuck Leverdd456472006-12-05 16:35:44 -0500924static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400925{
Chuck Lever51971132006-12-05 16:35:19 -0500926 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400927 size_t len, used;
928 char *p;
929
Chuck Lever51971132006-12-05 16:35:19 -0500930 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
931 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500932 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500933 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400934 if (used != len)
935 return;
Chuck Lever808012f2005-08-25 16:25:49 -0700936
Chuck Lever51971132006-12-05 16:35:19 -0500937 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
938 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -0500939 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -0400940 else
Chuck Levere136d092006-12-05 16:35:23 -0500941 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -0500942 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -0700943
Chuck Levere136d092006-12-05 16:35:23 -0500944 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500945 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -0700946
Chuck Levera246b012005-08-11 16:25:23 -0400947 /* Sanity check of the record length */
Chuck Lever51971132006-12-05 16:35:19 -0500948 if (unlikely(transport->tcp_reclen < 4)) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500949 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebust3ebb0672007-11-06 18:40:12 -0500950 xprt_force_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400951 return;
Chuck Levera246b012005-08-11 16:25:23 -0400952 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500953 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -0500954 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400955}
956
Chuck Lever51971132006-12-05 16:35:19 -0500957static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400958{
Chuck Lever51971132006-12-05 16:35:19 -0500959 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -0500960 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500961 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -0500962 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
963 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
964 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -0500965 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400966 }
967 }
968}
969
Chuck Leverdd456472006-12-05 16:35:44 -0500970static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400971{
972 size_t len, used;
973 char *p;
974
Chuck Lever51971132006-12-05 16:35:19 -0500975 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -0500976 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -0500977 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500978 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500979 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400980 if (used != len)
981 return;
Chuck Levere136d092006-12-05 16:35:23 -0500982 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
983 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -0500984 transport->tcp_copied = 4;
Chuck Lever46121cf2007-01-31 12:14:08 -0500985 dprintk("RPC: reading reply for XID %08x\n",
Chuck Lever51971132006-12-05 16:35:19 -0500986 ntohl(transport->tcp_xid));
987 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400988}
989
Chuck Leverdd456472006-12-05 16:35:44 -0500990static inline void xs_tcp_read_request(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400991{
Chuck Lever51971132006-12-05 16:35:19 -0500992 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400993 struct rpc_rqst *req;
994 struct xdr_buf *rcvbuf;
995 size_t len;
996 ssize_t r;
997
998 /* Find and lock the request corresponding to this xid */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400999 spin_lock(&xprt->transport_lock);
Chuck Lever51971132006-12-05 16:35:19 -05001000 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
Chuck Levera246b012005-08-11 16:25:23 -04001001 if (!req) {
Chuck Levere136d092006-12-05 16:35:23 -05001002 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001003 dprintk("RPC: XID %08x request not found!\n",
Chuck Lever51971132006-12-05 16:35:19 -05001004 ntohl(transport->tcp_xid));
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001005 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001006 return;
1007 }
1008
1009 rcvbuf = &req->rq_private_buf;
1010 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -05001011 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -05001012 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -04001013
Chuck Lever51971132006-12-05 16:35:19 -05001014 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001015 memcpy(&my_desc, desc, sizeof(my_desc));
1016 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -05001017 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001018 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001019 desc->count -= r;
1020 desc->offset += r;
1021 } else
Chuck Lever51971132006-12-05 16:35:19 -05001022 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001023 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001024
1025 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001026 transport->tcp_copied += r;
1027 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001028 }
1029 if (r != len) {
1030 /* Error when copying to the receive buffer,
1031 * usually because we weren't able to allocate
1032 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001033 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001034 * will not receive any additional updates,
1035 * and time out.
1036 * Any remaining data from this record will
1037 * be discarded.
1038 */
Chuck Levere136d092006-12-05 16:35:23 -05001039 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001040 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001041 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001042 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1043 "tcp_offset = %u, tcp_reclen = %u\n",
1044 xprt, transport->tcp_copied,
1045 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001046 goto out;
1047 }
1048
Chuck Lever46121cf2007-01-31 12:14:08 -05001049 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001050 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001051 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1052 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1053 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001054
Chuck Lever51971132006-12-05 16:35:19 -05001055 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001056 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001057 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001058 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1059 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001060 }
1061
1062out:
Chuck Levere136d092006-12-05 16:35:23 -05001063 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001064 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001065 spin_unlock(&xprt->transport_lock);
Chuck Lever51971132006-12-05 16:35:19 -05001066 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001067}
1068
Chuck Leverdd456472006-12-05 16:35:44 -05001069static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001070{
1071 size_t len;
1072
Chuck Lever51971132006-12-05 16:35:19 -05001073 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001074 if (len > desc->count)
1075 len = desc->count;
1076 desc->count -= len;
1077 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001078 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001079 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001080 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001081}
1082
Chuck Lever9903cd12005-08-11 16:25:26 -04001083static 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 -04001084{
1085 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001086 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001087 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001088 .skb = skb,
1089 .offset = offset,
1090 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001091 };
Chuck Levera246b012005-08-11 16:25:23 -04001092
Chuck Lever46121cf2007-01-31 12:14:08 -05001093 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001094 do {
1095 /* Read in a new fragment marker if necessary */
1096 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001097 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001098 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001099 continue;
1100 }
1101 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001102 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001103 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001104 continue;
1105 }
1106 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001107 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001108 xs_tcp_read_request(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001109 continue;
1110 }
1111 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001112 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001113 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001114 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001115 return len - desc.count;
1116}
1117
Chuck Lever9903cd12005-08-11 16:25:26 -04001118/**
1119 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1120 * @sk: socket with data to read
1121 * @bytes: how much data to read
1122 *
1123 */
1124static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001125{
1126 struct rpc_xprt *xprt;
1127 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001128 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001129
Chuck Lever46121cf2007-01-31 12:14:08 -05001130 dprintk("RPC: xs_tcp_data_ready...\n");
1131
Chuck Levera246b012005-08-11 16:25:23 -04001132 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001133 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001134 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001135 if (xprt->shutdown)
1136 goto out;
1137
Chuck Lever9903cd12005-08-11 16:25:26 -04001138 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001139 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001140 do {
1141 rd_desc.count = 65536;
1142 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1143 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001144out:
1145 read_unlock(&sk->sk_callback_lock);
1146}
1147
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001148/*
1149 * Do the equivalent of linger/linger2 handling for dealing with
1150 * broken servers that don't close the socket in a timely
1151 * fashion
1152 */
1153static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1154 unsigned long timeout)
1155{
1156 struct sock_xprt *transport;
1157
1158 if (xprt_test_and_set_connecting(xprt))
1159 return;
1160 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1161 transport = container_of(xprt, struct sock_xprt, xprt);
1162 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1163 timeout);
1164}
1165
1166static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1167{
1168 struct sock_xprt *transport;
1169
1170 transport = container_of(xprt, struct sock_xprt, xprt);
1171
1172 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1173 !cancel_delayed_work(&transport->connect_worker))
1174 return;
1175 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1176 xprt_clear_connecting(xprt);
1177}
1178
1179static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1180{
1181 smp_mb__before_clear_bit();
1182 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1183 clear_bit(XPRT_CLOSING, &xprt->state);
1184 smp_mb__after_clear_bit();
1185 /* Mark transport as closed and wake up all pending tasks */
1186 xprt_disconnect_done(xprt);
1187}
1188
Chuck Lever9903cd12005-08-11 16:25:26 -04001189/**
1190 * xs_tcp_state_change - callback to handle TCP socket state changes
1191 * @sk: socket whose state has changed
1192 *
1193 */
1194static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001195{
Chuck Lever9903cd12005-08-11 16:25:26 -04001196 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001197
1198 read_lock(&sk->sk_callback_lock);
1199 if (!(xprt = xprt_from_sock(sk)))
1200 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001201 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
1202 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
1203 sk->sk_state, xprt_connected(xprt),
1204 sock_flag(sk, SOCK_DEAD),
1205 sock_flag(sk, SOCK_ZAPPED));
Chuck Levera246b012005-08-11 16:25:23 -04001206
1207 switch (sk->sk_state) {
1208 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001209 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001210 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001211 struct sock_xprt *transport = container_of(xprt,
1212 struct sock_xprt, xprt);
1213
Chuck Levera246b012005-08-11 16:25:23 -04001214 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001215 transport->tcp_offset = 0;
1216 transport->tcp_reclen = 0;
1217 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001218 transport->tcp_flags =
1219 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001220
Trond Myklebust2a491992009-03-11 14:38:00 -04001221 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001222 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001223 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001224 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001225 case TCP_FIN_WAIT1:
1226 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001227 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001228 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001229 set_bit(XPRT_CLOSING, &xprt->state);
1230 smp_mb__before_clear_bit();
1231 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001232 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001233 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001234 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001235 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001236 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001237 /* The server initiated a shutdown of the socket */
Trond Myklebust66af1e52007-11-06 10:18:36 -05001238 xprt_force_disconnect(xprt);
Trond Myklebust663b8852008-01-01 18:42:12 -05001239 case TCP_SYN_SENT:
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001240 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001241 case TCP_CLOSING:
1242 /*
1243 * If the server closed down the connection, make sure that
1244 * we back off before reconnecting
1245 */
1246 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1247 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001248 break;
1249 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001250 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001251 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001252 smp_mb__before_clear_bit();
1253 clear_bit(XPRT_CONNECTED, &xprt->state);
1254 smp_mb__after_clear_bit();
1255 break;
1256 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001257 xs_tcp_cancel_linger_timeout(xprt);
1258 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001259 }
1260 out:
1261 read_unlock(&sk->sk_callback_lock);
1262}
1263
Chuck Lever9903cd12005-08-11 16:25:26 -04001264/**
Trond Myklebust482f32e2009-03-11 14:38:00 -04001265 * xs_error_report - callback mainly for catching socket errors
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001266 * @sk: socket
1267 */
Trond Myklebust482f32e2009-03-11 14:38:00 -04001268static void xs_error_report(struct sock *sk)
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001269{
1270 struct rpc_xprt *xprt;
1271
1272 read_lock(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001273 if (!(xprt = xprt_from_sock(sk)))
1274 goto out;
1275 dprintk("RPC: %s client %p...\n"
1276 "RPC: error %d\n",
1277 __func__, xprt, sk->sk_err);
Trond Myklebust482f32e2009-03-11 14:38:00 -04001278 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001279out:
1280 read_unlock(&sk->sk_callback_lock);
1281}
1282
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001283static void xs_write_space(struct sock *sk)
1284{
1285 struct socket *sock;
1286 struct rpc_xprt *xprt;
1287
1288 if (unlikely(!(sock = sk->sk_socket)))
1289 return;
1290 clear_bit(SOCK_NOSPACE, &sock->flags);
1291
1292 if (unlikely(!(xprt = xprt_from_sock(sk))))
1293 return;
1294 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1295 return;
1296
1297 xprt_write_space(xprt);
1298}
1299
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001300/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001301 * xs_udp_write_space - callback invoked when socket buffer space
1302 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001303 * @sk: socket whose state has changed
1304 *
Chuck Levera246b012005-08-11 16:25:23 -04001305 * Called when more output buffer space is available for this socket.
1306 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001307 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001308 * with a bunch of small requests.
1309 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001310static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001311{
Chuck Levera246b012005-08-11 16:25:23 -04001312 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001313
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001314 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001315 if (sock_writeable(sk))
1316 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001317
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001318 read_unlock(&sk->sk_callback_lock);
1319}
Chuck Levera246b012005-08-11 16:25:23 -04001320
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001321/**
1322 * xs_tcp_write_space - callback invoked when socket buffer space
1323 * becomes available
1324 * @sk: socket whose state has changed
1325 *
1326 * Called when more output buffer space is available for this socket.
1327 * We try not to wake our writers until they can make "significant"
1328 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1329 * with a bunch of small requests.
1330 */
1331static void xs_tcp_write_space(struct sock *sk)
1332{
1333 read_lock(&sk->sk_callback_lock);
1334
1335 /* from net/core/stream.c:sk_stream_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001336 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1337 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001338
Chuck Levera246b012005-08-11 16:25:23 -04001339 read_unlock(&sk->sk_callback_lock);
1340}
1341
Chuck Lever470056c2005-08-25 16:25:56 -07001342static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001343{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001344 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1345 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001346
Chuck Lever7c6e0662006-12-05 16:35:30 -05001347 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001348 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001349 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001350 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001351 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001352 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001353 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001354 sk->sk_write_space(sk);
1355 }
1356}
1357
Chuck Lever43118c22005-08-25 16:25:49 -07001358/**
Chuck Lever470056c2005-08-25 16:25:56 -07001359 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001360 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001361 * @sndsize: requested size of send buffer, in bytes
1362 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001363 *
Chuck Lever470056c2005-08-25 16:25:56 -07001364 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001365 */
Chuck Lever470056c2005-08-25 16:25:56 -07001366static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001367{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001368 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1369
1370 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001371 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001372 transport->sndsize = sndsize + 1024;
1373 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001374 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001375 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001376
1377 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001378}
1379
Chuck Lever46c0ee82005-08-25 16:25:52 -07001380/**
1381 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1382 * @task: task that timed out
1383 *
1384 * Adjust the congestion window after a retransmit timeout has occurred.
1385 */
1386static void xs_udp_timer(struct rpc_task *task)
1387{
1388 xprt_adjust_cwnd(task, -ETIMEDOUT);
1389}
1390
Chuck Leverb85d8802006-05-25 01:40:49 -04001391static unsigned short xs_get_random_port(void)
1392{
1393 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1394 unsigned short rand = (unsigned short) net_random() % range;
1395 return rand + xprt_min_resvport;
1396}
1397
Chuck Lever92200412006-01-03 09:55:51 +01001398/**
1399 * xs_set_port - reset the port number in the remote endpoint address
1400 * @xprt: generic transport
1401 * @port: new port number
1402 *
1403 */
1404static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1405{
Chuck Lever95392c52007-08-06 11:57:58 -04001406 struct sockaddr *addr = xs_addr(xprt);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001407
Chuck Lever46121cf2007-01-31 12:14:08 -05001408 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001409
Chuck Lever20612002007-08-06 11:57:23 -04001410 switch (addr->sa_family) {
1411 case AF_INET:
1412 ((struct sockaddr_in *)addr)->sin_port = htons(port);
1413 break;
1414 case AF_INET6:
1415 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
1416 break;
1417 default:
1418 BUG();
1419 }
Chuck Lever92200412006-01-03 09:55:51 +01001420}
1421
Trond Myklebust67a391d2007-11-05 17:40:58 -05001422static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
1423{
1424 unsigned short port = transport->port;
1425
1426 if (port == 0 && transport->xprt.resvport)
1427 port = xs_get_random_port();
1428 return port;
1429}
1430
1431static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
1432{
1433 if (transport->port != 0)
1434 transport->port = 0;
1435 if (!transport->xprt.resvport)
1436 return 0;
1437 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1438 return xprt_max_resvport;
1439 return --port;
1440}
1441
Chuck Lever7dc753f2007-08-06 11:57:28 -04001442static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001443{
1444 struct sockaddr_in myaddr = {
1445 .sin_family = AF_INET,
1446 };
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001447 struct sockaddr_in *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001448 int err, nloop = 0;
1449 unsigned short port = xs_get_srcport(transport, sock);
1450 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001451
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001452 sa = (struct sockaddr_in *)&transport->addr;
1453 myaddr.sin_addr = sa->sin_addr;
Chuck Levera246b012005-08-11 16:25:23 -04001454 do {
1455 myaddr.sin_port = htons(port);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001456 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
Chuck Levera246b012005-08-11 16:25:23 -04001457 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001458 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001459 break;
Chuck Levera246b012005-08-11 16:25:23 -04001460 if (err == 0) {
Chuck Leverc8475462006-12-05 16:35:26 -05001461 transport->port = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001462 break;
Chuck Levera246b012005-08-11 16:25:23 -04001463 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001464 last = port;
1465 port = xs_next_srcport(transport, sock, port);
1466 if (port > last)
1467 nloop++;
1468 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison21454aa2008-10-31 00:54:56 -07001469 dprintk("RPC: %s %pI4:%u: %s (%d)\n",
1470 __func__, &myaddr.sin_addr,
Chuck Lever7dc753f2007-08-06 11:57:28 -04001471 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001472 return err;
1473}
1474
Chuck Lever90058d32007-08-06 11:57:33 -04001475static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
1476{
1477 struct sockaddr_in6 myaddr = {
1478 .sin6_family = AF_INET6,
1479 };
1480 struct sockaddr_in6 *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001481 int err, nloop = 0;
1482 unsigned short port = xs_get_srcport(transport, sock);
1483 unsigned short last;
Chuck Lever90058d32007-08-06 11:57:33 -04001484
Chuck Lever90058d32007-08-06 11:57:33 -04001485 sa = (struct sockaddr_in6 *)&transport->addr;
1486 myaddr.sin6_addr = sa->sin6_addr;
1487 do {
1488 myaddr.sin6_port = htons(port);
1489 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1490 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001491 if (port == 0)
Chuck Lever90058d32007-08-06 11:57:33 -04001492 break;
1493 if (err == 0) {
1494 transport->port = port;
1495 break;
1496 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001497 last = port;
1498 port = xs_next_srcport(transport, sock, port);
1499 if (port > last)
1500 nloop++;
1501 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001502 dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07001503 &myaddr.sin6_addr, port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001504 return err;
1505}
1506
Peter Zijlstraed075362006-12-06 20:35:24 -08001507#ifdef CONFIG_DEBUG_LOCK_ALLOC
1508static struct lock_class_key xs_key[2];
1509static struct lock_class_key xs_slock_key[2];
1510
Chuck Lever8945ee52007-08-06 11:58:04 -04001511static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001512{
1513 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001514
John Heffner02b3d342007-09-12 10:42:12 +02001515 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001516 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1517 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1518}
Peter Zijlstraed075362006-12-06 20:35:24 -08001519
Chuck Lever8945ee52007-08-06 11:58:04 -04001520static inline void xs_reclassify_socket6(struct socket *sock)
1521{
1522 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001523
Linus Torvaldsf4921af2007-10-15 10:46:05 -07001524 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001525 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1526 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001527}
1528#else
Chuck Lever8945ee52007-08-06 11:58:04 -04001529static inline void xs_reclassify_socket4(struct socket *sock)
1530{
1531}
1532
1533static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001534{
1535}
1536#endif
1537
Chuck Lever16be2d22007-08-06 11:57:38 -04001538static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001539{
Chuck Lever16be2d22007-08-06 11:57:38 -04001540 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001541
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001542 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001543 struct sock *sk = sock->sk;
1544
1545 write_lock_bh(&sk->sk_callback_lock);
1546
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001547 xs_save_old_callbacks(transport, sk);
1548
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001549 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001550 sk->sk_data_ready = xs_udp_data_ready;
1551 sk->sk_write_space = xs_udp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001552 sk->sk_error_report = xs_error_report;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001553 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa72005-12-13 16:13:52 -05001554 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001555
1556 xprt_set_connected(xprt);
1557
1558 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001559 transport->sock = sock;
1560 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001561
1562 write_unlock_bh(&sk->sk_callback_lock);
1563 }
Chuck Lever470056c2005-08-25 16:25:56 -07001564 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04001565}
1566
Chuck Levera246b012005-08-11 16:25:23 -04001567/**
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001568 * xs_udp_connect_worker4 - set up a UDP socket
Chuck Levera246b012005-08-11 16:25:23 -04001569 * @work: RPC transport to connect
1570 *
1571 * Invoked by a work queue tasklet.
1572 */
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001573static void xs_udp_connect_worker4(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04001574{
1575 struct sock_xprt *transport =
1576 container_of(work, struct sock_xprt, connect_worker.work);
1577 struct rpc_xprt *xprt = &transport->xprt;
1578 struct socket *sock = transport->sock;
1579 int err, status = -EIO;
1580
Trond Myklebust01d37c42009-03-11 14:09:39 -04001581 if (xprt->shutdown)
Chuck Lever9903cd12005-08-11 16:25:26 -04001582 goto out;
1583
Chuck Levera246b012005-08-11 16:25:23 -04001584 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001585 xs_reset_transport(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001586
Chuck Leverfe315e72009-03-11 14:10:21 -04001587 err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1588 if (err < 0) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001589 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001590 goto out;
1591 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001592 xs_reclassify_socket4(sock);
Chuck Levera246b012005-08-11 16:25:23 -04001593
Chuck Lever7dc753f2007-08-06 11:57:28 -04001594 if (xs_bind4(transport, sock)) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001595 sock_release(sock);
1596 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001597 }
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001598
1599 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1600 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1601
Chuck Lever16be2d22007-08-06 11:57:38 -04001602 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001603 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001604out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001605 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001606 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001607}
1608
Chuck Lever68e220b2007-08-06 11:57:48 -04001609/**
1610 * xs_udp_connect_worker6 - set up a UDP socket
1611 * @work: RPC transport to connect
1612 *
1613 * Invoked by a work queue tasklet.
1614 */
1615static void xs_udp_connect_worker6(struct work_struct *work)
1616{
1617 struct sock_xprt *transport =
1618 container_of(work, struct sock_xprt, connect_worker.work);
1619 struct rpc_xprt *xprt = &transport->xprt;
1620 struct socket *sock = transport->sock;
1621 int err, status = -EIO;
1622
Trond Myklebust01d37c42009-03-11 14:09:39 -04001623 if (xprt->shutdown)
Chuck Lever68e220b2007-08-06 11:57:48 -04001624 goto out;
1625
1626 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001627 xs_reset_transport(transport);
Chuck Lever68e220b2007-08-06 11:57:48 -04001628
Chuck Leverfe315e72009-03-11 14:10:21 -04001629 err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
1630 if (err < 0) {
Chuck Lever68e220b2007-08-06 11:57:48 -04001631 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1632 goto out;
1633 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001634 xs_reclassify_socket6(sock);
Chuck Lever68e220b2007-08-06 11:57:48 -04001635
1636 if (xs_bind6(transport, sock) < 0) {
1637 sock_release(sock);
1638 goto out;
1639 }
1640
1641 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1642 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1643
1644 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001645 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001646out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001647 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001648 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001649}
1650
Chuck Lever3167e122005-08-25 16:25:55 -07001651/*
1652 * We need to preserve the port number so the reply cache on the server can
1653 * find our cached RPC replies when we get around to reconnecting.
1654 */
Trond Myklebust40d25492009-03-11 14:37:58 -04001655static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07001656{
1657 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07001658 struct sockaddr any;
1659
Chuck Lever46121cf2007-01-31 12:14:08 -05001660 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
Chuck Lever3167e122005-08-25 16:25:55 -07001661
1662 /*
1663 * Disconnect the transport socket by doing a connect operation
1664 * with AF_UNSPEC. This should return immediately...
1665 */
1666 memset(&any, 0, sizeof(any));
1667 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001668 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001669 if (!result)
1670 xs_sock_mark_closed(xprt);
1671 else
Chuck Lever46121cf2007-01-31 12:14:08 -05001672 dprintk("RPC: AF_UNSPEC connect return code %d\n",
Chuck Lever3167e122005-08-25 16:25:55 -07001673 result);
1674}
1675
Trond Myklebust40d25492009-03-11 14:37:58 -04001676static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
1677{
1678 unsigned int state = transport->inet->sk_state;
1679
1680 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED)
1681 return;
1682 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT))
1683 return;
1684 xs_abort_connection(xprt, transport);
1685}
1686
Chuck Lever16be2d22007-08-06 11:57:38 -04001687static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001688{
Chuck Lever16be2d22007-08-06 11:57:38 -04001689 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001690
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001691 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001692 struct sock *sk = sock->sk;
1693
1694 write_lock_bh(&sk->sk_callback_lock);
1695
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001696 xs_save_old_callbacks(transport, sk);
1697
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001698 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001699 sk->sk_data_ready = xs_tcp_data_ready;
1700 sk->sk_state_change = xs_tcp_state_change;
1701 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001702 sk->sk_error_report = xs_error_report;
Trond Myklebustb079fa72005-12-13 16:13:52 -05001703 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07001704
1705 /* socket options */
1706 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1707 sock_reset_flag(sk, SOCK_LINGER);
1708 tcp_sk(sk)->linger2 = 0;
1709 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001710
1711 xprt_clear_connected(xprt);
1712
1713 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001714 transport->sock = sock;
1715 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001716
1717 write_unlock_bh(&sk->sk_callback_lock);
1718 }
1719
Trond Myklebust01d37c42009-03-11 14:09:39 -04001720 if (!xprt_bound(xprt))
1721 return -ENOTCONN;
1722
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001723 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05001724 xprt->stat.connect_count++;
1725 xprt->stat.connect_start = jiffies;
Chuck Lever95392c52007-08-06 11:57:58 -04001726 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
Chuck Lever16be2d22007-08-06 11:57:38 -04001727}
1728
1729/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001730 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1731 * @xprt: RPC transport to connect
1732 * @transport: socket transport to connect
1733 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04001734 *
1735 * Invoked by a work queue tasklet.
1736 */
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001737static void xs_tcp_setup_socket(struct rpc_xprt *xprt,
1738 struct sock_xprt *transport,
1739 struct socket *(*create_sock)(struct rpc_xprt *,
1740 struct sock_xprt *))
Chuck Lever16be2d22007-08-06 11:57:38 -04001741{
Chuck Lever16be2d22007-08-06 11:57:38 -04001742 struct socket *sock = transport->sock;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001743 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04001744
Trond Myklebust01d37c42009-03-11 14:09:39 -04001745 if (xprt->shutdown)
Chuck Lever16be2d22007-08-06 11:57:38 -04001746 goto out;
1747
1748 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001749 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001750 sock = create_sock(xprt, transport);
1751 if (IS_ERR(sock)) {
1752 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04001753 goto out;
1754 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001755 } else {
1756 int abort_and_exit;
1757
1758 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
1759 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04001760 /* "close" the socket, preserving the local port */
Trond Myklebust40d25492009-03-11 14:37:58 -04001761 xs_tcp_reuse_connection(xprt, transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04001762
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001763 if (abort_and_exit)
1764 goto out_eagain;
1765 }
1766
Chuck Lever16be2d22007-08-06 11:57:38 -04001767 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1768 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1769
1770 status = xs_tcp_finish_connecting(xprt, sock);
Chuck Lever46121cf2007-01-31 12:14:08 -05001771 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1772 xprt, -status, xprt_connected(xprt),
1773 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04001774 switch (status) {
Trond Myklebust8a2cec22009-03-11 14:38:01 -04001775 case -ECONNREFUSED:
1776 case -ECONNRESET:
1777 case -ENETUNREACH:
1778 /* retry with existing socket, after a delay */
Trond Myklebust2a491992009-03-11 14:38:00 -04001779 case 0:
1780 case -EINPROGRESS:
1781 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001782 xprt_clear_connecting(xprt);
1783 return;
Chuck Levera246b012005-08-11 16:25:23 -04001784 }
Trond Myklebust8a2cec22009-03-11 14:38:01 -04001785 /* get rid of existing socket, and retry */
1786 xs_tcp_shutdown(xprt);
1787 printk("%s: connect returned unhandled error %d\n",
1788 __func__, status);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001789out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04001790 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04001791out:
Chuck Lever2226feb2005-08-11 16:25:38 -04001792 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001793 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001794}
1795
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001796static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt,
1797 struct sock_xprt *transport)
1798{
1799 struct socket *sock;
1800 int err;
1801
1802 /* start from scratch */
1803 err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1804 if (err < 0) {
1805 dprintk("RPC: can't create TCP transport socket (%d).\n",
1806 -err);
1807 goto out_err;
1808 }
1809 xs_reclassify_socket4(sock);
1810
1811 if (xs_bind4(transport, sock) < 0) {
1812 sock_release(sock);
1813 goto out_err;
1814 }
1815 return sock;
1816out_err:
1817 return ERR_PTR(-EIO);
1818}
1819
1820/**
Chuck Levera246b012005-08-11 16:25:23 -04001821 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1822 * @work: RPC transport to connect
1823 *
1824 * Invoked by a work queue tasklet.
1825 */
1826static void xs_tcp_connect_worker4(struct work_struct *work)
1827{
1828 struct sock_xprt *transport =
1829 container_of(work, struct sock_xprt, connect_worker.work);
1830 struct rpc_xprt *xprt = &transport->xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001831
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001832 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4);
1833}
Chuck Levera246b012005-08-11 16:25:23 -04001834
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001835static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt,
1836 struct sock_xprt *transport)
1837{
1838 struct socket *sock;
1839 int err;
Chuck Lever9903cd12005-08-11 16:25:26 -04001840
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001841 /* start from scratch */
1842 err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock);
1843 if (err < 0) {
1844 dprintk("RPC: can't create TCP transport socket (%d).\n",
1845 -err);
1846 goto out_err;
Chuck Levera246b012005-08-11 16:25:23 -04001847 }
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001848 xs_reclassify_socket6(sock);
1849
1850 if (xs_bind6(transport, sock) < 0) {
1851 sock_release(sock);
1852 goto out_err;
1853 }
1854 return sock;
1855out_err:
1856 return ERR_PTR(-EIO);
Chuck Levera246b012005-08-11 16:25:23 -04001857}
1858
Chuck Lever9903cd12005-08-11 16:25:26 -04001859/**
Chuck Lever68e220b2007-08-06 11:57:48 -04001860 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
1861 * @work: RPC transport to connect
1862 *
1863 * Invoked by a work queue tasklet.
1864 */
1865static void xs_tcp_connect_worker6(struct work_struct *work)
1866{
1867 struct sock_xprt *transport =
1868 container_of(work, struct sock_xprt, connect_worker.work);
1869 struct rpc_xprt *xprt = &transport->xprt;
Chuck Lever68e220b2007-08-06 11:57:48 -04001870
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001871 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6);
Chuck Lever68e220b2007-08-06 11:57:48 -04001872}
1873
1874/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001875 * xs_connect - connect a socket to a remote endpoint
1876 * @task: address of RPC task that manages state of connect request
1877 *
1878 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07001879 *
1880 * UDP socket connects are synchronous, but we use a work queue anyway
1881 * to guarantee that even unprivileged user processes can set up a
1882 * socket on a privileged port.
1883 *
1884 * If a UDP socket connect fails, the delay behavior here prevents
1885 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04001886 */
1887static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04001888{
1889 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001890 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001891
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001892 if (xprt_test_and_set_connecting(xprt))
1893 return;
1894
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001895 if (transport->sock != NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001896 dprintk("RPC: xs_connect delayed xprt %p for %lu "
1897 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07001898 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04001899 queue_delayed_work(rpciod_workqueue,
1900 &transport->connect_worker,
1901 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07001902 xprt->reestablish_timeout <<= 1;
1903 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
1904 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001905 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05001906 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04001907 queue_delayed_work(rpciod_workqueue,
1908 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04001909 }
1910}
1911
Trond Myklebuste06799f2007-11-05 15:44:12 -05001912static void xs_tcp_connect(struct rpc_task *task)
1913{
1914 struct rpc_xprt *xprt = task->tk_xprt;
1915
Trond Myklebuste06799f2007-11-05 15:44:12 -05001916 /* Exit if we need to wait for socket shutdown to complete */
1917 if (test_bit(XPRT_CLOSING, &xprt->state))
1918 return;
1919 xs_connect(task);
1920}
1921
Chuck Lever262ca072006-03-20 13:44:16 -05001922/**
1923 * xs_udp_print_stats - display UDP socket-specifc stats
1924 * @xprt: rpc_xprt struct containing statistics
1925 * @seq: output file
1926 *
1927 */
1928static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1929{
Chuck Leverc8475462006-12-05 16:35:26 -05001930 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1931
Chuck Lever262ca072006-03-20 13:44:16 -05001932 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
Chuck Leverc8475462006-12-05 16:35:26 -05001933 transport->port,
Chuck Lever262ca072006-03-20 13:44:16 -05001934 xprt->stat.bind_count,
1935 xprt->stat.sends,
1936 xprt->stat.recvs,
1937 xprt->stat.bad_xids,
1938 xprt->stat.req_u,
1939 xprt->stat.bklog_u);
1940}
1941
1942/**
1943 * xs_tcp_print_stats - display TCP socket-specifc stats
1944 * @xprt: rpc_xprt struct containing statistics
1945 * @seq: output file
1946 *
1947 */
1948static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1949{
Chuck Leverc8475462006-12-05 16:35:26 -05001950 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05001951 long idle_time = 0;
1952
1953 if (xprt_connected(xprt))
1954 idle_time = (long)(jiffies - xprt->last_used) / HZ;
1955
1956 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
Chuck Leverc8475462006-12-05 16:35:26 -05001957 transport->port,
Chuck Lever262ca072006-03-20 13:44:16 -05001958 xprt->stat.bind_count,
1959 xprt->stat.connect_count,
1960 xprt->stat.connect_time,
1961 idle_time,
1962 xprt->stat.sends,
1963 xprt->stat.recvs,
1964 xprt->stat.bad_xids,
1965 xprt->stat.req_u,
1966 xprt->stat.bklog_u);
1967}
1968
Chuck Lever262965f2005-08-11 16:25:56 -04001969static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07001970 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07001971 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07001972 .release_xprt = xprt_release_xprt_cong,
Chuck Lever45160d62007-07-01 12:13:17 -04001973 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01001974 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04001975 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01001976 .buf_alloc = rpc_malloc,
1977 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04001978 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001979 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07001980 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07001981 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04001982 .close = xs_close,
1983 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05001984 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04001985};
1986
1987static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07001988 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001989 .release_xprt = xs_tcp_release_xprt,
Chuck Lever45160d62007-07-01 12:13:17 -04001990 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01001991 .set_port = xs_set_port,
Trond Myklebuste06799f2007-11-05 15:44:12 -05001992 .connect = xs_tcp_connect,
Chuck Lever02107142006-01-03 09:55:49 +01001993 .buf_alloc = rpc_malloc,
1994 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04001995 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001996 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebuste06799f2007-11-05 15:44:12 -05001997 .close = xs_tcp_shutdown,
Chuck Lever9903cd12005-08-11 16:25:26 -04001998 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05001999 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002000};
2001
\"Talpey, Thomas\3c341b02007-09-10 13:47:07 -04002002static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002003 unsigned int slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002004{
2005 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002006 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002007
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002008 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002009 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002010 return ERR_PTR(-EBADF);
2011 }
2012
Chuck Leverffc2e512006-12-05 16:35:11 -05002013 new = kzalloc(sizeof(*new), GFP_KERNEL);
2014 if (new == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002015 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2016 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002017 return ERR_PTR(-ENOMEM);
2018 }
Chuck Leverffc2e512006-12-05 16:35:11 -05002019 xprt = &new->xprt;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002020
2021 xprt->max_reqs = slot_table_size;
2022 xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
2023 if (xprt->slot == NULL) {
2024 kfree(xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05002025 dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
2026 "table\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002027 return ERR_PTR(-ENOMEM);
2028 }
2029
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002030 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2031 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002032 if (args->srcaddr)
2033 memcpy(&new->addr, args->srcaddr, args->addrlen);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002034
2035 return xprt;
2036}
2037
Trond Myklebust2881ae72007-12-20 16:03:54 -05002038static const struct rpc_timeout xs_udp_default_timeout = {
2039 .to_initval = 5 * HZ,
2040 .to_maxval = 30 * HZ,
2041 .to_increment = 5 * HZ,
2042 .to_retries = 5,
2043};
2044
Chuck Lever9903cd12005-08-11 16:25:26 -04002045/**
2046 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002047 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002048 *
2049 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002050static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002051{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002052 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002053 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002054 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04002055
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002056 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002057 if (IS_ERR(xprt))
2058 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002059 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002060
Chuck Leverec739ef2006-08-22 20:06:15 -04002061 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002062 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002063 /* XXX: header size can vary due to auth type, IPv6, etc. */
2064 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2065
Chuck Lever03bf4b72005-08-25 16:25:55 -07002066 xprt->bind_timeout = XS_BIND_TO;
2067 xprt->connect_timeout = XS_UDP_CONN_TO;
2068 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2069 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002070
Chuck Lever262965f2005-08-11 16:25:56 -04002071 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002072
Trond Myklebustba7392b2007-12-20 16:03:55 -05002073 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002074
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002075 switch (addr->sa_family) {
2076 case AF_INET:
2077 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2078 xprt_set_bound(xprt);
2079
2080 INIT_DELAYED_WORK(&transport->connect_worker,
2081 xs_udp_connect_worker4);
Chuck Leverb454ae92008-01-07 18:34:48 -05002082 xs_format_ipv4_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002083 break;
2084 case AF_INET6:
2085 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2086 xprt_set_bound(xprt);
2087
2088 INIT_DELAYED_WORK(&transport->connect_worker,
2089 xs_udp_connect_worker6);
Chuck Leverb454ae92008-01-07 18:34:48 -05002090 xs_format_ipv6_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002091 break;
2092 default:
2093 kfree(xprt);
2094 return ERR_PTR(-EAFNOSUPPORT);
2095 }
2096
Chuck Lever46121cf2007-01-31 12:14:08 -05002097 dprintk("RPC: set up transport to address %s\n",
Chuck Lever7559c7a2006-12-05 16:35:37 -05002098 xprt->address_strings[RPC_DISPLAY_ALL]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002099
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002100 if (try_module_get(THIS_MODULE))
2101 return xprt;
2102
2103 kfree(xprt->slot);
2104 kfree(xprt);
2105 return ERR_PTR(-EINVAL);
Chuck Levera246b012005-08-11 16:25:23 -04002106}
2107
Trond Myklebust2881ae72007-12-20 16:03:54 -05002108static const struct rpc_timeout xs_tcp_default_timeout = {
2109 .to_initval = 60 * HZ,
2110 .to_maxval = 60 * HZ,
2111 .to_retries = 2,
2112};
2113
Chuck Lever9903cd12005-08-11 16:25:26 -04002114/**
2115 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002116 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002117 *
2118 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002119static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002120{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002121 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002122 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002123 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04002124
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002125 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002126 if (IS_ERR(xprt))
2127 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002128 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002129
Chuck Leverec739ef2006-08-22 20:06:15 -04002130 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002131 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002132 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002133
Chuck Lever03bf4b72005-08-25 16:25:55 -07002134 xprt->bind_timeout = XS_BIND_TO;
2135 xprt->connect_timeout = XS_TCP_CONN_TO;
2136 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2137 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002138
Chuck Lever262965f2005-08-11 16:25:56 -04002139 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002140 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002141
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002142 switch (addr->sa_family) {
2143 case AF_INET:
2144 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2145 xprt_set_bound(xprt);
2146
2147 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4);
Chuck Leverb454ae92008-01-07 18:34:48 -05002148 xs_format_ipv4_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002149 break;
2150 case AF_INET6:
2151 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2152 xprt_set_bound(xprt);
2153
2154 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker6);
Chuck Leverb454ae92008-01-07 18:34:48 -05002155 xs_format_ipv6_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002156 break;
2157 default:
2158 kfree(xprt);
2159 return ERR_PTR(-EAFNOSUPPORT);
2160 }
2161
Chuck Lever46121cf2007-01-31 12:14:08 -05002162 dprintk("RPC: set up transport to address %s\n",
Chuck Lever7559c7a2006-12-05 16:35:37 -05002163 xprt->address_strings[RPC_DISPLAY_ALL]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002164
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002165 if (try_module_get(THIS_MODULE))
2166 return xprt;
2167
2168 kfree(xprt->slot);
2169 kfree(xprt);
2170 return ERR_PTR(-EINVAL);
Chuck Levera246b012005-08-11 16:25:23 -04002171}
Chuck Lever282b32e2006-12-05 16:35:51 -05002172
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002173static struct xprt_class xs_udp_transport = {
2174 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2175 .name = "udp",
2176 .owner = THIS_MODULE,
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -04002177 .ident = IPPROTO_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002178 .setup = xs_setup_udp,
2179};
2180
2181static struct xprt_class xs_tcp_transport = {
2182 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2183 .name = "tcp",
2184 .owner = THIS_MODULE,
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -04002185 .ident = IPPROTO_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002186 .setup = xs_setup_tcp,
2187};
2188
Chuck Lever282b32e2006-12-05 16:35:51 -05002189/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002190 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002191 *
2192 */
2193int init_socket_xprt(void)
2194{
Chuck Leverfbf76682006-12-05 16:35:54 -05002195#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002196 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002197 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002198#endif
2199
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002200 xprt_register_transport(&xs_udp_transport);
2201 xprt_register_transport(&xs_tcp_transport);
2202
Chuck Lever282b32e2006-12-05 16:35:51 -05002203 return 0;
2204}
2205
2206/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002207 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05002208 *
2209 */
2210void cleanup_socket_xprt(void)
2211{
Chuck Leverfbf76682006-12-05 16:35:54 -05002212#ifdef RPC_DEBUG
2213 if (sunrpc_table_header) {
2214 unregister_sysctl_table(sunrpc_table_header);
2215 sunrpc_table_header = NULL;
2216 }
2217#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002218
2219 xprt_unregister_transport(&xs_udp_transport);
2220 xprt_unregister_transport(&xs_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05002221}