blob: 02fc7f04dd17afbd036dafe5e81234adab364d16 [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 }
1038
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001039 return;
1040}
1041
1042/*
1043 * Finds the request corresponding to the RPC xid and invokes the common
1044 * tcp read code to read the data.
1045 */
1046static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1047 struct xdr_skb_reader *desc)
1048{
1049 struct sock_xprt *transport =
1050 container_of(xprt, struct sock_xprt, xprt);
1051 struct rpc_rqst *req;
1052
1053 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1054
1055 /* Find and lock the request corresponding to this xid */
1056 spin_lock(&xprt->transport_lock);
1057 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1058 if (!req) {
1059 dprintk("RPC: XID %08x request not found!\n",
1060 ntohl(transport->tcp_xid));
1061 spin_unlock(&xprt->transport_lock);
1062 return -1;
1063 }
1064
1065 xs_tcp_read_common(xprt, desc, req);
1066
Chuck Levere136d092006-12-05 16:35:23 -05001067 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001068 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001069
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001070 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001071 return 0;
1072}
1073
1074#if defined(CONFIG_NFS_V4_1)
1075/*
1076 * Obtains an rpc_rqst previously allocated and invokes the common
1077 * tcp read code to read the data. The result is placed in the callback
1078 * queue.
1079 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1080 * connection and return -1.
1081 */
1082static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1083 struct xdr_skb_reader *desc)
1084{
1085 struct sock_xprt *transport =
1086 container_of(xprt, struct sock_xprt, xprt);
1087 struct rpc_rqst *req;
1088
1089 req = xprt_alloc_bc_request(xprt);
1090 if (req == NULL) {
1091 printk(KERN_WARNING "Callback slot table overflowed\n");
1092 xprt_force_disconnect(xprt);
1093 return -1;
1094 }
1095
1096 req->rq_xid = transport->tcp_xid;
1097 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1098 xs_tcp_read_common(xprt, desc, req);
1099
1100 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1101 struct svc_serv *bc_serv = xprt->bc_serv;
1102
1103 /*
1104 * Add callback request to callback list. The callback
1105 * service sleeps on the sv_cb_waitq waiting for new
1106 * requests. Wake it up after adding enqueing the
1107 * request.
1108 */
1109 dprintk("RPC: add callback request to list\n");
1110 spin_lock(&bc_serv->sv_cb_lock);
1111 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1112 spin_unlock(&bc_serv->sv_cb_lock);
1113 wake_up(&bc_serv->sv_cb_waitq);
1114 }
1115
1116 req->rq_private_buf.len = transport->tcp_copied;
1117
1118 return 0;
1119}
1120
1121static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1122 struct xdr_skb_reader *desc)
1123{
1124 struct sock_xprt *transport =
1125 container_of(xprt, struct sock_xprt, xprt);
1126
1127 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1128 xs_tcp_read_reply(xprt, desc) :
1129 xs_tcp_read_callback(xprt, desc);
1130}
1131#else
1132static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1133 struct xdr_skb_reader *desc)
1134{
1135 return xs_tcp_read_reply(xprt, desc);
1136}
1137#endif /* CONFIG_NFS_V4_1 */
1138
1139/*
1140 * Read data off the transport. This can be either an RPC_CALL or an
1141 * RPC_REPLY. Relay the processing to helper functions.
1142 */
1143static void xs_tcp_read_data(struct rpc_xprt *xprt,
1144 struct xdr_skb_reader *desc)
1145{
1146 struct sock_xprt *transport =
1147 container_of(xprt, struct sock_xprt, xprt);
1148
1149 if (_xs_tcp_read_data(xprt, desc) == 0)
1150 xs_tcp_check_fraghdr(transport);
1151 else {
1152 /*
1153 * The transport_lock protects the request handling.
1154 * There's no need to hold it to update the tcp_flags.
1155 */
1156 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1157 }
Chuck Levera246b012005-08-11 16:25:23 -04001158}
1159
Chuck Leverdd456472006-12-05 16:35:44 -05001160static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001161{
1162 size_t len;
1163
Chuck Lever51971132006-12-05 16:35:19 -05001164 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001165 if (len > desc->count)
1166 len = desc->count;
1167 desc->count -= len;
1168 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001169 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001170 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001171 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001172}
1173
Chuck Lever9903cd12005-08-11 16:25:26 -04001174static 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 -04001175{
1176 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001177 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001178 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001179 .skb = skb,
1180 .offset = offset,
1181 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001182 };
Chuck Levera246b012005-08-11 16:25:23 -04001183
Chuck Lever46121cf2007-01-31 12:14:08 -05001184 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001185 do {
1186 /* Read in a new fragment marker if necessary */
1187 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001188 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001189 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001190 continue;
1191 }
1192 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001193 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001194 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001195 continue;
1196 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001197 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001198 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001199 xs_tcp_read_calldir(transport, &desc);
1200 continue;
1201 }
Chuck Levera246b012005-08-11 16:25:23 -04001202 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001203 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001204 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001205 continue;
1206 }
1207 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001208 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001209 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001210 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001211 return len - desc.count;
1212}
1213
Chuck Lever9903cd12005-08-11 16:25:26 -04001214/**
1215 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1216 * @sk: socket with data to read
1217 * @bytes: how much data to read
1218 *
1219 */
1220static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001221{
1222 struct rpc_xprt *xprt;
1223 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001224 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001225
Chuck Lever46121cf2007-01-31 12:14:08 -05001226 dprintk("RPC: xs_tcp_data_ready...\n");
1227
Chuck Levera246b012005-08-11 16:25:23 -04001228 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001229 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001230 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001231 if (xprt->shutdown)
1232 goto out;
1233
Neil Brown61d0a8e2009-09-23 14:36:37 -04001234 /* Any data means we had a useful conversation, so
1235 * the we don't need to delay the next reconnect
1236 */
1237 if (xprt->reestablish_timeout)
1238 xprt->reestablish_timeout = 0;
1239
Chuck Lever9903cd12005-08-11 16:25:26 -04001240 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001241 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001242 do {
1243 rd_desc.count = 65536;
1244 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1245 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001246out:
1247 read_unlock(&sk->sk_callback_lock);
1248}
1249
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001250/*
1251 * Do the equivalent of linger/linger2 handling for dealing with
1252 * broken servers that don't close the socket in a timely
1253 * fashion
1254 */
1255static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1256 unsigned long timeout)
1257{
1258 struct sock_xprt *transport;
1259
1260 if (xprt_test_and_set_connecting(xprt))
1261 return;
1262 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1263 transport = container_of(xprt, struct sock_xprt, xprt);
1264 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1265 timeout);
1266}
1267
1268static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1269{
1270 struct sock_xprt *transport;
1271
1272 transport = container_of(xprt, struct sock_xprt, xprt);
1273
1274 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1275 !cancel_delayed_work(&transport->connect_worker))
1276 return;
1277 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1278 xprt_clear_connecting(xprt);
1279}
1280
1281static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1282{
1283 smp_mb__before_clear_bit();
1284 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1285 clear_bit(XPRT_CLOSING, &xprt->state);
1286 smp_mb__after_clear_bit();
1287 /* Mark transport as closed and wake up all pending tasks */
1288 xprt_disconnect_done(xprt);
1289}
1290
Chuck Lever9903cd12005-08-11 16:25:26 -04001291/**
1292 * xs_tcp_state_change - callback to handle TCP socket state changes
1293 * @sk: socket whose state has changed
1294 *
1295 */
1296static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001297{
Chuck Lever9903cd12005-08-11 16:25:26 -04001298 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001299
1300 read_lock(&sk->sk_callback_lock);
1301 if (!(xprt = xprt_from_sock(sk)))
1302 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001303 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
1304 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
1305 sk->sk_state, xprt_connected(xprt),
1306 sock_flag(sk, SOCK_DEAD),
1307 sock_flag(sk, SOCK_ZAPPED));
Chuck Levera246b012005-08-11 16:25:23 -04001308
1309 switch (sk->sk_state) {
1310 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001311 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001312 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001313 struct sock_xprt *transport = container_of(xprt,
1314 struct sock_xprt, xprt);
1315
Chuck Levera246b012005-08-11 16:25:23 -04001316 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001317 transport->tcp_offset = 0;
1318 transport->tcp_reclen = 0;
1319 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001320 transport->tcp_flags =
1321 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001322
Trond Myklebust2a491992009-03-11 14:38:00 -04001323 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001324 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001325 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001326 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001327 case TCP_FIN_WAIT1:
1328 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001329 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001330 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001331 set_bit(XPRT_CLOSING, &xprt->state);
1332 smp_mb__before_clear_bit();
1333 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001334 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001335 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001336 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001337 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001338 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001339 /* The server initiated a shutdown of the socket */
Trond Myklebust66af1e52007-11-06 10:18:36 -05001340 xprt_force_disconnect(xprt);
Trond Myklebust663b8852008-01-01 18:42:12 -05001341 case TCP_SYN_SENT:
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001342 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001343 case TCP_CLOSING:
1344 /*
1345 * If the server closed down the connection, make sure that
1346 * we back off before reconnecting
1347 */
1348 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1349 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001350 break;
1351 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001352 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001353 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001354 smp_mb__before_clear_bit();
1355 clear_bit(XPRT_CONNECTED, &xprt->state);
1356 smp_mb__after_clear_bit();
1357 break;
1358 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001359 xs_tcp_cancel_linger_timeout(xprt);
1360 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001361 }
1362 out:
1363 read_unlock(&sk->sk_callback_lock);
1364}
1365
Chuck Lever9903cd12005-08-11 16:25:26 -04001366/**
Trond Myklebust482f32e2009-03-11 14:38:00 -04001367 * xs_error_report - callback mainly for catching socket errors
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001368 * @sk: socket
1369 */
Trond Myklebust482f32e2009-03-11 14:38:00 -04001370static void xs_error_report(struct sock *sk)
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001371{
1372 struct rpc_xprt *xprt;
1373
1374 read_lock(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001375 if (!(xprt = xprt_from_sock(sk)))
1376 goto out;
1377 dprintk("RPC: %s client %p...\n"
1378 "RPC: error %d\n",
1379 __func__, xprt, sk->sk_err);
Trond Myklebust482f32e2009-03-11 14:38:00 -04001380 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001381out:
1382 read_unlock(&sk->sk_callback_lock);
1383}
1384
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001385static void xs_write_space(struct sock *sk)
1386{
1387 struct socket *sock;
1388 struct rpc_xprt *xprt;
1389
1390 if (unlikely(!(sock = sk->sk_socket)))
1391 return;
1392 clear_bit(SOCK_NOSPACE, &sock->flags);
1393
1394 if (unlikely(!(xprt = xprt_from_sock(sk))))
1395 return;
1396 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1397 return;
1398
1399 xprt_write_space(xprt);
1400}
1401
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001402/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001403 * xs_udp_write_space - callback invoked when socket buffer space
1404 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001405 * @sk: socket whose state has changed
1406 *
Chuck Levera246b012005-08-11 16:25:23 -04001407 * Called when more output buffer space is available for this socket.
1408 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001409 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001410 * with a bunch of small requests.
1411 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001412static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001413{
Chuck Levera246b012005-08-11 16:25:23 -04001414 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001415
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001416 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001417 if (sock_writeable(sk))
1418 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001419
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001420 read_unlock(&sk->sk_callback_lock);
1421}
Chuck Levera246b012005-08-11 16:25:23 -04001422
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001423/**
1424 * xs_tcp_write_space - callback invoked when socket buffer space
1425 * becomes available
1426 * @sk: socket whose state has changed
1427 *
1428 * Called when more output buffer space is available for this socket.
1429 * We try not to wake our writers until they can make "significant"
1430 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1431 * with a bunch of small requests.
1432 */
1433static void xs_tcp_write_space(struct sock *sk)
1434{
1435 read_lock(&sk->sk_callback_lock);
1436
1437 /* from net/core/stream.c:sk_stream_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001438 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1439 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001440
Chuck Levera246b012005-08-11 16:25:23 -04001441 read_unlock(&sk->sk_callback_lock);
1442}
1443
Chuck Lever470056c2005-08-25 16:25:56 -07001444static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001445{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001446 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1447 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001448
Chuck Lever7c6e0662006-12-05 16:35:30 -05001449 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001450 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001451 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001452 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001453 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001454 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001455 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001456 sk->sk_write_space(sk);
1457 }
1458}
1459
Chuck Lever43118c22005-08-25 16:25:49 -07001460/**
Chuck Lever470056c2005-08-25 16:25:56 -07001461 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001462 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001463 * @sndsize: requested size of send buffer, in bytes
1464 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001465 *
Chuck Lever470056c2005-08-25 16:25:56 -07001466 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001467 */
Chuck Lever470056c2005-08-25 16:25:56 -07001468static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001469{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001470 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1471
1472 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001473 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001474 transport->sndsize = sndsize + 1024;
1475 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001476 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001477 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001478
1479 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001480}
1481
Chuck Lever46c0ee82005-08-25 16:25:52 -07001482/**
1483 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1484 * @task: task that timed out
1485 *
1486 * Adjust the congestion window after a retransmit timeout has occurred.
1487 */
1488static void xs_udp_timer(struct rpc_task *task)
1489{
1490 xprt_adjust_cwnd(task, -ETIMEDOUT);
1491}
1492
Chuck Leverb85d8802006-05-25 01:40:49 -04001493static unsigned short xs_get_random_port(void)
1494{
1495 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1496 unsigned short rand = (unsigned short) net_random() % range;
1497 return rand + xprt_min_resvport;
1498}
1499
Chuck Lever92200412006-01-03 09:55:51 +01001500/**
1501 * xs_set_port - reset the port number in the remote endpoint address
1502 * @xprt: generic transport
1503 * @port: new port number
1504 *
1505 */
1506static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1507{
Chuck Lever46121cf2007-01-31 12:14:08 -05001508 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001509
Chuck Lever9dc3b092009-08-09 15:09:46 -04001510 rpc_set_port(xs_addr(xprt), port);
1511 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001512}
1513
Trond Myklebust67a391d2007-11-05 17:40:58 -05001514static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
1515{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001516 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001517
1518 if (port == 0 && transport->xprt.resvport)
1519 port = xs_get_random_port();
1520 return port;
1521}
1522
1523static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
1524{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001525 if (transport->srcport != 0)
1526 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001527 if (!transport->xprt.resvport)
1528 return 0;
1529 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1530 return xprt_max_resvport;
1531 return --port;
1532}
1533
Chuck Lever7dc753f2007-08-06 11:57:28 -04001534static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001535{
1536 struct sockaddr_in myaddr = {
1537 .sin_family = AF_INET,
1538 };
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001539 struct sockaddr_in *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001540 int err, nloop = 0;
1541 unsigned short port = xs_get_srcport(transport, sock);
1542 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001543
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001544 sa = (struct sockaddr_in *)&transport->srcaddr;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001545 myaddr.sin_addr = sa->sin_addr;
Chuck Levera246b012005-08-11 16:25:23 -04001546 do {
1547 myaddr.sin_port = htons(port);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001548 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
Chuck Levera246b012005-08-11 16:25:23 -04001549 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001550 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001551 break;
Chuck Levera246b012005-08-11 16:25:23 -04001552 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001553 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001554 break;
Chuck Levera246b012005-08-11 16:25:23 -04001555 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001556 last = port;
1557 port = xs_next_srcport(transport, sock, port);
1558 if (port > last)
1559 nloop++;
1560 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison21454aa2008-10-31 00:54:56 -07001561 dprintk("RPC: %s %pI4:%u: %s (%d)\n",
1562 __func__, &myaddr.sin_addr,
Chuck Lever7dc753f2007-08-06 11:57:28 -04001563 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001564 return err;
1565}
1566
Chuck Lever90058d32007-08-06 11:57:33 -04001567static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
1568{
1569 struct sockaddr_in6 myaddr = {
1570 .sin6_family = AF_INET6,
1571 };
1572 struct sockaddr_in6 *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001573 int err, nloop = 0;
1574 unsigned short port = xs_get_srcport(transport, sock);
1575 unsigned short last;
Chuck Lever90058d32007-08-06 11:57:33 -04001576
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001577 sa = (struct sockaddr_in6 *)&transport->srcaddr;
Chuck Lever90058d32007-08-06 11:57:33 -04001578 myaddr.sin6_addr = sa->sin6_addr;
1579 do {
1580 myaddr.sin6_port = htons(port);
1581 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1582 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001583 if (port == 0)
Chuck Lever90058d32007-08-06 11:57:33 -04001584 break;
1585 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001586 transport->srcport = port;
Chuck Lever90058d32007-08-06 11:57:33 -04001587 break;
1588 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001589 last = port;
1590 port = xs_next_srcport(transport, sock, port);
1591 if (port > last)
1592 nloop++;
1593 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001594 dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07001595 &myaddr.sin6_addr, port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001596 return err;
1597}
1598
Peter Zijlstraed075362006-12-06 20:35:24 -08001599#ifdef CONFIG_DEBUG_LOCK_ALLOC
1600static struct lock_class_key xs_key[2];
1601static struct lock_class_key xs_slock_key[2];
1602
Chuck Lever8945ee52007-08-06 11:58:04 -04001603static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001604{
1605 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001606
John Heffner02b3d342007-09-12 10:42:12 +02001607 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001608 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1609 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1610}
Peter Zijlstraed075362006-12-06 20:35:24 -08001611
Chuck Lever8945ee52007-08-06 11:58:04 -04001612static inline void xs_reclassify_socket6(struct socket *sock)
1613{
1614 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001615
Linus Torvaldsf4921af2007-10-15 10:46:05 -07001616 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001617 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1618 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001619}
1620#else
Chuck Lever8945ee52007-08-06 11:58:04 -04001621static inline void xs_reclassify_socket4(struct socket *sock)
1622{
1623}
1624
1625static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001626{
1627}
1628#endif
1629
Chuck Lever16be2d22007-08-06 11:57:38 -04001630static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001631{
Chuck Lever16be2d22007-08-06 11:57:38 -04001632 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001633
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001634 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001635 struct sock *sk = sock->sk;
1636
1637 write_lock_bh(&sk->sk_callback_lock);
1638
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001639 xs_save_old_callbacks(transport, sk);
1640
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001641 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001642 sk->sk_data_ready = xs_udp_data_ready;
1643 sk->sk_write_space = xs_udp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001644 sk->sk_error_report = xs_error_report;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001645 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001646 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001647
1648 xprt_set_connected(xprt);
1649
1650 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001651 transport->sock = sock;
1652 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001653
1654 write_unlock_bh(&sk->sk_callback_lock);
1655 }
Chuck Lever470056c2005-08-25 16:25:56 -07001656 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04001657}
1658
Chuck Levera246b012005-08-11 16:25:23 -04001659/**
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001660 * xs_udp_connect_worker4 - set up a UDP socket
Chuck Levera246b012005-08-11 16:25:23 -04001661 * @work: RPC transport to connect
1662 *
1663 * Invoked by a work queue tasklet.
1664 */
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001665static void xs_udp_connect_worker4(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04001666{
1667 struct sock_xprt *transport =
1668 container_of(work, struct sock_xprt, connect_worker.work);
1669 struct rpc_xprt *xprt = &transport->xprt;
1670 struct socket *sock = transport->sock;
1671 int err, status = -EIO;
1672
Trond Myklebust01d37c42009-03-11 14:09:39 -04001673 if (xprt->shutdown)
Chuck Lever9903cd12005-08-11 16:25:26 -04001674 goto out;
1675
Chuck Levera246b012005-08-11 16:25:23 -04001676 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001677 xs_reset_transport(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001678
Chuck Leverfe315e72009-03-11 14:10:21 -04001679 err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1680 if (err < 0) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001681 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001682 goto out;
1683 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001684 xs_reclassify_socket4(sock);
Chuck Levera246b012005-08-11 16:25:23 -04001685
Chuck Lever7dc753f2007-08-06 11:57:28 -04001686 if (xs_bind4(transport, sock)) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001687 sock_release(sock);
1688 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001689 }
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001690
Chuck Leverc740eff2009-08-09 15:09:46 -04001691 dprintk("RPC: worker connecting xprt %p via %s to "
1692 "%s (port %s)\n", xprt,
1693 xprt->address_strings[RPC_DISPLAY_PROTO],
1694 xprt->address_strings[RPC_DISPLAY_ADDR],
1695 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001696
Chuck Lever16be2d22007-08-06 11:57:38 -04001697 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001698 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001699out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001700 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001701 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001702}
1703
Chuck Lever68e220b2007-08-06 11:57:48 -04001704/**
1705 * xs_udp_connect_worker6 - set up a UDP socket
1706 * @work: RPC transport to connect
1707 *
1708 * Invoked by a work queue tasklet.
1709 */
1710static void xs_udp_connect_worker6(struct work_struct *work)
1711{
1712 struct sock_xprt *transport =
1713 container_of(work, struct sock_xprt, connect_worker.work);
1714 struct rpc_xprt *xprt = &transport->xprt;
1715 struct socket *sock = transport->sock;
1716 int err, status = -EIO;
1717
Trond Myklebust01d37c42009-03-11 14:09:39 -04001718 if (xprt->shutdown)
Chuck Lever68e220b2007-08-06 11:57:48 -04001719 goto out;
1720
1721 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001722 xs_reset_transport(transport);
Chuck Lever68e220b2007-08-06 11:57:48 -04001723
Chuck Leverfe315e72009-03-11 14:10:21 -04001724 err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
1725 if (err < 0) {
Chuck Lever68e220b2007-08-06 11:57:48 -04001726 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1727 goto out;
1728 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001729 xs_reclassify_socket6(sock);
Chuck Lever68e220b2007-08-06 11:57:48 -04001730
1731 if (xs_bind6(transport, sock) < 0) {
1732 sock_release(sock);
1733 goto out;
1734 }
1735
Chuck Leverc740eff2009-08-09 15:09:46 -04001736 dprintk("RPC: worker connecting xprt %p via %s to "
1737 "%s (port %s)\n", xprt,
1738 xprt->address_strings[RPC_DISPLAY_PROTO],
1739 xprt->address_strings[RPC_DISPLAY_ADDR],
1740 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04001741
1742 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001743 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001744out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001745 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001746 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001747}
1748
Chuck Lever3167e122005-08-25 16:25:55 -07001749/*
1750 * We need to preserve the port number so the reply cache on the server can
1751 * find our cached RPC replies when we get around to reconnecting.
1752 */
Trond Myklebust40d25492009-03-11 14:37:58 -04001753static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07001754{
1755 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07001756 struct sockaddr any;
1757
Chuck Lever46121cf2007-01-31 12:14:08 -05001758 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
Chuck Lever3167e122005-08-25 16:25:55 -07001759
1760 /*
1761 * Disconnect the transport socket by doing a connect operation
1762 * with AF_UNSPEC. This should return immediately...
1763 */
1764 memset(&any, 0, sizeof(any));
1765 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001766 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001767 if (!result)
1768 xs_sock_mark_closed(xprt);
1769 else
Chuck Lever46121cf2007-01-31 12:14:08 -05001770 dprintk("RPC: AF_UNSPEC connect return code %d\n",
Chuck Lever3167e122005-08-25 16:25:55 -07001771 result);
1772}
1773
Trond Myklebust40d25492009-03-11 14:37:58 -04001774static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
1775{
1776 unsigned int state = transport->inet->sk_state;
1777
1778 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED)
1779 return;
1780 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT))
1781 return;
1782 xs_abort_connection(xprt, transport);
1783}
1784
Chuck Lever16be2d22007-08-06 11:57:38 -04001785static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001786{
Chuck Lever16be2d22007-08-06 11:57:38 -04001787 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001788
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001789 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001790 struct sock *sk = sock->sk;
1791
1792 write_lock_bh(&sk->sk_callback_lock);
1793
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001794 xs_save_old_callbacks(transport, sk);
1795
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001796 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001797 sk->sk_data_ready = xs_tcp_data_ready;
1798 sk->sk_state_change = xs_tcp_state_change;
1799 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001800 sk->sk_error_report = xs_error_report;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001801 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07001802
1803 /* socket options */
1804 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1805 sock_reset_flag(sk, SOCK_LINGER);
1806 tcp_sk(sk)->linger2 = 0;
1807 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001808
1809 xprt_clear_connected(xprt);
1810
1811 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001812 transport->sock = sock;
1813 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001814
1815 write_unlock_bh(&sk->sk_callback_lock);
1816 }
1817
Trond Myklebust01d37c42009-03-11 14:09:39 -04001818 if (!xprt_bound(xprt))
1819 return -ENOTCONN;
1820
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001821 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05001822 xprt->stat.connect_count++;
1823 xprt->stat.connect_start = jiffies;
Chuck Lever95392c52007-08-06 11:57:58 -04001824 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
Chuck Lever16be2d22007-08-06 11:57:38 -04001825}
1826
1827/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001828 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1829 * @xprt: RPC transport to connect
1830 * @transport: socket transport to connect
1831 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04001832 *
1833 * Invoked by a work queue tasklet.
1834 */
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001835static void xs_tcp_setup_socket(struct rpc_xprt *xprt,
1836 struct sock_xprt *transport,
1837 struct socket *(*create_sock)(struct rpc_xprt *,
1838 struct sock_xprt *))
Chuck Lever16be2d22007-08-06 11:57:38 -04001839{
Chuck Lever16be2d22007-08-06 11:57:38 -04001840 struct socket *sock = transport->sock;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001841 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04001842
Trond Myklebust01d37c42009-03-11 14:09:39 -04001843 if (xprt->shutdown)
Chuck Lever16be2d22007-08-06 11:57:38 -04001844 goto out;
1845
1846 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001847 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001848 sock = create_sock(xprt, transport);
1849 if (IS_ERR(sock)) {
1850 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04001851 goto out;
1852 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001853 } else {
1854 int abort_and_exit;
1855
1856 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
1857 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04001858 /* "close" the socket, preserving the local port */
Trond Myklebust40d25492009-03-11 14:37:58 -04001859 xs_tcp_reuse_connection(xprt, transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04001860
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001861 if (abort_and_exit)
1862 goto out_eagain;
1863 }
1864
Chuck Leverc740eff2009-08-09 15:09:46 -04001865 dprintk("RPC: worker connecting xprt %p via %s to "
1866 "%s (port %s)\n", xprt,
1867 xprt->address_strings[RPC_DISPLAY_PROTO],
1868 xprt->address_strings[RPC_DISPLAY_ADDR],
1869 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04001870
1871 status = xs_tcp_finish_connecting(xprt, sock);
Chuck Lever46121cf2007-01-31 12:14:08 -05001872 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1873 xprt, -status, xprt_connected(xprt),
1874 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04001875 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04001876 default:
1877 printk("%s: connect returned unhandled error %d\n",
1878 __func__, status);
1879 case -EADDRNOTAVAIL:
1880 /* We're probably in TIME_WAIT. Get rid of existing socket,
1881 * and retry
1882 */
1883 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1884 xprt_force_disconnect(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07001885 break;
Trond Myklebust8a2cec22009-03-11 14:38:01 -04001886 case -ECONNREFUSED:
1887 case -ECONNRESET:
1888 case -ENETUNREACH:
1889 /* retry with existing socket, after a delay */
Trond Myklebust2a491992009-03-11 14:38:00 -04001890 case 0:
1891 case -EINPROGRESS:
1892 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001893 xprt_clear_connecting(xprt);
1894 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05001895 case -EINVAL:
1896 /* Happens, for instance, if the user specified a link
1897 * local IPv6 address without a scope-id.
1898 */
1899 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001900 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001901out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04001902 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04001903out:
Chuck Lever2226feb2005-08-11 16:25:38 -04001904 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001905 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001906}
1907
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001908static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt,
1909 struct sock_xprt *transport)
1910{
1911 struct socket *sock;
1912 int err;
1913
1914 /* start from scratch */
1915 err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1916 if (err < 0) {
1917 dprintk("RPC: can't create TCP transport socket (%d).\n",
1918 -err);
1919 goto out_err;
1920 }
1921 xs_reclassify_socket4(sock);
1922
1923 if (xs_bind4(transport, sock) < 0) {
1924 sock_release(sock);
1925 goto out_err;
1926 }
1927 return sock;
1928out_err:
1929 return ERR_PTR(-EIO);
1930}
1931
1932/**
Chuck Levera246b012005-08-11 16:25:23 -04001933 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1934 * @work: RPC transport to connect
1935 *
1936 * Invoked by a work queue tasklet.
1937 */
1938static void xs_tcp_connect_worker4(struct work_struct *work)
1939{
1940 struct sock_xprt *transport =
1941 container_of(work, struct sock_xprt, connect_worker.work);
1942 struct rpc_xprt *xprt = &transport->xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001943
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001944 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4);
1945}
Chuck Levera246b012005-08-11 16:25:23 -04001946
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001947static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt,
1948 struct sock_xprt *transport)
1949{
1950 struct socket *sock;
1951 int err;
Chuck Lever9903cd12005-08-11 16:25:26 -04001952
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001953 /* start from scratch */
1954 err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock);
1955 if (err < 0) {
1956 dprintk("RPC: can't create TCP transport socket (%d).\n",
1957 -err);
1958 goto out_err;
Chuck Levera246b012005-08-11 16:25:23 -04001959 }
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001960 xs_reclassify_socket6(sock);
1961
1962 if (xs_bind6(transport, sock) < 0) {
1963 sock_release(sock);
1964 goto out_err;
1965 }
1966 return sock;
1967out_err:
1968 return ERR_PTR(-EIO);
Chuck Levera246b012005-08-11 16:25:23 -04001969}
1970
Chuck Lever9903cd12005-08-11 16:25:26 -04001971/**
Chuck Lever68e220b2007-08-06 11:57:48 -04001972 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
1973 * @work: RPC transport to connect
1974 *
1975 * Invoked by a work queue tasklet.
1976 */
1977static void xs_tcp_connect_worker6(struct work_struct *work)
1978{
1979 struct sock_xprt *transport =
1980 container_of(work, struct sock_xprt, connect_worker.work);
1981 struct rpc_xprt *xprt = &transport->xprt;
Chuck Lever68e220b2007-08-06 11:57:48 -04001982
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001983 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6);
Chuck Lever68e220b2007-08-06 11:57:48 -04001984}
1985
1986/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001987 * xs_connect - connect a socket to a remote endpoint
1988 * @task: address of RPC task that manages state of connect request
1989 *
1990 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07001991 *
1992 * UDP socket connects are synchronous, but we use a work queue anyway
1993 * to guarantee that even unprivileged user processes can set up a
1994 * socket on a privileged port.
1995 *
1996 * If a UDP socket connect fails, the delay behavior here prevents
1997 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04001998 */
1999static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002000{
2001 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002002 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002003
Chuck Lever09a21c42009-12-03 15:58:56 -05002004 if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002005 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2006 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002007 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002008 queue_delayed_work(rpciod_workqueue,
2009 &transport->connect_worker,
2010 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07002011 xprt->reestablish_timeout <<= 1;
Neil Brown61d0a8e2009-09-23 14:36:37 -04002012 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2013 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002014 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2015 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002016 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05002017 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002018 queue_delayed_work(rpciod_workqueue,
2019 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002020 }
2021}
2022
Chuck Lever262ca072006-03-20 13:44:16 -05002023/**
2024 * xs_udp_print_stats - display UDP socket-specifc stats
2025 * @xprt: rpc_xprt struct containing statistics
2026 * @seq: output file
2027 *
2028 */
2029static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2030{
Chuck Leverc8475462006-12-05 16:35:26 -05002031 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2032
Chuck Lever262ca072006-03-20 13:44:16 -05002033 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002034 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002035 xprt->stat.bind_count,
2036 xprt->stat.sends,
2037 xprt->stat.recvs,
2038 xprt->stat.bad_xids,
2039 xprt->stat.req_u,
2040 xprt->stat.bklog_u);
2041}
2042
2043/**
2044 * xs_tcp_print_stats - display TCP socket-specifc stats
2045 * @xprt: rpc_xprt struct containing statistics
2046 * @seq: output file
2047 *
2048 */
2049static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2050{
Chuck Leverc8475462006-12-05 16:35:26 -05002051 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002052 long idle_time = 0;
2053
2054 if (xprt_connected(xprt))
2055 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2056
2057 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002058 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002059 xprt->stat.bind_count,
2060 xprt->stat.connect_count,
2061 xprt->stat.connect_time,
2062 idle_time,
2063 xprt->stat.sends,
2064 xprt->stat.recvs,
2065 xprt->stat.bad_xids,
2066 xprt->stat.req_u,
2067 xprt->stat.bklog_u);
2068}
2069
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002070/*
2071 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2072 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2073 * to use the server side send routines.
2074 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002075static void *bc_malloc(struct rpc_task *task, size_t size)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002076{
2077 struct page *page;
2078 struct rpc_buffer *buf;
2079
2080 BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2081 page = alloc_page(GFP_KERNEL);
2082
2083 if (!page)
2084 return NULL;
2085
2086 buf = page_address(page);
2087 buf->len = PAGE_SIZE;
2088
2089 return buf->data;
2090}
2091
2092/*
2093 * Free the space allocated in the bc_alloc routine
2094 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002095static void bc_free(void *buffer)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002096{
2097 struct rpc_buffer *buf;
2098
2099 if (!buffer)
2100 return;
2101
2102 buf = container_of(buffer, struct rpc_buffer, data);
2103 free_page((unsigned long)buf);
2104}
2105
2106/*
2107 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2108 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2109 */
2110static int bc_sendto(struct rpc_rqst *req)
2111{
2112 int len;
2113 struct xdr_buf *xbufp = &req->rq_snd_buf;
2114 struct rpc_xprt *xprt = req->rq_xprt;
2115 struct sock_xprt *transport =
2116 container_of(xprt, struct sock_xprt, xprt);
2117 struct socket *sock = transport->sock;
2118 unsigned long headoff;
2119 unsigned long tailoff;
2120
2121 /*
2122 * Set up the rpc header and record marker stuff
2123 */
2124 xs_encode_tcp_record_marker(xbufp);
2125
2126 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2127 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2128 len = svc_send_common(sock, xbufp,
2129 virt_to_page(xbufp->head[0].iov_base), headoff,
2130 xbufp->tail[0].iov_base, tailoff);
2131
2132 if (len != xbufp->len) {
2133 printk(KERN_NOTICE "Error sending entire callback!\n");
2134 len = -EAGAIN;
2135 }
2136
2137 return len;
2138}
2139
2140/*
2141 * The send routine. Borrows from svc_send
2142 */
2143static int bc_send_request(struct rpc_task *task)
2144{
2145 struct rpc_rqst *req = task->tk_rqstp;
2146 struct svc_xprt *xprt;
2147 struct svc_sock *svsk;
2148 u32 len;
2149
2150 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2151 /*
2152 * Get the server socket associated with this callback xprt
2153 */
2154 xprt = req->rq_xprt->bc_xprt;
2155 svsk = container_of(xprt, struct svc_sock, sk_xprt);
2156
2157 /*
2158 * Grab the mutex to serialize data as the connection is shared
2159 * with the fore channel
2160 */
2161 if (!mutex_trylock(&xprt->xpt_mutex)) {
2162 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2163 if (!mutex_trylock(&xprt->xpt_mutex))
2164 return -EAGAIN;
2165 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2166 }
2167 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2168 len = -ENOTCONN;
2169 else
2170 len = bc_sendto(req);
2171 mutex_unlock(&xprt->xpt_mutex);
2172
2173 if (len > 0)
2174 len = 0;
2175
2176 return len;
2177}
2178
2179/*
2180 * The close routine. Since this is client initiated, we do nothing
2181 */
2182
2183static void bc_close(struct rpc_xprt *xprt)
2184{
2185 return;
2186}
2187
2188/*
2189 * The xprt destroy routine. Again, because this connection is client
2190 * initiated, we do nothing
2191 */
2192
2193static void bc_destroy(struct rpc_xprt *xprt)
2194{
2195 return;
2196}
2197
Chuck Lever262965f2005-08-11 16:25:56 -04002198static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002199 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002200 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002201 .release_xprt = xprt_release_xprt_cong,
Chuck Lever45160d62007-07-01 12:13:17 -04002202 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002203 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002204 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002205 .buf_alloc = rpc_malloc,
2206 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002207 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002208 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002209 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002210 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002211 .close = xs_close,
2212 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002213 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04002214};
2215
2216static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002217 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002218 .release_xprt = xs_tcp_release_xprt,
Chuck Lever45160d62007-07-01 12:13:17 -04002219 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002220 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002221 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002222 .buf_alloc = rpc_malloc,
2223 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002224 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002225 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustf75e6742009-04-21 17:18:20 -04002226 .close = xs_tcp_close,
Chuck Lever9903cd12005-08-11 16:25:26 -04002227 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002228 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002229};
2230
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002231/*
2232 * The rpc_xprt_ops for the server backchannel
2233 */
2234
2235static struct rpc_xprt_ops bc_tcp_ops = {
2236 .reserve_xprt = xprt_reserve_xprt,
2237 .release_xprt = xprt_release_xprt,
2238 .buf_alloc = bc_malloc,
2239 .buf_free = bc_free,
2240 .send_request = bc_send_request,
2241 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2242 .close = bc_close,
2243 .destroy = bc_destroy,
2244 .print_stats = xs_tcp_print_stats,
2245};
2246
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002247static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002248 unsigned int slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002249{
2250 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002251 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002252
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002253 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002254 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002255 return ERR_PTR(-EBADF);
2256 }
2257
Chuck Leverffc2e512006-12-05 16:35:11 -05002258 new = kzalloc(sizeof(*new), GFP_KERNEL);
2259 if (new == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002260 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2261 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002262 return ERR_PTR(-ENOMEM);
2263 }
Chuck Leverffc2e512006-12-05 16:35:11 -05002264 xprt = &new->xprt;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002265
2266 xprt->max_reqs = slot_table_size;
2267 xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
2268 if (xprt->slot == NULL) {
2269 kfree(xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05002270 dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
2271 "table\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002272 return ERR_PTR(-ENOMEM);
2273 }
2274
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002275 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2276 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002277 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002278 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002279
2280 return xprt;
2281}
2282
Trond Myklebust2881ae72007-12-20 16:03:54 -05002283static const struct rpc_timeout xs_udp_default_timeout = {
2284 .to_initval = 5 * HZ,
2285 .to_maxval = 30 * HZ,
2286 .to_increment = 5 * HZ,
2287 .to_retries = 5,
2288};
2289
Chuck Lever9903cd12005-08-11 16:25:26 -04002290/**
2291 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002292 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002293 *
2294 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002295static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002296{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002297 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002298 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002299 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04002300
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002301 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002302 if (IS_ERR(xprt))
2303 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002304 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002305
Chuck Leverec739ef2006-08-22 20:06:15 -04002306 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002307 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002308 /* XXX: header size can vary due to auth type, IPv6, etc. */
2309 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2310
Chuck Lever03bf4b72005-08-25 16:25:55 -07002311 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002312 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2313 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002314
Chuck Lever262965f2005-08-11 16:25:56 -04002315 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002316
Trond Myklebustba7392b2007-12-20 16:03:55 -05002317 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002318
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002319 switch (addr->sa_family) {
2320 case AF_INET:
2321 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2322 xprt_set_bound(xprt);
2323
2324 INIT_DELAYED_WORK(&transport->connect_worker,
2325 xs_udp_connect_worker4);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002326 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002327 break;
2328 case AF_INET6:
2329 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2330 xprt_set_bound(xprt);
2331
2332 INIT_DELAYED_WORK(&transport->connect_worker,
2333 xs_udp_connect_worker6);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002334 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002335 break;
2336 default:
2337 kfree(xprt);
2338 return ERR_PTR(-EAFNOSUPPORT);
2339 }
2340
Chuck Leverc740eff2009-08-09 15:09:46 -04002341 if (xprt_bound(xprt))
2342 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2343 xprt->address_strings[RPC_DISPLAY_ADDR],
2344 xprt->address_strings[RPC_DISPLAY_PORT],
2345 xprt->address_strings[RPC_DISPLAY_PROTO]);
2346 else
2347 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2348 xprt->address_strings[RPC_DISPLAY_ADDR],
2349 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002350
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002351 if (try_module_get(THIS_MODULE))
2352 return xprt;
2353
2354 kfree(xprt->slot);
2355 kfree(xprt);
2356 return ERR_PTR(-EINVAL);
Chuck Levera246b012005-08-11 16:25:23 -04002357}
2358
Trond Myklebust2881ae72007-12-20 16:03:54 -05002359static const struct rpc_timeout xs_tcp_default_timeout = {
2360 .to_initval = 60 * HZ,
2361 .to_maxval = 60 * HZ,
2362 .to_retries = 2,
2363};
2364
Chuck Lever9903cd12005-08-11 16:25:26 -04002365/**
2366 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002367 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002368 *
2369 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002370static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002371{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002372 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002373 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002374 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04002375
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002376 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002377 if (IS_ERR(xprt))
2378 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002379 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002380
Chuck Leverec739ef2006-08-22 20:06:15 -04002381 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002382 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002383 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002384
Chuck Lever03bf4b72005-08-25 16:25:55 -07002385 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002386 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2387 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002388
Chuck Lever262965f2005-08-11 16:25:56 -04002389 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002390 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002391
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002392 switch (addr->sa_family) {
2393 case AF_INET:
2394 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2395 xprt_set_bound(xprt);
2396
Chuck Lever9dc3b092009-08-09 15:09:46 -04002397 INIT_DELAYED_WORK(&transport->connect_worker,
2398 xs_tcp_connect_worker4);
2399 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002400 break;
2401 case AF_INET6:
2402 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2403 xprt_set_bound(xprt);
2404
Chuck Lever9dc3b092009-08-09 15:09:46 -04002405 INIT_DELAYED_WORK(&transport->connect_worker,
2406 xs_tcp_connect_worker6);
2407 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002408 break;
2409 default:
2410 kfree(xprt);
2411 return ERR_PTR(-EAFNOSUPPORT);
2412 }
2413
Chuck Leverc740eff2009-08-09 15:09:46 -04002414 if (xprt_bound(xprt))
2415 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2416 xprt->address_strings[RPC_DISPLAY_ADDR],
2417 xprt->address_strings[RPC_DISPLAY_PORT],
2418 xprt->address_strings[RPC_DISPLAY_PROTO]);
2419 else
2420 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2421 xprt->address_strings[RPC_DISPLAY_ADDR],
2422 xprt->address_strings[RPC_DISPLAY_PROTO]);
2423
Chuck Leveredb267a2006-08-22 20:06:18 -04002424
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002425 if (try_module_get(THIS_MODULE))
2426 return xprt;
2427
2428 kfree(xprt->slot);
2429 kfree(xprt);
2430 return ERR_PTR(-EINVAL);
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;
2444
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002445 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2446 if (IS_ERR(xprt))
2447 return xprt;
2448 transport = container_of(xprt, struct sock_xprt, xprt);
2449
2450 xprt->prot = IPPROTO_TCP;
2451 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2452 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2453 xprt->timeout = &xs_tcp_default_timeout;
2454
2455 /* backchannel */
2456 xprt_set_bound(xprt);
2457 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002458 xprt->reestablish_timeout = 0;
2459 xprt->idle_timeout = 0;
2460
2461 /*
2462 * The backchannel uses the same socket connection as the
2463 * forechannel
2464 */
2465 xprt->bc_xprt = args->bc_xprt;
2466 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2467 bc_sock->sk_bc_xprt = xprt;
2468 transport->sock = bc_sock->sk_sock;
2469 transport->inet = bc_sock->sk_sk;
2470
2471 xprt->ops = &bc_tcp_ops;
2472
2473 switch (addr->sa_family) {
2474 case AF_INET:
2475 xs_format_peer_addresses(xprt, "tcp",
2476 RPCBIND_NETID_TCP);
2477 break;
2478 case AF_INET6:
2479 xs_format_peer_addresses(xprt, "tcp",
2480 RPCBIND_NETID_TCP6);
2481 break;
2482 default:
2483 kfree(xprt);
2484 return ERR_PTR(-EAFNOSUPPORT);
2485 }
2486
2487 if (xprt_bound(xprt))
2488 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2489 xprt->address_strings[RPC_DISPLAY_ADDR],
2490 xprt->address_strings[RPC_DISPLAY_PORT],
2491 xprt->address_strings[RPC_DISPLAY_PROTO]);
2492 else
2493 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2494 xprt->address_strings[RPC_DISPLAY_ADDR],
2495 xprt->address_strings[RPC_DISPLAY_PROTO]);
2496
2497 /*
2498 * Since we don't want connections for the backchannel, we set
2499 * the xprt status to connected
2500 */
2501 xprt_set_connected(xprt);
2502
2503
2504 if (try_module_get(THIS_MODULE))
2505 return xprt;
2506 kfree(xprt->slot);
2507 kfree(xprt);
2508 return ERR_PTR(-EINVAL);
2509}
2510
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002511static struct xprt_class xs_udp_transport = {
2512 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2513 .name = "udp",
2514 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002515 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002516 .setup = xs_setup_udp,
2517};
2518
2519static struct xprt_class xs_tcp_transport = {
2520 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2521 .name = "tcp",
2522 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002523 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002524 .setup = xs_setup_tcp,
2525};
2526
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002527static struct xprt_class xs_bc_tcp_transport = {
2528 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2529 .name = "tcp NFSv4.1 backchannel",
2530 .owner = THIS_MODULE,
2531 .ident = XPRT_TRANSPORT_BC_TCP,
2532 .setup = xs_setup_bc_tcp,
2533};
2534
Chuck Lever282b32e2006-12-05 16:35:51 -05002535/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002536 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002537 *
2538 */
2539int init_socket_xprt(void)
2540{
Chuck Leverfbf76682006-12-05 16:35:54 -05002541#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002542 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002543 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002544#endif
2545
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002546 xprt_register_transport(&xs_udp_transport);
2547 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002548 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002549
Chuck Lever282b32e2006-12-05 16:35:51 -05002550 return 0;
2551}
2552
2553/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002554 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05002555 *
2556 */
2557void cleanup_socket_xprt(void)
2558{
Chuck Leverfbf76682006-12-05 16:35:54 -05002559#ifdef RPC_DEBUG
2560 if (sunrpc_table_header) {
2561 unregister_sysctl_table(sunrpc_table_header);
2562 sunrpc_table_header = NULL;
2563 }
2564#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002565
2566 xprt_unregister_transport(&xs_udp_transport);
2567 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002568 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05002569}
Trond Myklebustcbf11072009-08-09 15:06:19 -04002570
2571static int param_set_uint_minmax(const char *val, struct kernel_param *kp,
2572 unsigned int min, unsigned int max)
2573{
2574 unsigned long num;
2575 int ret;
2576
2577 if (!val)
2578 return -EINVAL;
2579 ret = strict_strtoul(val, 0, &num);
2580 if (ret == -EINVAL || num < min || num > max)
2581 return -EINVAL;
2582 *((unsigned int *)kp->arg) = num;
2583 return 0;
2584}
2585
2586static int param_set_portnr(const char *val, struct kernel_param *kp)
2587{
2588 return param_set_uint_minmax(val, kp,
2589 RPC_MIN_RESVPORT,
2590 RPC_MAX_RESVPORT);
2591}
2592
2593static int param_get_portnr(char *buffer, struct kernel_param *kp)
2594{
2595 return param_get_uint(buffer, kp);
2596}
2597#define param_check_portnr(name, p) \
2598 __param_check(name, p, unsigned int);
2599
2600module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
2601module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
2602
2603static int param_set_slot_table_size(const char *val, struct kernel_param *kp)
2604{
2605 return param_set_uint_minmax(val, kp,
2606 RPC_MIN_SLOT_TABLE,
2607 RPC_MAX_SLOT_TABLE);
2608}
2609
2610static int param_get_slot_table_size(char *buffer, struct kernel_param *kp)
2611{
2612 return param_get_uint(buffer, kp);
2613}
2614#define param_check_slot_table_size(name, p) \
2615 __param_check(name, p, unsigned int);
2616
2617module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
2618 slot_table_size, 0644);
2619module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
2620 slot_table_size, 0644);
2621