blob: 2a9675136c68e487adcf02849c54680ed7fd0e98 [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>
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030035#include <linux/sunrpc/svcsock.h>
\"Talpey, Thomas\49c36fc2007-09-10 13:47:31 -040036#include <linux/sunrpc/xprtsock.h>
Chuck Levera246b012005-08-11 16:25:23 -040037#include <linux/file.h>
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -040038#ifdef CONFIG_NFS_V4_1
39#include <linux/sunrpc/bc_xprt.h>
40#endif
Chuck Levera246b012005-08-11 16:25:23 -040041
42#include <net/sock.h>
43#include <net/checksum.h>
44#include <net/udp.h>
45#include <net/tcp.h>
46
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030047#include "sunrpc.h"
Chuck Lever9903cd12005-08-11 16:25:26 -040048/*
Chuck Leverc556b752005-11-01 12:24:48 -050049 * xprtsock tunables
50 */
51unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
52unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
53
54unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
55unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
56
Trond Myklebust7d1e8252009-03-11 14:38:03 -040057#define XS_TCP_LINGER_TO (15U * HZ)
Trond Myklebust25fe6142009-03-11 14:38:03 -040058static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
Trond Myklebust7d1e8252009-03-11 14:38:03 -040059
Chuck Leverc556b752005-11-01 12:24:48 -050060/*
Chuck Leverfbf76682006-12-05 16:35:54 -050061 * We can register our own files under /proc/sys/sunrpc by
62 * calling register_sysctl_table() again. The files in that
63 * directory become the union of all files registered there.
64 *
65 * We simply need to make sure that we don't collide with
66 * someone else's file names!
67 */
68
69#ifdef RPC_DEBUG
70
71static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
72static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
73static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
74static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
75
76static struct ctl_table_header *sunrpc_table_header;
77
78/*
79 * FIXME: changing the UDP slot table size should also resize the UDP
80 * socket buffers for existing UDP transports
81 */
82static ctl_table xs_tunables_table[] = {
83 {
Chuck Leverfbf76682006-12-05 16:35:54 -050084 .procname = "udp_slot_table_entries",
85 .data = &xprt_udp_slot_table_entries,
86 .maxlen = sizeof(unsigned int),
87 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080088 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -050089 .extra1 = &min_slot_table_size,
90 .extra2 = &max_slot_table_size
91 },
92 {
Chuck Leverfbf76682006-12-05 16:35:54 -050093 .procname = "tcp_slot_table_entries",
94 .data = &xprt_tcp_slot_table_entries,
95 .maxlen = sizeof(unsigned int),
96 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080097 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -050098 .extra1 = &min_slot_table_size,
99 .extra2 = &max_slot_table_size
100 },
101 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500102 .procname = "min_resvport",
103 .data = &xprt_min_resvport,
104 .maxlen = sizeof(unsigned int),
105 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800106 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500107 .extra1 = &xprt_min_resvport_limit,
108 .extra2 = &xprt_max_resvport_limit
109 },
110 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500111 .procname = "max_resvport",
112 .data = &xprt_max_resvport,
113 .maxlen = sizeof(unsigned int),
114 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800115 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500116 .extra1 = &xprt_min_resvport_limit,
117 .extra2 = &xprt_max_resvport_limit
118 },
119 {
Trond Myklebust25fe6142009-03-11 14:38:03 -0400120 .procname = "tcp_fin_timeout",
121 .data = &xs_tcp_fin_timeout,
122 .maxlen = sizeof(xs_tcp_fin_timeout),
123 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800124 .proc_handler = proc_dointvec_jiffies,
Trond Myklebust25fe6142009-03-11 14:38:03 -0400125 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800126 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500127};
128
129static ctl_table sunrpc_table[] = {
130 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500131 .procname = "sunrpc",
132 .mode = 0555,
133 .child = xs_tunables_table
134 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800135 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500136};
137
138#endif
139
140/*
Chuck Lever03bf4b72005-08-25 16:25:55 -0700141 * Wait duration for a reply from the RPC portmapper.
142 */
143#define XS_BIND_TO (60U * HZ)
144
145/*
146 * Delay if a UDP socket connect error occurs. This is most likely some
147 * kind of resource problem on the local host.
148 */
149#define XS_UDP_REEST_TO (2U * HZ)
150
151/*
152 * The reestablish timeout allows clients to delay for a bit before attempting
153 * to reconnect to a server that just dropped our connection.
154 *
155 * We implement an exponential backoff when trying to reestablish a TCP
156 * transport connection with the server. Some servers like to drop a TCP
157 * connection when they are overworked, so we start with a short timeout and
158 * increase over time if the server is down or not responding.
159 */
160#define XS_TCP_INIT_REEST_TO (3U * HZ)
161#define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
162
163/*
164 * TCP idle timeout; client drops the transport socket if it is idle
165 * for this long. Note that we also timeout UDP sockets to prevent
166 * holding port numbers when there is no RPC traffic.
167 */
168#define XS_IDLE_DISC_TO (5U * 60 * HZ)
169
Chuck Levera246b012005-08-11 16:25:23 -0400170#ifdef RPC_DEBUG
171# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400172# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400173#endif
174
Chuck Levera246b012005-08-11 16:25:23 -0400175#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400176static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400177{
Chuck Lever9903cd12005-08-11 16:25:26 -0400178 u8 *buf = (u8 *) packet;
179 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400180
Chuck Lever46121cf2007-01-31 12:14:08 -0500181 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400182 for (j = 0; j < count && j < 128; j += 4) {
183 if (!(j & 31)) {
184 if (j)
185 dprintk("\n");
186 dprintk("0x%04x ", j);
187 }
188 dprintk("%02x%02x%02x%02x ",
189 buf[j], buf[j+1], buf[j+2], buf[j+3]);
190 }
191 dprintk("\n");
192}
193#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400194static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400195{
196 /* NOP */
197}
198#endif
199
Chuck Leverffc2e512006-12-05 16:35:11 -0500200struct sock_xprt {
201 struct rpc_xprt xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500202
203 /*
204 * Network layer
205 */
206 struct socket * sock;
207 struct sock * inet;
Chuck Lever51971132006-12-05 16:35:19 -0500208
209 /*
210 * State of TCP reply receive
211 */
212 __be32 tcp_fraghdr,
213 tcp_xid;
214
215 u32 tcp_offset,
216 tcp_reclen;
217
218 unsigned long tcp_copied,
219 tcp_flags;
Chuck Leverc8475462006-12-05 16:35:26 -0500220
221 /*
222 * Connection of transports
223 */
Trond Myklebust34161db2006-12-07 15:48:15 -0500224 struct delayed_work connect_worker;
Chuck Leverfbfffbd2009-08-09 15:09:46 -0400225 struct sockaddr_storage srcaddr;
226 unsigned short srcport;
Chuck Lever7c6e0662006-12-05 16:35:30 -0500227
228 /*
229 * UDP socket buffer size parameters
230 */
231 size_t rcvsize,
232 sndsize;
Chuck Lever314dfd72006-12-05 16:35:34 -0500233
234 /*
235 * Saved socket callback addresses
236 */
237 void (*old_data_ready)(struct sock *, int);
238 void (*old_state_change)(struct sock *);
239 void (*old_write_space)(struct sock *);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400240 void (*old_error_report)(struct sock *);
Chuck Leverffc2e512006-12-05 16:35:11 -0500241};
242
Chuck Levere136d092006-12-05 16:35:23 -0500243/*
244 * TCP receive state flags
245 */
246#define TCP_RCV_LAST_FRAG (1UL << 0)
247#define TCP_RCV_COPY_FRAGHDR (1UL << 1)
248#define TCP_RCV_COPY_XID (1UL << 2)
249#define TCP_RCV_COPY_DATA (1UL << 3)
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400250#define TCP_RCV_READ_CALLDIR (1UL << 4)
251#define TCP_RCV_COPY_CALLDIR (1UL << 5)
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400252
253/*
254 * TCP RPC flags
255 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400256#define TCP_RPC_REPLY (1UL << 6)
Chuck Levere136d092006-12-05 16:35:23 -0500257
Chuck Lever95392c52007-08-06 11:57:58 -0400258static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400259{
Chuck Lever95392c52007-08-06 11:57:58 -0400260 return (struct sockaddr *) &xprt->addr;
261}
262
263static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
264{
265 return (struct sockaddr_in *) &xprt->addr;
266}
267
268static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
269{
270 return (struct sockaddr_in6 *) &xprt->addr;
271}
272
Chuck Leverc877b842009-08-09 15:09:36 -0400273static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400274{
Chuck Leverc877b842009-08-09 15:09:36 -0400275 struct sockaddr *sap = xs_addr(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400276 struct sockaddr_in6 *sin6;
277 struct sockaddr_in *sin;
Chuck Leverc877b842009-08-09 15:09:36 -0400278 char buf[128];
Chuck Leveredb267a2006-08-22 20:06:18 -0400279
Chuck Leverc877b842009-08-09 15:09:36 -0400280 (void)rpc_ntop(sap, buf, sizeof(buf));
281 xprt->address_strings[RPC_DISPLAY_ADDR] = kstrdup(buf, GFP_KERNEL);
282
Chuck Lever9dc3b092009-08-09 15:09:46 -0400283 switch (sap->sa_family) {
284 case AF_INET:
285 sin = xs_addr_in(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800286 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
Chuck Lever9dc3b092009-08-09 15:09:46 -0400287 break;
288 case AF_INET6:
289 sin6 = xs_addr_in6(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800290 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400291 break;
292 default:
293 BUG();
Chuck Leveredb267a2006-08-22 20:06:18 -0400294 }
Chuck Lever9dc3b092009-08-09 15:09:46 -0400295 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
Chuck Leveredb267a2006-08-22 20:06:18 -0400296}
297
Chuck Lever9dc3b092009-08-09 15:09:46 -0400298static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400299{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400300 struct sockaddr *sap = xs_addr(xprt);
301 char buf[128];
Chuck Lever4b6473f2007-08-06 11:57:12 -0400302
Joe Perches81160e662010-03-08 12:15:59 -0800303 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400304 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400305
Joe Perches81160e662010-03-08 12:15:59 -0800306 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400307 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
308}
Chuck Lever4b6473f2007-08-06 11:57:12 -0400309
Chuck Lever9dc3b092009-08-09 15:09:46 -0400310static void xs_format_peer_addresses(struct rpc_xprt *xprt,
311 const char *protocol,
312 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400313{
Chuck Leverb454ae92008-01-07 18:34:48 -0500314 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leverb454ae92008-01-07 18:34:48 -0500315 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leverc877b842009-08-09 15:09:36 -0400316 xs_format_common_peer_addresses(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400317 xs_format_common_peer_ports(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400318}
319
Chuck Lever9dc3b092009-08-09 15:09:46 -0400320static void xs_update_peer_port(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400321{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400322 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
323 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400324
Chuck Lever9dc3b092009-08-09 15:09:46 -0400325 xs_format_common_peer_ports(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400326}
327
328static void xs_free_peer_addresses(struct rpc_xprt *xprt)
329{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500330 unsigned int i;
331
332 for (i = 0; i < RPC_DISPLAY_MAX; i++)
333 switch (i) {
334 case RPC_DISPLAY_PROTO:
335 case RPC_DISPLAY_NETID:
336 continue;
337 default:
338 kfree(xprt->address_strings[i]);
339 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400340}
341
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400342#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
343
Trond Myklebust24c56842006-10-17 15:06:22 -0400344static 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 -0400345{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400346 struct msghdr msg = {
347 .msg_name = addr,
348 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400349 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
350 };
351 struct kvec iov = {
352 .iov_base = vec->iov_base + base,
353 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400354 };
355
Trond Myklebust24c56842006-10-17 15:06:22 -0400356 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400357 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
358 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
359}
360
Trond Myklebust24c56842006-10-17 15:06:22 -0400361static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400362{
Trond Myklebust24c56842006-10-17 15:06:22 -0400363 struct page **ppage;
364 unsigned int remainder;
365 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400366
Trond Myklebust24c56842006-10-17 15:06:22 -0400367 remainder = xdr->page_len - base;
368 base += xdr->page_base;
369 ppage = xdr->pages + (base >> PAGE_SHIFT);
370 base &= ~PAGE_MASK;
371 for(;;) {
372 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
373 int flags = XS_SENDMSG_FLAGS;
374
375 remainder -= len;
376 if (remainder != 0 || more)
377 flags |= MSG_MORE;
378 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
379 if (remainder == 0 || err != len)
380 break;
381 sent += err;
382 ppage++;
383 base = 0;
384 }
385 if (sent == 0)
386 return err;
387 if (err > 0)
388 sent += err;
389 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400390}
391
Chuck Lever9903cd12005-08-11 16:25:26 -0400392/**
393 * xs_sendpages - write pages directly to a socket
394 * @sock: socket to send on
395 * @addr: UDP only -- address of destination
396 * @addrlen: UDP only -- length of destination address
397 * @xdr: buffer containing this request
398 * @base: starting position in the buffer
399 *
Chuck Levera246b012005-08-11 16:25:23 -0400400 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400401static 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 -0400402{
Trond Myklebust24c56842006-10-17 15:06:22 -0400403 unsigned int remainder = xdr->len - base;
404 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400405
Chuck Lever262965f2005-08-11 16:25:56 -0400406 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400407 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400408
409 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400410 if (base != 0) {
411 addr = NULL;
412 addrlen = 0;
413 }
Chuck Lever262965f2005-08-11 16:25:56 -0400414
Trond Myklebust24c56842006-10-17 15:06:22 -0400415 if (base < xdr->head[0].iov_len || addr != NULL) {
416 unsigned int len = xdr->head[0].iov_len - base;
417 remainder -= len;
418 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
419 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400420 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400421 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400422 base = 0;
423 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400424 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400425
Trond Myklebust24c56842006-10-17 15:06:22 -0400426 if (base < xdr->page_len) {
427 unsigned int len = xdr->page_len - base;
428 remainder -= len;
429 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
430 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400431 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400432 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400433 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400434 } else
435 base -= xdr->page_len;
436
437 if (base >= xdr->tail[0].iov_len)
438 return sent;
439 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400440out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400441 if (sent == 0)
442 return err;
443 if (err > 0)
444 sent += err;
445 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400446}
447
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400448static void xs_nospace_callback(struct rpc_task *task)
449{
450 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
451
452 transport->inet->sk_write_pending--;
453 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
454}
455
Chuck Lever9903cd12005-08-11 16:25:26 -0400456/**
Chuck Lever262965f2005-08-11 16:25:56 -0400457 * xs_nospace - place task on wait queue if transmit was incomplete
458 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400459 *
Chuck Levera246b012005-08-11 16:25:23 -0400460 */
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400461static int xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400462{
Chuck Lever262965f2005-08-11 16:25:56 -0400463 struct rpc_rqst *req = task->tk_rqstp;
464 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500465 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400466 int ret = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400467
Chuck Lever46121cf2007-01-31 12:14:08 -0500468 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400469 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
470 req->rq_slen);
471
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400472 /* Protect against races with write_space */
473 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400474
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400475 /* Don't race with disconnect */
476 if (xprt_connected(xprt)) {
477 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400478 ret = -EAGAIN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400479 /*
480 * Notify TCP that we're limited by the application
481 * window size
482 */
483 set_bit(SOCK_NOSPACE, &transport->sock->flags);
484 transport->inet->sk_write_pending++;
485 /* ...and wait for more buffer space */
486 xprt_wait_for_buffer_space(task, xs_nospace_callback);
487 }
488 } else {
489 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400490 ret = -ENOTCONN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400491 }
Chuck Lever262965f2005-08-11 16:25:56 -0400492
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400493 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400494 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400495}
496
497/**
498 * xs_udp_send_request - write an RPC request to a UDP socket
499 * @task: address of RPC task that manages the state of an RPC request
500 *
501 * Return values:
502 * 0: The request has been sent
503 * EAGAIN: The socket was blocked, please call again later to
504 * complete the request
505 * ENOTCONN: Caller needs to invoke connect logic then call again
506 * other: Some other error occured, the request was not sent
507 */
508static int xs_udp_send_request(struct rpc_task *task)
509{
510 struct rpc_rqst *req = task->tk_rqstp;
511 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500512 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400513 struct xdr_buf *xdr = &req->rq_snd_buf;
514 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400515
Chuck Lever9903cd12005-08-11 16:25:26 -0400516 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400517 req->rq_svec->iov_base,
518 req->rq_svec->iov_len);
519
Trond Myklebust01d37c42009-03-11 14:09:39 -0400520 if (!xprt_bound(xprt))
521 return -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500522 status = xs_sendpages(transport->sock,
Chuck Lever95392c52007-08-06 11:57:58 -0400523 xs_addr(xprt),
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500524 xprt->addrlen, xdr,
525 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400526
Chuck Lever46121cf2007-01-31 12:14:08 -0500527 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400528 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400529
Trond Myklebust21997002007-10-01 11:43:37 -0400530 if (status >= 0) {
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400531 req->rq_xmit_bytes_sent += status;
Trond Myklebust21997002007-10-01 11:43:37 -0400532 if (status >= req->rq_slen)
533 return 0;
534 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400535 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400536 }
Chuck Levera246b012005-08-11 16:25:23 -0400537
Chuck Lever262965f2005-08-11 16:25:56 -0400538 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400539 case -ENOTSOCK:
540 status = -ENOTCONN;
541 /* Should we call xs_close() here? */
542 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400543 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400544 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400545 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400546 default:
547 dprintk("RPC: sendmsg returned unrecognized error %d\n",
548 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400549 case -ENETUNREACH:
550 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400551 case -ECONNREFUSED:
552 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400553 * prompts ECONNREFUSED. */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400554 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400555 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800556
Chuck Lever262965f2005-08-11 16:25:56 -0400557 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400558}
559
Trond Myklebuste06799f2007-11-05 15:44:12 -0500560/**
561 * xs_tcp_shutdown - gracefully shut down a TCP socket
562 * @xprt: transport
563 *
564 * Initiates a graceful shutdown of the TCP socket by calling the
565 * equivalent of shutdown(SHUT_WR);
566 */
567static void xs_tcp_shutdown(struct rpc_xprt *xprt)
568{
569 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
570 struct socket *sock = transport->sock;
571
572 if (sock != NULL)
573 kernel_sock_shutdown(sock, SHUT_WR);
574}
575
Chuck Lever808012f2005-08-25 16:25:49 -0700576static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
577{
578 u32 reclen = buf->len - sizeof(rpc_fraghdr);
579 rpc_fraghdr *base = buf->head[0].iov_base;
580 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
581}
582
Chuck Lever9903cd12005-08-11 16:25:26 -0400583/**
Chuck Lever262965f2005-08-11 16:25:56 -0400584 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400585 * @task: address of RPC task that manages the state of an RPC request
586 *
587 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400588 * 0: The request has been sent
589 * EAGAIN: The socket was blocked, please call again later to
590 * complete the request
591 * ENOTCONN: Caller needs to invoke connect logic then call again
592 * other: Some other error occured, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400593 *
594 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400595 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400596 */
Chuck Lever262965f2005-08-11 16:25:56 -0400597static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400598{
599 struct rpc_rqst *req = task->tk_rqstp;
600 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500601 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400602 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Leverb595bb12007-08-06 11:56:42 -0400603 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400604
Chuck Lever808012f2005-08-25 16:25:49 -0700605 xs_encode_tcp_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400606
Chuck Lever262965f2005-08-11 16:25:56 -0400607 xs_pktdump("packet data:",
608 req->rq_svec->iov_base,
609 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400610
611 /* Continue transmitting the packet/record. We must be careful
612 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400613 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400614 while (1) {
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500615 status = xs_sendpages(transport->sock,
616 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400617
Chuck Lever46121cf2007-01-31 12:14:08 -0500618 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400619 xdr->len - req->rq_bytes_sent, status);
620
621 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400622 break;
623
Chuck Lever262965f2005-08-11 16:25:56 -0400624 /* If we've sent the entire packet, immediately
625 * reset the count of bytes sent. */
626 req->rq_bytes_sent += status;
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400627 req->rq_xmit_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400628 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
629 req->rq_bytes_sent = 0;
630 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400631 }
632
Trond Myklebust06b4b682008-04-16 16:51:38 -0400633 if (status != 0)
634 continue;
Chuck Levera246b012005-08-11 16:25:23 -0400635 status = -EAGAIN;
Trond Myklebust06b4b682008-04-16 16:51:38 -0400636 break;
Chuck Levera246b012005-08-11 16:25:23 -0400637 }
638
Chuck Lever262965f2005-08-11 16:25:56 -0400639 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400640 case -ENOTSOCK:
641 status = -ENOTCONN;
642 /* Should we call xs_close() here? */
643 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400644 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400645 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400646 break;
647 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500648 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400649 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400650 case -ECONNRESET:
Trond Myklebust55420c22009-03-11 15:29:24 -0400651 case -EPIPE:
Trond Myklebuste06799f2007-11-05 15:44:12 -0500652 xs_tcp_shutdown(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400653 case -ECONNREFUSED:
654 case -ENOTCONN:
Chuck Lever262965f2005-08-11 16:25:56 -0400655 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400656 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800657
Chuck Levera246b012005-08-11 16:25:23 -0400658 return status;
659}
660
Chuck Lever9903cd12005-08-11 16:25:26 -0400661/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400662 * xs_tcp_release_xprt - clean up after a tcp transmission
663 * @xprt: transport
664 * @task: rpc task
665 *
666 * This cleans up if an error causes us to abort the transmission of a request.
667 * In this case, the socket may need to be reset in order to avoid confusing
668 * the server.
669 */
670static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
671{
672 struct rpc_rqst *req;
673
674 if (task != xprt->snd_task)
675 return;
676 if (task == NULL)
677 goto out_release;
678 req = task->tk_rqstp;
679 if (req->rq_bytes_sent == 0)
680 goto out_release;
681 if (req->rq_bytes_sent == req->rq_snd_buf.len)
682 goto out_release;
683 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
684out_release:
685 xprt_release_xprt(xprt, task);
686}
687
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400688static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
689{
690 transport->old_data_ready = sk->sk_data_ready;
691 transport->old_state_change = sk->sk_state_change;
692 transport->old_write_space = sk->sk_write_space;
693 transport->old_error_report = sk->sk_error_report;
694}
695
696static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
697{
698 sk->sk_data_ready = transport->old_data_ready;
699 sk->sk_state_change = transport->old_state_change;
700 sk->sk_write_space = transport->old_write_space;
701 sk->sk_error_report = transport->old_error_report;
702}
703
Chuck Leverfe315e72009-03-11 14:10:21 -0400704static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400705{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500706 struct socket *sock = transport->sock;
707 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400708
Chuck Leverfe315e72009-03-11 14:10:21 -0400709 if (sk == NULL)
710 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400711
Chuck Levera246b012005-08-11 16:25:23 -0400712 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500713 transport->inet = NULL;
714 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400715
Chuck Lever9903cd12005-08-11 16:25:26 -0400716 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400717
718 xs_restore_old_callbacks(transport, sk);
Chuck Levera246b012005-08-11 16:25:23 -0400719 write_unlock_bh(&sk->sk_callback_lock);
720
Chuck Lever9903cd12005-08-11 16:25:26 -0400721 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400722
723 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -0400724}
725
726/**
727 * xs_close - close a socket
728 * @xprt: transport
729 *
730 * This is used when all requests are complete; ie, no DRC state remains
731 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400732 *
733 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
734 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e72009-03-11 14:10:21 -0400735 */
736static void xs_close(struct rpc_xprt *xprt)
737{
738 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
739
740 dprintk("RPC: xs_close xprt %p\n", xprt);
741
742 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -0400743 xprt->reestablish_timeout = 0;
Chuck Leverfe315e72009-03-11 14:10:21 -0400744
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100745 smp_mb__before_clear_bit();
Trond Myklebust7d1e8252009-03-11 14:38:03 -0400746 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100747 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -0500748 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100749 smp_mb__after_clear_bit();
Trond Myklebust62da3b22007-11-06 18:44:20 -0500750 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400751}
752
Trond Myklebustf75e6742009-04-21 17:18:20 -0400753static void xs_tcp_close(struct rpc_xprt *xprt)
754{
755 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
756 xs_close(xprt);
757 else
758 xs_tcp_shutdown(xprt);
759}
760
Chuck Lever9903cd12005-08-11 16:25:26 -0400761/**
762 * xs_destroy - prepare to shutdown a transport
763 * @xprt: doomed transport
764 *
765 */
766static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400767{
Chuck Leverc8475462006-12-05 16:35:26 -0500768 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
769
Chuck Lever46121cf2007-01-31 12:14:08 -0500770 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400771
Trond Myklebustc1384c92007-06-14 18:00:42 -0400772 cancel_rearming_delayed_work(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400773
Chuck Lever9903cd12005-08-11 16:25:26 -0400774 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400775 xs_free_peer_addresses(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400776 kfree(xprt->slot);
Chuck Leverc8541ec2006-10-17 14:44:27 -0400777 kfree(xprt);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -0400778 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400779}
780
Chuck Lever9903cd12005-08-11 16:25:26 -0400781static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400782{
Chuck Lever9903cd12005-08-11 16:25:26 -0400783 return (struct rpc_xprt *) sk->sk_user_data;
784}
785
786/**
787 * xs_udp_data_ready - "data ready" callback for UDP sockets
788 * @sk: socket with data to read
789 * @len: how much data to read
790 *
791 */
792static void xs_udp_data_ready(struct sock *sk, int len)
793{
794 struct rpc_task *task;
795 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400796 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400797 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400798 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700799 u32 _xid;
800 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400801
802 read_lock(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500803 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400804 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400805 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400806
807 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
808 goto out;
809
810 if (xprt->shutdown)
811 goto dropit;
812
813 repsize = skb->len - sizeof(struct udphdr);
814 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500815 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400816 goto dropit;
817 }
818
819 /* Copy the XID from the skb... */
820 xp = skb_header_pointer(skb, sizeof(struct udphdr),
821 sizeof(_xid), &_xid);
822 if (xp == NULL)
823 goto dropit;
824
825 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400826 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400827 rovr = xprt_lookup_rqst(xprt, *xp);
828 if (!rovr)
829 goto out_unlock;
830 task = rovr->rq_task;
831
Chuck Levera246b012005-08-11 16:25:23 -0400832 if ((copied = rovr->rq_private_buf.buflen) > repsize)
833 copied = repsize;
834
835 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -0800836 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
837 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -0400838 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -0800839 }
840
841 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -0400842
843 /* Something worked... */
Eric Dumazetadf30902009-06-02 05:19:30 +0000844 dst_confirm(skb_dst(skb));
Chuck Levera246b012005-08-11 16:25:23 -0400845
Chuck Lever1570c1e2005-08-25 16:25:52 -0700846 xprt_adjust_cwnd(task, copied);
Chuck Lever1570c1e2005-08-25 16:25:52 -0700847 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -0400848
849 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400850 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400851 dropit:
852 skb_free_datagram(sk, skb);
853 out:
854 read_unlock(&sk->sk_callback_lock);
855}
856
Chuck Leverdd456472006-12-05 16:35:44 -0500857static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400858{
Chuck Lever51971132006-12-05 16:35:19 -0500859 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400860 size_t len, used;
861 char *p;
862
Chuck Lever51971132006-12-05 16:35:19 -0500863 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
864 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500865 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500866 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400867 if (used != len)
868 return;
Chuck Lever808012f2005-08-25 16:25:49 -0700869
Chuck Lever51971132006-12-05 16:35:19 -0500870 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
871 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -0500872 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -0400873 else
Chuck Levere136d092006-12-05 16:35:23 -0500874 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -0500875 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -0700876
Chuck Levere136d092006-12-05 16:35:23 -0500877 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500878 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -0700879
Chuck Levera246b012005-08-11 16:25:23 -0400880 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400881 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500882 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebust3ebb0672007-11-06 18:40:12 -0500883 xprt_force_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400884 return;
Chuck Levera246b012005-08-11 16:25:23 -0400885 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500886 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -0500887 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400888}
889
Chuck Lever51971132006-12-05 16:35:19 -0500890static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400891{
Chuck Lever51971132006-12-05 16:35:19 -0500892 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -0500893 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500894 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -0500895 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
896 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
897 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -0500898 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400899 }
900 }
901}
902
Chuck Leverdd456472006-12-05 16:35:44 -0500903static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400904{
905 size_t len, used;
906 char *p;
907
Chuck Lever51971132006-12-05 16:35:19 -0500908 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -0500909 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -0500910 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500911 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500912 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400913 if (used != len)
914 return;
Chuck Levere136d092006-12-05 16:35:23 -0500915 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400916 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -0500917 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400918 dprintk("RPC: reading %s XID %08x\n",
919 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
920 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -0500921 ntohl(transport->tcp_xid));
922 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400923}
924
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400925static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
926 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400927{
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400928 size_t len, used;
929 u32 offset;
930 __be32 calldir;
931
932 /*
933 * We want transport->tcp_offset to be 8 at the end of this routine
934 * (4 bytes for the xid and 4 bytes for the call/reply flag).
935 * When this function is called for the first time,
936 * transport->tcp_offset is 4 (after having already read the xid).
937 */
938 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
939 len = sizeof(calldir) - offset;
940 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
941 used = xdr_skb_read_bits(desc, &calldir, len);
942 transport->tcp_offset += used;
943 if (used != len)
944 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400945 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
946 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400947 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400948 /*
949 * We don't yet have the XDR buffer, so we will write the calldir
950 * out after we get the buffer from the 'struct rpc_rqst'
951 */
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400952 if (ntohl(calldir) == RPC_REPLY)
953 transport->tcp_flags |= TCP_RPC_REPLY;
954 else
955 transport->tcp_flags &= ~TCP_RPC_REPLY;
956 dprintk("RPC: reading %s CALL/REPLY flag %08x\n",
957 (transport->tcp_flags & TCP_RPC_REPLY) ?
958 "reply for" : "request with", calldir);
959 xs_tcp_check_fraghdr(transport);
960}
961
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -0400962static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
963 struct xdr_skb_reader *desc,
964 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -0400965{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -0400966 struct sock_xprt *transport =
967 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400968 struct xdr_buf *rcvbuf;
969 size_t len;
970 ssize_t r;
971
Chuck Levera246b012005-08-11 16:25:23 -0400972 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400973
974 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
975 /*
976 * Save the RPC direction in the XDR buffer
977 */
978 __be32 calldir = transport->tcp_flags & TCP_RPC_REPLY ?
979 htonl(RPC_REPLY) : 0;
980
981 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
982 &calldir, sizeof(calldir));
983 transport->tcp_copied += sizeof(calldir);
984 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -0400985 }
986
Chuck Levera246b012005-08-11 16:25:23 -0400987 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -0500988 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -0500989 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -0400990
Chuck Lever51971132006-12-05 16:35:19 -0500991 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -0400992 memcpy(&my_desc, desc, sizeof(my_desc));
993 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -0500994 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -0500995 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -0400996 desc->count -= r;
997 desc->offset += r;
998 } else
Chuck Lever51971132006-12-05 16:35:19 -0500999 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001000 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001001
1002 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001003 transport->tcp_copied += r;
1004 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001005 }
1006 if (r != len) {
1007 /* Error when copying to the receive buffer,
1008 * usually because we weren't able to allocate
1009 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001010 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001011 * will not receive any additional updates,
1012 * and time out.
1013 * Any remaining data from this record will
1014 * be discarded.
1015 */
Chuck Levere136d092006-12-05 16:35:23 -05001016 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001017 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001018 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001019 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1020 "tcp_offset = %u, tcp_reclen = %u\n",
1021 xprt, transport->tcp_copied,
1022 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001023 return;
Chuck Levera246b012005-08-11 16:25:23 -04001024 }
1025
Chuck Lever46121cf2007-01-31 12:14:08 -05001026 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001027 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001028 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1029 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1030 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001031
Chuck Lever51971132006-12-05 16:35:19 -05001032 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001033 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001034 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001035 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1036 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001037 }
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001038}
1039
1040/*
1041 * Finds the request corresponding to the RPC xid and invokes the common
1042 * tcp read code to read the data.
1043 */
1044static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1045 struct xdr_skb_reader *desc)
1046{
1047 struct sock_xprt *transport =
1048 container_of(xprt, struct sock_xprt, xprt);
1049 struct rpc_rqst *req;
1050
1051 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1052
1053 /* Find and lock the request corresponding to this xid */
1054 spin_lock(&xprt->transport_lock);
1055 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1056 if (!req) {
1057 dprintk("RPC: XID %08x request not found!\n",
1058 ntohl(transport->tcp_xid));
1059 spin_unlock(&xprt->transport_lock);
1060 return -1;
1061 }
1062
1063 xs_tcp_read_common(xprt, desc, req);
1064
Chuck Levere136d092006-12-05 16:35:23 -05001065 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001066 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001067
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001068 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001069 return 0;
1070}
1071
1072#if defined(CONFIG_NFS_V4_1)
1073/*
1074 * Obtains an rpc_rqst previously allocated and invokes the common
1075 * tcp read code to read the data. The result is placed in the callback
1076 * queue.
1077 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1078 * connection and return -1.
1079 */
1080static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1081 struct xdr_skb_reader *desc)
1082{
1083 struct sock_xprt *transport =
1084 container_of(xprt, struct sock_xprt, xprt);
1085 struct rpc_rqst *req;
1086
1087 req = xprt_alloc_bc_request(xprt);
1088 if (req == NULL) {
1089 printk(KERN_WARNING "Callback slot table overflowed\n");
1090 xprt_force_disconnect(xprt);
1091 return -1;
1092 }
1093
1094 req->rq_xid = transport->tcp_xid;
1095 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1096 xs_tcp_read_common(xprt, desc, req);
1097
1098 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1099 struct svc_serv *bc_serv = xprt->bc_serv;
1100
1101 /*
1102 * Add callback request to callback list. The callback
1103 * service sleeps on the sv_cb_waitq waiting for new
1104 * requests. Wake it up after adding enqueing the
1105 * request.
1106 */
1107 dprintk("RPC: add callback request to list\n");
1108 spin_lock(&bc_serv->sv_cb_lock);
1109 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1110 spin_unlock(&bc_serv->sv_cb_lock);
1111 wake_up(&bc_serv->sv_cb_waitq);
1112 }
1113
1114 req->rq_private_buf.len = transport->tcp_copied;
1115
1116 return 0;
1117}
1118
1119static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1120 struct xdr_skb_reader *desc)
1121{
1122 struct sock_xprt *transport =
1123 container_of(xprt, struct sock_xprt, xprt);
1124
1125 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1126 xs_tcp_read_reply(xprt, desc) :
1127 xs_tcp_read_callback(xprt, desc);
1128}
1129#else
1130static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1131 struct xdr_skb_reader *desc)
1132{
1133 return xs_tcp_read_reply(xprt, desc);
1134}
1135#endif /* CONFIG_NFS_V4_1 */
1136
1137/*
1138 * Read data off the transport. This can be either an RPC_CALL or an
1139 * RPC_REPLY. Relay the processing to helper functions.
1140 */
1141static void xs_tcp_read_data(struct rpc_xprt *xprt,
1142 struct xdr_skb_reader *desc)
1143{
1144 struct sock_xprt *transport =
1145 container_of(xprt, struct sock_xprt, xprt);
1146
1147 if (_xs_tcp_read_data(xprt, desc) == 0)
1148 xs_tcp_check_fraghdr(transport);
1149 else {
1150 /*
1151 * The transport_lock protects the request handling.
1152 * There's no need to hold it to update the tcp_flags.
1153 */
1154 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1155 }
Chuck Levera246b012005-08-11 16:25:23 -04001156}
1157
Chuck Leverdd456472006-12-05 16:35:44 -05001158static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001159{
1160 size_t len;
1161
Chuck Lever51971132006-12-05 16:35:19 -05001162 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001163 if (len > desc->count)
1164 len = desc->count;
1165 desc->count -= len;
1166 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001167 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001168 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001169 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001170}
1171
Chuck Lever9903cd12005-08-11 16:25:26 -04001172static 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 -04001173{
1174 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001175 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001176 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001177 .skb = skb,
1178 .offset = offset,
1179 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001180 };
Chuck Levera246b012005-08-11 16:25:23 -04001181
Chuck Lever46121cf2007-01-31 12:14:08 -05001182 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001183 do {
1184 /* Read in a new fragment marker if necessary */
1185 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001186 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001187 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001188 continue;
1189 }
1190 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001191 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001192 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001193 continue;
1194 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001195 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001196 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001197 xs_tcp_read_calldir(transport, &desc);
1198 continue;
1199 }
Chuck Levera246b012005-08-11 16:25:23 -04001200 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001201 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001202 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001203 continue;
1204 }
1205 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001206 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001207 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001208 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001209 return len - desc.count;
1210}
1211
Chuck Lever9903cd12005-08-11 16:25:26 -04001212/**
1213 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1214 * @sk: socket with data to read
1215 * @bytes: how much data to read
1216 *
1217 */
1218static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001219{
1220 struct rpc_xprt *xprt;
1221 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001222 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001223
Chuck Lever46121cf2007-01-31 12:14:08 -05001224 dprintk("RPC: xs_tcp_data_ready...\n");
1225
Chuck Levera246b012005-08-11 16:25:23 -04001226 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001227 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001228 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001229 if (xprt->shutdown)
1230 goto out;
1231
Neil Brown61d0a8e2009-09-23 14:36:37 -04001232 /* Any data means we had a useful conversation, so
1233 * the we don't need to delay the next reconnect
1234 */
1235 if (xprt->reestablish_timeout)
1236 xprt->reestablish_timeout = 0;
1237
Chuck Lever9903cd12005-08-11 16:25:26 -04001238 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001239 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001240 do {
1241 rd_desc.count = 65536;
1242 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1243 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001244out:
1245 read_unlock(&sk->sk_callback_lock);
1246}
1247
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001248/*
1249 * Do the equivalent of linger/linger2 handling for dealing with
1250 * broken servers that don't close the socket in a timely
1251 * fashion
1252 */
1253static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1254 unsigned long timeout)
1255{
1256 struct sock_xprt *transport;
1257
1258 if (xprt_test_and_set_connecting(xprt))
1259 return;
1260 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1261 transport = container_of(xprt, struct sock_xprt, xprt);
1262 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1263 timeout);
1264}
1265
1266static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1267{
1268 struct sock_xprt *transport;
1269
1270 transport = container_of(xprt, struct sock_xprt, xprt);
1271
1272 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1273 !cancel_delayed_work(&transport->connect_worker))
1274 return;
1275 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1276 xprt_clear_connecting(xprt);
1277}
1278
1279static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1280{
1281 smp_mb__before_clear_bit();
1282 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1283 clear_bit(XPRT_CLOSING, &xprt->state);
1284 smp_mb__after_clear_bit();
1285 /* Mark transport as closed and wake up all pending tasks */
1286 xprt_disconnect_done(xprt);
1287}
1288
Chuck Lever9903cd12005-08-11 16:25:26 -04001289/**
1290 * xs_tcp_state_change - callback to handle TCP socket state changes
1291 * @sk: socket whose state has changed
1292 *
1293 */
1294static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001295{
Chuck Lever9903cd12005-08-11 16:25:26 -04001296 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001297
1298 read_lock(&sk->sk_callback_lock);
1299 if (!(xprt = xprt_from_sock(sk)))
1300 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001301 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
1302 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
1303 sk->sk_state, xprt_connected(xprt),
1304 sock_flag(sk, SOCK_DEAD),
1305 sock_flag(sk, SOCK_ZAPPED));
Chuck Levera246b012005-08-11 16:25:23 -04001306
1307 switch (sk->sk_state) {
1308 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001309 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001310 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001311 struct sock_xprt *transport = container_of(xprt,
1312 struct sock_xprt, xprt);
1313
Chuck Levera246b012005-08-11 16:25:23 -04001314 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001315 transport->tcp_offset = 0;
1316 transport->tcp_reclen = 0;
1317 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001318 transport->tcp_flags =
1319 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001320
Trond Myklebust2a491992009-03-11 14:38:00 -04001321 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001322 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001323 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001324 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001325 case TCP_FIN_WAIT1:
1326 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001327 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001328 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001329 set_bit(XPRT_CLOSING, &xprt->state);
1330 smp_mb__before_clear_bit();
1331 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001332 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001333 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001334 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001335 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001336 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001337 /* The server initiated a shutdown of the socket */
Trond Myklebust66af1e52007-11-06 10:18:36 -05001338 xprt_force_disconnect(xprt);
Trond Myklebust663b8852008-01-01 18:42:12 -05001339 case TCP_SYN_SENT:
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001340 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001341 case TCP_CLOSING:
1342 /*
1343 * If the server closed down the connection, make sure that
1344 * we back off before reconnecting
1345 */
1346 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1347 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001348 break;
1349 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001350 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001351 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001352 smp_mb__before_clear_bit();
1353 clear_bit(XPRT_CONNECTED, &xprt->state);
1354 smp_mb__after_clear_bit();
1355 break;
1356 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001357 xs_tcp_cancel_linger_timeout(xprt);
1358 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001359 }
1360 out:
1361 read_unlock(&sk->sk_callback_lock);
1362}
1363
Chuck Lever9903cd12005-08-11 16:25:26 -04001364/**
Trond Myklebust482f32e2009-03-11 14:38:00 -04001365 * xs_error_report - callback mainly for catching socket errors
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001366 * @sk: socket
1367 */
Trond Myklebust482f32e2009-03-11 14:38:00 -04001368static void xs_error_report(struct sock *sk)
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001369{
1370 struct rpc_xprt *xprt;
1371
1372 read_lock(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001373 if (!(xprt = xprt_from_sock(sk)))
1374 goto out;
1375 dprintk("RPC: %s client %p...\n"
1376 "RPC: error %d\n",
1377 __func__, xprt, sk->sk_err);
Trond Myklebust482f32e2009-03-11 14:38:00 -04001378 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001379out:
1380 read_unlock(&sk->sk_callback_lock);
1381}
1382
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001383static void xs_write_space(struct sock *sk)
1384{
1385 struct socket *sock;
1386 struct rpc_xprt *xprt;
1387
1388 if (unlikely(!(sock = sk->sk_socket)))
1389 return;
1390 clear_bit(SOCK_NOSPACE, &sock->flags);
1391
1392 if (unlikely(!(xprt = xprt_from_sock(sk))))
1393 return;
1394 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1395 return;
1396
1397 xprt_write_space(xprt);
1398}
1399
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001400/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001401 * xs_udp_write_space - callback invoked when socket buffer space
1402 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001403 * @sk: socket whose state has changed
1404 *
Chuck Levera246b012005-08-11 16:25:23 -04001405 * Called when more output buffer space is available for this socket.
1406 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001407 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001408 * with a bunch of small requests.
1409 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001410static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001411{
Chuck Levera246b012005-08-11 16:25:23 -04001412 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001413
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001414 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001415 if (sock_writeable(sk))
1416 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001417
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001418 read_unlock(&sk->sk_callback_lock);
1419}
Chuck Levera246b012005-08-11 16:25:23 -04001420
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001421/**
1422 * xs_tcp_write_space - callback invoked when socket buffer space
1423 * becomes available
1424 * @sk: socket whose state has changed
1425 *
1426 * Called when more output buffer space is available for this socket.
1427 * We try not to wake our writers until they can make "significant"
1428 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1429 * with a bunch of small requests.
1430 */
1431static void xs_tcp_write_space(struct sock *sk)
1432{
1433 read_lock(&sk->sk_callback_lock);
1434
1435 /* from net/core/stream.c:sk_stream_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001436 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1437 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001438
Chuck Levera246b012005-08-11 16:25:23 -04001439 read_unlock(&sk->sk_callback_lock);
1440}
1441
Chuck Lever470056c2005-08-25 16:25:56 -07001442static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001443{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001444 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1445 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001446
Chuck Lever7c6e0662006-12-05 16:35:30 -05001447 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001448 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001449 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001450 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001451 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001452 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001453 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001454 sk->sk_write_space(sk);
1455 }
1456}
1457
Chuck Lever43118c22005-08-25 16:25:49 -07001458/**
Chuck Lever470056c2005-08-25 16:25:56 -07001459 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001460 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001461 * @sndsize: requested size of send buffer, in bytes
1462 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001463 *
Chuck Lever470056c2005-08-25 16:25:56 -07001464 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001465 */
Chuck Lever470056c2005-08-25 16:25:56 -07001466static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001467{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001468 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1469
1470 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001471 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001472 transport->sndsize = sndsize + 1024;
1473 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001474 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001475 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001476
1477 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001478}
1479
Chuck Lever46c0ee82005-08-25 16:25:52 -07001480/**
1481 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1482 * @task: task that timed out
1483 *
1484 * Adjust the congestion window after a retransmit timeout has occurred.
1485 */
1486static void xs_udp_timer(struct rpc_task *task)
1487{
1488 xprt_adjust_cwnd(task, -ETIMEDOUT);
1489}
1490
Chuck Leverb85d8802006-05-25 01:40:49 -04001491static unsigned short xs_get_random_port(void)
1492{
1493 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1494 unsigned short rand = (unsigned short) net_random() % range;
1495 return rand + xprt_min_resvport;
1496}
1497
Chuck Lever92200412006-01-03 09:55:51 +01001498/**
1499 * xs_set_port - reset the port number in the remote endpoint address
1500 * @xprt: generic transport
1501 * @port: new port number
1502 *
1503 */
1504static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1505{
Chuck Lever46121cf2007-01-31 12:14:08 -05001506 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001507
Chuck Lever9dc3b092009-08-09 15:09:46 -04001508 rpc_set_port(xs_addr(xprt), port);
1509 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001510}
1511
Trond Myklebust67a391d2007-11-05 17:40:58 -05001512static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
1513{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001514 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001515
1516 if (port == 0 && transport->xprt.resvport)
1517 port = xs_get_random_port();
1518 return port;
1519}
1520
1521static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
1522{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001523 if (transport->srcport != 0)
1524 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001525 if (!transport->xprt.resvport)
1526 return 0;
1527 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1528 return xprt_max_resvport;
1529 return --port;
1530}
1531
Chuck Lever7dc753f2007-08-06 11:57:28 -04001532static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001533{
1534 struct sockaddr_in myaddr = {
1535 .sin_family = AF_INET,
1536 };
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001537 struct sockaddr_in *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001538 int err, nloop = 0;
1539 unsigned short port = xs_get_srcport(transport, sock);
1540 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001541
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001542 sa = (struct sockaddr_in *)&transport->srcaddr;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001543 myaddr.sin_addr = sa->sin_addr;
Chuck Levera246b012005-08-11 16:25:23 -04001544 do {
1545 myaddr.sin_port = htons(port);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001546 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
Chuck Levera246b012005-08-11 16:25:23 -04001547 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001548 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001549 break;
Chuck Levera246b012005-08-11 16:25:23 -04001550 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001551 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001552 break;
Chuck Levera246b012005-08-11 16:25:23 -04001553 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001554 last = port;
1555 port = xs_next_srcport(transport, sock, port);
1556 if (port > last)
1557 nloop++;
1558 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison21454aa2008-10-31 00:54:56 -07001559 dprintk("RPC: %s %pI4:%u: %s (%d)\n",
1560 __func__, &myaddr.sin_addr,
Chuck Lever7dc753f2007-08-06 11:57:28 -04001561 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001562 return err;
1563}
1564
Chuck Lever90058d32007-08-06 11:57:33 -04001565static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
1566{
1567 struct sockaddr_in6 myaddr = {
1568 .sin6_family = AF_INET6,
1569 };
1570 struct sockaddr_in6 *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001571 int err, nloop = 0;
1572 unsigned short port = xs_get_srcport(transport, sock);
1573 unsigned short last;
Chuck Lever90058d32007-08-06 11:57:33 -04001574
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001575 sa = (struct sockaddr_in6 *)&transport->srcaddr;
Chuck Lever90058d32007-08-06 11:57:33 -04001576 myaddr.sin6_addr = sa->sin6_addr;
1577 do {
1578 myaddr.sin6_port = htons(port);
1579 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1580 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001581 if (port == 0)
Chuck Lever90058d32007-08-06 11:57:33 -04001582 break;
1583 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001584 transport->srcport = port;
Chuck Lever90058d32007-08-06 11:57:33 -04001585 break;
1586 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001587 last = port;
1588 port = xs_next_srcport(transport, sock, port);
1589 if (port > last)
1590 nloop++;
1591 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001592 dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07001593 &myaddr.sin6_addr, port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001594 return err;
1595}
1596
Peter Zijlstraed075362006-12-06 20:35:24 -08001597#ifdef CONFIG_DEBUG_LOCK_ALLOC
1598static struct lock_class_key xs_key[2];
1599static struct lock_class_key xs_slock_key[2];
1600
Chuck Lever8945ee52007-08-06 11:58:04 -04001601static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001602{
1603 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001604
John Heffner02b3d342007-09-12 10:42:12 +02001605 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001606 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1607 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1608}
Peter Zijlstraed075362006-12-06 20:35:24 -08001609
Chuck Lever8945ee52007-08-06 11:58:04 -04001610static inline void xs_reclassify_socket6(struct socket *sock)
1611{
1612 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001613
Linus Torvaldsf4921af2007-10-15 10:46:05 -07001614 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001615 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1616 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001617}
1618#else
Chuck Lever8945ee52007-08-06 11:58:04 -04001619static inline void xs_reclassify_socket4(struct socket *sock)
1620{
1621}
1622
1623static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001624{
1625}
1626#endif
1627
Chuck Lever16be2d22007-08-06 11:57:38 -04001628static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001629{
Chuck Lever16be2d22007-08-06 11:57:38 -04001630 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001631
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001632 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001633 struct sock *sk = sock->sk;
1634
1635 write_lock_bh(&sk->sk_callback_lock);
1636
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001637 xs_save_old_callbacks(transport, sk);
1638
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001639 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001640 sk->sk_data_ready = xs_udp_data_ready;
1641 sk->sk_write_space = xs_udp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001642 sk->sk_error_report = xs_error_report;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001643 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001644 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001645
1646 xprt_set_connected(xprt);
1647
1648 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001649 transport->sock = sock;
1650 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001651
1652 write_unlock_bh(&sk->sk_callback_lock);
1653 }
Chuck Lever470056c2005-08-25 16:25:56 -07001654 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04001655}
1656
Chuck Levera246b012005-08-11 16:25:23 -04001657/**
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001658 * xs_udp_connect_worker4 - set up a UDP socket
Chuck Levera246b012005-08-11 16:25:23 -04001659 * @work: RPC transport to connect
1660 *
1661 * Invoked by a work queue tasklet.
1662 */
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001663static void xs_udp_connect_worker4(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04001664{
1665 struct sock_xprt *transport =
1666 container_of(work, struct sock_xprt, connect_worker.work);
1667 struct rpc_xprt *xprt = &transport->xprt;
1668 struct socket *sock = transport->sock;
1669 int err, status = -EIO;
1670
Trond Myklebust01d37c42009-03-11 14:09:39 -04001671 if (xprt->shutdown)
Chuck Lever9903cd12005-08-11 16:25:26 -04001672 goto out;
1673
Chuck Levera246b012005-08-11 16:25:23 -04001674 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001675 xs_reset_transport(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001676
Chuck Leverfe315e72009-03-11 14:10:21 -04001677 err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1678 if (err < 0) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001679 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001680 goto out;
1681 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001682 xs_reclassify_socket4(sock);
Chuck Levera246b012005-08-11 16:25:23 -04001683
Chuck Lever7dc753f2007-08-06 11:57:28 -04001684 if (xs_bind4(transport, sock)) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001685 sock_release(sock);
1686 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001687 }
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001688
Chuck Leverc740eff2009-08-09 15:09:46 -04001689 dprintk("RPC: worker connecting xprt %p via %s to "
1690 "%s (port %s)\n", xprt,
1691 xprt->address_strings[RPC_DISPLAY_PROTO],
1692 xprt->address_strings[RPC_DISPLAY_ADDR],
1693 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001694
Chuck Lever16be2d22007-08-06 11:57:38 -04001695 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001696 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001697out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001698 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001699 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001700}
1701
Chuck Lever68e220b2007-08-06 11:57:48 -04001702/**
1703 * xs_udp_connect_worker6 - set up a UDP socket
1704 * @work: RPC transport to connect
1705 *
1706 * Invoked by a work queue tasklet.
1707 */
1708static void xs_udp_connect_worker6(struct work_struct *work)
1709{
1710 struct sock_xprt *transport =
1711 container_of(work, struct sock_xprt, connect_worker.work);
1712 struct rpc_xprt *xprt = &transport->xprt;
1713 struct socket *sock = transport->sock;
1714 int err, status = -EIO;
1715
Trond Myklebust01d37c42009-03-11 14:09:39 -04001716 if (xprt->shutdown)
Chuck Lever68e220b2007-08-06 11:57:48 -04001717 goto out;
1718
1719 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001720 xs_reset_transport(transport);
Chuck Lever68e220b2007-08-06 11:57:48 -04001721
Chuck Leverfe315e72009-03-11 14:10:21 -04001722 err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
1723 if (err < 0) {
Chuck Lever68e220b2007-08-06 11:57:48 -04001724 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1725 goto out;
1726 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001727 xs_reclassify_socket6(sock);
Chuck Lever68e220b2007-08-06 11:57:48 -04001728
1729 if (xs_bind6(transport, sock) < 0) {
1730 sock_release(sock);
1731 goto out;
1732 }
1733
Chuck Leverc740eff2009-08-09 15:09:46 -04001734 dprintk("RPC: worker connecting xprt %p via %s to "
1735 "%s (port %s)\n", xprt,
1736 xprt->address_strings[RPC_DISPLAY_PROTO],
1737 xprt->address_strings[RPC_DISPLAY_ADDR],
1738 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04001739
1740 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001741 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001742out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001743 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001744 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001745}
1746
Chuck Lever3167e122005-08-25 16:25:55 -07001747/*
1748 * We need to preserve the port number so the reply cache on the server can
1749 * find our cached RPC replies when we get around to reconnecting.
1750 */
Trond Myklebust40d25492009-03-11 14:37:58 -04001751static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07001752{
1753 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07001754 struct sockaddr any;
1755
Chuck Lever46121cf2007-01-31 12:14:08 -05001756 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
Chuck Lever3167e122005-08-25 16:25:55 -07001757
1758 /*
1759 * Disconnect the transport socket by doing a connect operation
1760 * with AF_UNSPEC. This should return immediately...
1761 */
1762 memset(&any, 0, sizeof(any));
1763 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001764 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001765 if (!result)
1766 xs_sock_mark_closed(xprt);
1767 else
Chuck Lever46121cf2007-01-31 12:14:08 -05001768 dprintk("RPC: AF_UNSPEC connect return code %d\n",
Chuck Lever3167e122005-08-25 16:25:55 -07001769 result);
1770}
1771
Trond Myklebust40d25492009-03-11 14:37:58 -04001772static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
1773{
1774 unsigned int state = transport->inet->sk_state;
1775
1776 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED)
1777 return;
1778 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT))
1779 return;
1780 xs_abort_connection(xprt, transport);
1781}
1782
Chuck Lever16be2d22007-08-06 11:57:38 -04001783static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001784{
Chuck Lever16be2d22007-08-06 11:57:38 -04001785 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001786
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001787 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001788 struct sock *sk = sock->sk;
1789
1790 write_lock_bh(&sk->sk_callback_lock);
1791
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001792 xs_save_old_callbacks(transport, sk);
1793
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001794 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001795 sk->sk_data_ready = xs_tcp_data_ready;
1796 sk->sk_state_change = xs_tcp_state_change;
1797 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001798 sk->sk_error_report = xs_error_report;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001799 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07001800
1801 /* socket options */
1802 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1803 sock_reset_flag(sk, SOCK_LINGER);
1804 tcp_sk(sk)->linger2 = 0;
1805 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001806
1807 xprt_clear_connected(xprt);
1808
1809 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001810 transport->sock = sock;
1811 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001812
1813 write_unlock_bh(&sk->sk_callback_lock);
1814 }
1815
Trond Myklebust01d37c42009-03-11 14:09:39 -04001816 if (!xprt_bound(xprt))
1817 return -ENOTCONN;
1818
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001819 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05001820 xprt->stat.connect_count++;
1821 xprt->stat.connect_start = jiffies;
Chuck Lever95392c52007-08-06 11:57:58 -04001822 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
Chuck Lever16be2d22007-08-06 11:57:38 -04001823}
1824
1825/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001826 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1827 * @xprt: RPC transport to connect
1828 * @transport: socket transport to connect
1829 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04001830 *
1831 * Invoked by a work queue tasklet.
1832 */
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001833static void xs_tcp_setup_socket(struct rpc_xprt *xprt,
1834 struct sock_xprt *transport,
1835 struct socket *(*create_sock)(struct rpc_xprt *,
1836 struct sock_xprt *))
Chuck Lever16be2d22007-08-06 11:57:38 -04001837{
Chuck Lever16be2d22007-08-06 11:57:38 -04001838 struct socket *sock = transport->sock;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001839 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04001840
Trond Myklebust01d37c42009-03-11 14:09:39 -04001841 if (xprt->shutdown)
Chuck Lever16be2d22007-08-06 11:57:38 -04001842 goto out;
1843
1844 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001845 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001846 sock = create_sock(xprt, transport);
1847 if (IS_ERR(sock)) {
1848 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04001849 goto out;
1850 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001851 } else {
1852 int abort_and_exit;
1853
1854 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
1855 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04001856 /* "close" the socket, preserving the local port */
Trond Myklebust40d25492009-03-11 14:37:58 -04001857 xs_tcp_reuse_connection(xprt, transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04001858
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001859 if (abort_and_exit)
1860 goto out_eagain;
1861 }
1862
Chuck Leverc740eff2009-08-09 15:09:46 -04001863 dprintk("RPC: worker connecting xprt %p via %s to "
1864 "%s (port %s)\n", xprt,
1865 xprt->address_strings[RPC_DISPLAY_PROTO],
1866 xprt->address_strings[RPC_DISPLAY_ADDR],
1867 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04001868
1869 status = xs_tcp_finish_connecting(xprt, sock);
Chuck Lever46121cf2007-01-31 12:14:08 -05001870 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1871 xprt, -status, xprt_connected(xprt),
1872 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04001873 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04001874 default:
1875 printk("%s: connect returned unhandled error %d\n",
1876 __func__, status);
1877 case -EADDRNOTAVAIL:
1878 /* We're probably in TIME_WAIT. Get rid of existing socket,
1879 * and retry
1880 */
1881 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1882 xprt_force_disconnect(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07001883 break;
Trond Myklebust8a2cec22009-03-11 14:38:01 -04001884 case -ECONNREFUSED:
1885 case -ECONNRESET:
1886 case -ENETUNREACH:
1887 /* retry with existing socket, after a delay */
Trond Myklebust2a491992009-03-11 14:38:00 -04001888 case 0:
1889 case -EINPROGRESS:
1890 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001891 xprt_clear_connecting(xprt);
1892 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05001893 case -EINVAL:
1894 /* Happens, for instance, if the user specified a link
1895 * local IPv6 address without a scope-id.
1896 */
1897 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001898 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001899out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04001900 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04001901out:
Chuck Lever2226feb2005-08-11 16:25:38 -04001902 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001903 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001904}
1905
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001906static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt,
1907 struct sock_xprt *transport)
1908{
1909 struct socket *sock;
1910 int err;
1911
1912 /* start from scratch */
1913 err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1914 if (err < 0) {
1915 dprintk("RPC: can't create TCP transport socket (%d).\n",
1916 -err);
1917 goto out_err;
1918 }
1919 xs_reclassify_socket4(sock);
1920
1921 if (xs_bind4(transport, sock) < 0) {
1922 sock_release(sock);
1923 goto out_err;
1924 }
1925 return sock;
1926out_err:
1927 return ERR_PTR(-EIO);
1928}
1929
1930/**
Chuck Levera246b012005-08-11 16:25:23 -04001931 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1932 * @work: RPC transport to connect
1933 *
1934 * Invoked by a work queue tasklet.
1935 */
1936static void xs_tcp_connect_worker4(struct work_struct *work)
1937{
1938 struct sock_xprt *transport =
1939 container_of(work, struct sock_xprt, connect_worker.work);
1940 struct rpc_xprt *xprt = &transport->xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001941
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001942 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4);
1943}
Chuck Levera246b012005-08-11 16:25:23 -04001944
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001945static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt,
1946 struct sock_xprt *transport)
1947{
1948 struct socket *sock;
1949 int err;
Chuck Lever9903cd12005-08-11 16:25:26 -04001950
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001951 /* start from scratch */
1952 err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock);
1953 if (err < 0) {
1954 dprintk("RPC: can't create TCP transport socket (%d).\n",
1955 -err);
1956 goto out_err;
Chuck Levera246b012005-08-11 16:25:23 -04001957 }
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001958 xs_reclassify_socket6(sock);
1959
1960 if (xs_bind6(transport, sock) < 0) {
1961 sock_release(sock);
1962 goto out_err;
1963 }
1964 return sock;
1965out_err:
1966 return ERR_PTR(-EIO);
Chuck Levera246b012005-08-11 16:25:23 -04001967}
1968
Chuck Lever9903cd12005-08-11 16:25:26 -04001969/**
Chuck Lever68e220b2007-08-06 11:57:48 -04001970 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
1971 * @work: RPC transport to connect
1972 *
1973 * Invoked by a work queue tasklet.
1974 */
1975static void xs_tcp_connect_worker6(struct work_struct *work)
1976{
1977 struct sock_xprt *transport =
1978 container_of(work, struct sock_xprt, connect_worker.work);
1979 struct rpc_xprt *xprt = &transport->xprt;
Chuck Lever68e220b2007-08-06 11:57:48 -04001980
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001981 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6);
Chuck Lever68e220b2007-08-06 11:57:48 -04001982}
1983
1984/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001985 * xs_connect - connect a socket to a remote endpoint
1986 * @task: address of RPC task that manages state of connect request
1987 *
1988 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07001989 *
1990 * UDP socket connects are synchronous, but we use a work queue anyway
1991 * to guarantee that even unprivileged user processes can set up a
1992 * socket on a privileged port.
1993 *
1994 * If a UDP socket connect fails, the delay behavior here prevents
1995 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04001996 */
1997static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04001998{
1999 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002000 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002001
Chuck Lever09a21c42009-12-03 15:58:56 -05002002 if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002003 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2004 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002005 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002006 queue_delayed_work(rpciod_workqueue,
2007 &transport->connect_worker,
2008 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07002009 xprt->reestablish_timeout <<= 1;
Neil Brown61d0a8e2009-09-23 14:36:37 -04002010 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2011 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002012 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2013 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002014 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05002015 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002016 queue_delayed_work(rpciod_workqueue,
2017 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002018 }
2019}
2020
Chuck Lever262ca072006-03-20 13:44:16 -05002021/**
2022 * xs_udp_print_stats - display UDP socket-specifc stats
2023 * @xprt: rpc_xprt struct containing statistics
2024 * @seq: output file
2025 *
2026 */
2027static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2028{
Chuck Leverc8475462006-12-05 16:35:26 -05002029 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2030
Chuck Lever262ca072006-03-20 13:44:16 -05002031 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002032 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002033 xprt->stat.bind_count,
2034 xprt->stat.sends,
2035 xprt->stat.recvs,
2036 xprt->stat.bad_xids,
2037 xprt->stat.req_u,
2038 xprt->stat.bklog_u);
2039}
2040
2041/**
2042 * xs_tcp_print_stats - display TCP socket-specifc stats
2043 * @xprt: rpc_xprt struct containing statistics
2044 * @seq: output file
2045 *
2046 */
2047static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2048{
Chuck Leverc8475462006-12-05 16:35:26 -05002049 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002050 long idle_time = 0;
2051
2052 if (xprt_connected(xprt))
2053 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2054
2055 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002056 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002057 xprt->stat.bind_count,
2058 xprt->stat.connect_count,
2059 xprt->stat.connect_time,
2060 idle_time,
2061 xprt->stat.sends,
2062 xprt->stat.recvs,
2063 xprt->stat.bad_xids,
2064 xprt->stat.req_u,
2065 xprt->stat.bklog_u);
2066}
2067
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002068/*
2069 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2070 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2071 * to use the server side send routines.
2072 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002073static void *bc_malloc(struct rpc_task *task, size_t size)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002074{
2075 struct page *page;
2076 struct rpc_buffer *buf;
2077
2078 BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2079 page = alloc_page(GFP_KERNEL);
2080
2081 if (!page)
2082 return NULL;
2083
2084 buf = page_address(page);
2085 buf->len = PAGE_SIZE;
2086
2087 return buf->data;
2088}
2089
2090/*
2091 * Free the space allocated in the bc_alloc routine
2092 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002093static void bc_free(void *buffer)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002094{
2095 struct rpc_buffer *buf;
2096
2097 if (!buffer)
2098 return;
2099
2100 buf = container_of(buffer, struct rpc_buffer, data);
2101 free_page((unsigned long)buf);
2102}
2103
2104/*
2105 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2106 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2107 */
2108static int bc_sendto(struct rpc_rqst *req)
2109{
2110 int len;
2111 struct xdr_buf *xbufp = &req->rq_snd_buf;
2112 struct rpc_xprt *xprt = req->rq_xprt;
2113 struct sock_xprt *transport =
2114 container_of(xprt, struct sock_xprt, xprt);
2115 struct socket *sock = transport->sock;
2116 unsigned long headoff;
2117 unsigned long tailoff;
2118
2119 /*
2120 * Set up the rpc header and record marker stuff
2121 */
2122 xs_encode_tcp_record_marker(xbufp);
2123
2124 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2125 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2126 len = svc_send_common(sock, xbufp,
2127 virt_to_page(xbufp->head[0].iov_base), headoff,
2128 xbufp->tail[0].iov_base, tailoff);
2129
2130 if (len != xbufp->len) {
2131 printk(KERN_NOTICE "Error sending entire callback!\n");
2132 len = -EAGAIN;
2133 }
2134
2135 return len;
2136}
2137
2138/*
2139 * The send routine. Borrows from svc_send
2140 */
2141static int bc_send_request(struct rpc_task *task)
2142{
2143 struct rpc_rqst *req = task->tk_rqstp;
2144 struct svc_xprt *xprt;
2145 struct svc_sock *svsk;
2146 u32 len;
2147
2148 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2149 /*
2150 * Get the server socket associated with this callback xprt
2151 */
2152 xprt = req->rq_xprt->bc_xprt;
2153 svsk = container_of(xprt, struct svc_sock, sk_xprt);
2154
2155 /*
2156 * Grab the mutex to serialize data as the connection is shared
2157 * with the fore channel
2158 */
2159 if (!mutex_trylock(&xprt->xpt_mutex)) {
2160 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2161 if (!mutex_trylock(&xprt->xpt_mutex))
2162 return -EAGAIN;
2163 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2164 }
2165 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2166 len = -ENOTCONN;
2167 else
2168 len = bc_sendto(req);
2169 mutex_unlock(&xprt->xpt_mutex);
2170
2171 if (len > 0)
2172 len = 0;
2173
2174 return len;
2175}
2176
2177/*
2178 * The close routine. Since this is client initiated, we do nothing
2179 */
2180
2181static void bc_close(struct rpc_xprt *xprt)
2182{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002183}
2184
2185/*
2186 * The xprt destroy routine. Again, because this connection is client
2187 * initiated, we do nothing
2188 */
2189
2190static void bc_destroy(struct rpc_xprt *xprt)
2191{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002192}
2193
Chuck Lever262965f2005-08-11 16:25:56 -04002194static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002195 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002196 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002197 .release_xprt = xprt_release_xprt_cong,
Chuck Lever45160d62007-07-01 12:13:17 -04002198 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002199 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002200 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002201 .buf_alloc = rpc_malloc,
2202 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002203 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002204 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002205 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002206 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002207 .close = xs_close,
2208 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002209 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04002210};
2211
2212static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002213 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002214 .release_xprt = xs_tcp_release_xprt,
Chuck Lever45160d62007-07-01 12:13:17 -04002215 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002216 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002217 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002218 .buf_alloc = rpc_malloc,
2219 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002220 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002221 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustf75e6742009-04-21 17:18:20 -04002222 .close = xs_tcp_close,
Chuck Lever9903cd12005-08-11 16:25:26 -04002223 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002224 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002225};
2226
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002227/*
2228 * The rpc_xprt_ops for the server backchannel
2229 */
2230
2231static struct rpc_xprt_ops bc_tcp_ops = {
2232 .reserve_xprt = xprt_reserve_xprt,
2233 .release_xprt = xprt_release_xprt,
2234 .buf_alloc = bc_malloc,
2235 .buf_free = bc_free,
2236 .send_request = bc_send_request,
2237 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2238 .close = bc_close,
2239 .destroy = bc_destroy,
2240 .print_stats = xs_tcp_print_stats,
2241};
2242
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002243static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002244 unsigned int slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002245{
2246 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002247 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002248
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002249 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002250 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002251 return ERR_PTR(-EBADF);
2252 }
2253
Chuck Leverffc2e512006-12-05 16:35:11 -05002254 new = kzalloc(sizeof(*new), GFP_KERNEL);
2255 if (new == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002256 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2257 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002258 return ERR_PTR(-ENOMEM);
2259 }
Chuck Leverffc2e512006-12-05 16:35:11 -05002260 xprt = &new->xprt;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002261
2262 xprt->max_reqs = slot_table_size;
2263 xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
2264 if (xprt->slot == NULL) {
2265 kfree(xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05002266 dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
2267 "table\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002268 return ERR_PTR(-ENOMEM);
2269 }
2270
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002271 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2272 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002273 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002274 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002275
2276 return xprt;
2277}
2278
Trond Myklebust2881ae72007-12-20 16:03:54 -05002279static const struct rpc_timeout xs_udp_default_timeout = {
2280 .to_initval = 5 * HZ,
2281 .to_maxval = 30 * HZ,
2282 .to_increment = 5 * HZ,
2283 .to_retries = 5,
2284};
2285
Chuck Lever9903cd12005-08-11 16:25:26 -04002286/**
2287 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002288 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002289 *
2290 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002291static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002292{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002293 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002294 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002295 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002296 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002297
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002298 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002299 if (IS_ERR(xprt))
2300 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002301 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002302
Chuck Leverec739ef2006-08-22 20:06:15 -04002303 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002304 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002305 /* XXX: header size can vary due to auth type, IPv6, etc. */
2306 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2307
Chuck Lever03bf4b72005-08-25 16:25:55 -07002308 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002309 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2310 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002311
Chuck Lever262965f2005-08-11 16:25:56 -04002312 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002313
Trond Myklebustba7392b2007-12-20 16:03:55 -05002314 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002315
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002316 switch (addr->sa_family) {
2317 case AF_INET:
2318 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2319 xprt_set_bound(xprt);
2320
2321 INIT_DELAYED_WORK(&transport->connect_worker,
2322 xs_udp_connect_worker4);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002323 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002324 break;
2325 case AF_INET6:
2326 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2327 xprt_set_bound(xprt);
2328
2329 INIT_DELAYED_WORK(&transport->connect_worker,
2330 xs_udp_connect_worker6);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002331 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002332 break;
2333 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002334 ret = ERR_PTR(-EAFNOSUPPORT);
2335 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002336 }
2337
Chuck Leverc740eff2009-08-09 15:09:46 -04002338 if (xprt_bound(xprt))
2339 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2340 xprt->address_strings[RPC_DISPLAY_ADDR],
2341 xprt->address_strings[RPC_DISPLAY_PORT],
2342 xprt->address_strings[RPC_DISPLAY_PROTO]);
2343 else
2344 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2345 xprt->address_strings[RPC_DISPLAY_ADDR],
2346 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002347
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002348 if (try_module_get(THIS_MODULE))
2349 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002350 ret = ERR_PTR(-EINVAL);
2351out_err:
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002352 kfree(xprt->slot);
2353 kfree(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002354 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002355}
2356
Trond Myklebust2881ae72007-12-20 16:03:54 -05002357static const struct rpc_timeout xs_tcp_default_timeout = {
2358 .to_initval = 60 * HZ,
2359 .to_maxval = 60 * HZ,
2360 .to_retries = 2,
2361};
2362
Chuck Lever9903cd12005-08-11 16:25:26 -04002363/**
2364 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002365 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002366 *
2367 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002368static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002369{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002370 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002371 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002372 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002373 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002374
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002375 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002376 if (IS_ERR(xprt))
2377 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002378 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002379
Chuck Leverec739ef2006-08-22 20:06:15 -04002380 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002381 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002382 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002383
Chuck Lever03bf4b72005-08-25 16:25:55 -07002384 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002385 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2386 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002387
Chuck Lever262965f2005-08-11 16:25:56 -04002388 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002389 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002390
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002391 switch (addr->sa_family) {
2392 case AF_INET:
2393 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2394 xprt_set_bound(xprt);
2395
Chuck Lever9dc3b092009-08-09 15:09:46 -04002396 INIT_DELAYED_WORK(&transport->connect_worker,
2397 xs_tcp_connect_worker4);
2398 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002399 break;
2400 case AF_INET6:
2401 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2402 xprt_set_bound(xprt);
2403
Chuck Lever9dc3b092009-08-09 15:09:46 -04002404 INIT_DELAYED_WORK(&transport->connect_worker,
2405 xs_tcp_connect_worker6);
2406 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002407 break;
2408 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002409 ret = ERR_PTR(-EAFNOSUPPORT);
2410 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002411 }
2412
Chuck Leverc740eff2009-08-09 15:09:46 -04002413 if (xprt_bound(xprt))
2414 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2415 xprt->address_strings[RPC_DISPLAY_ADDR],
2416 xprt->address_strings[RPC_DISPLAY_PORT],
2417 xprt->address_strings[RPC_DISPLAY_PROTO]);
2418 else
2419 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2420 xprt->address_strings[RPC_DISPLAY_ADDR],
2421 xprt->address_strings[RPC_DISPLAY_PROTO]);
2422
Chuck Leveredb267a2006-08-22 20:06:18 -04002423
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002424 if (try_module_get(THIS_MODULE))
2425 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002426 ret = ERR_PTR(-EINVAL);
2427out_err:
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002428 kfree(xprt->slot);
2429 kfree(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002430 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002431}
Chuck Lever282b32e2006-12-05 16:35:51 -05002432
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002433/**
2434 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2435 * @args: rpc transport creation arguments
2436 *
2437 */
2438static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2439{
2440 struct sockaddr *addr = args->dstaddr;
2441 struct rpc_xprt *xprt;
2442 struct sock_xprt *transport;
2443 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002444 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002445
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002446 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2447 if (IS_ERR(xprt))
2448 return xprt;
2449 transport = container_of(xprt, struct sock_xprt, xprt);
2450
2451 xprt->prot = IPPROTO_TCP;
2452 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2453 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2454 xprt->timeout = &xs_tcp_default_timeout;
2455
2456 /* backchannel */
2457 xprt_set_bound(xprt);
2458 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002459 xprt->reestablish_timeout = 0;
2460 xprt->idle_timeout = 0;
2461
2462 /*
2463 * The backchannel uses the same socket connection as the
2464 * forechannel
2465 */
2466 xprt->bc_xprt = args->bc_xprt;
2467 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2468 bc_sock->sk_bc_xprt = xprt;
2469 transport->sock = bc_sock->sk_sock;
2470 transport->inet = bc_sock->sk_sk;
2471
2472 xprt->ops = &bc_tcp_ops;
2473
2474 switch (addr->sa_family) {
2475 case AF_INET:
2476 xs_format_peer_addresses(xprt, "tcp",
2477 RPCBIND_NETID_TCP);
2478 break;
2479 case AF_INET6:
2480 xs_format_peer_addresses(xprt, "tcp",
2481 RPCBIND_NETID_TCP6);
2482 break;
2483 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002484 ret = ERR_PTR(-EAFNOSUPPORT);
2485 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002486 }
2487
2488 if (xprt_bound(xprt))
2489 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2490 xprt->address_strings[RPC_DISPLAY_ADDR],
2491 xprt->address_strings[RPC_DISPLAY_PORT],
2492 xprt->address_strings[RPC_DISPLAY_PROTO]);
2493 else
2494 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2495 xprt->address_strings[RPC_DISPLAY_ADDR],
2496 xprt->address_strings[RPC_DISPLAY_PROTO]);
2497
2498 /*
2499 * Since we don't want connections for the backchannel, we set
2500 * the xprt status to connected
2501 */
2502 xprt_set_connected(xprt);
2503
2504
2505 if (try_module_get(THIS_MODULE))
2506 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002507 ret = ERR_PTR(-EINVAL);
2508out_err:
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002509 kfree(xprt->slot);
2510 kfree(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002511 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002512}
2513
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002514static struct xprt_class xs_udp_transport = {
2515 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2516 .name = "udp",
2517 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002518 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002519 .setup = xs_setup_udp,
2520};
2521
2522static struct xprt_class xs_tcp_transport = {
2523 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2524 .name = "tcp",
2525 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002526 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002527 .setup = xs_setup_tcp,
2528};
2529
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002530static struct xprt_class xs_bc_tcp_transport = {
2531 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2532 .name = "tcp NFSv4.1 backchannel",
2533 .owner = THIS_MODULE,
2534 .ident = XPRT_TRANSPORT_BC_TCP,
2535 .setup = xs_setup_bc_tcp,
2536};
2537
Chuck Lever282b32e2006-12-05 16:35:51 -05002538/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002539 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002540 *
2541 */
2542int init_socket_xprt(void)
2543{
Chuck Leverfbf76682006-12-05 16:35:54 -05002544#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002545 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002546 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002547#endif
2548
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002549 xprt_register_transport(&xs_udp_transport);
2550 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002551 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002552
Chuck Lever282b32e2006-12-05 16:35:51 -05002553 return 0;
2554}
2555
2556/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002557 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05002558 *
2559 */
2560void cleanup_socket_xprt(void)
2561{
Chuck Leverfbf76682006-12-05 16:35:54 -05002562#ifdef RPC_DEBUG
2563 if (sunrpc_table_header) {
2564 unregister_sysctl_table(sunrpc_table_header);
2565 sunrpc_table_header = NULL;
2566 }
2567#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002568
2569 xprt_unregister_transport(&xs_udp_transport);
2570 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002571 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05002572}
Trond Myklebustcbf11072009-08-09 15:06:19 -04002573
2574static int param_set_uint_minmax(const char *val, struct kernel_param *kp,
2575 unsigned int min, unsigned int max)
2576{
2577 unsigned long num;
2578 int ret;
2579
2580 if (!val)
2581 return -EINVAL;
2582 ret = strict_strtoul(val, 0, &num);
2583 if (ret == -EINVAL || num < min || num > max)
2584 return -EINVAL;
2585 *((unsigned int *)kp->arg) = num;
2586 return 0;
2587}
2588
2589static int param_set_portnr(const char *val, struct kernel_param *kp)
2590{
2591 return param_set_uint_minmax(val, kp,
2592 RPC_MIN_RESVPORT,
2593 RPC_MAX_RESVPORT);
2594}
2595
2596static int param_get_portnr(char *buffer, struct kernel_param *kp)
2597{
2598 return param_get_uint(buffer, kp);
2599}
2600#define param_check_portnr(name, p) \
2601 __param_check(name, p, unsigned int);
2602
2603module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
2604module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
2605
2606static int param_set_slot_table_size(const char *val, struct kernel_param *kp)
2607{
2608 return param_set_uint_minmax(val, kp,
2609 RPC_MIN_SLOT_TABLE,
2610 RPC_MAX_SLOT_TABLE);
2611}
2612
2613static int param_get_slot_table_size(char *buffer, struct kernel_param *kp)
2614{
2615 return param_get_uint(buffer, kp);
2616}
2617#define param_check_slot_table_size(name, p) \
2618 __param_check(name, p, unsigned int);
2619
2620module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
2621 slot_table_size, 0644);
2622module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
2623 slot_table_size, 0644);
2624