blob: d7f97ef265904f0ddc0d691360f1e920dc63210b [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>
Chuck Lever176e21e2011-05-09 15:22:44 -040022#include <linux/string.h>
Chuck Levera246b012005-08-11 16:25:23 -040023#include <linux/slab.h>
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -040024#include <linux/module.h>
Chuck Levera246b012005-08-11 16:25:23 -040025#include <linux/capability.h>
Chuck Levera246b012005-08-11 16:25:23 -040026#include <linux/pagemap.h>
27#include <linux/errno.h>
28#include <linux/socket.h>
29#include <linux/in.h>
30#include <linux/net.h>
31#include <linux/mm.h>
Chuck Lever176e21e2011-05-09 15:22:44 -040032#include <linux/un.h>
Chuck Levera246b012005-08-11 16:25:23 -040033#include <linux/udp.h>
34#include <linux/tcp.h>
35#include <linux/sunrpc/clnt.h>
Chuck Lever02107142006-01-03 09:55:49 +010036#include <linux/sunrpc/sched.h>
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030037#include <linux/sunrpc/svcsock.h>
\"Talpey, Thomas\49c36fc2007-09-10 13:47:31 -040038#include <linux/sunrpc/xprtsock.h>
Chuck Levera246b012005-08-11 16:25:23 -040039#include <linux/file.h>
Trond Myklebust9e00abc2011-07-13 19:20:49 -040040#ifdef CONFIG_SUNRPC_BACKCHANNEL
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -040041#include <linux/sunrpc/bc_xprt.h>
42#endif
Chuck Levera246b012005-08-11 16:25:23 -040043
44#include <net/sock.h>
45#include <net/checksum.h>
46#include <net/udp.h>
47#include <net/tcp.h>
48
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030049#include "sunrpc.h"
Chuck Lever176e21e2011-05-09 15:22:44 -040050
51static void xs_close(struct rpc_xprt *xprt);
52
Chuck Lever9903cd12005-08-11 16:25:26 -040053/*
Chuck Leverc556b752005-11-01 12:24:48 -050054 * xprtsock tunables
55 */
56unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
Trond Myklebustd9ba1312011-07-17 18:11:30 -040057unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
58unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
Chuck Leverc556b752005-11-01 12:24:48 -050059
60unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
61unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
62
Trond Myklebust7d1e8252009-03-11 14:38:03 -040063#define XS_TCP_LINGER_TO (15U * HZ)
Trond Myklebust25fe6142009-03-11 14:38:03 -040064static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
Trond Myklebust7d1e8252009-03-11 14:38:03 -040065
Chuck Leverc556b752005-11-01 12:24:48 -050066/*
Chuck Leverfbf76682006-12-05 16:35:54 -050067 * We can register our own files under /proc/sys/sunrpc by
68 * calling register_sysctl_table() again. The files in that
69 * directory become the union of all files registered there.
70 *
71 * We simply need to make sure that we don't collide with
72 * someone else's file names!
73 */
74
75#ifdef RPC_DEBUG
76
77static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
78static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
Trond Myklebustd9ba1312011-07-17 18:11:30 -040079static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Leverfbf76682006-12-05 16:35:54 -050080static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
81static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
82
83static struct ctl_table_header *sunrpc_table_header;
84
85/*
86 * FIXME: changing the UDP slot table size should also resize the UDP
87 * socket buffers for existing UDP transports
88 */
89static ctl_table xs_tunables_table[] = {
90 {
Chuck Leverfbf76682006-12-05 16:35:54 -050091 .procname = "udp_slot_table_entries",
92 .data = &xprt_udp_slot_table_entries,
93 .maxlen = sizeof(unsigned int),
94 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080095 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -050096 .extra1 = &min_slot_table_size,
97 .extra2 = &max_slot_table_size
98 },
99 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500100 .procname = "tcp_slot_table_entries",
101 .data = &xprt_tcp_slot_table_entries,
102 .maxlen = sizeof(unsigned int),
103 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800104 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500105 .extra1 = &min_slot_table_size,
106 .extra2 = &max_slot_table_size
107 },
108 {
Trond Myklebustd9ba1312011-07-17 18:11:30 -0400109 .procname = "tcp_max_slot_table_entries",
110 .data = &xprt_max_tcp_slot_table_entries,
111 .maxlen = sizeof(unsigned int),
112 .mode = 0644,
113 .proc_handler = proc_dointvec_minmax,
114 .extra1 = &min_slot_table_size,
115 .extra2 = &max_tcp_slot_table_limit
116 },
117 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500118 .procname = "min_resvport",
119 .data = &xprt_min_resvport,
120 .maxlen = sizeof(unsigned int),
121 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800122 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500123 .extra1 = &xprt_min_resvport_limit,
124 .extra2 = &xprt_max_resvport_limit
125 },
126 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500127 .procname = "max_resvport",
128 .data = &xprt_max_resvport,
129 .maxlen = sizeof(unsigned int),
130 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800131 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500132 .extra1 = &xprt_min_resvport_limit,
133 .extra2 = &xprt_max_resvport_limit
134 },
135 {
Trond Myklebust25fe6142009-03-11 14:38:03 -0400136 .procname = "tcp_fin_timeout",
137 .data = &xs_tcp_fin_timeout,
138 .maxlen = sizeof(xs_tcp_fin_timeout),
139 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800140 .proc_handler = proc_dointvec_jiffies,
Trond Myklebust25fe6142009-03-11 14:38:03 -0400141 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800142 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500143};
144
145static ctl_table sunrpc_table[] = {
146 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500147 .procname = "sunrpc",
148 .mode = 0555,
149 .child = xs_tunables_table
150 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800151 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500152};
153
154#endif
155
156/*
Chuck Lever03bf4b72005-08-25 16:25:55 -0700157 * Wait duration for a reply from the RPC portmapper.
158 */
159#define XS_BIND_TO (60U * HZ)
160
161/*
162 * Delay if a UDP socket connect error occurs. This is most likely some
163 * kind of resource problem on the local host.
164 */
165#define XS_UDP_REEST_TO (2U * HZ)
166
167/*
168 * The reestablish timeout allows clients to delay for a bit before attempting
169 * to reconnect to a server that just dropped our connection.
170 *
171 * We implement an exponential backoff when trying to reestablish a TCP
172 * transport connection with the server. Some servers like to drop a TCP
173 * connection when they are overworked, so we start with a short timeout and
174 * increase over time if the server is down or not responding.
175 */
176#define XS_TCP_INIT_REEST_TO (3U * HZ)
177#define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
178
179/*
180 * TCP idle timeout; client drops the transport socket if it is idle
181 * for this long. Note that we also timeout UDP sockets to prevent
182 * holding port numbers when there is no RPC traffic.
183 */
184#define XS_IDLE_DISC_TO (5U * 60 * HZ)
185
Chuck Levera246b012005-08-11 16:25:23 -0400186#ifdef RPC_DEBUG
187# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400188# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400189#endif
190
Chuck Levera246b012005-08-11 16:25:23 -0400191#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400192static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400193{
Chuck Lever9903cd12005-08-11 16:25:26 -0400194 u8 *buf = (u8 *) packet;
195 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400196
Chuck Lever46121cf2007-01-31 12:14:08 -0500197 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400198 for (j = 0; j < count && j < 128; j += 4) {
199 if (!(j & 31)) {
200 if (j)
201 dprintk("\n");
202 dprintk("0x%04x ", j);
203 }
204 dprintk("%02x%02x%02x%02x ",
205 buf[j], buf[j+1], buf[j+2], buf[j+3]);
206 }
207 dprintk("\n");
208}
209#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400210static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400211{
212 /* NOP */
213}
214#endif
215
Chuck Leverffc2e512006-12-05 16:35:11 -0500216struct sock_xprt {
217 struct rpc_xprt xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500218
219 /*
220 * Network layer
221 */
222 struct socket * sock;
223 struct sock * inet;
Chuck Lever51971132006-12-05 16:35:19 -0500224
225 /*
226 * State of TCP reply receive
227 */
228 __be32 tcp_fraghdr,
Trond Myklebustb76ce562010-06-16 13:57:32 -0400229 tcp_xid,
230 tcp_calldir;
Chuck Lever51971132006-12-05 16:35:19 -0500231
232 u32 tcp_offset,
233 tcp_reclen;
234
235 unsigned long tcp_copied,
236 tcp_flags;
Chuck Leverc8475462006-12-05 16:35:26 -0500237
238 /*
239 * Connection of transports
240 */
Trond Myklebust34161db2006-12-07 15:48:15 -0500241 struct delayed_work connect_worker;
Chuck Leverfbfffbd2009-08-09 15:09:46 -0400242 struct sockaddr_storage srcaddr;
243 unsigned short srcport;
Chuck Lever7c6e0662006-12-05 16:35:30 -0500244
245 /*
246 * UDP socket buffer size parameters
247 */
248 size_t rcvsize,
249 sndsize;
Chuck Lever314dfd72006-12-05 16:35:34 -0500250
251 /*
252 * Saved socket callback addresses
253 */
254 void (*old_data_ready)(struct sock *, int);
255 void (*old_state_change)(struct sock *);
256 void (*old_write_space)(struct sock *);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400257 void (*old_error_report)(struct sock *);
Chuck Leverffc2e512006-12-05 16:35:11 -0500258};
259
Chuck Levere136d092006-12-05 16:35:23 -0500260/*
261 * TCP receive state flags
262 */
263#define TCP_RCV_LAST_FRAG (1UL << 0)
264#define TCP_RCV_COPY_FRAGHDR (1UL << 1)
265#define TCP_RCV_COPY_XID (1UL << 2)
266#define TCP_RCV_COPY_DATA (1UL << 3)
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400267#define TCP_RCV_READ_CALLDIR (1UL << 4)
268#define TCP_RCV_COPY_CALLDIR (1UL << 5)
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400269
270/*
271 * TCP RPC flags
272 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400273#define TCP_RPC_REPLY (1UL << 6)
Chuck Levere136d092006-12-05 16:35:23 -0500274
Chuck Lever95392c52007-08-06 11:57:58 -0400275static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400276{
Chuck Lever95392c52007-08-06 11:57:58 -0400277 return (struct sockaddr *) &xprt->addr;
278}
279
Chuck Lever176e21e2011-05-09 15:22:44 -0400280static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
281{
282 return (struct sockaddr_un *) &xprt->addr;
283}
284
Chuck Lever95392c52007-08-06 11:57:58 -0400285static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
286{
287 return (struct sockaddr_in *) &xprt->addr;
288}
289
290static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
291{
292 return (struct sockaddr_in6 *) &xprt->addr;
293}
294
Chuck Leverc877b842009-08-09 15:09:36 -0400295static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400296{
Chuck Leverc877b842009-08-09 15:09:36 -0400297 struct sockaddr *sap = xs_addr(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400298 struct sockaddr_in6 *sin6;
299 struct sockaddr_in *sin;
Chuck Lever176e21e2011-05-09 15:22:44 -0400300 struct sockaddr_un *sun;
Chuck Leverc877b842009-08-09 15:09:36 -0400301 char buf[128];
Chuck Leveredb267a2006-08-22 20:06:18 -0400302
Chuck Lever9dc3b092009-08-09 15:09:46 -0400303 switch (sap->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400304 case AF_LOCAL:
305 sun = xs_addr_un(xprt);
306 strlcpy(buf, sun->sun_path, sizeof(buf));
307 xprt->address_strings[RPC_DISPLAY_ADDR] =
308 kstrdup(buf, GFP_KERNEL);
309 break;
Chuck Lever9dc3b092009-08-09 15:09:46 -0400310 case AF_INET:
Chuck Lever176e21e2011-05-09 15:22:44 -0400311 (void)rpc_ntop(sap, buf, sizeof(buf));
312 xprt->address_strings[RPC_DISPLAY_ADDR] =
313 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400314 sin = xs_addr_in(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800315 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
Chuck Lever9dc3b092009-08-09 15:09:46 -0400316 break;
317 case AF_INET6:
Chuck Lever176e21e2011-05-09 15:22:44 -0400318 (void)rpc_ntop(sap, buf, sizeof(buf));
319 xprt->address_strings[RPC_DISPLAY_ADDR] =
320 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400321 sin6 = xs_addr_in6(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800322 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400323 break;
324 default:
325 BUG();
Chuck Leveredb267a2006-08-22 20:06:18 -0400326 }
Chuck Lever176e21e2011-05-09 15:22:44 -0400327
Chuck Lever9dc3b092009-08-09 15:09:46 -0400328 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
Chuck Leveredb267a2006-08-22 20:06:18 -0400329}
330
Chuck Lever9dc3b092009-08-09 15:09:46 -0400331static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400332{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400333 struct sockaddr *sap = xs_addr(xprt);
334 char buf[128];
Chuck Lever4b6473f2007-08-06 11:57:12 -0400335
Joe Perches81160e662010-03-08 12:15:59 -0800336 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400337 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400338
Joe Perches81160e662010-03-08 12:15:59 -0800339 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400340 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
341}
Chuck Lever4b6473f2007-08-06 11:57:12 -0400342
Chuck Lever9dc3b092009-08-09 15:09:46 -0400343static void xs_format_peer_addresses(struct rpc_xprt *xprt,
344 const char *protocol,
345 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400346{
Chuck Leverb454ae92008-01-07 18:34:48 -0500347 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leverb454ae92008-01-07 18:34:48 -0500348 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leverc877b842009-08-09 15:09:36 -0400349 xs_format_common_peer_addresses(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400350 xs_format_common_peer_ports(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400351}
352
Chuck Lever9dc3b092009-08-09 15:09:46 -0400353static void xs_update_peer_port(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400354{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400355 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
356 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400357
Chuck Lever9dc3b092009-08-09 15:09:46 -0400358 xs_format_common_peer_ports(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400359}
360
361static void xs_free_peer_addresses(struct rpc_xprt *xprt)
362{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500363 unsigned int i;
364
365 for (i = 0; i < RPC_DISPLAY_MAX; i++)
366 switch (i) {
367 case RPC_DISPLAY_PROTO:
368 case RPC_DISPLAY_NETID:
369 continue;
370 default:
371 kfree(xprt->address_strings[i]);
372 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400373}
374
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400375#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
376
Trond Myklebust24c56842006-10-17 15:06:22 -0400377static 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 -0400378{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400379 struct msghdr msg = {
380 .msg_name = addr,
381 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400382 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
383 };
384 struct kvec iov = {
385 .iov_base = vec->iov_base + base,
386 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400387 };
388
Trond Myklebust24c56842006-10-17 15:06:22 -0400389 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400390 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
391 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
392}
393
Trond Myklebust24c56842006-10-17 15:06:22 -0400394static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400395{
Trond Myklebust24c56842006-10-17 15:06:22 -0400396 struct page **ppage;
397 unsigned int remainder;
398 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400399
Trond Myklebust24c56842006-10-17 15:06:22 -0400400 remainder = xdr->page_len - base;
401 base += xdr->page_base;
402 ppage = xdr->pages + (base >> PAGE_SHIFT);
403 base &= ~PAGE_MASK;
404 for(;;) {
405 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
406 int flags = XS_SENDMSG_FLAGS;
407
408 remainder -= len;
409 if (remainder != 0 || more)
410 flags |= MSG_MORE;
411 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
412 if (remainder == 0 || err != len)
413 break;
414 sent += err;
415 ppage++;
416 base = 0;
417 }
418 if (sent == 0)
419 return err;
420 if (err > 0)
421 sent += err;
422 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400423}
424
Chuck Lever9903cd12005-08-11 16:25:26 -0400425/**
426 * xs_sendpages - write pages directly to a socket
427 * @sock: socket to send on
428 * @addr: UDP only -- address of destination
429 * @addrlen: UDP only -- length of destination address
430 * @xdr: buffer containing this request
431 * @base: starting position in the buffer
432 *
Chuck Levera246b012005-08-11 16:25:23 -0400433 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400434static 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 -0400435{
Trond Myklebust24c56842006-10-17 15:06:22 -0400436 unsigned int remainder = xdr->len - base;
437 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400438
Chuck Lever262965f2005-08-11 16:25:56 -0400439 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400440 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400441
442 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400443 if (base != 0) {
444 addr = NULL;
445 addrlen = 0;
446 }
Chuck Lever262965f2005-08-11 16:25:56 -0400447
Trond Myklebust24c56842006-10-17 15:06:22 -0400448 if (base < xdr->head[0].iov_len || addr != NULL) {
449 unsigned int len = xdr->head[0].iov_len - base;
450 remainder -= len;
451 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
452 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400453 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400454 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400455 base = 0;
456 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400457 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400458
Trond Myklebust24c56842006-10-17 15:06:22 -0400459 if (base < xdr->page_len) {
460 unsigned int len = xdr->page_len - base;
461 remainder -= len;
462 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
463 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400464 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400465 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400466 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400467 } else
468 base -= xdr->page_len;
469
470 if (base >= xdr->tail[0].iov_len)
471 return sent;
472 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400473out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400474 if (sent == 0)
475 return err;
476 if (err > 0)
477 sent += err;
478 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400479}
480
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400481static void xs_nospace_callback(struct rpc_task *task)
482{
483 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
484
485 transport->inet->sk_write_pending--;
486 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
487}
488
Chuck Lever9903cd12005-08-11 16:25:26 -0400489/**
Chuck Lever262965f2005-08-11 16:25:56 -0400490 * xs_nospace - place task on wait queue if transmit was incomplete
491 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400492 *
Chuck Levera246b012005-08-11 16:25:23 -0400493 */
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400494static int xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400495{
Chuck Lever262965f2005-08-11 16:25:56 -0400496 struct rpc_rqst *req = task->tk_rqstp;
497 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500498 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400499 int ret = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400500
Chuck Lever46121cf2007-01-31 12:14:08 -0500501 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400502 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
503 req->rq_slen);
504
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400505 /* Protect against races with write_space */
506 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400507
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400508 /* Don't race with disconnect */
509 if (xprt_connected(xprt)) {
510 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400511 ret = -EAGAIN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400512 /*
513 * Notify TCP that we're limited by the application
514 * window size
515 */
516 set_bit(SOCK_NOSPACE, &transport->sock->flags);
517 transport->inet->sk_write_pending++;
518 /* ...and wait for more buffer space */
519 xprt_wait_for_buffer_space(task, xs_nospace_callback);
520 }
521 } else {
522 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400523 ret = -ENOTCONN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400524 }
Chuck Lever262965f2005-08-11 16:25:56 -0400525
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400526 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400527 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400528}
529
Chuck Lever61677ee2011-05-09 15:22:34 -0400530/*
531 * Construct a stream transport record marker in @buf.
532 */
533static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
534{
535 u32 reclen = buf->len - sizeof(rpc_fraghdr);
536 rpc_fraghdr *base = buf->head[0].iov_base;
537 *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
538}
539
Chuck Lever262965f2005-08-11 16:25:56 -0400540/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400541 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
542 * @task: RPC task that manages the state of an RPC request
543 *
544 * Return values:
545 * 0: The request has been sent
546 * EAGAIN: The socket was blocked, please call again later to
547 * complete the request
548 * ENOTCONN: Caller needs to invoke connect logic then call again
549 * other: Some other error occured, the request was not sent
550 */
551static int xs_local_send_request(struct rpc_task *task)
552{
553 struct rpc_rqst *req = task->tk_rqstp;
554 struct rpc_xprt *xprt = req->rq_xprt;
555 struct sock_xprt *transport =
556 container_of(xprt, struct sock_xprt, xprt);
557 struct xdr_buf *xdr = &req->rq_snd_buf;
558 int status;
559
560 xs_encode_stream_record_marker(&req->rq_snd_buf);
561
562 xs_pktdump("packet data:",
563 req->rq_svec->iov_base, req->rq_svec->iov_len);
564
565 status = xs_sendpages(transport->sock, NULL, 0,
566 xdr, req->rq_bytes_sent);
567 dprintk("RPC: %s(%u) = %d\n",
568 __func__, xdr->len - req->rq_bytes_sent, status);
569 if (likely(status >= 0)) {
570 req->rq_bytes_sent += status;
571 req->rq_xmit_bytes_sent += status;
572 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
573 req->rq_bytes_sent = 0;
574 return 0;
575 }
576 status = -EAGAIN;
577 }
578
579 switch (status) {
580 case -EAGAIN:
581 status = xs_nospace(task);
582 break;
583 default:
584 dprintk("RPC: sendmsg returned unrecognized error %d\n",
585 -status);
586 case -EPIPE:
587 xs_close(xprt);
588 status = -ENOTCONN;
589 }
590
591 return status;
592}
593
594/**
Chuck Lever262965f2005-08-11 16:25:56 -0400595 * xs_udp_send_request - write an RPC request to a UDP socket
596 * @task: address of RPC task that manages the state of an RPC request
597 *
598 * Return values:
599 * 0: The request has been sent
600 * EAGAIN: The socket was blocked, please call again later to
601 * complete the request
602 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300603 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -0400604 */
605static int xs_udp_send_request(struct rpc_task *task)
606{
607 struct rpc_rqst *req = task->tk_rqstp;
608 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500609 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400610 struct xdr_buf *xdr = &req->rq_snd_buf;
611 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400612
Chuck Lever9903cd12005-08-11 16:25:26 -0400613 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400614 req->rq_svec->iov_base,
615 req->rq_svec->iov_len);
616
Trond Myklebust01d37c42009-03-11 14:09:39 -0400617 if (!xprt_bound(xprt))
618 return -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500619 status = xs_sendpages(transport->sock,
Chuck Lever95392c52007-08-06 11:57:58 -0400620 xs_addr(xprt),
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500621 xprt->addrlen, xdr,
622 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400623
Chuck Lever46121cf2007-01-31 12:14:08 -0500624 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400625 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400626
Trond Myklebust21997002007-10-01 11:43:37 -0400627 if (status >= 0) {
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400628 req->rq_xmit_bytes_sent += status;
Trond Myklebust21997002007-10-01 11:43:37 -0400629 if (status >= req->rq_slen)
630 return 0;
631 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400632 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400633 }
Chuck Levera246b012005-08-11 16:25:23 -0400634
Chuck Lever262965f2005-08-11 16:25:56 -0400635 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400636 case -ENOTSOCK:
637 status = -ENOTCONN;
638 /* Should we call xs_close() here? */
639 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400640 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400641 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400642 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400643 default:
644 dprintk("RPC: sendmsg returned unrecognized error %d\n",
645 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400646 case -ENETUNREACH:
647 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400648 case -ECONNREFUSED:
649 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400650 * prompts ECONNREFUSED. */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400651 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400652 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800653
Chuck Lever262965f2005-08-11 16:25:56 -0400654 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400655}
656
Trond Myklebuste06799f2007-11-05 15:44:12 -0500657/**
658 * xs_tcp_shutdown - gracefully shut down a TCP socket
659 * @xprt: transport
660 *
661 * Initiates a graceful shutdown of the TCP socket by calling the
662 * equivalent of shutdown(SHUT_WR);
663 */
664static void xs_tcp_shutdown(struct rpc_xprt *xprt)
665{
666 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
667 struct socket *sock = transport->sock;
668
669 if (sock != NULL)
670 kernel_sock_shutdown(sock, SHUT_WR);
671}
672
Chuck Lever9903cd12005-08-11 16:25:26 -0400673/**
Chuck Lever262965f2005-08-11 16:25:56 -0400674 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400675 * @task: address of RPC task that manages the state of an RPC request
676 *
677 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400678 * 0: The request has been sent
679 * EAGAIN: The socket was blocked, please call again later to
680 * complete the request
681 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300682 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400683 *
684 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400685 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400686 */
Chuck Lever262965f2005-08-11 16:25:56 -0400687static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400688{
689 struct rpc_rqst *req = task->tk_rqstp;
690 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500691 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400692 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Leverb595bb12007-08-06 11:56:42 -0400693 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400694
Chuck Lever61677ee2011-05-09 15:22:34 -0400695 xs_encode_stream_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400696
Chuck Lever262965f2005-08-11 16:25:56 -0400697 xs_pktdump("packet data:",
698 req->rq_svec->iov_base,
699 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400700
701 /* Continue transmitting the packet/record. We must be careful
702 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400703 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400704 while (1) {
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500705 status = xs_sendpages(transport->sock,
706 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400707
Chuck Lever46121cf2007-01-31 12:14:08 -0500708 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400709 xdr->len - req->rq_bytes_sent, status);
710
711 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400712 break;
713
Chuck Lever262965f2005-08-11 16:25:56 -0400714 /* If we've sent the entire packet, immediately
715 * reset the count of bytes sent. */
716 req->rq_bytes_sent += status;
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400717 req->rq_xmit_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400718 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
719 req->rq_bytes_sent = 0;
720 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400721 }
722
Trond Myklebust06b4b682008-04-16 16:51:38 -0400723 if (status != 0)
724 continue;
Chuck Levera246b012005-08-11 16:25:23 -0400725 status = -EAGAIN;
Trond Myklebust06b4b682008-04-16 16:51:38 -0400726 break;
Chuck Levera246b012005-08-11 16:25:23 -0400727 }
728
Chuck Lever262965f2005-08-11 16:25:56 -0400729 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400730 case -ENOTSOCK:
731 status = -ENOTCONN;
732 /* Should we call xs_close() here? */
733 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400734 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400735 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400736 break;
737 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500738 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400739 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400740 case -ECONNRESET:
Trond Myklebust55420c22009-03-11 15:29:24 -0400741 case -EPIPE:
Trond Myklebuste06799f2007-11-05 15:44:12 -0500742 xs_tcp_shutdown(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400743 case -ECONNREFUSED:
744 case -ENOTCONN:
Chuck Lever262965f2005-08-11 16:25:56 -0400745 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400746 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800747
Chuck Levera246b012005-08-11 16:25:23 -0400748 return status;
749}
750
Chuck Lever9903cd12005-08-11 16:25:26 -0400751/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400752 * xs_tcp_release_xprt - clean up after a tcp transmission
753 * @xprt: transport
754 * @task: rpc task
755 *
756 * This cleans up if an error causes us to abort the transmission of a request.
757 * In this case, the socket may need to be reset in order to avoid confusing
758 * the server.
759 */
760static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
761{
762 struct rpc_rqst *req;
763
764 if (task != xprt->snd_task)
765 return;
766 if (task == NULL)
767 goto out_release;
768 req = task->tk_rqstp;
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400769 if (req == NULL)
770 goto out_release;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400771 if (req->rq_bytes_sent == 0)
772 goto out_release;
773 if (req->rq_bytes_sent == req->rq_snd_buf.len)
774 goto out_release;
775 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
776out_release:
777 xprt_release_xprt(xprt, task);
778}
779
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400780static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
781{
782 transport->old_data_ready = sk->sk_data_ready;
783 transport->old_state_change = sk->sk_state_change;
784 transport->old_write_space = sk->sk_write_space;
785 transport->old_error_report = sk->sk_error_report;
786}
787
788static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
789{
790 sk->sk_data_ready = transport->old_data_ready;
791 sk->sk_state_change = transport->old_state_change;
792 sk->sk_write_space = transport->old_write_space;
793 sk->sk_error_report = transport->old_error_report;
794}
795
Chuck Leverfe315e72009-03-11 14:10:21 -0400796static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400797{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500798 struct socket *sock = transport->sock;
799 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400800
Chuck Leverfe315e72009-03-11 14:10:21 -0400801 if (sk == NULL)
802 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400803
Trond Myklebust246408d2011-03-22 18:40:10 -0400804 transport->srcport = 0;
805
Chuck Levera246b012005-08-11 16:25:23 -0400806 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500807 transport->inet = NULL;
808 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400809
Chuck Lever9903cd12005-08-11 16:25:26 -0400810 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400811
812 xs_restore_old_callbacks(transport, sk);
Chuck Levera246b012005-08-11 16:25:23 -0400813 write_unlock_bh(&sk->sk_callback_lock);
814
Chuck Lever9903cd12005-08-11 16:25:26 -0400815 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400816
817 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -0400818}
819
820/**
821 * xs_close - close a socket
822 * @xprt: transport
823 *
824 * This is used when all requests are complete; ie, no DRC state remains
825 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400826 *
827 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
828 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e72009-03-11 14:10:21 -0400829 */
830static void xs_close(struct rpc_xprt *xprt)
831{
832 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
833
834 dprintk("RPC: xs_close xprt %p\n", xprt);
835
836 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -0400837 xprt->reestablish_timeout = 0;
Chuck Leverfe315e72009-03-11 14:10:21 -0400838
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100839 smp_mb__before_clear_bit();
Trond Myklebust7d1e8252009-03-11 14:38:03 -0400840 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100841 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -0500842 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100843 smp_mb__after_clear_bit();
Trond Myklebust62da3b22007-11-06 18:44:20 -0500844 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400845}
846
Trond Myklebustf75e6742009-04-21 17:18:20 -0400847static void xs_tcp_close(struct rpc_xprt *xprt)
848{
849 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
850 xs_close(xprt);
851 else
852 xs_tcp_shutdown(xprt);
853}
854
Chuck Lever9903cd12005-08-11 16:25:26 -0400855/**
856 * xs_destroy - prepare to shutdown a transport
857 * @xprt: doomed transport
858 *
859 */
860static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400861{
Chuck Leverc8475462006-12-05 16:35:26 -0500862 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
863
Chuck Lever46121cf2007-01-31 12:14:08 -0500864 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400865
Tejun Heoafe2c512010-12-14 16:21:17 +0100866 cancel_delayed_work_sync(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400867
Chuck Lever9903cd12005-08-11 16:25:26 -0400868 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400869 xs_free_peer_addresses(xprt);
Pavel Emelyanove204e622010-09-29 16:03:13 +0400870 xprt_free(xprt);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -0400871 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400872}
873
Chuck Lever9903cd12005-08-11 16:25:26 -0400874static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400875{
Chuck Lever9903cd12005-08-11 16:25:26 -0400876 return (struct rpc_xprt *) sk->sk_user_data;
877}
878
Chuck Lever176e21e2011-05-09 15:22:44 -0400879static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
880{
881 struct xdr_skb_reader desc = {
882 .skb = skb,
883 .offset = sizeof(rpc_fraghdr),
884 .count = skb->len - sizeof(rpc_fraghdr),
885 };
886
887 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
888 return -1;
889 if (desc.count)
890 return -1;
891 return 0;
892}
893
894/**
895 * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets
896 * @sk: socket with data to read
897 * @len: how much data to read
898 *
899 * Currently this assumes we can read the whole reply in a single gulp.
900 */
901static void xs_local_data_ready(struct sock *sk, int len)
902{
903 struct rpc_task *task;
904 struct rpc_xprt *xprt;
905 struct rpc_rqst *rovr;
906 struct sk_buff *skb;
907 int err, repsize, copied;
908 u32 _xid;
909 __be32 *xp;
910
911 read_lock_bh(&sk->sk_callback_lock);
912 dprintk("RPC: %s...\n", __func__);
913 xprt = xprt_from_sock(sk);
914 if (xprt == NULL)
915 goto out;
916
917 skb = skb_recv_datagram(sk, 0, 1, &err);
918 if (skb == NULL)
919 goto out;
920
921 if (xprt->shutdown)
922 goto dropit;
923
924 repsize = skb->len - sizeof(rpc_fraghdr);
925 if (repsize < 4) {
926 dprintk("RPC: impossible RPC reply size %d\n", repsize);
927 goto dropit;
928 }
929
930 /* Copy the XID from the skb... */
931 xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
932 if (xp == NULL)
933 goto dropit;
934
935 /* Look up and lock the request corresponding to the given XID */
936 spin_lock(&xprt->transport_lock);
937 rovr = xprt_lookup_rqst(xprt, *xp);
938 if (!rovr)
939 goto out_unlock;
940 task = rovr->rq_task;
941
942 copied = rovr->rq_private_buf.buflen;
943 if (copied > repsize)
944 copied = repsize;
945
946 if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
947 dprintk("RPC: sk_buff copy failed\n");
948 goto out_unlock;
949 }
950
951 xprt_complete_rqst(task, copied);
952
953 out_unlock:
954 spin_unlock(&xprt->transport_lock);
955 dropit:
956 skb_free_datagram(sk, skb);
957 out:
958 read_unlock_bh(&sk->sk_callback_lock);
959}
960
Chuck Lever9903cd12005-08-11 16:25:26 -0400961/**
962 * xs_udp_data_ready - "data ready" callback for UDP sockets
963 * @sk: socket with data to read
964 * @len: how much data to read
965 *
966 */
967static void xs_udp_data_ready(struct sock *sk, int len)
968{
969 struct rpc_task *task;
970 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400971 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400972 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400973 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700974 u32 _xid;
975 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400976
Eric Dumazetf064af12010-09-22 12:43:39 +0000977 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500978 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400979 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400980 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400981
982 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
983 goto out;
984
985 if (xprt->shutdown)
986 goto dropit;
987
988 repsize = skb->len - sizeof(struct udphdr);
989 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500990 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400991 goto dropit;
992 }
993
994 /* Copy the XID from the skb... */
995 xp = skb_header_pointer(skb, sizeof(struct udphdr),
996 sizeof(_xid), &_xid);
997 if (xp == NULL)
998 goto dropit;
999
1000 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001001 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001002 rovr = xprt_lookup_rqst(xprt, *xp);
1003 if (!rovr)
1004 goto out_unlock;
1005 task = rovr->rq_task;
1006
Chuck Levera246b012005-08-11 16:25:23 -04001007 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1008 copied = repsize;
1009
1010 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001011 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
1012 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -04001013 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001014 }
1015
1016 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -04001017
1018 /* Something worked... */
Eric Dumazetadf30902009-06-02 05:19:30 +00001019 dst_confirm(skb_dst(skb));
Chuck Levera246b012005-08-11 16:25:23 -04001020
Chuck Lever1570c1e2005-08-25 16:25:52 -07001021 xprt_adjust_cwnd(task, copied);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001022 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -04001023
1024 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001025 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001026 dropit:
1027 skb_free_datagram(sk, skb);
1028 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001029 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001030}
1031
Chuck Leverdd456472006-12-05 16:35:44 -05001032static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001033{
Chuck Lever51971132006-12-05 16:35:19 -05001034 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001035 size_t len, used;
1036 char *p;
1037
Chuck Lever51971132006-12-05 16:35:19 -05001038 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
1039 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001040 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001041 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001042 if (used != len)
1043 return;
Chuck Lever808012f2005-08-25 16:25:49 -07001044
Chuck Lever51971132006-12-05 16:35:19 -05001045 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
1046 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -05001047 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -04001048 else
Chuck Levere136d092006-12-05 16:35:23 -05001049 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -05001050 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -07001051
Chuck Levere136d092006-12-05 16:35:23 -05001052 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001053 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -07001054
Chuck Levera246b012005-08-11 16:25:23 -04001055 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001056 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001057 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebust3ebb0672007-11-06 18:40:12 -05001058 xprt_force_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001059 return;
Chuck Levera246b012005-08-11 16:25:23 -04001060 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001061 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -05001062 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001063}
1064
Chuck Lever51971132006-12-05 16:35:19 -05001065static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001066{
Chuck Lever51971132006-12-05 16:35:19 -05001067 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001068 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001069 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001070 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
1071 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1072 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001073 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001074 }
1075 }
1076}
1077
Chuck Leverdd456472006-12-05 16:35:44 -05001078static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001079{
1080 size_t len, used;
1081 char *p;
1082
Chuck Lever51971132006-12-05 16:35:19 -05001083 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -05001084 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001085 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001086 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001087 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001088 if (used != len)
1089 return;
Chuck Levere136d092006-12-05 16:35:23 -05001090 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001091 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -05001092 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001093 dprintk("RPC: reading %s XID %08x\n",
1094 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1095 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -05001096 ntohl(transport->tcp_xid));
1097 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001098}
1099
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001100static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1101 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001102{
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001103 size_t len, used;
1104 u32 offset;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001105 char *p;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001106
1107 /*
1108 * We want transport->tcp_offset to be 8 at the end of this routine
1109 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1110 * When this function is called for the first time,
1111 * transport->tcp_offset is 4 (after having already read the xid).
1112 */
1113 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001114 len = sizeof(transport->tcp_calldir) - offset;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001115 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001116 p = ((char *) &transport->tcp_calldir) + offset;
1117 used = xdr_skb_read_bits(desc, p, len);
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001118 transport->tcp_offset += used;
1119 if (used != len)
1120 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001121 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001122 /*
1123 * We don't yet have the XDR buffer, so we will write the calldir
1124 * out after we get the buffer from the 'struct rpc_rqst'
1125 */
Trond Myklebustb76ce562010-06-16 13:57:32 -04001126 switch (ntohl(transport->tcp_calldir)) {
1127 case RPC_REPLY:
1128 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1129 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001130 transport->tcp_flags |= TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001131 break;
1132 case RPC_CALL:
1133 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1134 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001135 transport->tcp_flags &= ~TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001136 break;
1137 default:
1138 dprintk("RPC: invalid request message type\n");
1139 xprt_force_disconnect(&transport->xprt);
1140 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001141 xs_tcp_check_fraghdr(transport);
1142}
1143
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001144static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
1145 struct xdr_skb_reader *desc,
1146 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001147{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001148 struct sock_xprt *transport =
1149 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001150 struct xdr_buf *rcvbuf;
1151 size_t len;
1152 ssize_t r;
1153
Chuck Levera246b012005-08-11 16:25:23 -04001154 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001155
1156 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1157 /*
1158 * Save the RPC direction in the XDR buffer
1159 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001160 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
Trond Myklebustb76ce562010-06-16 13:57:32 -04001161 &transport->tcp_calldir,
1162 sizeof(transport->tcp_calldir));
1163 transport->tcp_copied += sizeof(transport->tcp_calldir);
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001164 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -04001165 }
1166
Chuck Levera246b012005-08-11 16:25:23 -04001167 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -05001168 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -05001169 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -04001170
Chuck Lever51971132006-12-05 16:35:19 -05001171 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001172 memcpy(&my_desc, desc, sizeof(my_desc));
1173 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -05001174 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001175 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001176 desc->count -= r;
1177 desc->offset += r;
1178 } else
Chuck Lever51971132006-12-05 16:35:19 -05001179 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001180 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001181
1182 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001183 transport->tcp_copied += r;
1184 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001185 }
1186 if (r != len) {
1187 /* Error when copying to the receive buffer,
1188 * usually because we weren't able to allocate
1189 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001190 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001191 * will not receive any additional updates,
1192 * and time out.
1193 * Any remaining data from this record will
1194 * be discarded.
1195 */
Chuck Levere136d092006-12-05 16:35:23 -05001196 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001197 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001198 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001199 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1200 "tcp_offset = %u, tcp_reclen = %u\n",
1201 xprt, transport->tcp_copied,
1202 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001203 return;
Chuck Levera246b012005-08-11 16:25:23 -04001204 }
1205
Chuck Lever46121cf2007-01-31 12:14:08 -05001206 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001207 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001208 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1209 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1210 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001211
Chuck Lever51971132006-12-05 16:35:19 -05001212 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001213 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001214 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001215 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1216 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001217 }
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001218}
1219
1220/*
1221 * Finds the request corresponding to the RPC xid and invokes the common
1222 * tcp read code to read the data.
1223 */
1224static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1225 struct xdr_skb_reader *desc)
1226{
1227 struct sock_xprt *transport =
1228 container_of(xprt, struct sock_xprt, xprt);
1229 struct rpc_rqst *req;
1230
1231 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1232
1233 /* Find and lock the request corresponding to this xid */
1234 spin_lock(&xprt->transport_lock);
1235 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1236 if (!req) {
1237 dprintk("RPC: XID %08x request not found!\n",
1238 ntohl(transport->tcp_xid));
1239 spin_unlock(&xprt->transport_lock);
1240 return -1;
1241 }
1242
1243 xs_tcp_read_common(xprt, desc, req);
1244
Chuck Levere136d092006-12-05 16:35:23 -05001245 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001246 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001247
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001248 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001249 return 0;
1250}
1251
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001252#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001253/*
1254 * Obtains an rpc_rqst previously allocated and invokes the common
1255 * tcp read code to read the data. The result is placed in the callback
1256 * queue.
1257 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1258 * connection and return -1.
1259 */
1260static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1261 struct xdr_skb_reader *desc)
1262{
1263 struct sock_xprt *transport =
1264 container_of(xprt, struct sock_xprt, xprt);
1265 struct rpc_rqst *req;
1266
1267 req = xprt_alloc_bc_request(xprt);
1268 if (req == NULL) {
1269 printk(KERN_WARNING "Callback slot table overflowed\n");
1270 xprt_force_disconnect(xprt);
1271 return -1;
1272 }
1273
1274 req->rq_xid = transport->tcp_xid;
1275 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1276 xs_tcp_read_common(xprt, desc, req);
1277
1278 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1279 struct svc_serv *bc_serv = xprt->bc_serv;
1280
1281 /*
1282 * Add callback request to callback list. The callback
1283 * service sleeps on the sv_cb_waitq waiting for new
1284 * requests. Wake it up after adding enqueing the
1285 * request.
1286 */
1287 dprintk("RPC: add callback request to list\n");
1288 spin_lock(&bc_serv->sv_cb_lock);
1289 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1290 spin_unlock(&bc_serv->sv_cb_lock);
1291 wake_up(&bc_serv->sv_cb_waitq);
1292 }
1293
1294 req->rq_private_buf.len = transport->tcp_copied;
1295
1296 return 0;
1297}
1298
1299static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1300 struct xdr_skb_reader *desc)
1301{
1302 struct sock_xprt *transport =
1303 container_of(xprt, struct sock_xprt, xprt);
1304
1305 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1306 xs_tcp_read_reply(xprt, desc) :
1307 xs_tcp_read_callback(xprt, desc);
1308}
1309#else
1310static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1311 struct xdr_skb_reader *desc)
1312{
1313 return xs_tcp_read_reply(xprt, desc);
1314}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001315#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001316
1317/*
1318 * Read data off the transport. This can be either an RPC_CALL or an
1319 * RPC_REPLY. Relay the processing to helper functions.
1320 */
1321static void xs_tcp_read_data(struct rpc_xprt *xprt,
1322 struct xdr_skb_reader *desc)
1323{
1324 struct sock_xprt *transport =
1325 container_of(xprt, struct sock_xprt, xprt);
1326
1327 if (_xs_tcp_read_data(xprt, desc) == 0)
1328 xs_tcp_check_fraghdr(transport);
1329 else {
1330 /*
1331 * The transport_lock protects the request handling.
1332 * There's no need to hold it to update the tcp_flags.
1333 */
1334 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1335 }
Chuck Levera246b012005-08-11 16:25:23 -04001336}
1337
Chuck Leverdd456472006-12-05 16:35:44 -05001338static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001339{
1340 size_t len;
1341
Chuck Lever51971132006-12-05 16:35:19 -05001342 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001343 if (len > desc->count)
1344 len = desc->count;
1345 desc->count -= len;
1346 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001347 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001348 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001349 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001350}
1351
Chuck Lever9903cd12005-08-11 16:25:26 -04001352static 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 -04001353{
1354 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001355 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001356 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001357 .skb = skb,
1358 .offset = offset,
1359 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001360 };
Chuck Levera246b012005-08-11 16:25:23 -04001361
Chuck Lever46121cf2007-01-31 12:14:08 -05001362 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001363 do {
1364 /* Read in a new fragment marker if necessary */
1365 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001366 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001367 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001368 continue;
1369 }
1370 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001371 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001372 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001373 continue;
1374 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001375 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001376 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001377 xs_tcp_read_calldir(transport, &desc);
1378 continue;
1379 }
Chuck Levera246b012005-08-11 16:25:23 -04001380 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001381 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001382 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001383 continue;
1384 }
1385 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001386 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001387 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001388 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001389 return len - desc.count;
1390}
1391
Chuck Lever9903cd12005-08-11 16:25:26 -04001392/**
1393 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1394 * @sk: socket with data to read
1395 * @bytes: how much data to read
1396 *
1397 */
1398static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001399{
1400 struct rpc_xprt *xprt;
1401 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001402 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001403
Chuck Lever46121cf2007-01-31 12:14:08 -05001404 dprintk("RPC: xs_tcp_data_ready...\n");
1405
Eric Dumazetf064af12010-09-22 12:43:39 +00001406 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001407 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001408 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001409 if (xprt->shutdown)
1410 goto out;
1411
Neil Brown61d0a8e2009-09-23 14:36:37 -04001412 /* Any data means we had a useful conversation, so
1413 * the we don't need to delay the next reconnect
1414 */
1415 if (xprt->reestablish_timeout)
1416 xprt->reestablish_timeout = 0;
1417
Chuck Lever9903cd12005-08-11 16:25:26 -04001418 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001419 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001420 do {
1421 rd_desc.count = 65536;
1422 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1423 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001424out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001425 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001426}
1427
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001428/*
1429 * Do the equivalent of linger/linger2 handling for dealing with
1430 * broken servers that don't close the socket in a timely
1431 * fashion
1432 */
1433static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1434 unsigned long timeout)
1435{
1436 struct sock_xprt *transport;
1437
1438 if (xprt_test_and_set_connecting(xprt))
1439 return;
1440 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1441 transport = container_of(xprt, struct sock_xprt, xprt);
1442 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1443 timeout);
1444}
1445
1446static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1447{
1448 struct sock_xprt *transport;
1449
1450 transport = container_of(xprt, struct sock_xprt, xprt);
1451
1452 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1453 !cancel_delayed_work(&transport->connect_worker))
1454 return;
1455 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1456 xprt_clear_connecting(xprt);
1457}
1458
1459static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1460{
1461 smp_mb__before_clear_bit();
1462 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1463 clear_bit(XPRT_CLOSING, &xprt->state);
1464 smp_mb__after_clear_bit();
1465 /* Mark transport as closed and wake up all pending tasks */
1466 xprt_disconnect_done(xprt);
1467}
1468
Chuck Lever9903cd12005-08-11 16:25:26 -04001469/**
1470 * xs_tcp_state_change - callback to handle TCP socket state changes
1471 * @sk: socket whose state has changed
1472 *
1473 */
1474static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001475{
Chuck Lever9903cd12005-08-11 16:25:26 -04001476 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001477
Eric Dumazetf064af12010-09-22 12:43:39 +00001478 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001479 if (!(xprt = xprt_from_sock(sk)))
1480 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001481 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001482 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001483 sk->sk_state, xprt_connected(xprt),
1484 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001485 sock_flag(sk, SOCK_ZAPPED),
1486 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001487
1488 switch (sk->sk_state) {
1489 case TCP_ESTABLISHED:
Eric Dumazetf064af12010-09-22 12:43:39 +00001490 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001491 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001492 struct sock_xprt *transport = container_of(xprt,
1493 struct sock_xprt, xprt);
1494
Chuck Levera246b012005-08-11 16:25:23 -04001495 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001496 transport->tcp_offset = 0;
1497 transport->tcp_reclen = 0;
1498 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001499 transport->tcp_flags =
1500 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001501
Trond Myklebust2a491992009-03-11 14:38:00 -04001502 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001503 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001504 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001505 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001506 case TCP_FIN_WAIT1:
1507 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001508 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001509 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001510 set_bit(XPRT_CLOSING, &xprt->state);
1511 smp_mb__before_clear_bit();
1512 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001513 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001514 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001515 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001516 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001517 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001518 /* The server initiated a shutdown of the socket */
Trond Myklebust66af1e52007-11-06 10:18:36 -05001519 xprt_force_disconnect(xprt);
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001520 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001521 case TCP_CLOSING:
1522 /*
1523 * If the server closed down the connection, make sure that
1524 * we back off before reconnecting
1525 */
1526 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1527 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001528 break;
1529 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001530 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001531 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001532 smp_mb__before_clear_bit();
1533 clear_bit(XPRT_CONNECTED, &xprt->state);
1534 smp_mb__after_clear_bit();
1535 break;
1536 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001537 xs_tcp_cancel_linger_timeout(xprt);
1538 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001539 }
1540 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001541 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001542}
1543
Chuck Lever9903cd12005-08-11 16:25:26 -04001544/**
Trond Myklebust482f32e2009-03-11 14:38:00 -04001545 * xs_error_report - callback mainly for catching socket errors
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001546 * @sk: socket
1547 */
Trond Myklebust482f32e2009-03-11 14:38:00 -04001548static void xs_error_report(struct sock *sk)
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001549{
1550 struct rpc_xprt *xprt;
1551
Eric Dumazetf064af12010-09-22 12:43:39 +00001552 read_lock_bh(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001553 if (!(xprt = xprt_from_sock(sk)))
1554 goto out;
1555 dprintk("RPC: %s client %p...\n"
1556 "RPC: error %d\n",
1557 __func__, xprt, sk->sk_err);
Trond Myklebust482f32e2009-03-11 14:38:00 -04001558 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001559out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001560 read_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001561}
1562
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001563static void xs_write_space(struct sock *sk)
1564{
1565 struct socket *sock;
1566 struct rpc_xprt *xprt;
1567
1568 if (unlikely(!(sock = sk->sk_socket)))
1569 return;
1570 clear_bit(SOCK_NOSPACE, &sock->flags);
1571
1572 if (unlikely(!(xprt = xprt_from_sock(sk))))
1573 return;
1574 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1575 return;
1576
1577 xprt_write_space(xprt);
1578}
1579
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001580/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001581 * xs_udp_write_space - callback invoked when socket buffer space
1582 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001583 * @sk: socket whose state has changed
1584 *
Chuck Levera246b012005-08-11 16:25:23 -04001585 * Called when more output buffer space is available for this socket.
1586 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001587 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001588 * with a bunch of small requests.
1589 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001590static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001591{
Eric Dumazetf064af12010-09-22 12:43:39 +00001592 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001593
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001594 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001595 if (sock_writeable(sk))
1596 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001597
Eric Dumazetf064af12010-09-22 12:43:39 +00001598 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001599}
Chuck Levera246b012005-08-11 16:25:23 -04001600
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001601/**
1602 * xs_tcp_write_space - callback invoked when socket buffer space
1603 * becomes available
1604 * @sk: socket whose state has changed
1605 *
1606 * Called when more output buffer space is available for this socket.
1607 * We try not to wake our writers until they can make "significant"
1608 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1609 * with a bunch of small requests.
1610 */
1611static void xs_tcp_write_space(struct sock *sk)
1612{
Eric Dumazetf064af12010-09-22 12:43:39 +00001613 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001614
1615 /* from net/core/stream.c:sk_stream_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001616 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1617 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001618
Eric Dumazetf064af12010-09-22 12:43:39 +00001619 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001620}
1621
Chuck Lever470056c2005-08-25 16:25:56 -07001622static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001623{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001624 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1625 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001626
Chuck Lever7c6e0662006-12-05 16:35:30 -05001627 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001628 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001629 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001630 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001631 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001632 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001633 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001634 sk->sk_write_space(sk);
1635 }
1636}
1637
Chuck Lever43118c22005-08-25 16:25:49 -07001638/**
Chuck Lever470056c2005-08-25 16:25:56 -07001639 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001640 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001641 * @sndsize: requested size of send buffer, in bytes
1642 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001643 *
Chuck Lever470056c2005-08-25 16:25:56 -07001644 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001645 */
Chuck Lever470056c2005-08-25 16:25:56 -07001646static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001647{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001648 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1649
1650 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001651 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001652 transport->sndsize = sndsize + 1024;
1653 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001654 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001655 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001656
1657 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001658}
1659
Chuck Lever46c0ee82005-08-25 16:25:52 -07001660/**
1661 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1662 * @task: task that timed out
1663 *
1664 * Adjust the congestion window after a retransmit timeout has occurred.
1665 */
1666static void xs_udp_timer(struct rpc_task *task)
1667{
1668 xprt_adjust_cwnd(task, -ETIMEDOUT);
1669}
1670
Chuck Leverb85d8802006-05-25 01:40:49 -04001671static unsigned short xs_get_random_port(void)
1672{
1673 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1674 unsigned short rand = (unsigned short) net_random() % range;
1675 return rand + xprt_min_resvport;
1676}
1677
Chuck Lever92200412006-01-03 09:55:51 +01001678/**
1679 * xs_set_port - reset the port number in the remote endpoint address
1680 * @xprt: generic transport
1681 * @port: new port number
1682 *
1683 */
1684static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1685{
Chuck Lever46121cf2007-01-31 12:14:08 -05001686 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001687
Chuck Lever9dc3b092009-08-09 15:09:46 -04001688 rpc_set_port(xs_addr(xprt), port);
1689 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001690}
1691
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001692static unsigned short xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001693{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001694 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001695
1696 if (port == 0 && transport->xprt.resvport)
1697 port = xs_get_random_port();
1698 return port;
1699}
1700
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001701static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001702{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001703 if (transport->srcport != 0)
1704 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001705 if (!transport->xprt.resvport)
1706 return 0;
1707 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1708 return xprt_max_resvport;
1709 return --port;
1710}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001711static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001712{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001713 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001714 int err, nloop = 0;
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001715 unsigned short port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001716 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001717
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001718 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001719 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001720 rpc_set_port((struct sockaddr *)&myaddr, port);
1721 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1722 transport->xprt.addrlen);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001723 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001724 break;
Chuck Levera246b012005-08-11 16:25:23 -04001725 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001726 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001727 break;
Chuck Levera246b012005-08-11 16:25:23 -04001728 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001729 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001730 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001731 if (port > last)
1732 nloop++;
1733 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001734
Chuck Lever4232e862010-10-20 11:52:51 -04001735 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001736 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1737 &((struct sockaddr_in *)&myaddr)->sin_addr,
1738 port, err ? "failed" : "ok", err);
1739 else
1740 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1741 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1742 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001743 return err;
1744}
1745
Chuck Lever176e21e2011-05-09 15:22:44 -04001746/*
1747 * We don't support autobind on AF_LOCAL sockets
1748 */
1749static void xs_local_rpcbind(struct rpc_task *task)
1750{
1751 xprt_set_bound(task->tk_xprt);
1752}
1753
1754static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1755{
1756}
Chuck Levera246b012005-08-11 16:25:23 -04001757
Peter Zijlstraed075362006-12-06 20:35:24 -08001758#ifdef CONFIG_DEBUG_LOCK_ALLOC
1759static struct lock_class_key xs_key[2];
1760static struct lock_class_key xs_slock_key[2];
1761
Chuck Lever176e21e2011-05-09 15:22:44 -04001762static inline void xs_reclassify_socketu(struct socket *sock)
1763{
1764 struct sock *sk = sock->sk;
1765
1766 BUG_ON(sock_owned_by_user(sk));
1767 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1768 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1769}
1770
Chuck Lever8945ee52007-08-06 11:58:04 -04001771static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001772{
1773 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001774
John Heffner02b3d342007-09-12 10:42:12 +02001775 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001776 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1777 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1778}
Peter Zijlstraed075362006-12-06 20:35:24 -08001779
Chuck Lever8945ee52007-08-06 11:58:04 -04001780static inline void xs_reclassify_socket6(struct socket *sock)
1781{
1782 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001783
Linus Torvaldsf4921af2007-10-15 10:46:05 -07001784 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001785 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1786 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001787}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001788
1789static inline void xs_reclassify_socket(int family, struct socket *sock)
1790{
Chuck Lever4232e862010-10-20 11:52:51 -04001791 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001792 case AF_LOCAL:
1793 xs_reclassify_socketu(sock);
1794 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001795 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001796 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001797 break;
1798 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001799 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001800 break;
1801 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001802}
Peter Zijlstraed075362006-12-06 20:35:24 -08001803#else
Chuck Lever176e21e2011-05-09 15:22:44 -04001804static inline void xs_reclassify_socketu(struct socket *sock)
1805{
1806}
1807
Chuck Lever8945ee52007-08-06 11:58:04 -04001808static inline void xs_reclassify_socket4(struct socket *sock)
1809{
1810}
1811
1812static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001813{
1814}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001815
1816static inline void xs_reclassify_socket(int family, struct socket *sock)
1817{
1818}
Peter Zijlstraed075362006-12-06 20:35:24 -08001819#endif
1820
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001821static struct socket *xs_create_sock(struct rpc_xprt *xprt,
1822 struct sock_xprt *transport, int family, int type, int protocol)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001823{
1824 struct socket *sock;
1825 int err;
1826
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001827 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001828 if (err < 0) {
1829 dprintk("RPC: can't create %d transport socket (%d).\n",
1830 protocol, -err);
1831 goto out;
1832 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001833 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001834
Ben Hutchings4cea2882011-02-22 21:54:34 +00001835 err = xs_bind(transport, sock);
1836 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001837 sock_release(sock);
1838 goto out;
1839 }
1840
1841 return sock;
1842out:
1843 return ERR_PTR(err);
1844}
1845
Chuck Lever176e21e2011-05-09 15:22:44 -04001846static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1847 struct socket *sock)
1848{
1849 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1850 xprt);
1851
1852 if (!transport->inet) {
1853 struct sock *sk = sock->sk;
1854
1855 write_lock_bh(&sk->sk_callback_lock);
1856
1857 xs_save_old_callbacks(transport, sk);
1858
1859 sk->sk_user_data = xprt;
1860 sk->sk_data_ready = xs_local_data_ready;
1861 sk->sk_write_space = xs_udp_write_space;
1862 sk->sk_error_report = xs_error_report;
1863 sk->sk_allocation = GFP_ATOMIC;
1864
1865 xprt_clear_connected(xprt);
1866
1867 /* Reset to new socket */
1868 transport->sock = sock;
1869 transport->inet = sk;
1870
1871 write_unlock_bh(&sk->sk_callback_lock);
1872 }
1873
1874 /* Tell the socket layer to start connecting... */
1875 xprt->stat.connect_count++;
1876 xprt->stat.connect_start = jiffies;
1877 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1878}
1879
1880/**
1881 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1882 * @xprt: RPC transport to connect
1883 * @transport: socket transport to connect
1884 * @create_sock: function to create a socket of the correct type
1885 *
1886 * Invoked by a work queue tasklet.
1887 */
1888static void xs_local_setup_socket(struct work_struct *work)
1889{
1890 struct sock_xprt *transport =
1891 container_of(work, struct sock_xprt, connect_worker.work);
1892 struct rpc_xprt *xprt = &transport->xprt;
1893 struct socket *sock;
1894 int status = -EIO;
1895
1896 if (xprt->shutdown)
1897 goto out;
1898
1899 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1900 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1901 SOCK_STREAM, 0, &sock, 1);
1902 if (status < 0) {
1903 dprintk("RPC: can't create AF_LOCAL "
1904 "transport socket (%d).\n", -status);
1905 goto out;
1906 }
1907 xs_reclassify_socketu(sock);
1908
1909 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1910 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1911
1912 status = xs_local_finish_connecting(xprt, sock);
1913 switch (status) {
1914 case 0:
1915 dprintk("RPC: xprt %p connected to %s\n",
1916 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1917 xprt_set_connected(xprt);
1918 break;
1919 case -ENOENT:
1920 dprintk("RPC: xprt %p: socket %s does not exist\n",
1921 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1922 break;
1923 default:
1924 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1925 __func__, -status,
1926 xprt->address_strings[RPC_DISPLAY_ADDR]);
1927 }
1928
1929out:
1930 xprt_clear_connecting(xprt);
1931 xprt_wake_pending_tasks(xprt, status);
1932}
1933
Chuck Lever16be2d22007-08-06 11:57:38 -04001934static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001935{
Chuck Lever16be2d22007-08-06 11:57:38 -04001936 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001937
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001938 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001939 struct sock *sk = sock->sk;
1940
1941 write_lock_bh(&sk->sk_callback_lock);
1942
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001943 xs_save_old_callbacks(transport, sk);
1944
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001945 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001946 sk->sk_data_ready = xs_udp_data_ready;
1947 sk->sk_write_space = xs_udp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001948 sk->sk_error_report = xs_error_report;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001949 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001950 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001951
1952 xprt_set_connected(xprt);
1953
1954 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001955 transport->sock = sock;
1956 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001957
1958 write_unlock_bh(&sk->sk_callback_lock);
1959 }
Chuck Lever470056c2005-08-25 16:25:56 -07001960 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04001961}
1962
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04001963static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04001964{
1965 struct sock_xprt *transport =
1966 container_of(work, struct sock_xprt, connect_worker.work);
1967 struct rpc_xprt *xprt = &transport->xprt;
1968 struct socket *sock = transport->sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04001969 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04001970
Trond Myklebust01d37c42009-03-11 14:09:39 -04001971 if (xprt->shutdown)
Chuck Lever9903cd12005-08-11 16:25:26 -04001972 goto out;
1973
Chuck Levera246b012005-08-11 16:25:23 -04001974 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001975 xs_reset_transport(transport);
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04001976 sock = xs_create_sock(xprt, transport,
1977 xs_addr(xprt)->sa_family, SOCK_DGRAM, IPPROTO_UDP);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04001978 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04001979 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04001980
Chuck Leverc740eff2009-08-09 15:09:46 -04001981 dprintk("RPC: worker connecting xprt %p via %s to "
1982 "%s (port %s)\n", xprt,
1983 xprt->address_strings[RPC_DISPLAY_PROTO],
1984 xprt->address_strings[RPC_DISPLAY_ADDR],
1985 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04001986
1987 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001988 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001989out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001990 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001991 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001992}
1993
Chuck Lever3167e122005-08-25 16:25:55 -07001994/*
1995 * We need to preserve the port number so the reply cache on the server can
1996 * find our cached RPC replies when we get around to reconnecting.
1997 */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04001998static void xs_abort_connection(struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07001999{
2000 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07002001 struct sockaddr any;
2002
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002003 dprintk("RPC: disconnecting xprt %p to reuse port\n", transport);
Chuck Lever3167e122005-08-25 16:25:55 -07002004
2005 /*
2006 * Disconnect the transport socket by doing a connect operation
2007 * with AF_UNSPEC. This should return immediately...
2008 */
2009 memset(&any, 0, sizeof(any));
2010 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002011 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002012 if (!result)
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002013 xs_sock_mark_closed(&transport->xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002014 else
Chuck Lever46121cf2007-01-31 12:14:08 -05002015 dprintk("RPC: AF_UNSPEC connect return code %d\n",
Chuck Lever3167e122005-08-25 16:25:55 -07002016 result);
2017}
2018
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002019static void xs_tcp_reuse_connection(struct sock_xprt *transport)
Trond Myklebust40d25492009-03-11 14:37:58 -04002020{
2021 unsigned int state = transport->inet->sk_state;
2022
Andy Chittenden669502f2010-08-10 10:19:53 -04002023 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) {
2024 /* we don't need to abort the connection if the socket
2025 * hasn't undergone a shutdown
2026 */
2027 if (transport->inet->sk_shutdown == 0)
2028 return;
2029 dprintk("RPC: %s: TCP_CLOSEd and sk_shutdown set to %d\n",
2030 __func__, transport->inet->sk_shutdown);
2031 }
2032 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) {
2033 /* we don't need to abort the connection if the socket
2034 * hasn't undergone a shutdown
2035 */
2036 if (transport->inet->sk_shutdown == 0)
2037 return;
2038 dprintk("RPC: %s: ESTABLISHED/SYN_SENT "
2039 "sk_shutdown set to %d\n",
2040 __func__, transport->inet->sk_shutdown);
2041 }
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002042 xs_abort_connection(transport);
Trond Myklebust40d25492009-03-11 14:37:58 -04002043}
2044
Chuck Lever16be2d22007-08-06 11:57:38 -04002045static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002046{
Chuck Lever16be2d22007-08-06 11:57:38 -04002047 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002048 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002049
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002050 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002051 struct sock *sk = sock->sk;
2052
2053 write_lock_bh(&sk->sk_callback_lock);
2054
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002055 xs_save_old_callbacks(transport, sk);
2056
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002057 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002058 sk->sk_data_ready = xs_tcp_data_ready;
2059 sk->sk_state_change = xs_tcp_state_change;
2060 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04002061 sk->sk_error_report = xs_error_report;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05002062 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07002063
2064 /* socket options */
2065 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
2066 sock_reset_flag(sk, SOCK_LINGER);
2067 tcp_sk(sk)->linger2 = 0;
2068 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002069
2070 xprt_clear_connected(xprt);
2071
2072 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002073 transport->sock = sock;
2074 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002075
2076 write_unlock_bh(&sk->sk_callback_lock);
2077 }
2078
Trond Myklebust01d37c42009-03-11 14:09:39 -04002079 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002080 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002081
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002082 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05002083 xprt->stat.connect_count++;
2084 xprt->stat.connect_start = jiffies;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002085 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2086 switch (ret) {
2087 case 0:
2088 case -EINPROGRESS:
2089 /* SYN_SENT! */
2090 xprt->connect_cookie++;
2091 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2092 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2093 }
2094out:
2095 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002096}
2097
2098/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002099 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2100 * @xprt: RPC transport to connect
2101 * @transport: socket transport to connect
2102 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04002103 *
2104 * Invoked by a work queue tasklet.
2105 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002106static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002107{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002108 struct sock_xprt *transport =
2109 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002110 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002111 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002112 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002113
Trond Myklebust01d37c42009-03-11 14:09:39 -04002114 if (xprt->shutdown)
Chuck Lever16be2d22007-08-06 11:57:38 -04002115 goto out;
2116
2117 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002118 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002119 sock = xs_create_sock(xprt, transport,
2120 xs_addr(xprt)->sa_family, SOCK_STREAM, IPPROTO_TCP);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002121 if (IS_ERR(sock)) {
2122 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002123 goto out;
2124 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002125 } else {
2126 int abort_and_exit;
2127
2128 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
2129 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04002130 /* "close" the socket, preserving the local port */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002131 xs_tcp_reuse_connection(transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04002132
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002133 if (abort_and_exit)
2134 goto out_eagain;
2135 }
2136
Chuck Leverc740eff2009-08-09 15:09:46 -04002137 dprintk("RPC: worker connecting xprt %p via %s to "
2138 "%s (port %s)\n", xprt,
2139 xprt->address_strings[RPC_DISPLAY_PROTO],
2140 xprt->address_strings[RPC_DISPLAY_ADDR],
2141 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002142
2143 status = xs_tcp_finish_connecting(xprt, sock);
Chuck Lever46121cf2007-01-31 12:14:08 -05002144 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2145 xprt, -status, xprt_connected(xprt),
2146 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002147 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002148 default:
2149 printk("%s: connect returned unhandled error %d\n",
2150 __func__, status);
2151 case -EADDRNOTAVAIL:
2152 /* We're probably in TIME_WAIT. Get rid of existing socket,
2153 * and retry
2154 */
2155 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
2156 xprt_force_disconnect(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002157 break;
Trond Myklebust8a2cec22009-03-11 14:38:01 -04002158 case -ECONNREFUSED:
2159 case -ECONNRESET:
2160 case -ENETUNREACH:
2161 /* retry with existing socket, after a delay */
Trond Myklebust2a491992009-03-11 14:38:00 -04002162 case 0:
2163 case -EINPROGRESS:
2164 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002165 xprt_clear_connecting(xprt);
2166 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002167 case -EINVAL:
2168 /* Happens, for instance, if the user specified a link
2169 * local IPv6 address without a scope-id.
2170 */
2171 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002172 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002173out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04002174 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002175out:
Chuck Lever2226feb2005-08-11 16:25:38 -04002176 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002177 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04002178}
2179
Chuck Lever68e220b2007-08-06 11:57:48 -04002180/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002181 * xs_connect - connect a socket to a remote endpoint
2182 * @task: address of RPC task that manages state of connect request
2183 *
2184 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002185 *
2186 * UDP socket connects are synchronous, but we use a work queue anyway
2187 * to guarantee that even unprivileged user processes can set up a
2188 * socket on a privileged port.
2189 *
2190 * If a UDP socket connect fails, the delay behavior here prevents
2191 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002192 */
2193static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002194{
2195 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002196 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002197
Chuck Lever09a21c42009-12-03 15:58:56 -05002198 if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002199 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2200 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002201 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002202 queue_delayed_work(rpciod_workqueue,
2203 &transport->connect_worker,
2204 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07002205 xprt->reestablish_timeout <<= 1;
Neil Brown61d0a8e2009-09-23 14:36:37 -04002206 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2207 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002208 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2209 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002210 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05002211 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002212 queue_delayed_work(rpciod_workqueue,
2213 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002214 }
2215}
2216
Chuck Lever262ca072006-03-20 13:44:16 -05002217/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002218 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2219 * @xprt: rpc_xprt struct containing statistics
2220 * @seq: output file
2221 *
2222 */
2223static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2224{
2225 long idle_time = 0;
2226
2227 if (xprt_connected(xprt))
2228 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2229
2230 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
2231 "%llu %llu\n",
2232 xprt->stat.bind_count,
2233 xprt->stat.connect_count,
2234 xprt->stat.connect_time,
2235 idle_time,
2236 xprt->stat.sends,
2237 xprt->stat.recvs,
2238 xprt->stat.bad_xids,
2239 xprt->stat.req_u,
2240 xprt->stat.bklog_u);
2241}
2242
2243/**
Chuck Lever262ca072006-03-20 13:44:16 -05002244 * xs_udp_print_stats - display UDP socket-specifc stats
2245 * @xprt: rpc_xprt struct containing statistics
2246 * @seq: output file
2247 *
2248 */
2249static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2250{
Chuck Leverc8475462006-12-05 16:35:26 -05002251 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2252
Chuck Lever262ca072006-03-20 13:44:16 -05002253 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002254 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002255 xprt->stat.bind_count,
2256 xprt->stat.sends,
2257 xprt->stat.recvs,
2258 xprt->stat.bad_xids,
2259 xprt->stat.req_u,
2260 xprt->stat.bklog_u);
2261}
2262
2263/**
2264 * xs_tcp_print_stats - display TCP socket-specifc stats
2265 * @xprt: rpc_xprt struct containing statistics
2266 * @seq: output file
2267 *
2268 */
2269static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2270{
Chuck Leverc8475462006-12-05 16:35:26 -05002271 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002272 long idle_time = 0;
2273
2274 if (xprt_connected(xprt))
2275 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2276
2277 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002278 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002279 xprt->stat.bind_count,
2280 xprt->stat.connect_count,
2281 xprt->stat.connect_time,
2282 idle_time,
2283 xprt->stat.sends,
2284 xprt->stat.recvs,
2285 xprt->stat.bad_xids,
2286 xprt->stat.req_u,
2287 xprt->stat.bklog_u);
2288}
2289
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002290/*
2291 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2292 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2293 * to use the server side send routines.
2294 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002295static void *bc_malloc(struct rpc_task *task, size_t size)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002296{
2297 struct page *page;
2298 struct rpc_buffer *buf;
2299
2300 BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2301 page = alloc_page(GFP_KERNEL);
2302
2303 if (!page)
2304 return NULL;
2305
2306 buf = page_address(page);
2307 buf->len = PAGE_SIZE;
2308
2309 return buf->data;
2310}
2311
2312/*
2313 * Free the space allocated in the bc_alloc routine
2314 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002315static void bc_free(void *buffer)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002316{
2317 struct rpc_buffer *buf;
2318
2319 if (!buffer)
2320 return;
2321
2322 buf = container_of(buffer, struct rpc_buffer, data);
2323 free_page((unsigned long)buf);
2324}
2325
2326/*
2327 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2328 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2329 */
2330static int bc_sendto(struct rpc_rqst *req)
2331{
2332 int len;
2333 struct xdr_buf *xbufp = &req->rq_snd_buf;
2334 struct rpc_xprt *xprt = req->rq_xprt;
2335 struct sock_xprt *transport =
2336 container_of(xprt, struct sock_xprt, xprt);
2337 struct socket *sock = transport->sock;
2338 unsigned long headoff;
2339 unsigned long tailoff;
2340
Chuck Lever61677ee2011-05-09 15:22:34 -04002341 xs_encode_stream_record_marker(xbufp);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002342
2343 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2344 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2345 len = svc_send_common(sock, xbufp,
2346 virt_to_page(xbufp->head[0].iov_base), headoff,
2347 xbufp->tail[0].iov_base, tailoff);
2348
2349 if (len != xbufp->len) {
2350 printk(KERN_NOTICE "Error sending entire callback!\n");
2351 len = -EAGAIN;
2352 }
2353
2354 return len;
2355}
2356
2357/*
2358 * The send routine. Borrows from svc_send
2359 */
2360static int bc_send_request(struct rpc_task *task)
2361{
2362 struct rpc_rqst *req = task->tk_rqstp;
2363 struct svc_xprt *xprt;
2364 struct svc_sock *svsk;
2365 u32 len;
2366
2367 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2368 /*
2369 * Get the server socket associated with this callback xprt
2370 */
2371 xprt = req->rq_xprt->bc_xprt;
2372 svsk = container_of(xprt, struct svc_sock, sk_xprt);
2373
2374 /*
2375 * Grab the mutex to serialize data as the connection is shared
2376 * with the fore channel
2377 */
2378 if (!mutex_trylock(&xprt->xpt_mutex)) {
2379 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2380 if (!mutex_trylock(&xprt->xpt_mutex))
2381 return -EAGAIN;
2382 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2383 }
2384 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2385 len = -ENOTCONN;
2386 else
2387 len = bc_sendto(req);
2388 mutex_unlock(&xprt->xpt_mutex);
2389
2390 if (len > 0)
2391 len = 0;
2392
2393 return len;
2394}
2395
2396/*
2397 * The close routine. Since this is client initiated, we do nothing
2398 */
2399
2400static void bc_close(struct rpc_xprt *xprt)
2401{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002402}
2403
2404/*
2405 * The xprt destroy routine. Again, because this connection is client
2406 * initiated, we do nothing
2407 */
2408
2409static void bc_destroy(struct rpc_xprt *xprt)
2410{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002411}
2412
Chuck Lever176e21e2011-05-09 15:22:44 -04002413static struct rpc_xprt_ops xs_local_ops = {
2414 .reserve_xprt = xprt_reserve_xprt,
2415 .release_xprt = xs_tcp_release_xprt,
2416 .rpcbind = xs_local_rpcbind,
2417 .set_port = xs_local_set_port,
2418 .connect = xs_connect,
2419 .buf_alloc = rpc_malloc,
2420 .buf_free = rpc_free,
2421 .send_request = xs_local_send_request,
2422 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2423 .close = xs_close,
2424 .destroy = xs_destroy,
2425 .print_stats = xs_local_print_stats,
2426};
2427
Chuck Lever262965f2005-08-11 16:25:56 -04002428static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002429 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002430 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002431 .release_xprt = xprt_release_xprt_cong,
Chuck Lever45160d62007-07-01 12:13:17 -04002432 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002433 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002434 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002435 .buf_alloc = rpc_malloc,
2436 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002437 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002438 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002439 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002440 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002441 .close = xs_close,
2442 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002443 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04002444};
2445
2446static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002447 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002448 .release_xprt = xs_tcp_release_xprt,
Chuck Lever45160d62007-07-01 12:13:17 -04002449 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002450 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002451 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002452 .buf_alloc = rpc_malloc,
2453 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002454 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002455 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustf75e6742009-04-21 17:18:20 -04002456 .close = xs_tcp_close,
Chuck Lever9903cd12005-08-11 16:25:26 -04002457 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002458 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002459};
2460
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002461/*
2462 * The rpc_xprt_ops for the server backchannel
2463 */
2464
2465static struct rpc_xprt_ops bc_tcp_ops = {
2466 .reserve_xprt = xprt_reserve_xprt,
2467 .release_xprt = xprt_release_xprt,
2468 .buf_alloc = bc_malloc,
2469 .buf_free = bc_free,
2470 .send_request = bc_send_request,
2471 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2472 .close = bc_close,
2473 .destroy = bc_destroy,
2474 .print_stats = xs_tcp_print_stats,
2475};
2476
Chuck Lever92476852010-10-20 11:53:01 -04002477static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2478{
2479 static const struct sockaddr_in sin = {
2480 .sin_family = AF_INET,
2481 .sin_addr.s_addr = htonl(INADDR_ANY),
2482 };
2483 static const struct sockaddr_in6 sin6 = {
2484 .sin6_family = AF_INET6,
2485 .sin6_addr = IN6ADDR_ANY_INIT,
2486 };
2487
2488 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002489 case AF_LOCAL:
2490 break;
Chuck Lever92476852010-10-20 11:53:01 -04002491 case AF_INET:
2492 memcpy(sap, &sin, sizeof(sin));
2493 break;
2494 case AF_INET6:
2495 memcpy(sap, &sin6, sizeof(sin6));
2496 break;
2497 default:
2498 dprintk("RPC: %s: Bad address family\n", __func__);
2499 return -EAFNOSUPPORT;
2500 }
2501 return 0;
2502}
2503
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002504static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002505 unsigned int slot_table_size,
2506 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002507{
2508 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002509 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002510
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002511 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002512 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002513 return ERR_PTR(-EBADF);
2514 }
2515
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002516 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2517 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002518 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002519 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2520 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002521 return ERR_PTR(-ENOMEM);
2522 }
2523
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002524 new = container_of(xprt, struct sock_xprt, xprt);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002525 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2526 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002527 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002528 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002529 else {
2530 int err;
2531 err = xs_init_anyaddr(args->dstaddr->sa_family,
2532 (struct sockaddr *)&new->srcaddr);
2533 if (err != 0)
2534 return ERR_PTR(err);
2535 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002536
2537 return xprt;
2538}
2539
Chuck Lever176e21e2011-05-09 15:22:44 -04002540static const struct rpc_timeout xs_local_default_timeout = {
2541 .to_initval = 10 * HZ,
2542 .to_maxval = 10 * HZ,
2543 .to_retries = 2,
2544};
2545
2546/**
2547 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2548 * @args: rpc transport creation arguments
2549 *
2550 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2551 */
2552static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2553{
2554 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2555 struct sock_xprt *transport;
2556 struct rpc_xprt *xprt;
2557 struct rpc_xprt *ret;
2558
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002559 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2560 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002561 if (IS_ERR(xprt))
2562 return xprt;
2563 transport = container_of(xprt, struct sock_xprt, xprt);
2564
2565 xprt->prot = 0;
2566 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2567 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2568
2569 xprt->bind_timeout = XS_BIND_TO;
2570 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2571 xprt->idle_timeout = XS_IDLE_DISC_TO;
2572
2573 xprt->ops = &xs_local_ops;
2574 xprt->timeout = &xs_local_default_timeout;
2575
2576 switch (sun->sun_family) {
2577 case AF_LOCAL:
2578 if (sun->sun_path[0] != '/') {
2579 dprintk("RPC: bad AF_LOCAL address: %s\n",
2580 sun->sun_path);
2581 ret = ERR_PTR(-EINVAL);
2582 goto out_err;
2583 }
2584 xprt_set_bound(xprt);
2585 INIT_DELAYED_WORK(&transport->connect_worker,
2586 xs_local_setup_socket);
2587 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
2588 break;
2589 default:
2590 ret = ERR_PTR(-EAFNOSUPPORT);
2591 goto out_err;
2592 }
2593
2594 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2595 xprt->address_strings[RPC_DISPLAY_ADDR]);
2596
2597 if (try_module_get(THIS_MODULE))
2598 return xprt;
2599 ret = ERR_PTR(-EINVAL);
2600out_err:
2601 xprt_free(xprt);
2602 return ret;
2603}
2604
Trond Myklebust2881ae72007-12-20 16:03:54 -05002605static const struct rpc_timeout xs_udp_default_timeout = {
2606 .to_initval = 5 * HZ,
2607 .to_maxval = 30 * HZ,
2608 .to_increment = 5 * HZ,
2609 .to_retries = 5,
2610};
2611
Chuck Lever9903cd12005-08-11 16:25:26 -04002612/**
2613 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002614 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002615 *
2616 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002617static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002618{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002619 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002620 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002621 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002622 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002623
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002624 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2625 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002626 if (IS_ERR(xprt))
2627 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002628 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002629
Chuck Leverec739ef2006-08-22 20:06:15 -04002630 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002631 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002632 /* XXX: header size can vary due to auth type, IPv6, etc. */
2633 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2634
Chuck Lever03bf4b72005-08-25 16:25:55 -07002635 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002636 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2637 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002638
Chuck Lever262965f2005-08-11 16:25:56 -04002639 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002640
Trond Myklebustba7392b2007-12-20 16:03:55 -05002641 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002642
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002643 switch (addr->sa_family) {
2644 case AF_INET:
2645 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2646 xprt_set_bound(xprt);
2647
2648 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002649 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002650 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002651 break;
2652 case AF_INET6:
2653 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2654 xprt_set_bound(xprt);
2655
2656 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002657 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002658 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002659 break;
2660 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002661 ret = ERR_PTR(-EAFNOSUPPORT);
2662 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002663 }
2664
Chuck Leverc740eff2009-08-09 15:09:46 -04002665 if (xprt_bound(xprt))
2666 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2667 xprt->address_strings[RPC_DISPLAY_ADDR],
2668 xprt->address_strings[RPC_DISPLAY_PORT],
2669 xprt->address_strings[RPC_DISPLAY_PROTO]);
2670 else
2671 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2672 xprt->address_strings[RPC_DISPLAY_ADDR],
2673 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002674
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002675 if (try_module_get(THIS_MODULE))
2676 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002677 ret = ERR_PTR(-EINVAL);
2678out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002679 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002680 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002681}
2682
Trond Myklebust2881ae72007-12-20 16:03:54 -05002683static const struct rpc_timeout xs_tcp_default_timeout = {
2684 .to_initval = 60 * HZ,
2685 .to_maxval = 60 * HZ,
2686 .to_retries = 2,
2687};
2688
Chuck Lever9903cd12005-08-11 16:25:26 -04002689/**
2690 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002691 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002692 *
2693 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002694static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002695{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002696 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002697 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002698 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002699 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002700
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002701 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2702 xprt_max_tcp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002703 if (IS_ERR(xprt))
2704 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002705 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002706
Chuck Leverec739ef2006-08-22 20:06:15 -04002707 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002708 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002709 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002710
Chuck Lever03bf4b72005-08-25 16:25:55 -07002711 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002712 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2713 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002714
Chuck Lever262965f2005-08-11 16:25:56 -04002715 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002716 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002717
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002718 switch (addr->sa_family) {
2719 case AF_INET:
2720 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2721 xprt_set_bound(xprt);
2722
Chuck Lever9dc3b092009-08-09 15:09:46 -04002723 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002724 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002725 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002726 break;
2727 case AF_INET6:
2728 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2729 xprt_set_bound(xprt);
2730
Chuck Lever9dc3b092009-08-09 15:09:46 -04002731 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002732 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002733 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002734 break;
2735 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002736 ret = ERR_PTR(-EAFNOSUPPORT);
2737 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002738 }
2739
Chuck Leverc740eff2009-08-09 15:09:46 -04002740 if (xprt_bound(xprt))
2741 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2742 xprt->address_strings[RPC_DISPLAY_ADDR],
2743 xprt->address_strings[RPC_DISPLAY_PORT],
2744 xprt->address_strings[RPC_DISPLAY_PROTO]);
2745 else
2746 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2747 xprt->address_strings[RPC_DISPLAY_ADDR],
2748 xprt->address_strings[RPC_DISPLAY_PROTO]);
2749
Chuck Leveredb267a2006-08-22 20:06:18 -04002750
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002751 if (try_module_get(THIS_MODULE))
2752 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002753 ret = ERR_PTR(-EINVAL);
2754out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002755 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002756 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002757}
Chuck Lever282b32e2006-12-05 16:35:51 -05002758
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002759/**
2760 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2761 * @args: rpc transport creation arguments
2762 *
2763 */
2764static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2765{
2766 struct sockaddr *addr = args->dstaddr;
2767 struct rpc_xprt *xprt;
2768 struct sock_xprt *transport;
2769 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002770 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002771
J. Bruce Fieldsf0418aa2010-12-08 13:48:19 -05002772 if (args->bc_xprt->xpt_bc_xprt) {
2773 /*
2774 * This server connection already has a backchannel
2775 * export; we can't create a new one, as we wouldn't be
2776 * able to match replies based on xid any more. So,
2777 * reuse the already-existing one:
2778 */
2779 return args->bc_xprt->xpt_bc_xprt;
2780 }
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002781 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2782 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002783 if (IS_ERR(xprt))
2784 return xprt;
2785 transport = container_of(xprt, struct sock_xprt, xprt);
2786
2787 xprt->prot = IPPROTO_TCP;
2788 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2789 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2790 xprt->timeout = &xs_tcp_default_timeout;
2791
2792 /* backchannel */
2793 xprt_set_bound(xprt);
2794 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002795 xprt->reestablish_timeout = 0;
2796 xprt->idle_timeout = 0;
2797
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002798 xprt->ops = &bc_tcp_ops;
2799
2800 switch (addr->sa_family) {
2801 case AF_INET:
2802 xs_format_peer_addresses(xprt, "tcp",
2803 RPCBIND_NETID_TCP);
2804 break;
2805 case AF_INET6:
2806 xs_format_peer_addresses(xprt, "tcp",
2807 RPCBIND_NETID_TCP6);
2808 break;
2809 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002810 ret = ERR_PTR(-EAFNOSUPPORT);
2811 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002812 }
2813
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04002814 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2815 xprt->address_strings[RPC_DISPLAY_ADDR],
2816 xprt->address_strings[RPC_DISPLAY_PORT],
2817 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002818
2819 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002820 * Once we've associated a backchannel xprt with a connection,
2821 * we want to keep it around as long as long as the connection
2822 * lasts, in case we need to start using it for a backchannel
2823 * again; this reference won't be dropped until bc_xprt is
2824 * destroyed.
2825 */
2826 xprt_get(xprt);
2827 args->bc_xprt->xpt_bc_xprt = xprt;
2828 xprt->bc_xprt = args->bc_xprt;
2829 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2830 transport->sock = bc_sock->sk_sock;
2831 transport->inet = bc_sock->sk_sk;
2832
2833 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002834 * Since we don't want connections for the backchannel, we set
2835 * the xprt status to connected
2836 */
2837 xprt_set_connected(xprt);
2838
2839
2840 if (try_module_get(THIS_MODULE))
2841 return xprt;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002842 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002843 ret = ERR_PTR(-EINVAL);
2844out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002845 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002846 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002847}
2848
Chuck Lever176e21e2011-05-09 15:22:44 -04002849static struct xprt_class xs_local_transport = {
2850 .list = LIST_HEAD_INIT(xs_local_transport.list),
2851 .name = "named UNIX socket",
2852 .owner = THIS_MODULE,
2853 .ident = XPRT_TRANSPORT_LOCAL,
2854 .setup = xs_setup_local,
2855};
2856
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002857static struct xprt_class xs_udp_transport = {
2858 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2859 .name = "udp",
2860 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002861 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002862 .setup = xs_setup_udp,
2863};
2864
2865static struct xprt_class xs_tcp_transport = {
2866 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2867 .name = "tcp",
2868 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002869 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002870 .setup = xs_setup_tcp,
2871};
2872
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002873static struct xprt_class xs_bc_tcp_transport = {
2874 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2875 .name = "tcp NFSv4.1 backchannel",
2876 .owner = THIS_MODULE,
2877 .ident = XPRT_TRANSPORT_BC_TCP,
2878 .setup = xs_setup_bc_tcp,
2879};
2880
Chuck Lever282b32e2006-12-05 16:35:51 -05002881/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002882 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002883 *
2884 */
2885int init_socket_xprt(void)
2886{
Chuck Leverfbf76682006-12-05 16:35:54 -05002887#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002888 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002889 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002890#endif
2891
Chuck Lever176e21e2011-05-09 15:22:44 -04002892 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002893 xprt_register_transport(&xs_udp_transport);
2894 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002895 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002896
Chuck Lever282b32e2006-12-05 16:35:51 -05002897 return 0;
2898}
2899
2900/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002901 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05002902 *
2903 */
2904void cleanup_socket_xprt(void)
2905{
Chuck Leverfbf76682006-12-05 16:35:54 -05002906#ifdef RPC_DEBUG
2907 if (sunrpc_table_header) {
2908 unregister_sysctl_table(sunrpc_table_header);
2909 sunrpc_table_header = NULL;
2910 }
2911#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002912
Chuck Lever176e21e2011-05-09 15:22:44 -04002913 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002914 xprt_unregister_transport(&xs_udp_transport);
2915 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002916 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05002917}
Trond Myklebustcbf11072009-08-09 15:06:19 -04002918
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002919static int param_set_uint_minmax(const char *val,
2920 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04002921 unsigned int min, unsigned int max)
2922{
2923 unsigned long num;
2924 int ret;
2925
2926 if (!val)
2927 return -EINVAL;
2928 ret = strict_strtoul(val, 0, &num);
2929 if (ret == -EINVAL || num < min || num > max)
2930 return -EINVAL;
2931 *((unsigned int *)kp->arg) = num;
2932 return 0;
2933}
2934
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002935static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04002936{
2937 return param_set_uint_minmax(val, kp,
2938 RPC_MIN_RESVPORT,
2939 RPC_MAX_RESVPORT);
2940}
2941
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002942static struct kernel_param_ops param_ops_portnr = {
2943 .set = param_set_portnr,
2944 .get = param_get_uint,
2945};
2946
Trond Myklebustcbf11072009-08-09 15:06:19 -04002947#define param_check_portnr(name, p) \
2948 __param_check(name, p, unsigned int);
2949
2950module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
2951module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
2952
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002953static int param_set_slot_table_size(const char *val,
2954 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04002955{
2956 return param_set_uint_minmax(val, kp,
2957 RPC_MIN_SLOT_TABLE,
2958 RPC_MAX_SLOT_TABLE);
2959}
2960
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002961static struct kernel_param_ops param_ops_slot_table_size = {
2962 .set = param_set_slot_table_size,
2963 .get = param_get_uint,
2964};
2965
Trond Myklebustcbf11072009-08-09 15:06:19 -04002966#define param_check_slot_table_size(name, p) \
2967 __param_check(name, p, unsigned int);
2968
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002969static int param_set_max_slot_table_size(const char *val,
2970 const struct kernel_param *kp)
2971{
2972 return param_set_uint_minmax(val, kp,
2973 RPC_MIN_SLOT_TABLE,
2974 RPC_MAX_SLOT_TABLE_LIMIT);
2975}
2976
2977static struct kernel_param_ops param_ops_max_slot_table_size = {
2978 .set = param_set_max_slot_table_size,
2979 .get = param_get_uint,
2980};
2981
2982#define param_check_max_slot_table_size(name, p) \
2983 __param_check(name, p, unsigned int);
2984
Trond Myklebustcbf11072009-08-09 15:06:19 -04002985module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
2986 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002987module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
2988 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04002989module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
2990 slot_table_size, 0644);
2991