blob: 62438f3a914d6cb9c6f8c47b89a6cd5e026dd933 [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>
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -040037#ifdef CONFIG_NFS_V4_1
38#include <linux/sunrpc/bc_xprt.h>
39#endif
Chuck Levera246b012005-08-11 16:25:23 -040040
41#include <net/sock.h>
42#include <net/checksum.h>
43#include <net/udp.h>
44#include <net/tcp.h>
45
Chuck Lever9903cd12005-08-11 16:25:26 -040046/*
Chuck Leverc556b752005-11-01 12:24:48 -050047 * xprtsock tunables
48 */
49unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
50unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
51
52unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
53unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
54
Trond Myklebust7d1e8252009-03-11 14:38:03 -040055#define XS_TCP_LINGER_TO (15U * HZ)
Trond Myklebust25fe6142009-03-11 14:38:03 -040056static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
Trond Myklebust7d1e8252009-03-11 14:38:03 -040057
Chuck Leverc556b752005-11-01 12:24:48 -050058/*
Chuck Leverfbf76682006-12-05 16:35:54 -050059 * We can register our own files under /proc/sys/sunrpc by
60 * calling register_sysctl_table() again. The files in that
61 * directory become the union of all files registered there.
62 *
63 * We simply need to make sure that we don't collide with
64 * someone else's file names!
65 */
66
67#ifdef RPC_DEBUG
68
69static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
70static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
71static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
72static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
73
74static struct ctl_table_header *sunrpc_table_header;
75
76/*
77 * FIXME: changing the UDP slot table size should also resize the UDP
78 * socket buffers for existing UDP transports
79 */
80static ctl_table xs_tunables_table[] = {
81 {
82 .ctl_name = CTL_SLOTTABLE_UDP,
83 .procname = "udp_slot_table_entries",
84 .data = &xprt_udp_slot_table_entries,
85 .maxlen = sizeof(unsigned int),
86 .mode = 0644,
87 .proc_handler = &proc_dointvec_minmax,
88 .strategy = &sysctl_intvec,
89 .extra1 = &min_slot_table_size,
90 .extra2 = &max_slot_table_size
91 },
92 {
93 .ctl_name = CTL_SLOTTABLE_TCP,
94 .procname = "tcp_slot_table_entries",
95 .data = &xprt_tcp_slot_table_entries,
96 .maxlen = sizeof(unsigned int),
97 .mode = 0644,
98 .proc_handler = &proc_dointvec_minmax,
99 .strategy = &sysctl_intvec,
100 .extra1 = &min_slot_table_size,
101 .extra2 = &max_slot_table_size
102 },
103 {
104 .ctl_name = CTL_MIN_RESVPORT,
105 .procname = "min_resvport",
106 .data = &xprt_min_resvport,
107 .maxlen = sizeof(unsigned int),
108 .mode = 0644,
109 .proc_handler = &proc_dointvec_minmax,
110 .strategy = &sysctl_intvec,
111 .extra1 = &xprt_min_resvport_limit,
112 .extra2 = &xprt_max_resvport_limit
113 },
114 {
115 .ctl_name = CTL_MAX_RESVPORT,
116 .procname = "max_resvport",
117 .data = &xprt_max_resvport,
118 .maxlen = sizeof(unsigned int),
119 .mode = 0644,
120 .proc_handler = &proc_dointvec_minmax,
121 .strategy = &sysctl_intvec,
122 .extra1 = &xprt_min_resvport_limit,
123 .extra2 = &xprt_max_resvport_limit
124 },
125 {
Trond Myklebust25fe6142009-03-11 14:38:03 -0400126 .procname = "tcp_fin_timeout",
127 .data = &xs_tcp_fin_timeout,
128 .maxlen = sizeof(xs_tcp_fin_timeout),
129 .mode = 0644,
130 .proc_handler = &proc_dointvec_jiffies,
131 .strategy = sysctl_jiffies
132 },
133 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500134 .ctl_name = 0,
135 },
136};
137
138static ctl_table sunrpc_table[] = {
139 {
140 .ctl_name = CTL_SUNRPC,
141 .procname = "sunrpc",
142 .mode = 0555,
143 .child = xs_tunables_table
144 },
145 {
146 .ctl_name = 0,
147 },
148};
149
150#endif
151
152/*
Chuck Lever03bf4b72005-08-25 16:25:55 -0700153 * Time out for an RPC UDP socket connect. UDP socket connects are
154 * synchronous, but we set a timeout anyway in case of resource
155 * exhaustion on the local host.
156 */
157#define XS_UDP_CONN_TO (5U * HZ)
158
159/*
160 * Wait duration for an RPC TCP connection to be established. Solaris
161 * NFS over TCP uses 60 seconds, for example, which is in line with how
162 * long a server takes to reboot.
163 */
164#define XS_TCP_CONN_TO (60U * HZ)
165
166/*
167 * Wait duration for a reply from the RPC portmapper.
168 */
169#define XS_BIND_TO (60U * HZ)
170
171/*
172 * Delay if a UDP socket connect error occurs. This is most likely some
173 * kind of resource problem on the local host.
174 */
175#define XS_UDP_REEST_TO (2U * HZ)
176
177/*
178 * The reestablish timeout allows clients to delay for a bit before attempting
179 * to reconnect to a server that just dropped our connection.
180 *
181 * We implement an exponential backoff when trying to reestablish a TCP
182 * transport connection with the server. Some servers like to drop a TCP
183 * connection when they are overworked, so we start with a short timeout and
184 * increase over time if the server is down or not responding.
185 */
186#define XS_TCP_INIT_REEST_TO (3U * HZ)
187#define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
188
189/*
190 * TCP idle timeout; client drops the transport socket if it is idle
191 * for this long. Note that we also timeout UDP sockets to prevent
192 * holding port numbers when there is no RPC traffic.
193 */
194#define XS_IDLE_DISC_TO (5U * 60 * HZ)
195
Chuck Levera246b012005-08-11 16:25:23 -0400196#ifdef RPC_DEBUG
197# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400198# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400199#endif
200
Chuck Levera246b012005-08-11 16:25:23 -0400201#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400202static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400203{
Chuck Lever9903cd12005-08-11 16:25:26 -0400204 u8 *buf = (u8 *) packet;
205 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400206
Chuck Lever46121cf2007-01-31 12:14:08 -0500207 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400208 for (j = 0; j < count && j < 128; j += 4) {
209 if (!(j & 31)) {
210 if (j)
211 dprintk("\n");
212 dprintk("0x%04x ", j);
213 }
214 dprintk("%02x%02x%02x%02x ",
215 buf[j], buf[j+1], buf[j+2], buf[j+3]);
216 }
217 dprintk("\n");
218}
219#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400220static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400221{
222 /* NOP */
223}
224#endif
225
Chuck Leverffc2e512006-12-05 16:35:11 -0500226struct sock_xprt {
227 struct rpc_xprt xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500228
229 /*
230 * Network layer
231 */
232 struct socket * sock;
233 struct sock * inet;
Chuck Lever51971132006-12-05 16:35:19 -0500234
235 /*
236 * State of TCP reply receive
237 */
238 __be32 tcp_fraghdr,
239 tcp_xid;
240
241 u32 tcp_offset,
242 tcp_reclen;
243
244 unsigned long tcp_copied,
245 tcp_flags;
Chuck Leverc8475462006-12-05 16:35:26 -0500246
247 /*
248 * Connection of transports
249 */
Trond Myklebust34161db2006-12-07 15:48:15 -0500250 struct delayed_work connect_worker;
Chuck Leverfbfffbd2009-08-09 15:09:46 -0400251 struct sockaddr_storage srcaddr;
252 unsigned short srcport;
Chuck Lever7c6e0662006-12-05 16:35:30 -0500253
254 /*
255 * UDP socket buffer size parameters
256 */
257 size_t rcvsize,
258 sndsize;
Chuck Lever314dfd72006-12-05 16:35:34 -0500259
260 /*
261 * Saved socket callback addresses
262 */
263 void (*old_data_ready)(struct sock *, int);
264 void (*old_state_change)(struct sock *);
265 void (*old_write_space)(struct sock *);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400266 void (*old_error_report)(struct sock *);
Chuck Leverffc2e512006-12-05 16:35:11 -0500267};
268
Chuck Levere136d092006-12-05 16:35:23 -0500269/*
270 * TCP receive state flags
271 */
272#define TCP_RCV_LAST_FRAG (1UL << 0)
273#define TCP_RCV_COPY_FRAGHDR (1UL << 1)
274#define TCP_RCV_COPY_XID (1UL << 2)
275#define TCP_RCV_COPY_DATA (1UL << 3)
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400276#define TCP_RCV_READ_CALLDIR (1UL << 4)
277#define TCP_RCV_COPY_CALLDIR (1UL << 5)
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400278
279/*
280 * TCP RPC flags
281 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400282#define TCP_RPC_REPLY (1UL << 6)
Chuck Levere136d092006-12-05 16:35:23 -0500283
Chuck Lever95392c52007-08-06 11:57:58 -0400284static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400285{
Chuck Lever95392c52007-08-06 11:57:58 -0400286 return (struct sockaddr *) &xprt->addr;
287}
288
289static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
290{
291 return (struct sockaddr_in *) &xprt->addr;
292}
293
294static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
295{
296 return (struct sockaddr_in6 *) &xprt->addr;
297}
298
Chuck Leverc877b842009-08-09 15:09:36 -0400299static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400300{
Chuck Leverc877b842009-08-09 15:09:36 -0400301 struct sockaddr *sap = xs_addr(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400302 struct sockaddr_in6 *sin6;
303 struct sockaddr_in *sin;
Chuck Leverc877b842009-08-09 15:09:36 -0400304 char buf[128];
Chuck Leveredb267a2006-08-22 20:06:18 -0400305
Chuck Leverc877b842009-08-09 15:09:36 -0400306 (void)rpc_ntop(sap, buf, sizeof(buf));
307 xprt->address_strings[RPC_DISPLAY_ADDR] = kstrdup(buf, GFP_KERNEL);
308
Chuck Lever9dc3b092009-08-09 15:09:46 -0400309 switch (sap->sa_family) {
310 case AF_INET:
311 sin = xs_addr_in(xprt);
312 (void)snprintf(buf, sizeof(buf), "%02x%02x%02x%02x",
313 NIPQUAD(sin->sin_addr.s_addr));
314 break;
315 case AF_INET6:
316 sin6 = xs_addr_in6(xprt);
317 (void)snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
318 break;
319 default:
320 BUG();
Chuck Leveredb267a2006-08-22 20:06:18 -0400321 }
Chuck Lever9dc3b092009-08-09 15:09:46 -0400322 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
Chuck Leveredb267a2006-08-22 20:06:18 -0400323}
324
Chuck Lever9dc3b092009-08-09 15:09:46 -0400325static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400326{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400327 struct sockaddr *sap = xs_addr(xprt);
328 char buf[128];
Chuck Lever4b6473f2007-08-06 11:57:12 -0400329
Chuck Leverc877b842009-08-09 15:09:36 -0400330 (void)snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
331 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400332
Chuck Leverc877b842009-08-09 15:09:36 -0400333 (void)snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
334 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
335}
Chuck Lever4b6473f2007-08-06 11:57:12 -0400336
Chuck Lever9dc3b092009-08-09 15:09:46 -0400337static void xs_format_peer_addresses(struct rpc_xprt *xprt,
338 const char *protocol,
339 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400340{
Chuck Leverb454ae92008-01-07 18:34:48 -0500341 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leverb454ae92008-01-07 18:34:48 -0500342 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leverc877b842009-08-09 15:09:36 -0400343 xs_format_common_peer_addresses(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400344 xs_format_common_peer_ports(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400345}
346
Chuck Lever9dc3b092009-08-09 15:09:46 -0400347static void xs_update_peer_port(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400348{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400349 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
350 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400351
Chuck Lever9dc3b092009-08-09 15:09:46 -0400352 xs_format_common_peer_ports(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400353}
354
355static void xs_free_peer_addresses(struct rpc_xprt *xprt)
356{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500357 unsigned int i;
358
359 for (i = 0; i < RPC_DISPLAY_MAX; i++)
360 switch (i) {
361 case RPC_DISPLAY_PROTO:
362 case RPC_DISPLAY_NETID:
363 continue;
364 default:
365 kfree(xprt->address_strings[i]);
366 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400367}
368
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400369#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
370
Trond Myklebust24c56842006-10-17 15:06:22 -0400371static 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 -0400372{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400373 struct msghdr msg = {
374 .msg_name = addr,
375 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400376 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
377 };
378 struct kvec iov = {
379 .iov_base = vec->iov_base + base,
380 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400381 };
382
Trond Myklebust24c56842006-10-17 15:06:22 -0400383 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400384 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
385 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
386}
387
Trond Myklebust24c56842006-10-17 15:06:22 -0400388static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400389{
Trond Myklebust24c56842006-10-17 15:06:22 -0400390 struct page **ppage;
391 unsigned int remainder;
392 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400393
Trond Myklebust24c56842006-10-17 15:06:22 -0400394 remainder = xdr->page_len - base;
395 base += xdr->page_base;
396 ppage = xdr->pages + (base >> PAGE_SHIFT);
397 base &= ~PAGE_MASK;
398 for(;;) {
399 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
400 int flags = XS_SENDMSG_FLAGS;
401
402 remainder -= len;
403 if (remainder != 0 || more)
404 flags |= MSG_MORE;
405 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
406 if (remainder == 0 || err != len)
407 break;
408 sent += err;
409 ppage++;
410 base = 0;
411 }
412 if (sent == 0)
413 return err;
414 if (err > 0)
415 sent += err;
416 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400417}
418
Chuck Lever9903cd12005-08-11 16:25:26 -0400419/**
420 * xs_sendpages - write pages directly to a socket
421 * @sock: socket to send on
422 * @addr: UDP only -- address of destination
423 * @addrlen: UDP only -- length of destination address
424 * @xdr: buffer containing this request
425 * @base: starting position in the buffer
426 *
Chuck Levera246b012005-08-11 16:25:23 -0400427 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400428static 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 -0400429{
Trond Myklebust24c56842006-10-17 15:06:22 -0400430 unsigned int remainder = xdr->len - base;
431 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400432
Chuck Lever262965f2005-08-11 16:25:56 -0400433 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400434 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400435
436 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400437 if (base != 0) {
438 addr = NULL;
439 addrlen = 0;
440 }
Chuck Lever262965f2005-08-11 16:25:56 -0400441
Trond Myklebust24c56842006-10-17 15:06:22 -0400442 if (base < xdr->head[0].iov_len || addr != NULL) {
443 unsigned int len = xdr->head[0].iov_len - base;
444 remainder -= len;
445 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
446 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400447 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400448 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400449 base = 0;
450 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400451 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400452
Trond Myklebust24c56842006-10-17 15:06:22 -0400453 if (base < xdr->page_len) {
454 unsigned int len = xdr->page_len - base;
455 remainder -= len;
456 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
457 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400458 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400459 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400460 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400461 } else
462 base -= xdr->page_len;
463
464 if (base >= xdr->tail[0].iov_len)
465 return sent;
466 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400467out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400468 if (sent == 0)
469 return err;
470 if (err > 0)
471 sent += err;
472 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400473}
474
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400475static void xs_nospace_callback(struct rpc_task *task)
476{
477 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
478
479 transport->inet->sk_write_pending--;
480 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
481}
482
Chuck Lever9903cd12005-08-11 16:25:26 -0400483/**
Chuck Lever262965f2005-08-11 16:25:56 -0400484 * xs_nospace - place task on wait queue if transmit was incomplete
485 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400486 *
Chuck Levera246b012005-08-11 16:25:23 -0400487 */
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400488static int xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400489{
Chuck Lever262965f2005-08-11 16:25:56 -0400490 struct rpc_rqst *req = task->tk_rqstp;
491 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500492 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400493 int ret = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400494
Chuck Lever46121cf2007-01-31 12:14:08 -0500495 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400496 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
497 req->rq_slen);
498
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400499 /* Protect against races with write_space */
500 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400501
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400502 /* Don't race with disconnect */
503 if (xprt_connected(xprt)) {
504 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400505 ret = -EAGAIN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400506 /*
507 * Notify TCP that we're limited by the application
508 * window size
509 */
510 set_bit(SOCK_NOSPACE, &transport->sock->flags);
511 transport->inet->sk_write_pending++;
512 /* ...and wait for more buffer space */
513 xprt_wait_for_buffer_space(task, xs_nospace_callback);
514 }
515 } else {
516 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400517 ret = -ENOTCONN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400518 }
Chuck Lever262965f2005-08-11 16:25:56 -0400519
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400520 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400521 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400522}
523
524/**
525 * xs_udp_send_request - write an RPC request to a UDP socket
526 * @task: address of RPC task that manages the state of an RPC request
527 *
528 * Return values:
529 * 0: The request has been sent
530 * EAGAIN: The socket was blocked, please call again later to
531 * complete the request
532 * ENOTCONN: Caller needs to invoke connect logic then call again
533 * other: Some other error occured, the request was not sent
534 */
535static int xs_udp_send_request(struct rpc_task *task)
536{
537 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);
Chuck Lever262965f2005-08-11 16:25:56 -0400540 struct xdr_buf *xdr = &req->rq_snd_buf;
541 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400542
Chuck Lever9903cd12005-08-11 16:25:26 -0400543 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400544 req->rq_svec->iov_base,
545 req->rq_svec->iov_len);
546
Trond Myklebust01d37c42009-03-11 14:09:39 -0400547 if (!xprt_bound(xprt))
548 return -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500549 status = xs_sendpages(transport->sock,
Chuck Lever95392c52007-08-06 11:57:58 -0400550 xs_addr(xprt),
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500551 xprt->addrlen, xdr,
552 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400553
Chuck Lever46121cf2007-01-31 12:14:08 -0500554 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400555 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400556
Trond Myklebust21997002007-10-01 11:43:37 -0400557 if (status >= 0) {
558 task->tk_bytes_sent += status;
559 if (status >= req->rq_slen)
560 return 0;
561 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400562 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400563 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400564 if (!transport->sock)
565 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400566
Chuck Lever262965f2005-08-11 16:25:56 -0400567 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400568 case -ENOTSOCK:
569 status = -ENOTCONN;
570 /* Should we call xs_close() here? */
571 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400572 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400573 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400574 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400575 default:
576 dprintk("RPC: sendmsg returned unrecognized error %d\n",
577 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400578 case -ENETUNREACH:
579 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400580 case -ECONNREFUSED:
581 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400582 * prompts ECONNREFUSED. */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400583 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400584 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400585out:
Chuck Lever262965f2005-08-11 16:25:56 -0400586 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400587}
588
Trond Myklebuste06799f2007-11-05 15:44:12 -0500589/**
590 * xs_tcp_shutdown - gracefully shut down a TCP socket
591 * @xprt: transport
592 *
593 * Initiates a graceful shutdown of the TCP socket by calling the
594 * equivalent of shutdown(SHUT_WR);
595 */
596static void xs_tcp_shutdown(struct rpc_xprt *xprt)
597{
598 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
599 struct socket *sock = transport->sock;
600
601 if (sock != NULL)
602 kernel_sock_shutdown(sock, SHUT_WR);
603}
604
Chuck Lever808012f2005-08-25 16:25:49 -0700605static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
606{
607 u32 reclen = buf->len - sizeof(rpc_fraghdr);
608 rpc_fraghdr *base = buf->head[0].iov_base;
609 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
610}
611
Chuck Lever9903cd12005-08-11 16:25:26 -0400612/**
Chuck Lever262965f2005-08-11 16:25:56 -0400613 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400614 * @task: address of RPC task that manages the state of an RPC request
615 *
616 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400617 * 0: The request has been sent
618 * EAGAIN: The socket was blocked, please call again later to
619 * complete the request
620 * ENOTCONN: Caller needs to invoke connect logic then call again
621 * other: Some other error occured, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400622 *
623 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400624 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400625 */
Chuck Lever262965f2005-08-11 16:25:56 -0400626static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400627{
628 struct rpc_rqst *req = task->tk_rqstp;
629 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500630 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400631 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Leverb595bb12007-08-06 11:56:42 -0400632 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400633
Chuck Lever808012f2005-08-25 16:25:49 -0700634 xs_encode_tcp_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400635
Chuck Lever262965f2005-08-11 16:25:56 -0400636 xs_pktdump("packet data:",
637 req->rq_svec->iov_base,
638 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400639
640 /* Continue transmitting the packet/record. We must be careful
641 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400642 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400643 while (1) {
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500644 status = xs_sendpages(transport->sock,
645 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400646
Chuck Lever46121cf2007-01-31 12:14:08 -0500647 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400648 xdr->len - req->rq_bytes_sent, status);
649
650 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400651 break;
652
Chuck Lever262965f2005-08-11 16:25:56 -0400653 /* If we've sent the entire packet, immediately
654 * reset the count of bytes sent. */
655 req->rq_bytes_sent += status;
Chuck Leveref759a22006-03-20 13:44:17 -0500656 task->tk_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400657 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
658 req->rq_bytes_sent = 0;
659 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400660 }
661
Trond Myklebust06b4b682008-04-16 16:51:38 -0400662 if (status != 0)
663 continue;
Chuck Levera246b012005-08-11 16:25:23 -0400664 status = -EAGAIN;
Trond Myklebust06b4b682008-04-16 16:51:38 -0400665 break;
Chuck Levera246b012005-08-11 16:25:23 -0400666 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400667 if (!transport->sock)
668 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400669
Chuck Lever262965f2005-08-11 16:25:56 -0400670 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400671 case -ENOTSOCK:
672 status = -ENOTCONN;
673 /* Should we call xs_close() here? */
674 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400675 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400676 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400677 break;
678 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500679 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400680 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400681 case -ECONNRESET:
Trond Myklebust55420c22009-03-11 15:29:24 -0400682 case -EPIPE:
Trond Myklebuste06799f2007-11-05 15:44:12 -0500683 xs_tcp_shutdown(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400684 case -ECONNREFUSED:
685 case -ENOTCONN:
Chuck Lever262965f2005-08-11 16:25:56 -0400686 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400687 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400688out:
Chuck Levera246b012005-08-11 16:25:23 -0400689 return status;
690}
691
Chuck Lever9903cd12005-08-11 16:25:26 -0400692/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400693 * xs_tcp_release_xprt - clean up after a tcp transmission
694 * @xprt: transport
695 * @task: rpc task
696 *
697 * This cleans up if an error causes us to abort the transmission of a request.
698 * In this case, the socket may need to be reset in order to avoid confusing
699 * the server.
700 */
701static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
702{
703 struct rpc_rqst *req;
704
705 if (task != xprt->snd_task)
706 return;
707 if (task == NULL)
708 goto out_release;
709 req = task->tk_rqstp;
710 if (req->rq_bytes_sent == 0)
711 goto out_release;
712 if (req->rq_bytes_sent == req->rq_snd_buf.len)
713 goto out_release;
714 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
715out_release:
716 xprt_release_xprt(xprt, task);
717}
718
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400719static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
720{
721 transport->old_data_ready = sk->sk_data_ready;
722 transport->old_state_change = sk->sk_state_change;
723 transport->old_write_space = sk->sk_write_space;
724 transport->old_error_report = sk->sk_error_report;
725}
726
727static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
728{
729 sk->sk_data_ready = transport->old_data_ready;
730 sk->sk_state_change = transport->old_state_change;
731 sk->sk_write_space = transport->old_write_space;
732 sk->sk_error_report = transport->old_error_report;
733}
734
Chuck Leverfe315e72009-03-11 14:10:21 -0400735static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400736{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500737 struct socket *sock = transport->sock;
738 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400739
Chuck Leverfe315e72009-03-11 14:10:21 -0400740 if (sk == NULL)
741 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400742
Chuck Levera246b012005-08-11 16:25:23 -0400743 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500744 transport->inet = NULL;
745 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400746
Chuck Lever9903cd12005-08-11 16:25:26 -0400747 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400748
749 xs_restore_old_callbacks(transport, sk);
Chuck Levera246b012005-08-11 16:25:23 -0400750 write_unlock_bh(&sk->sk_callback_lock);
751
Chuck Lever9903cd12005-08-11 16:25:26 -0400752 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400753
754 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -0400755}
756
757/**
758 * xs_close - close a socket
759 * @xprt: transport
760 *
761 * This is used when all requests are complete; ie, no DRC state remains
762 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400763 *
764 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
765 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e72009-03-11 14:10:21 -0400766 */
767static void xs_close(struct rpc_xprt *xprt)
768{
769 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
770
771 dprintk("RPC: xs_close xprt %p\n", xprt);
772
773 xs_reset_transport(transport);
774
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100775 smp_mb__before_clear_bit();
Trond Myklebust7d1e8252009-03-11 14:38:03 -0400776 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100777 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -0500778 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100779 smp_mb__after_clear_bit();
Trond Myklebust62da3b22007-11-06 18:44:20 -0500780 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400781}
782
Trond Myklebustf75e6742009-04-21 17:18:20 -0400783static void xs_tcp_close(struct rpc_xprt *xprt)
784{
785 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
786 xs_close(xprt);
787 else
788 xs_tcp_shutdown(xprt);
789}
790
Chuck Lever9903cd12005-08-11 16:25:26 -0400791/**
792 * xs_destroy - prepare to shutdown a transport
793 * @xprt: doomed transport
794 *
795 */
796static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400797{
Chuck Leverc8475462006-12-05 16:35:26 -0500798 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
799
Chuck Lever46121cf2007-01-31 12:14:08 -0500800 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400801
Trond Myklebustc1384c92007-06-14 18:00:42 -0400802 cancel_rearming_delayed_work(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400803
Chuck Lever9903cd12005-08-11 16:25:26 -0400804 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400805 xs_free_peer_addresses(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400806 kfree(xprt->slot);
Chuck Leverc8541ec2006-10-17 14:44:27 -0400807 kfree(xprt);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -0400808 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400809}
810
Chuck Lever9903cd12005-08-11 16:25:26 -0400811static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400812{
Chuck Lever9903cd12005-08-11 16:25:26 -0400813 return (struct rpc_xprt *) sk->sk_user_data;
814}
815
816/**
817 * xs_udp_data_ready - "data ready" callback for UDP sockets
818 * @sk: socket with data to read
819 * @len: how much data to read
820 *
821 */
822static void xs_udp_data_ready(struct sock *sk, int len)
823{
824 struct rpc_task *task;
825 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400826 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400827 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400828 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700829 u32 _xid;
830 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400831
832 read_lock(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500833 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400834 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400835 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400836
837 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
838 goto out;
839
840 if (xprt->shutdown)
841 goto dropit;
842
843 repsize = skb->len - sizeof(struct udphdr);
844 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500845 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400846 goto dropit;
847 }
848
849 /* Copy the XID from the skb... */
850 xp = skb_header_pointer(skb, sizeof(struct udphdr),
851 sizeof(_xid), &_xid);
852 if (xp == NULL)
853 goto dropit;
854
855 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400856 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400857 rovr = xprt_lookup_rqst(xprt, *xp);
858 if (!rovr)
859 goto out_unlock;
860 task = rovr->rq_task;
861
Chuck Levera246b012005-08-11 16:25:23 -0400862 if ((copied = rovr->rq_private_buf.buflen) > repsize)
863 copied = repsize;
864
865 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -0800866 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
867 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -0400868 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -0800869 }
870
871 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -0400872
873 /* Something worked... */
Eric Dumazetadf30902009-06-02 05:19:30 +0000874 dst_confirm(skb_dst(skb));
Chuck Levera246b012005-08-11 16:25:23 -0400875
Chuck Lever1570c1e2005-08-25 16:25:52 -0700876 xprt_adjust_cwnd(task, copied);
877 xprt_update_rtt(task);
878 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -0400879
880 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400881 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400882 dropit:
883 skb_free_datagram(sk, skb);
884 out:
885 read_unlock(&sk->sk_callback_lock);
886}
887
Chuck Leverdd456472006-12-05 16:35:44 -0500888static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400889{
Chuck Lever51971132006-12-05 16:35:19 -0500890 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400891 size_t len, used;
892 char *p;
893
Chuck Lever51971132006-12-05 16:35:19 -0500894 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
895 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500896 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500897 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400898 if (used != len)
899 return;
Chuck Lever808012f2005-08-25 16:25:49 -0700900
Chuck Lever51971132006-12-05 16:35:19 -0500901 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
902 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -0500903 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -0400904 else
Chuck Levere136d092006-12-05 16:35:23 -0500905 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -0500906 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -0700907
Chuck Levere136d092006-12-05 16:35:23 -0500908 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500909 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -0700910
Chuck Levera246b012005-08-11 16:25:23 -0400911 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400912 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500913 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebust3ebb0672007-11-06 18:40:12 -0500914 xprt_force_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400915 return;
Chuck Levera246b012005-08-11 16:25:23 -0400916 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500917 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -0500918 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400919}
920
Chuck Lever51971132006-12-05 16:35:19 -0500921static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400922{
Chuck Lever51971132006-12-05 16:35:19 -0500923 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -0500924 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500925 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -0500926 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
927 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
928 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -0500929 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400930 }
931 }
932}
933
Chuck Leverdd456472006-12-05 16:35:44 -0500934static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400935{
936 size_t len, used;
937 char *p;
938
Chuck Lever51971132006-12-05 16:35:19 -0500939 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -0500940 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -0500941 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500942 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500943 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400944 if (used != len)
945 return;
Chuck Levere136d092006-12-05 16:35:23 -0500946 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400947 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -0500948 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400949 dprintk("RPC: reading %s XID %08x\n",
950 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
951 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -0500952 ntohl(transport->tcp_xid));
953 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400954}
955
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400956static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
957 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400958{
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400959 size_t len, used;
960 u32 offset;
961 __be32 calldir;
962
963 /*
964 * We want transport->tcp_offset to be 8 at the end of this routine
965 * (4 bytes for the xid and 4 bytes for the call/reply flag).
966 * When this function is called for the first time,
967 * transport->tcp_offset is 4 (after having already read the xid).
968 */
969 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
970 len = sizeof(calldir) - offset;
971 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
972 used = xdr_skb_read_bits(desc, &calldir, len);
973 transport->tcp_offset += used;
974 if (used != len)
975 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400976 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
977 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400978 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400979 /*
980 * We don't yet have the XDR buffer, so we will write the calldir
981 * out after we get the buffer from the 'struct rpc_rqst'
982 */
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400983 if (ntohl(calldir) == RPC_REPLY)
984 transport->tcp_flags |= TCP_RPC_REPLY;
985 else
986 transport->tcp_flags &= ~TCP_RPC_REPLY;
987 dprintk("RPC: reading %s CALL/REPLY flag %08x\n",
988 (transport->tcp_flags & TCP_RPC_REPLY) ?
989 "reply for" : "request with", calldir);
990 xs_tcp_check_fraghdr(transport);
991}
992
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -0400993static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
994 struct xdr_skb_reader *desc,
995 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -0400996{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -0400997 struct sock_xprt *transport =
998 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400999 struct xdr_buf *rcvbuf;
1000 size_t len;
1001 ssize_t r;
1002
Chuck Levera246b012005-08-11 16:25:23 -04001003 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001004
1005 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1006 /*
1007 * Save the RPC direction in the XDR buffer
1008 */
1009 __be32 calldir = transport->tcp_flags & TCP_RPC_REPLY ?
1010 htonl(RPC_REPLY) : 0;
1011
1012 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
1013 &calldir, sizeof(calldir));
1014 transport->tcp_copied += sizeof(calldir);
1015 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -04001016 }
1017
Chuck Levera246b012005-08-11 16:25:23 -04001018 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -05001019 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -05001020 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -04001021
Chuck Lever51971132006-12-05 16:35:19 -05001022 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001023 memcpy(&my_desc, desc, sizeof(my_desc));
1024 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -05001025 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001026 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001027 desc->count -= r;
1028 desc->offset += r;
1029 } else
Chuck Lever51971132006-12-05 16:35:19 -05001030 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001031 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001032
1033 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001034 transport->tcp_copied += r;
1035 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001036 }
1037 if (r != len) {
1038 /* Error when copying to the receive buffer,
1039 * usually because we weren't able to allocate
1040 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001041 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001042 * will not receive any additional updates,
1043 * and time out.
1044 * Any remaining data from this record will
1045 * be discarded.
1046 */
Chuck Levere136d092006-12-05 16:35:23 -05001047 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001048 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001049 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001050 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1051 "tcp_offset = %u, tcp_reclen = %u\n",
1052 xprt, transport->tcp_copied,
1053 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001054 return;
Chuck Levera246b012005-08-11 16:25:23 -04001055 }
1056
Chuck Lever46121cf2007-01-31 12:14:08 -05001057 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001058 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001059 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1060 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1061 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001062
Chuck Lever51971132006-12-05 16:35:19 -05001063 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001064 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001065 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001066 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1067 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001068 }
1069
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001070 return;
1071}
1072
1073/*
1074 * Finds the request corresponding to the RPC xid and invokes the common
1075 * tcp read code to read the data.
1076 */
1077static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1078 struct xdr_skb_reader *desc)
1079{
1080 struct sock_xprt *transport =
1081 container_of(xprt, struct sock_xprt, xprt);
1082 struct rpc_rqst *req;
1083
1084 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1085
1086 /* Find and lock the request corresponding to this xid */
1087 spin_lock(&xprt->transport_lock);
1088 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1089 if (!req) {
1090 dprintk("RPC: XID %08x request not found!\n",
1091 ntohl(transport->tcp_xid));
1092 spin_unlock(&xprt->transport_lock);
1093 return -1;
1094 }
1095
1096 xs_tcp_read_common(xprt, desc, req);
1097
Chuck Levere136d092006-12-05 16:35:23 -05001098 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001099 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001100
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001101 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001102 return 0;
1103}
1104
1105#if defined(CONFIG_NFS_V4_1)
1106/*
1107 * Obtains an rpc_rqst previously allocated and invokes the common
1108 * tcp read code to read the data. The result is placed in the callback
1109 * queue.
1110 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1111 * connection and return -1.
1112 */
1113static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1114 struct xdr_skb_reader *desc)
1115{
1116 struct sock_xprt *transport =
1117 container_of(xprt, struct sock_xprt, xprt);
1118 struct rpc_rqst *req;
1119
1120 req = xprt_alloc_bc_request(xprt);
1121 if (req == NULL) {
1122 printk(KERN_WARNING "Callback slot table overflowed\n");
1123 xprt_force_disconnect(xprt);
1124 return -1;
1125 }
1126
1127 req->rq_xid = transport->tcp_xid;
1128 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1129 xs_tcp_read_common(xprt, desc, req);
1130
1131 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1132 struct svc_serv *bc_serv = xprt->bc_serv;
1133
1134 /*
1135 * Add callback request to callback list. The callback
1136 * service sleeps on the sv_cb_waitq waiting for new
1137 * requests. Wake it up after adding enqueing the
1138 * request.
1139 */
1140 dprintk("RPC: add callback request to list\n");
1141 spin_lock(&bc_serv->sv_cb_lock);
1142 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1143 spin_unlock(&bc_serv->sv_cb_lock);
1144 wake_up(&bc_serv->sv_cb_waitq);
1145 }
1146
1147 req->rq_private_buf.len = transport->tcp_copied;
1148
1149 return 0;
1150}
1151
1152static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1153 struct xdr_skb_reader *desc)
1154{
1155 struct sock_xprt *transport =
1156 container_of(xprt, struct sock_xprt, xprt);
1157
1158 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1159 xs_tcp_read_reply(xprt, desc) :
1160 xs_tcp_read_callback(xprt, desc);
1161}
1162#else
1163static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1164 struct xdr_skb_reader *desc)
1165{
1166 return xs_tcp_read_reply(xprt, desc);
1167}
1168#endif /* CONFIG_NFS_V4_1 */
1169
1170/*
1171 * Read data off the transport. This can be either an RPC_CALL or an
1172 * RPC_REPLY. Relay the processing to helper functions.
1173 */
1174static void xs_tcp_read_data(struct rpc_xprt *xprt,
1175 struct xdr_skb_reader *desc)
1176{
1177 struct sock_xprt *transport =
1178 container_of(xprt, struct sock_xprt, xprt);
1179
1180 if (_xs_tcp_read_data(xprt, desc) == 0)
1181 xs_tcp_check_fraghdr(transport);
1182 else {
1183 /*
1184 * The transport_lock protects the request handling.
1185 * There's no need to hold it to update the tcp_flags.
1186 */
1187 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1188 }
Chuck Levera246b012005-08-11 16:25:23 -04001189}
1190
Chuck Leverdd456472006-12-05 16:35:44 -05001191static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001192{
1193 size_t len;
1194
Chuck Lever51971132006-12-05 16:35:19 -05001195 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001196 if (len > desc->count)
1197 len = desc->count;
1198 desc->count -= len;
1199 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001200 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001201 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001202 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001203}
1204
Chuck Lever9903cd12005-08-11 16:25:26 -04001205static 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 -04001206{
1207 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001208 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001209 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001210 .skb = skb,
1211 .offset = offset,
1212 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001213 };
Chuck Levera246b012005-08-11 16:25:23 -04001214
Chuck Lever46121cf2007-01-31 12:14:08 -05001215 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001216 do {
1217 /* Read in a new fragment marker if necessary */
1218 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001219 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001220 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001221 continue;
1222 }
1223 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001224 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001225 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001226 continue;
1227 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001228 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001229 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001230 xs_tcp_read_calldir(transport, &desc);
1231 continue;
1232 }
Chuck Levera246b012005-08-11 16:25:23 -04001233 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001234 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001235 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001236 continue;
1237 }
1238 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001239 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001240 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001241 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001242 return len - desc.count;
1243}
1244
Chuck Lever9903cd12005-08-11 16:25:26 -04001245/**
1246 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1247 * @sk: socket with data to read
1248 * @bytes: how much data to read
1249 *
1250 */
1251static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001252{
1253 struct rpc_xprt *xprt;
1254 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001255 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001256
Chuck Lever46121cf2007-01-31 12:14:08 -05001257 dprintk("RPC: xs_tcp_data_ready...\n");
1258
Chuck Levera246b012005-08-11 16:25:23 -04001259 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001260 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001261 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001262 if (xprt->shutdown)
1263 goto out;
1264
Chuck Lever9903cd12005-08-11 16:25:26 -04001265 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001266 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001267 do {
1268 rd_desc.count = 65536;
1269 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1270 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001271out:
1272 read_unlock(&sk->sk_callback_lock);
1273}
1274
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001275/*
1276 * Do the equivalent of linger/linger2 handling for dealing with
1277 * broken servers that don't close the socket in a timely
1278 * fashion
1279 */
1280static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1281 unsigned long timeout)
1282{
1283 struct sock_xprt *transport;
1284
1285 if (xprt_test_and_set_connecting(xprt))
1286 return;
1287 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1288 transport = container_of(xprt, struct sock_xprt, xprt);
1289 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1290 timeout);
1291}
1292
1293static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1294{
1295 struct sock_xprt *transport;
1296
1297 transport = container_of(xprt, struct sock_xprt, xprt);
1298
1299 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1300 !cancel_delayed_work(&transport->connect_worker))
1301 return;
1302 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1303 xprt_clear_connecting(xprt);
1304}
1305
1306static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1307{
1308 smp_mb__before_clear_bit();
1309 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1310 clear_bit(XPRT_CLOSING, &xprt->state);
1311 smp_mb__after_clear_bit();
1312 /* Mark transport as closed and wake up all pending tasks */
1313 xprt_disconnect_done(xprt);
1314}
1315
Chuck Lever9903cd12005-08-11 16:25:26 -04001316/**
1317 * xs_tcp_state_change - callback to handle TCP socket state changes
1318 * @sk: socket whose state has changed
1319 *
1320 */
1321static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001322{
Chuck Lever9903cd12005-08-11 16:25:26 -04001323 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001324
1325 read_lock(&sk->sk_callback_lock);
1326 if (!(xprt = xprt_from_sock(sk)))
1327 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001328 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
1329 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
1330 sk->sk_state, xprt_connected(xprt),
1331 sock_flag(sk, SOCK_DEAD),
1332 sock_flag(sk, SOCK_ZAPPED));
Chuck Levera246b012005-08-11 16:25:23 -04001333
1334 switch (sk->sk_state) {
1335 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001336 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001337 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001338 struct sock_xprt *transport = container_of(xprt,
1339 struct sock_xprt, xprt);
1340
Chuck Levera246b012005-08-11 16:25:23 -04001341 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001342 transport->tcp_offset = 0;
1343 transport->tcp_reclen = 0;
1344 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001345 transport->tcp_flags =
1346 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001347
Trond Myklebust2a491992009-03-11 14:38:00 -04001348 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001349 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001350 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001351 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001352 case TCP_FIN_WAIT1:
1353 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001354 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001355 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001356 set_bit(XPRT_CLOSING, &xprt->state);
1357 smp_mb__before_clear_bit();
1358 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001359 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001360 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001361 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001362 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001363 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001364 /* The server initiated a shutdown of the socket */
Trond Myklebust66af1e52007-11-06 10:18:36 -05001365 xprt_force_disconnect(xprt);
Trond Myklebust663b8852008-01-01 18:42:12 -05001366 case TCP_SYN_SENT:
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001367 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001368 case TCP_CLOSING:
1369 /*
1370 * If the server closed down the connection, make sure that
1371 * we back off before reconnecting
1372 */
1373 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1374 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001375 break;
1376 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001377 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001378 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001379 smp_mb__before_clear_bit();
1380 clear_bit(XPRT_CONNECTED, &xprt->state);
1381 smp_mb__after_clear_bit();
1382 break;
1383 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001384 xs_tcp_cancel_linger_timeout(xprt);
1385 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001386 }
1387 out:
1388 read_unlock(&sk->sk_callback_lock);
1389}
1390
Chuck Lever9903cd12005-08-11 16:25:26 -04001391/**
Trond Myklebust482f32e2009-03-11 14:38:00 -04001392 * xs_error_report - callback mainly for catching socket errors
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001393 * @sk: socket
1394 */
Trond Myklebust482f32e2009-03-11 14:38:00 -04001395static void xs_error_report(struct sock *sk)
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001396{
1397 struct rpc_xprt *xprt;
1398
1399 read_lock(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001400 if (!(xprt = xprt_from_sock(sk)))
1401 goto out;
1402 dprintk("RPC: %s client %p...\n"
1403 "RPC: error %d\n",
1404 __func__, xprt, sk->sk_err);
Trond Myklebust482f32e2009-03-11 14:38:00 -04001405 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001406out:
1407 read_unlock(&sk->sk_callback_lock);
1408}
1409
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001410static void xs_write_space(struct sock *sk)
1411{
1412 struct socket *sock;
1413 struct rpc_xprt *xprt;
1414
1415 if (unlikely(!(sock = sk->sk_socket)))
1416 return;
1417 clear_bit(SOCK_NOSPACE, &sock->flags);
1418
1419 if (unlikely(!(xprt = xprt_from_sock(sk))))
1420 return;
1421 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1422 return;
1423
1424 xprt_write_space(xprt);
1425}
1426
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001427/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001428 * xs_udp_write_space - callback invoked when socket buffer space
1429 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001430 * @sk: socket whose state has changed
1431 *
Chuck Levera246b012005-08-11 16:25:23 -04001432 * Called when more output buffer space is available for this socket.
1433 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001434 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001435 * with a bunch of small requests.
1436 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001437static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001438{
Chuck Levera246b012005-08-11 16:25:23 -04001439 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001440
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001441 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001442 if (sock_writeable(sk))
1443 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001444
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001445 read_unlock(&sk->sk_callback_lock);
1446}
Chuck Levera246b012005-08-11 16:25:23 -04001447
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001448/**
1449 * xs_tcp_write_space - callback invoked when socket buffer space
1450 * becomes available
1451 * @sk: socket whose state has changed
1452 *
1453 * Called when more output buffer space is available for this socket.
1454 * We try not to wake our writers until they can make "significant"
1455 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1456 * with a bunch of small requests.
1457 */
1458static void xs_tcp_write_space(struct sock *sk)
1459{
1460 read_lock(&sk->sk_callback_lock);
1461
1462 /* from net/core/stream.c:sk_stream_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001463 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1464 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001465
Chuck Levera246b012005-08-11 16:25:23 -04001466 read_unlock(&sk->sk_callback_lock);
1467}
1468
Chuck Lever470056c2005-08-25 16:25:56 -07001469static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001470{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001471 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1472 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001473
Chuck Lever7c6e0662006-12-05 16:35:30 -05001474 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001475 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001476 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001477 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001478 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001479 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001480 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001481 sk->sk_write_space(sk);
1482 }
1483}
1484
Chuck Lever43118c22005-08-25 16:25:49 -07001485/**
Chuck Lever470056c2005-08-25 16:25:56 -07001486 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001487 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001488 * @sndsize: requested size of send buffer, in bytes
1489 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001490 *
Chuck Lever470056c2005-08-25 16:25:56 -07001491 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001492 */
Chuck Lever470056c2005-08-25 16:25:56 -07001493static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001494{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001495 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1496
1497 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001498 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001499 transport->sndsize = sndsize + 1024;
1500 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001501 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001502 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001503
1504 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001505}
1506
Chuck Lever46c0ee82005-08-25 16:25:52 -07001507/**
1508 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1509 * @task: task that timed out
1510 *
1511 * Adjust the congestion window after a retransmit timeout has occurred.
1512 */
1513static void xs_udp_timer(struct rpc_task *task)
1514{
1515 xprt_adjust_cwnd(task, -ETIMEDOUT);
1516}
1517
Chuck Leverb85d8802006-05-25 01:40:49 -04001518static unsigned short xs_get_random_port(void)
1519{
1520 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1521 unsigned short rand = (unsigned short) net_random() % range;
1522 return rand + xprt_min_resvport;
1523}
1524
Chuck Lever92200412006-01-03 09:55:51 +01001525/**
1526 * xs_set_port - reset the port number in the remote endpoint address
1527 * @xprt: generic transport
1528 * @port: new port number
1529 *
1530 */
1531static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1532{
Chuck Lever46121cf2007-01-31 12:14:08 -05001533 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001534
Chuck Lever9dc3b092009-08-09 15:09:46 -04001535 rpc_set_port(xs_addr(xprt), port);
1536 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001537}
1538
Trond Myklebust67a391d2007-11-05 17:40:58 -05001539static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
1540{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001541 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001542
1543 if (port == 0 && transport->xprt.resvport)
1544 port = xs_get_random_port();
1545 return port;
1546}
1547
1548static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
1549{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001550 if (transport->srcport != 0)
1551 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001552 if (!transport->xprt.resvport)
1553 return 0;
1554 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1555 return xprt_max_resvport;
1556 return --port;
1557}
1558
Chuck Lever7dc753f2007-08-06 11:57:28 -04001559static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001560{
1561 struct sockaddr_in myaddr = {
1562 .sin_family = AF_INET,
1563 };
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001564 struct sockaddr_in *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001565 int err, nloop = 0;
1566 unsigned short port = xs_get_srcport(transport, sock);
1567 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001568
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001569 sa = (struct sockaddr_in *)&transport->srcaddr;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001570 myaddr.sin_addr = sa->sin_addr;
Chuck Levera246b012005-08-11 16:25:23 -04001571 do {
1572 myaddr.sin_port = htons(port);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001573 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
Chuck Levera246b012005-08-11 16:25:23 -04001574 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001575 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001576 break;
Chuck Levera246b012005-08-11 16:25:23 -04001577 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001578 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001579 break;
Chuck Levera246b012005-08-11 16:25:23 -04001580 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001581 last = port;
1582 port = xs_next_srcport(transport, sock, port);
1583 if (port > last)
1584 nloop++;
1585 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison21454aa2008-10-31 00:54:56 -07001586 dprintk("RPC: %s %pI4:%u: %s (%d)\n",
1587 __func__, &myaddr.sin_addr,
Chuck Lever7dc753f2007-08-06 11:57:28 -04001588 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001589 return err;
1590}
1591
Chuck Lever90058d32007-08-06 11:57:33 -04001592static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
1593{
1594 struct sockaddr_in6 myaddr = {
1595 .sin6_family = AF_INET6,
1596 };
1597 struct sockaddr_in6 *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001598 int err, nloop = 0;
1599 unsigned short port = xs_get_srcport(transport, sock);
1600 unsigned short last;
Chuck Lever90058d32007-08-06 11:57:33 -04001601
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001602 sa = (struct sockaddr_in6 *)&transport->srcaddr;
Chuck Lever90058d32007-08-06 11:57:33 -04001603 myaddr.sin6_addr = sa->sin6_addr;
1604 do {
1605 myaddr.sin6_port = htons(port);
1606 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1607 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001608 if (port == 0)
Chuck Lever90058d32007-08-06 11:57:33 -04001609 break;
1610 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001611 transport->srcport = port;
Chuck Lever90058d32007-08-06 11:57:33 -04001612 break;
1613 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001614 last = port;
1615 port = xs_next_srcport(transport, sock, port);
1616 if (port > last)
1617 nloop++;
1618 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001619 dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07001620 &myaddr.sin6_addr, port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001621 return err;
1622}
1623
Peter Zijlstraed075362006-12-06 20:35:24 -08001624#ifdef CONFIG_DEBUG_LOCK_ALLOC
1625static struct lock_class_key xs_key[2];
1626static struct lock_class_key xs_slock_key[2];
1627
Chuck Lever8945ee52007-08-06 11:58:04 -04001628static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001629{
1630 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001631
John Heffner02b3d342007-09-12 10:42:12 +02001632 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001633 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1634 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1635}
Peter Zijlstraed075362006-12-06 20:35:24 -08001636
Chuck Lever8945ee52007-08-06 11:58:04 -04001637static inline void xs_reclassify_socket6(struct socket *sock)
1638{
1639 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001640
Linus Torvaldsf4921af2007-10-15 10:46:05 -07001641 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001642 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1643 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001644}
1645#else
Chuck Lever8945ee52007-08-06 11:58:04 -04001646static inline void xs_reclassify_socket4(struct socket *sock)
1647{
1648}
1649
1650static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001651{
1652}
1653#endif
1654
Chuck Lever16be2d22007-08-06 11:57:38 -04001655static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001656{
Chuck Lever16be2d22007-08-06 11:57:38 -04001657 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001658
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001659 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001660 struct sock *sk = sock->sk;
1661
1662 write_lock_bh(&sk->sk_callback_lock);
1663
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001664 xs_save_old_callbacks(transport, sk);
1665
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001666 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001667 sk->sk_data_ready = xs_udp_data_ready;
1668 sk->sk_write_space = xs_udp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001669 sk->sk_error_report = xs_error_report;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001670 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001671 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001672
1673 xprt_set_connected(xprt);
1674
1675 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001676 transport->sock = sock;
1677 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001678
1679 write_unlock_bh(&sk->sk_callback_lock);
1680 }
Chuck Lever470056c2005-08-25 16:25:56 -07001681 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04001682}
1683
Chuck Levera246b012005-08-11 16:25:23 -04001684/**
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001685 * xs_udp_connect_worker4 - set up a UDP socket
Chuck Levera246b012005-08-11 16:25:23 -04001686 * @work: RPC transport to connect
1687 *
1688 * Invoked by a work queue tasklet.
1689 */
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001690static void xs_udp_connect_worker4(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04001691{
1692 struct sock_xprt *transport =
1693 container_of(work, struct sock_xprt, connect_worker.work);
1694 struct rpc_xprt *xprt = &transport->xprt;
1695 struct socket *sock = transport->sock;
1696 int err, status = -EIO;
1697
Trond Myklebust01d37c42009-03-11 14:09:39 -04001698 if (xprt->shutdown)
Chuck Lever9903cd12005-08-11 16:25:26 -04001699 goto out;
1700
Chuck Levera246b012005-08-11 16:25:23 -04001701 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001702 xs_reset_transport(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001703
Chuck Leverfe315e72009-03-11 14:10:21 -04001704 err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1705 if (err < 0) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001706 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001707 goto out;
1708 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001709 xs_reclassify_socket4(sock);
Chuck Levera246b012005-08-11 16:25:23 -04001710
Chuck Lever7dc753f2007-08-06 11:57:28 -04001711 if (xs_bind4(transport, sock)) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001712 sock_release(sock);
1713 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001714 }
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001715
Chuck Leverc740eff2009-08-09 15:09:46 -04001716 dprintk("RPC: worker connecting xprt %p via %s to "
1717 "%s (port %s)\n", xprt,
1718 xprt->address_strings[RPC_DISPLAY_PROTO],
1719 xprt->address_strings[RPC_DISPLAY_ADDR],
1720 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001721
Chuck Lever16be2d22007-08-06 11:57:38 -04001722 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001723 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001724out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001725 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001726 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001727}
1728
Chuck Lever68e220b2007-08-06 11:57:48 -04001729/**
1730 * xs_udp_connect_worker6 - set up a UDP socket
1731 * @work: RPC transport to connect
1732 *
1733 * Invoked by a work queue tasklet.
1734 */
1735static void xs_udp_connect_worker6(struct work_struct *work)
1736{
1737 struct sock_xprt *transport =
1738 container_of(work, struct sock_xprt, connect_worker.work);
1739 struct rpc_xprt *xprt = &transport->xprt;
1740 struct socket *sock = transport->sock;
1741 int err, status = -EIO;
1742
Trond Myklebust01d37c42009-03-11 14:09:39 -04001743 if (xprt->shutdown)
Chuck Lever68e220b2007-08-06 11:57:48 -04001744 goto out;
1745
1746 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001747 xs_reset_transport(transport);
Chuck Lever68e220b2007-08-06 11:57:48 -04001748
Chuck Leverfe315e72009-03-11 14:10:21 -04001749 err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
1750 if (err < 0) {
Chuck Lever68e220b2007-08-06 11:57:48 -04001751 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1752 goto out;
1753 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001754 xs_reclassify_socket6(sock);
Chuck Lever68e220b2007-08-06 11:57:48 -04001755
1756 if (xs_bind6(transport, sock) < 0) {
1757 sock_release(sock);
1758 goto out;
1759 }
1760
Chuck Leverc740eff2009-08-09 15:09:46 -04001761 dprintk("RPC: worker connecting xprt %p via %s to "
1762 "%s (port %s)\n", xprt,
1763 xprt->address_strings[RPC_DISPLAY_PROTO],
1764 xprt->address_strings[RPC_DISPLAY_ADDR],
1765 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04001766
1767 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001768 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001769out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001770 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001771 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001772}
1773
Chuck Lever3167e122005-08-25 16:25:55 -07001774/*
1775 * We need to preserve the port number so the reply cache on the server can
1776 * find our cached RPC replies when we get around to reconnecting.
1777 */
Trond Myklebust40d25492009-03-11 14:37:58 -04001778static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07001779{
1780 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07001781 struct sockaddr any;
1782
Chuck Lever46121cf2007-01-31 12:14:08 -05001783 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
Chuck Lever3167e122005-08-25 16:25:55 -07001784
1785 /*
1786 * Disconnect the transport socket by doing a connect operation
1787 * with AF_UNSPEC. This should return immediately...
1788 */
1789 memset(&any, 0, sizeof(any));
1790 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001791 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001792 if (!result)
1793 xs_sock_mark_closed(xprt);
1794 else
Chuck Lever46121cf2007-01-31 12:14:08 -05001795 dprintk("RPC: AF_UNSPEC connect return code %d\n",
Chuck Lever3167e122005-08-25 16:25:55 -07001796 result);
1797}
1798
Trond Myklebust40d25492009-03-11 14:37:58 -04001799static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
1800{
1801 unsigned int state = transport->inet->sk_state;
1802
1803 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED)
1804 return;
1805 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT))
1806 return;
1807 xs_abort_connection(xprt, transport);
1808}
1809
Chuck Lever16be2d22007-08-06 11:57:38 -04001810static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001811{
Chuck Lever16be2d22007-08-06 11:57:38 -04001812 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001813
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001814 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001815 struct sock *sk = sock->sk;
1816
1817 write_lock_bh(&sk->sk_callback_lock);
1818
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001819 xs_save_old_callbacks(transport, sk);
1820
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001821 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001822 sk->sk_data_ready = xs_tcp_data_ready;
1823 sk->sk_state_change = xs_tcp_state_change;
1824 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001825 sk->sk_error_report = xs_error_report;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001826 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07001827
1828 /* socket options */
1829 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1830 sock_reset_flag(sk, SOCK_LINGER);
1831 tcp_sk(sk)->linger2 = 0;
1832 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001833
1834 xprt_clear_connected(xprt);
1835
1836 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001837 transport->sock = sock;
1838 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001839
1840 write_unlock_bh(&sk->sk_callback_lock);
1841 }
1842
Trond Myklebust01d37c42009-03-11 14:09:39 -04001843 if (!xprt_bound(xprt))
1844 return -ENOTCONN;
1845
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001846 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05001847 xprt->stat.connect_count++;
1848 xprt->stat.connect_start = jiffies;
Chuck Lever95392c52007-08-06 11:57:58 -04001849 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
Chuck Lever16be2d22007-08-06 11:57:38 -04001850}
1851
1852/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001853 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1854 * @xprt: RPC transport to connect
1855 * @transport: socket transport to connect
1856 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04001857 *
1858 * Invoked by a work queue tasklet.
1859 */
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001860static void xs_tcp_setup_socket(struct rpc_xprt *xprt,
1861 struct sock_xprt *transport,
1862 struct socket *(*create_sock)(struct rpc_xprt *,
1863 struct sock_xprt *))
Chuck Lever16be2d22007-08-06 11:57:38 -04001864{
Chuck Lever16be2d22007-08-06 11:57:38 -04001865 struct socket *sock = transport->sock;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001866 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04001867
Trond Myklebust01d37c42009-03-11 14:09:39 -04001868 if (xprt->shutdown)
Chuck Lever16be2d22007-08-06 11:57:38 -04001869 goto out;
1870
1871 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001872 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001873 sock = create_sock(xprt, transport);
1874 if (IS_ERR(sock)) {
1875 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04001876 goto out;
1877 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001878 } else {
1879 int abort_and_exit;
1880
1881 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
1882 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04001883 /* "close" the socket, preserving the local port */
Trond Myklebust40d25492009-03-11 14:37:58 -04001884 xs_tcp_reuse_connection(xprt, transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04001885
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001886 if (abort_and_exit)
1887 goto out_eagain;
1888 }
1889
Chuck Leverc740eff2009-08-09 15:09:46 -04001890 dprintk("RPC: worker connecting xprt %p via %s to "
1891 "%s (port %s)\n", xprt,
1892 xprt->address_strings[RPC_DISPLAY_PROTO],
1893 xprt->address_strings[RPC_DISPLAY_ADDR],
1894 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04001895
1896 status = xs_tcp_finish_connecting(xprt, sock);
Chuck Lever46121cf2007-01-31 12:14:08 -05001897 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1898 xprt, -status, xprt_connected(xprt),
1899 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04001900 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04001901 default:
1902 printk("%s: connect returned unhandled error %d\n",
1903 __func__, status);
1904 case -EADDRNOTAVAIL:
1905 /* We're probably in TIME_WAIT. Get rid of existing socket,
1906 * and retry
1907 */
1908 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1909 xprt_force_disconnect(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07001910 break;
Trond Myklebust8a2cec22009-03-11 14:38:01 -04001911 case -ECONNREFUSED:
1912 case -ECONNRESET:
1913 case -ENETUNREACH:
1914 /* retry with existing socket, after a delay */
Trond Myklebust2a491992009-03-11 14:38:00 -04001915 case 0:
1916 case -EINPROGRESS:
1917 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001918 xprt_clear_connecting(xprt);
1919 return;
Chuck Levera246b012005-08-11 16:25:23 -04001920 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001921out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04001922 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04001923out:
Chuck Lever2226feb2005-08-11 16:25:38 -04001924 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001925 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001926}
1927
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001928static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt,
1929 struct sock_xprt *transport)
1930{
1931 struct socket *sock;
1932 int err;
1933
1934 /* start from scratch */
1935 err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1936 if (err < 0) {
1937 dprintk("RPC: can't create TCP transport socket (%d).\n",
1938 -err);
1939 goto out_err;
1940 }
1941 xs_reclassify_socket4(sock);
1942
1943 if (xs_bind4(transport, sock) < 0) {
1944 sock_release(sock);
1945 goto out_err;
1946 }
1947 return sock;
1948out_err:
1949 return ERR_PTR(-EIO);
1950}
1951
1952/**
Chuck Levera246b012005-08-11 16:25:23 -04001953 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1954 * @work: RPC transport to connect
1955 *
1956 * Invoked by a work queue tasklet.
1957 */
1958static void xs_tcp_connect_worker4(struct work_struct *work)
1959{
1960 struct sock_xprt *transport =
1961 container_of(work, struct sock_xprt, connect_worker.work);
1962 struct rpc_xprt *xprt = &transport->xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001963
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001964 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4);
1965}
Chuck Levera246b012005-08-11 16:25:23 -04001966
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001967static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt,
1968 struct sock_xprt *transport)
1969{
1970 struct socket *sock;
1971 int err;
Chuck Lever9903cd12005-08-11 16:25:26 -04001972
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001973 /* start from scratch */
1974 err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock);
1975 if (err < 0) {
1976 dprintk("RPC: can't create TCP transport socket (%d).\n",
1977 -err);
1978 goto out_err;
Chuck Levera246b012005-08-11 16:25:23 -04001979 }
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001980 xs_reclassify_socket6(sock);
1981
1982 if (xs_bind6(transport, sock) < 0) {
1983 sock_release(sock);
1984 goto out_err;
1985 }
1986 return sock;
1987out_err:
1988 return ERR_PTR(-EIO);
Chuck Levera246b012005-08-11 16:25:23 -04001989}
1990
Chuck Lever9903cd12005-08-11 16:25:26 -04001991/**
Chuck Lever68e220b2007-08-06 11:57:48 -04001992 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
1993 * @work: RPC transport to connect
1994 *
1995 * Invoked by a work queue tasklet.
1996 */
1997static void xs_tcp_connect_worker6(struct work_struct *work)
1998{
1999 struct sock_xprt *transport =
2000 container_of(work, struct sock_xprt, connect_worker.work);
2001 struct rpc_xprt *xprt = &transport->xprt;
Chuck Lever68e220b2007-08-06 11:57:48 -04002002
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002003 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6);
Chuck Lever68e220b2007-08-06 11:57:48 -04002004}
2005
2006/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002007 * xs_connect - connect a socket to a remote endpoint
2008 * @task: address of RPC task that manages state of connect request
2009 *
2010 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002011 *
2012 * UDP socket connects are synchronous, but we use a work queue anyway
2013 * to guarantee that even unprivileged user processes can set up a
2014 * socket on a privileged port.
2015 *
2016 * If a UDP socket connect fails, the delay behavior here prevents
2017 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002018 */
2019static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002020{
2021 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002022 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002023
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002024 if (xprt_test_and_set_connecting(xprt))
2025 return;
2026
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002027 if (transport->sock != NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002028 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2029 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002030 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002031 queue_delayed_work(rpciod_workqueue,
2032 &transport->connect_worker,
2033 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07002034 xprt->reestablish_timeout <<= 1;
2035 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2036 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002037 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05002038 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002039 queue_delayed_work(rpciod_workqueue,
2040 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002041 }
2042}
2043
Trond Myklebuste06799f2007-11-05 15:44:12 -05002044static void xs_tcp_connect(struct rpc_task *task)
2045{
2046 struct rpc_xprt *xprt = task->tk_xprt;
2047
Trond Myklebuste06799f2007-11-05 15:44:12 -05002048 /* Exit if we need to wait for socket shutdown to complete */
2049 if (test_bit(XPRT_CLOSING, &xprt->state))
2050 return;
2051 xs_connect(task);
2052}
2053
Chuck Lever262ca072006-03-20 13:44:16 -05002054/**
2055 * xs_udp_print_stats - display UDP socket-specifc stats
2056 * @xprt: rpc_xprt struct containing statistics
2057 * @seq: output file
2058 *
2059 */
2060static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2061{
Chuck Leverc8475462006-12-05 16:35:26 -05002062 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2063
Chuck Lever262ca072006-03-20 13:44:16 -05002064 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002065 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002066 xprt->stat.bind_count,
2067 xprt->stat.sends,
2068 xprt->stat.recvs,
2069 xprt->stat.bad_xids,
2070 xprt->stat.req_u,
2071 xprt->stat.bklog_u);
2072}
2073
2074/**
2075 * xs_tcp_print_stats - display TCP socket-specifc stats
2076 * @xprt: rpc_xprt struct containing statistics
2077 * @seq: output file
2078 *
2079 */
2080static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2081{
Chuck Leverc8475462006-12-05 16:35:26 -05002082 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002083 long idle_time = 0;
2084
2085 if (xprt_connected(xprt))
2086 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2087
2088 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002089 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002090 xprt->stat.bind_count,
2091 xprt->stat.connect_count,
2092 xprt->stat.connect_time,
2093 idle_time,
2094 xprt->stat.sends,
2095 xprt->stat.recvs,
2096 xprt->stat.bad_xids,
2097 xprt->stat.req_u,
2098 xprt->stat.bklog_u);
2099}
2100
Chuck Lever262965f2005-08-11 16:25:56 -04002101static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002102 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002103 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002104 .release_xprt = xprt_release_xprt_cong,
Chuck Lever45160d62007-07-01 12:13:17 -04002105 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002106 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002107 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002108 .buf_alloc = rpc_malloc,
2109 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002110 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002111 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002112 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002113 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002114 .close = xs_close,
2115 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002116 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04002117};
2118
2119static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002120 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002121 .release_xprt = xs_tcp_release_xprt,
Chuck Lever45160d62007-07-01 12:13:17 -04002122 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002123 .set_port = xs_set_port,
Trond Myklebuste06799f2007-11-05 15:44:12 -05002124 .connect = xs_tcp_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002125 .buf_alloc = rpc_malloc,
2126 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002127 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002128 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Ricardo Labiaga0d90ba12009-04-01 09:23:04 -04002129#if defined(CONFIG_NFS_V4_1)
2130 .release_request = bc_release_request,
2131#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustf75e6742009-04-21 17:18:20 -04002132 .close = xs_tcp_close,
Chuck Lever9903cd12005-08-11 16:25:26 -04002133 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002134 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002135};
2136
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002137static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002138 unsigned int slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002139{
2140 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002141 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002142
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002143 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002144 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002145 return ERR_PTR(-EBADF);
2146 }
2147
Chuck Leverffc2e512006-12-05 16:35:11 -05002148 new = kzalloc(sizeof(*new), GFP_KERNEL);
2149 if (new == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002150 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2151 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002152 return ERR_PTR(-ENOMEM);
2153 }
Chuck Leverffc2e512006-12-05 16:35:11 -05002154 xprt = &new->xprt;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002155
2156 xprt->max_reqs = slot_table_size;
2157 xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
2158 if (xprt->slot == NULL) {
2159 kfree(xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05002160 dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
2161 "table\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002162 return ERR_PTR(-ENOMEM);
2163 }
2164
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002165 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2166 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002167 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002168 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002169
2170 return xprt;
2171}
2172
Trond Myklebust2881ae72007-12-20 16:03:54 -05002173static const struct rpc_timeout xs_udp_default_timeout = {
2174 .to_initval = 5 * HZ,
2175 .to_maxval = 30 * HZ,
2176 .to_increment = 5 * HZ,
2177 .to_retries = 5,
2178};
2179
Chuck Lever9903cd12005-08-11 16:25:26 -04002180/**
2181 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002182 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002183 *
2184 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002185static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002186{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002187 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002188 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002189 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04002190
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002191 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002192 if (IS_ERR(xprt))
2193 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002194 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002195
Chuck Leverec739ef2006-08-22 20:06:15 -04002196 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002197 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002198 /* XXX: header size can vary due to auth type, IPv6, etc. */
2199 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2200
Chuck Lever03bf4b72005-08-25 16:25:55 -07002201 xprt->bind_timeout = XS_BIND_TO;
2202 xprt->connect_timeout = XS_UDP_CONN_TO;
2203 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2204 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002205
Chuck Lever262965f2005-08-11 16:25:56 -04002206 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002207
Trond Myklebustba7392b2007-12-20 16:03:55 -05002208 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002209
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002210 switch (addr->sa_family) {
2211 case AF_INET:
2212 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2213 xprt_set_bound(xprt);
2214
2215 INIT_DELAYED_WORK(&transport->connect_worker,
2216 xs_udp_connect_worker4);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002217 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002218 break;
2219 case AF_INET6:
2220 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2221 xprt_set_bound(xprt);
2222
2223 INIT_DELAYED_WORK(&transport->connect_worker,
2224 xs_udp_connect_worker6);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002225 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002226 break;
2227 default:
2228 kfree(xprt);
2229 return ERR_PTR(-EAFNOSUPPORT);
2230 }
2231
Chuck Leverc740eff2009-08-09 15:09:46 -04002232 if (xprt_bound(xprt))
2233 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2234 xprt->address_strings[RPC_DISPLAY_ADDR],
2235 xprt->address_strings[RPC_DISPLAY_PORT],
2236 xprt->address_strings[RPC_DISPLAY_PROTO]);
2237 else
2238 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2239 xprt->address_strings[RPC_DISPLAY_ADDR],
2240 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002241
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002242 if (try_module_get(THIS_MODULE))
2243 return xprt;
2244
2245 kfree(xprt->slot);
2246 kfree(xprt);
2247 return ERR_PTR(-EINVAL);
Chuck Levera246b012005-08-11 16:25:23 -04002248}
2249
Trond Myklebust2881ae72007-12-20 16:03:54 -05002250static const struct rpc_timeout xs_tcp_default_timeout = {
2251 .to_initval = 60 * HZ,
2252 .to_maxval = 60 * HZ,
2253 .to_retries = 2,
2254};
2255
Chuck Lever9903cd12005-08-11 16:25:26 -04002256/**
2257 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002258 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002259 *
2260 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002261static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002262{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002263 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002264 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002265 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04002266
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002267 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002268 if (IS_ERR(xprt))
2269 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002270 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002271
Chuck Leverec739ef2006-08-22 20:06:15 -04002272 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002273 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002274 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002275
Chuck Lever03bf4b72005-08-25 16:25:55 -07002276 xprt->bind_timeout = XS_BIND_TO;
2277 xprt->connect_timeout = XS_TCP_CONN_TO;
2278 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2279 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002280
Chuck Lever262965f2005-08-11 16:25:56 -04002281 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002282 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002283
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002284 switch (addr->sa_family) {
2285 case AF_INET:
2286 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2287 xprt_set_bound(xprt);
2288
Chuck Lever9dc3b092009-08-09 15:09:46 -04002289 INIT_DELAYED_WORK(&transport->connect_worker,
2290 xs_tcp_connect_worker4);
2291 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002292 break;
2293 case AF_INET6:
2294 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2295 xprt_set_bound(xprt);
2296
Chuck Lever9dc3b092009-08-09 15:09:46 -04002297 INIT_DELAYED_WORK(&transport->connect_worker,
2298 xs_tcp_connect_worker6);
2299 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002300 break;
2301 default:
2302 kfree(xprt);
2303 return ERR_PTR(-EAFNOSUPPORT);
2304 }
2305
Chuck Leverc740eff2009-08-09 15:09:46 -04002306 if (xprt_bound(xprt))
2307 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2308 xprt->address_strings[RPC_DISPLAY_ADDR],
2309 xprt->address_strings[RPC_DISPLAY_PORT],
2310 xprt->address_strings[RPC_DISPLAY_PROTO]);
2311 else
2312 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2313 xprt->address_strings[RPC_DISPLAY_ADDR],
2314 xprt->address_strings[RPC_DISPLAY_PROTO]);
2315
Chuck Leveredb267a2006-08-22 20:06:18 -04002316
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002317 if (try_module_get(THIS_MODULE))
2318 return xprt;
2319
2320 kfree(xprt->slot);
2321 kfree(xprt);
2322 return ERR_PTR(-EINVAL);
Chuck Levera246b012005-08-11 16:25:23 -04002323}
Chuck Lever282b32e2006-12-05 16:35:51 -05002324
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002325static struct xprt_class xs_udp_transport = {
2326 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2327 .name = "udp",
2328 .owner = THIS_MODULE,
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -04002329 .ident = IPPROTO_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002330 .setup = xs_setup_udp,
2331};
2332
2333static struct xprt_class xs_tcp_transport = {
2334 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2335 .name = "tcp",
2336 .owner = THIS_MODULE,
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -04002337 .ident = IPPROTO_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002338 .setup = xs_setup_tcp,
2339};
2340
Chuck Lever282b32e2006-12-05 16:35:51 -05002341/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002342 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002343 *
2344 */
2345int init_socket_xprt(void)
2346{
Chuck Leverfbf76682006-12-05 16:35:54 -05002347#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002348 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002349 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002350#endif
2351
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002352 xprt_register_transport(&xs_udp_transport);
2353 xprt_register_transport(&xs_tcp_transport);
2354
Chuck Lever282b32e2006-12-05 16:35:51 -05002355 return 0;
2356}
2357
2358/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002359 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05002360 *
2361 */
2362void cleanup_socket_xprt(void)
2363{
Chuck Leverfbf76682006-12-05 16:35:54 -05002364#ifdef RPC_DEBUG
2365 if (sunrpc_table_header) {
2366 unregister_sysctl_table(sunrpc_table_header);
2367 sunrpc_table_header = NULL;
2368 }
2369#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002370
2371 xprt_unregister_transport(&xs_udp_transport);
2372 xprt_unregister_transport(&xs_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05002373}
Trond Myklebustcbf11072009-08-09 15:06:19 -04002374
2375static int param_set_uint_minmax(const char *val, struct kernel_param *kp,
2376 unsigned int min, unsigned int max)
2377{
2378 unsigned long num;
2379 int ret;
2380
2381 if (!val)
2382 return -EINVAL;
2383 ret = strict_strtoul(val, 0, &num);
2384 if (ret == -EINVAL || num < min || num > max)
2385 return -EINVAL;
2386 *((unsigned int *)kp->arg) = num;
2387 return 0;
2388}
2389
2390static int param_set_portnr(const char *val, struct kernel_param *kp)
2391{
2392 return param_set_uint_minmax(val, kp,
2393 RPC_MIN_RESVPORT,
2394 RPC_MAX_RESVPORT);
2395}
2396
2397static int param_get_portnr(char *buffer, struct kernel_param *kp)
2398{
2399 return param_get_uint(buffer, kp);
2400}
2401#define param_check_portnr(name, p) \
2402 __param_check(name, p, unsigned int);
2403
2404module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
2405module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
2406
2407static int param_set_slot_table_size(const char *val, struct kernel_param *kp)
2408{
2409 return param_set_uint_minmax(val, kp,
2410 RPC_MIN_SLOT_TABLE,
2411 RPC_MAX_SLOT_TABLE);
2412}
2413
2414static int param_get_slot_table_size(char *buffer, struct kernel_param *kp)
2415{
2416 return param_get_uint(buffer, kp);
2417}
2418#define param_check_slot_table_size(name, p) \
2419 __param_check(name, p, unsigned int);
2420
2421module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
2422 slot_table_size, 0644);
2423module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
2424 slot_table_size, 0644);
2425