blob: b34cf303d91693a1c988a0aac56c8842105dc221 [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 */
Trond Myklebust09acfea2012-03-11 15:22:54 -040056static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
57static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
58static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
Chuck Leverc556b752005-11-01 12:24:48 -050059
Trond Myklebust09acfea2012-03-11 15:22:54 -040060static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
61static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
Chuck Leverc556b752005-11-01 12:24:48 -050062
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 *);
Chuck Leverffc2e512006-12-05 16:35:11 -0500257};
258
Chuck Levere136d092006-12-05 16:35:23 -0500259/*
260 * TCP receive state flags
261 */
262#define TCP_RCV_LAST_FRAG (1UL << 0)
263#define TCP_RCV_COPY_FRAGHDR (1UL << 1)
264#define TCP_RCV_COPY_XID (1UL << 2)
265#define TCP_RCV_COPY_DATA (1UL << 3)
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400266#define TCP_RCV_READ_CALLDIR (1UL << 4)
267#define TCP_RCV_COPY_CALLDIR (1UL << 5)
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400268
269/*
270 * TCP RPC flags
271 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400272#define TCP_RPC_REPLY (1UL << 6)
Chuck Levere136d092006-12-05 16:35:23 -0500273
Chuck Lever95392c52007-08-06 11:57:58 -0400274static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400275{
Chuck Lever95392c52007-08-06 11:57:58 -0400276 return (struct sockaddr *) &xprt->addr;
277}
278
Chuck Lever176e21e2011-05-09 15:22:44 -0400279static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
280{
281 return (struct sockaddr_un *) &xprt->addr;
282}
283
Chuck Lever95392c52007-08-06 11:57:58 -0400284static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
285{
286 return (struct sockaddr_in *) &xprt->addr;
287}
288
289static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
290{
291 return (struct sockaddr_in6 *) &xprt->addr;
292}
293
Chuck Leverc877b842009-08-09 15:09:36 -0400294static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400295{
Chuck Leverc877b842009-08-09 15:09:36 -0400296 struct sockaddr *sap = xs_addr(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400297 struct sockaddr_in6 *sin6;
298 struct sockaddr_in *sin;
Chuck Lever176e21e2011-05-09 15:22:44 -0400299 struct sockaddr_un *sun;
Chuck Leverc877b842009-08-09 15:09:36 -0400300 char buf[128];
Chuck Leveredb267a2006-08-22 20:06:18 -0400301
Chuck Lever9dc3b092009-08-09 15:09:46 -0400302 switch (sap->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400303 case AF_LOCAL:
304 sun = xs_addr_un(xprt);
305 strlcpy(buf, sun->sun_path, sizeof(buf));
306 xprt->address_strings[RPC_DISPLAY_ADDR] =
307 kstrdup(buf, GFP_KERNEL);
308 break;
Chuck Lever9dc3b092009-08-09 15:09:46 -0400309 case AF_INET:
Chuck Lever176e21e2011-05-09 15:22:44 -0400310 (void)rpc_ntop(sap, buf, sizeof(buf));
311 xprt->address_strings[RPC_DISPLAY_ADDR] =
312 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400313 sin = xs_addr_in(xprt);
Joe Perchesfc0b5792010-03-08 12:15:28 -0800314 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
Chuck Lever9dc3b092009-08-09 15:09:46 -0400315 break;
316 case AF_INET6:
Chuck Lever176e21e2011-05-09 15:22:44 -0400317 (void)rpc_ntop(sap, buf, sizeof(buf));
318 xprt->address_strings[RPC_DISPLAY_ADDR] =
319 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400320 sin6 = xs_addr_in6(xprt);
Joe Perchesfc0b5792010-03-08 12:15:28 -0800321 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400322 break;
323 default:
324 BUG();
Chuck Leveredb267a2006-08-22 20:06:18 -0400325 }
Chuck Lever176e21e2011-05-09 15:22:44 -0400326
Chuck Lever9dc3b092009-08-09 15:09:46 -0400327 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
Chuck Leveredb267a2006-08-22 20:06:18 -0400328}
329
Chuck Lever9dc3b092009-08-09 15:09:46 -0400330static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400331{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400332 struct sockaddr *sap = xs_addr(xprt);
333 char buf[128];
Chuck Lever4b6473f2007-08-06 11:57:12 -0400334
Joe Perches81160e662010-03-08 12:15:59 -0800335 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400336 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400337
Joe Perches81160e662010-03-08 12:15:59 -0800338 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400339 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
340}
Chuck Lever4b6473f2007-08-06 11:57:12 -0400341
Chuck Lever9dc3b092009-08-09 15:09:46 -0400342static void xs_format_peer_addresses(struct rpc_xprt *xprt,
343 const char *protocol,
344 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400345{
Chuck Leverb454ae92008-01-07 18:34:48 -0500346 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leverb454ae92008-01-07 18:34:48 -0500347 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leverc877b842009-08-09 15:09:36 -0400348 xs_format_common_peer_addresses(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400349 xs_format_common_peer_ports(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400350}
351
Chuck Lever9dc3b092009-08-09 15:09:46 -0400352static void xs_update_peer_port(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400353{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400354 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
355 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400356
Chuck Lever9dc3b092009-08-09 15:09:46 -0400357 xs_format_common_peer_ports(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400358}
359
360static void xs_free_peer_addresses(struct rpc_xprt *xprt)
361{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500362 unsigned int i;
363
364 for (i = 0; i < RPC_DISPLAY_MAX; i++)
365 switch (i) {
366 case RPC_DISPLAY_PROTO:
367 case RPC_DISPLAY_NETID:
368 continue;
369 default:
370 kfree(xprt->address_strings[i]);
371 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400372}
373
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400374#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
375
Trond Myklebust24c56842006-10-17 15:06:22 -0400376static 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 -0400377{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400378 struct msghdr msg = {
379 .msg_name = addr,
380 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400381 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
382 };
383 struct kvec iov = {
384 .iov_base = vec->iov_base + base,
385 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400386 };
387
Trond Myklebust24c56842006-10-17 15:06:22 -0400388 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400389 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
390 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
391}
392
Trond Myklebust24c56842006-10-17 15:06:22 -0400393static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400394{
Trond Myklebust24c56842006-10-17 15:06:22 -0400395 struct page **ppage;
396 unsigned int remainder;
397 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400398
Trond Myklebust24c56842006-10-17 15:06:22 -0400399 remainder = xdr->page_len - base;
400 base += xdr->page_base;
401 ppage = xdr->pages + (base >> PAGE_SHIFT);
402 base &= ~PAGE_MASK;
403 for(;;) {
404 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
405 int flags = XS_SENDMSG_FLAGS;
406
407 remainder -= len;
408 if (remainder != 0 || more)
409 flags |= MSG_MORE;
410 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
411 if (remainder == 0 || err != len)
412 break;
413 sent += err;
414 ppage++;
415 base = 0;
416 }
417 if (sent == 0)
418 return err;
419 if (err > 0)
420 sent += err;
421 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400422}
423
Chuck Lever9903cd12005-08-11 16:25:26 -0400424/**
425 * xs_sendpages - write pages directly to a socket
426 * @sock: socket to send on
427 * @addr: UDP only -- address of destination
428 * @addrlen: UDP only -- length of destination address
429 * @xdr: buffer containing this request
430 * @base: starting position in the buffer
431 *
Chuck Levera246b012005-08-11 16:25:23 -0400432 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400433static 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 -0400434{
Trond Myklebust24c56842006-10-17 15:06:22 -0400435 unsigned int remainder = xdr->len - base;
436 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400437
Chuck Lever262965f2005-08-11 16:25:56 -0400438 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400439 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400440
441 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400442 if (base != 0) {
443 addr = NULL;
444 addrlen = 0;
445 }
Chuck Lever262965f2005-08-11 16:25:56 -0400446
Trond Myklebust24c56842006-10-17 15:06:22 -0400447 if (base < xdr->head[0].iov_len || addr != NULL) {
448 unsigned int len = xdr->head[0].iov_len - base;
449 remainder -= len;
450 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
451 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400452 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400453 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400454 base = 0;
455 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400456 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400457
Trond Myklebust24c56842006-10-17 15:06:22 -0400458 if (base < xdr->page_len) {
459 unsigned int len = xdr->page_len - base;
460 remainder -= len;
461 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
462 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400463 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400464 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400465 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400466 } else
467 base -= xdr->page_len;
468
469 if (base >= xdr->tail[0].iov_len)
470 return sent;
471 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400472out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400473 if (sent == 0)
474 return err;
475 if (err > 0)
476 sent += err;
477 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400478}
479
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400480static void xs_nospace_callback(struct rpc_task *task)
481{
482 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
483
484 transport->inet->sk_write_pending--;
485 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
486}
487
Chuck Lever9903cd12005-08-11 16:25:26 -0400488/**
Chuck Lever262965f2005-08-11 16:25:56 -0400489 * xs_nospace - place task on wait queue if transmit was incomplete
490 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400491 *
Chuck Levera246b012005-08-11 16:25:23 -0400492 */
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400493static int xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400494{
Chuck Lever262965f2005-08-11 16:25:56 -0400495 struct rpc_rqst *req = task->tk_rqstp;
496 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500497 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust24ca9a82011-11-22 14:44:28 +0200498 int ret = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400499
Chuck Lever46121cf2007-01-31 12:14:08 -0500500 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400501 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
502 req->rq_slen);
503
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400504 /* Protect against races with write_space */
505 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400506
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400507 /* Don't race with disconnect */
508 if (xprt_connected(xprt)) {
509 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
510 /*
511 * Notify TCP that we're limited by the application
512 * window size
513 */
514 set_bit(SOCK_NOSPACE, &transport->sock->flags);
515 transport->inet->sk_write_pending++;
516 /* ...and wait for more buffer space */
517 xprt_wait_for_buffer_space(task, xs_nospace_callback);
518 }
519 } else {
520 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400521 ret = -ENOTCONN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400522 }
Chuck Lever262965f2005-08-11 16:25:56 -0400523
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400524 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400525 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400526}
527
Chuck Lever61677ee2011-05-09 15:22:34 -0400528/*
529 * Construct a stream transport record marker in @buf.
530 */
531static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
532{
533 u32 reclen = buf->len - sizeof(rpc_fraghdr);
534 rpc_fraghdr *base = buf->head[0].iov_base;
535 *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
536}
537
Chuck Lever262965f2005-08-11 16:25:56 -0400538/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400539 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
540 * @task: RPC task that manages the state of an RPC request
541 *
542 * Return values:
543 * 0: The request has been sent
544 * EAGAIN: The socket was blocked, please call again later to
545 * complete the request
546 * ENOTCONN: Caller needs to invoke connect logic then call again
547 * other: Some other error occured, the request was not sent
548 */
549static int xs_local_send_request(struct rpc_task *task)
550{
551 struct rpc_rqst *req = task->tk_rqstp;
552 struct rpc_xprt *xprt = req->rq_xprt;
553 struct sock_xprt *transport =
554 container_of(xprt, struct sock_xprt, xprt);
555 struct xdr_buf *xdr = &req->rq_snd_buf;
556 int status;
557
558 xs_encode_stream_record_marker(&req->rq_snd_buf);
559
560 xs_pktdump("packet data:",
561 req->rq_svec->iov_base, req->rq_svec->iov_len);
562
563 status = xs_sendpages(transport->sock, NULL, 0,
564 xdr, req->rq_bytes_sent);
565 dprintk("RPC: %s(%u) = %d\n",
566 __func__, xdr->len - req->rq_bytes_sent, status);
567 if (likely(status >= 0)) {
568 req->rq_bytes_sent += status;
569 req->rq_xmit_bytes_sent += status;
570 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
571 req->rq_bytes_sent = 0;
572 return 0;
573 }
574 status = -EAGAIN;
575 }
576
577 switch (status) {
578 case -EAGAIN:
579 status = xs_nospace(task);
580 break;
581 default:
582 dprintk("RPC: sendmsg returned unrecognized error %d\n",
583 -status);
584 case -EPIPE:
585 xs_close(xprt);
586 status = -ENOTCONN;
587 }
588
589 return status;
590}
591
592/**
Chuck Lever262965f2005-08-11 16:25:56 -0400593 * xs_udp_send_request - write an RPC request to a UDP socket
594 * @task: address of RPC task that manages the state of an RPC request
595 *
596 * Return values:
597 * 0: The request has been sent
598 * EAGAIN: The socket was blocked, please call again later to
599 * complete the request
600 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300601 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -0400602 */
603static int xs_udp_send_request(struct rpc_task *task)
604{
605 struct rpc_rqst *req = task->tk_rqstp;
606 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500607 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400608 struct xdr_buf *xdr = &req->rq_snd_buf;
609 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400610
Chuck Lever9903cd12005-08-11 16:25:26 -0400611 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400612 req->rq_svec->iov_base,
613 req->rq_svec->iov_len);
614
Trond Myklebust01d37c42009-03-11 14:09:39 -0400615 if (!xprt_bound(xprt))
616 return -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500617 status = xs_sendpages(transport->sock,
Chuck Lever95392c52007-08-06 11:57:58 -0400618 xs_addr(xprt),
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500619 xprt->addrlen, xdr,
620 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400621
Chuck Lever46121cf2007-01-31 12:14:08 -0500622 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400623 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400624
Trond Myklebust21997002007-10-01 11:43:37 -0400625 if (status >= 0) {
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400626 req->rq_xmit_bytes_sent += status;
Trond Myklebust21997002007-10-01 11:43:37 -0400627 if (status >= req->rq_slen)
628 return 0;
629 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400630 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400631 }
Chuck Levera246b012005-08-11 16:25:23 -0400632
Chuck Lever262965f2005-08-11 16:25:56 -0400633 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400634 case -ENOTSOCK:
635 status = -ENOTCONN;
636 /* Should we call xs_close() here? */
637 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400638 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400639 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400640 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400641 default:
642 dprintk("RPC: sendmsg returned unrecognized error %d\n",
643 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400644 case -ENETUNREACH:
645 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400646 case -ECONNREFUSED:
647 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400648 * prompts ECONNREFUSED. */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400649 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400650 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800651
Chuck Lever262965f2005-08-11 16:25:56 -0400652 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400653}
654
Trond Myklebuste06799f2007-11-05 15:44:12 -0500655/**
656 * xs_tcp_shutdown - gracefully shut down a TCP socket
657 * @xprt: transport
658 *
659 * Initiates a graceful shutdown of the TCP socket by calling the
660 * equivalent of shutdown(SHUT_WR);
661 */
662static void xs_tcp_shutdown(struct rpc_xprt *xprt)
663{
664 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
665 struct socket *sock = transport->sock;
666
667 if (sock != NULL)
668 kernel_sock_shutdown(sock, SHUT_WR);
669}
670
Chuck Lever9903cd12005-08-11 16:25:26 -0400671/**
Chuck Lever262965f2005-08-11 16:25:56 -0400672 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400673 * @task: address of RPC task that manages the state of an RPC request
674 *
675 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400676 * 0: The request has been sent
677 * EAGAIN: The socket was blocked, please call again later to
678 * complete the request
679 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300680 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400681 *
682 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400683 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400684 */
Chuck Lever262965f2005-08-11 16:25:56 -0400685static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400686{
687 struct rpc_rqst *req = task->tk_rqstp;
688 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500689 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400690 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Leverb595bb12007-08-06 11:56:42 -0400691 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400692
Chuck Lever61677ee2011-05-09 15:22:34 -0400693 xs_encode_stream_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400694
Chuck Lever262965f2005-08-11 16:25:56 -0400695 xs_pktdump("packet data:",
696 req->rq_svec->iov_base,
697 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400698
699 /* Continue transmitting the packet/record. We must be careful
700 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400701 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400702 while (1) {
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500703 status = xs_sendpages(transport->sock,
704 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400705
Chuck Lever46121cf2007-01-31 12:14:08 -0500706 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400707 xdr->len - req->rq_bytes_sent, status);
708
709 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400710 break;
711
Chuck Lever262965f2005-08-11 16:25:56 -0400712 /* If we've sent the entire packet, immediately
713 * reset the count of bytes sent. */
714 req->rq_bytes_sent += status;
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400715 req->rq_xmit_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400716 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
717 req->rq_bytes_sent = 0;
718 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400719 }
720
Trond Myklebust06b4b682008-04-16 16:51:38 -0400721 if (status != 0)
722 continue;
Chuck Levera246b012005-08-11 16:25:23 -0400723 status = -EAGAIN;
Trond Myklebust06b4b682008-04-16 16:51:38 -0400724 break;
Chuck Levera246b012005-08-11 16:25:23 -0400725 }
726
Chuck Lever262965f2005-08-11 16:25:56 -0400727 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400728 case -ENOTSOCK:
729 status = -ENOTCONN;
730 /* Should we call xs_close() here? */
731 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400732 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400733 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400734 break;
735 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500736 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400737 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400738 case -ECONNRESET:
Trond Myklebust55420c22009-03-11 15:29:24 -0400739 case -EPIPE:
Trond Myklebuste06799f2007-11-05 15:44:12 -0500740 xs_tcp_shutdown(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400741 case -ECONNREFUSED:
742 case -ENOTCONN:
Chuck Lever262965f2005-08-11 16:25:56 -0400743 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400744 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800745
Chuck Levera246b012005-08-11 16:25:23 -0400746 return status;
747}
748
Chuck Lever9903cd12005-08-11 16:25:26 -0400749/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400750 * xs_tcp_release_xprt - clean up after a tcp transmission
751 * @xprt: transport
752 * @task: rpc task
753 *
754 * This cleans up if an error causes us to abort the transmission of a request.
755 * In this case, the socket may need to be reset in order to avoid confusing
756 * the server.
757 */
758static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
759{
760 struct rpc_rqst *req;
761
762 if (task != xprt->snd_task)
763 return;
764 if (task == NULL)
765 goto out_release;
766 req = task->tk_rqstp;
Trond Myklebust43cedbf2011-07-17 16:01:03 -0400767 if (req == NULL)
768 goto out_release;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400769 if (req->rq_bytes_sent == 0)
770 goto out_release;
771 if (req->rq_bytes_sent == req->rq_snd_buf.len)
772 goto out_release;
773 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
774out_release:
775 xprt_release_xprt(xprt, task);
776}
777
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400778static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
779{
780 transport->old_data_ready = sk->sk_data_ready;
781 transport->old_state_change = sk->sk_state_change;
782 transport->old_write_space = sk->sk_write_space;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400783}
784
785static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
786{
787 sk->sk_data_ready = transport->old_data_ready;
788 sk->sk_state_change = transport->old_state_change;
789 sk->sk_write_space = transport->old_write_space;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400790}
791
Chuck Leverfe315e72009-03-11 14:10:21 -0400792static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400793{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500794 struct socket *sock = transport->sock;
795 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400796
Chuck Leverfe315e72009-03-11 14:10:21 -0400797 if (sk == NULL)
798 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400799
Trond Myklebust246408d2011-03-22 18:40:10 -0400800 transport->srcport = 0;
801
Chuck Levera246b012005-08-11 16:25:23 -0400802 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500803 transport->inet = NULL;
804 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400805
Chuck Lever9903cd12005-08-11 16:25:26 -0400806 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400807
808 xs_restore_old_callbacks(transport, sk);
Chuck Levera246b012005-08-11 16:25:23 -0400809 write_unlock_bh(&sk->sk_callback_lock);
810
Chuck Lever9903cd12005-08-11 16:25:26 -0400811 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400812
813 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -0400814}
815
816/**
817 * xs_close - close a socket
818 * @xprt: transport
819 *
820 * This is used when all requests are complete; ie, no DRC state remains
821 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400822 *
823 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
824 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e72009-03-11 14:10:21 -0400825 */
826static void xs_close(struct rpc_xprt *xprt)
827{
828 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
829
830 dprintk("RPC: xs_close xprt %p\n", xprt);
831
832 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -0400833 xprt->reestablish_timeout = 0;
Chuck Leverfe315e72009-03-11 14:10:21 -0400834
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100835 smp_mb__before_clear_bit();
Trond Myklebust7d1e8252009-03-11 14:38:03 -0400836 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100837 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -0500838 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100839 smp_mb__after_clear_bit();
Trond Myklebust62da3b22007-11-06 18:44:20 -0500840 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400841}
842
Trond Myklebustf75e6742009-04-21 17:18:20 -0400843static void xs_tcp_close(struct rpc_xprt *xprt)
844{
845 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
846 xs_close(xprt);
847 else
848 xs_tcp_shutdown(xprt);
849}
850
Chuck Lever9903cd12005-08-11 16:25:26 -0400851/**
852 * xs_destroy - prepare to shutdown a transport
853 * @xprt: doomed transport
854 *
855 */
856static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400857{
Chuck Leverc8475462006-12-05 16:35:26 -0500858 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
859
Chuck Lever46121cf2007-01-31 12:14:08 -0500860 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400861
Tejun Heoafe2c512010-12-14 16:21:17 +0100862 cancel_delayed_work_sync(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400863
Chuck Lever9903cd12005-08-11 16:25:26 -0400864 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400865 xs_free_peer_addresses(xprt);
Pavel Emelyanove204e622010-09-29 16:03:13 +0400866 xprt_free(xprt);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -0400867 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400868}
869
Chuck Lever9903cd12005-08-11 16:25:26 -0400870static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400871{
Chuck Lever9903cd12005-08-11 16:25:26 -0400872 return (struct rpc_xprt *) sk->sk_user_data;
873}
874
Chuck Lever176e21e2011-05-09 15:22:44 -0400875static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
876{
877 struct xdr_skb_reader desc = {
878 .skb = skb,
879 .offset = sizeof(rpc_fraghdr),
880 .count = skb->len - sizeof(rpc_fraghdr),
881 };
882
883 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
884 return -1;
885 if (desc.count)
886 return -1;
887 return 0;
888}
889
890/**
891 * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets
892 * @sk: socket with data to read
893 * @len: how much data to read
894 *
895 * Currently this assumes we can read the whole reply in a single gulp.
896 */
897static void xs_local_data_ready(struct sock *sk, int len)
898{
899 struct rpc_task *task;
900 struct rpc_xprt *xprt;
901 struct rpc_rqst *rovr;
902 struct sk_buff *skb;
903 int err, repsize, copied;
904 u32 _xid;
905 __be32 *xp;
906
907 read_lock_bh(&sk->sk_callback_lock);
908 dprintk("RPC: %s...\n", __func__);
909 xprt = xprt_from_sock(sk);
910 if (xprt == NULL)
911 goto out;
912
913 skb = skb_recv_datagram(sk, 0, 1, &err);
914 if (skb == NULL)
915 goto out;
916
917 if (xprt->shutdown)
918 goto dropit;
919
920 repsize = skb->len - sizeof(rpc_fraghdr);
921 if (repsize < 4) {
922 dprintk("RPC: impossible RPC reply size %d\n", repsize);
923 goto dropit;
924 }
925
926 /* Copy the XID from the skb... */
927 xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
928 if (xp == NULL)
929 goto dropit;
930
931 /* Look up and lock the request corresponding to the given XID */
932 spin_lock(&xprt->transport_lock);
933 rovr = xprt_lookup_rqst(xprt, *xp);
934 if (!rovr)
935 goto out_unlock;
936 task = rovr->rq_task;
937
938 copied = rovr->rq_private_buf.buflen;
939 if (copied > repsize)
940 copied = repsize;
941
942 if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
943 dprintk("RPC: sk_buff copy failed\n");
944 goto out_unlock;
945 }
946
947 xprt_complete_rqst(task, copied);
948
949 out_unlock:
950 spin_unlock(&xprt->transport_lock);
951 dropit:
952 skb_free_datagram(sk, skb);
953 out:
954 read_unlock_bh(&sk->sk_callback_lock);
955}
956
Chuck Lever9903cd12005-08-11 16:25:26 -0400957/**
958 * xs_udp_data_ready - "data ready" callback for UDP sockets
959 * @sk: socket with data to read
960 * @len: how much data to read
961 *
962 */
963static void xs_udp_data_ready(struct sock *sk, int len)
964{
965 struct rpc_task *task;
966 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400967 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400968 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400969 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700970 u32 _xid;
971 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400972
Eric Dumazetf064af12010-09-22 12:43:39 +0000973 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500974 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400975 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400976 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400977
978 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
979 goto out;
980
981 if (xprt->shutdown)
982 goto dropit;
983
984 repsize = skb->len - sizeof(struct udphdr);
985 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500986 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400987 goto dropit;
988 }
989
990 /* Copy the XID from the skb... */
991 xp = skb_header_pointer(skb, sizeof(struct udphdr),
992 sizeof(_xid), &_xid);
993 if (xp == NULL)
994 goto dropit;
995
996 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400997 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400998 rovr = xprt_lookup_rqst(xprt, *xp);
999 if (!rovr)
1000 goto out_unlock;
1001 task = rovr->rq_task;
1002
Chuck Levera246b012005-08-11 16:25:23 -04001003 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1004 copied = repsize;
1005
1006 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001007 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
1008 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -04001009 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001010 }
1011
1012 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -04001013
1014 /* Something worked... */
Eric Dumazetadf30902009-06-02 05:19:30 +00001015 dst_confirm(skb_dst(skb));
Chuck Levera246b012005-08-11 16:25:23 -04001016
Chuck Lever1570c1e2005-08-25 16:25:52 -07001017 xprt_adjust_cwnd(task, copied);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001018 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -04001019
1020 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001021 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001022 dropit:
1023 skb_free_datagram(sk, skb);
1024 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001025 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001026}
1027
Trond Myklebuste23c80e2012-09-12 16:49:15 -04001028/*
1029 * Helper function to force a TCP close if the server is sending
1030 * junk and/or it has put us in CLOSE_WAIT
1031 */
1032static void xs_tcp_force_close(struct rpc_xprt *xprt)
1033{
1034 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1035 xprt_force_disconnect(xprt);
1036}
1037
Chuck Leverdd456472006-12-05 16:35:44 -05001038static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001039{
Chuck Lever51971132006-12-05 16:35:19 -05001040 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001041 size_t len, used;
1042 char *p;
1043
Chuck Lever51971132006-12-05 16:35:19 -05001044 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
1045 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001046 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001047 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001048 if (used != len)
1049 return;
Chuck Lever808012f2005-08-25 16:25:49 -07001050
Chuck Lever51971132006-12-05 16:35:19 -05001051 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
1052 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -05001053 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -04001054 else
Chuck Levere136d092006-12-05 16:35:23 -05001055 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -05001056 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -07001057
Chuck Levere136d092006-12-05 16:35:23 -05001058 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001059 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -07001060
Chuck Levera246b012005-08-11 16:25:23 -04001061 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001062 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001063 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebuste23c80e2012-09-12 16:49:15 -04001064 xs_tcp_force_close(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001065 return;
Chuck Levera246b012005-08-11 16:25:23 -04001066 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001067 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -05001068 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001069}
1070
Chuck Lever51971132006-12-05 16:35:19 -05001071static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001072{
Chuck Lever51971132006-12-05 16:35:19 -05001073 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001074 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001075 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001076 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
1077 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1078 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001079 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001080 }
1081 }
1082}
1083
Chuck Leverdd456472006-12-05 16:35:44 -05001084static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001085{
1086 size_t len, used;
1087 char *p;
1088
Chuck Lever51971132006-12-05 16:35:19 -05001089 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -05001090 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001091 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001092 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001093 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001094 if (used != len)
1095 return;
Chuck Levere136d092006-12-05 16:35:23 -05001096 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001097 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -05001098 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001099 dprintk("RPC: reading %s XID %08x\n",
1100 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1101 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -05001102 ntohl(transport->tcp_xid));
1103 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001104}
1105
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001106static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1107 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001108{
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001109 size_t len, used;
1110 u32 offset;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001111 char *p;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001112
1113 /*
1114 * We want transport->tcp_offset to be 8 at the end of this routine
1115 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1116 * When this function is called for the first time,
1117 * transport->tcp_offset is 4 (after having already read the xid).
1118 */
1119 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001120 len = sizeof(transport->tcp_calldir) - offset;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001121 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001122 p = ((char *) &transport->tcp_calldir) + offset;
1123 used = xdr_skb_read_bits(desc, p, len);
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001124 transport->tcp_offset += used;
1125 if (used != len)
1126 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001127 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001128 /*
1129 * We don't yet have the XDR buffer, so we will write the calldir
1130 * out after we get the buffer from the 'struct rpc_rqst'
1131 */
Trond Myklebustb76ce562010-06-16 13:57:32 -04001132 switch (ntohl(transport->tcp_calldir)) {
1133 case RPC_REPLY:
1134 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1135 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001136 transport->tcp_flags |= TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001137 break;
1138 case RPC_CALL:
1139 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1140 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001141 transport->tcp_flags &= ~TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001142 break;
1143 default:
1144 dprintk("RPC: invalid request message type\n");
Trond Myklebuste23c80e2012-09-12 16:49:15 -04001145 xs_tcp_force_close(&transport->xprt);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001146 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001147 xs_tcp_check_fraghdr(transport);
1148}
1149
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001150static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
1151 struct xdr_skb_reader *desc,
1152 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001153{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001154 struct sock_xprt *transport =
1155 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001156 struct xdr_buf *rcvbuf;
1157 size_t len;
1158 ssize_t r;
1159
Chuck Levera246b012005-08-11 16:25:23 -04001160 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001161
1162 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1163 /*
1164 * Save the RPC direction in the XDR buffer
1165 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001166 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
Trond Myklebustb76ce562010-06-16 13:57:32 -04001167 &transport->tcp_calldir,
1168 sizeof(transport->tcp_calldir));
1169 transport->tcp_copied += sizeof(transport->tcp_calldir);
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001170 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -04001171 }
1172
Chuck Levera246b012005-08-11 16:25:23 -04001173 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -05001174 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -05001175 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -04001176
Chuck Lever51971132006-12-05 16:35:19 -05001177 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001178 memcpy(&my_desc, desc, sizeof(my_desc));
1179 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -05001180 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001181 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001182 desc->count -= r;
1183 desc->offset += r;
1184 } else
Chuck Lever51971132006-12-05 16:35:19 -05001185 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001186 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001187
1188 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001189 transport->tcp_copied += r;
1190 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001191 }
1192 if (r != len) {
1193 /* Error when copying to the receive buffer,
1194 * usually because we weren't able to allocate
1195 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001196 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001197 * will not receive any additional updates,
1198 * and time out.
1199 * Any remaining data from this record will
1200 * be discarded.
1201 */
Chuck Levere136d092006-12-05 16:35:23 -05001202 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001203 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001204 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001205 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1206 "tcp_offset = %u, tcp_reclen = %u\n",
1207 xprt, transport->tcp_copied,
1208 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001209 return;
Chuck Levera246b012005-08-11 16:25:23 -04001210 }
1211
Chuck Lever46121cf2007-01-31 12:14:08 -05001212 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001213 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001214 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1215 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1216 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001217
Chuck Lever51971132006-12-05 16:35:19 -05001218 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001219 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001220 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001221 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1222 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001223 }
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001224}
1225
1226/*
1227 * Finds the request corresponding to the RPC xid and invokes the common
1228 * tcp read code to read the data.
1229 */
1230static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1231 struct xdr_skb_reader *desc)
1232{
1233 struct sock_xprt *transport =
1234 container_of(xprt, struct sock_xprt, xprt);
1235 struct rpc_rqst *req;
1236
1237 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1238
1239 /* Find and lock the request corresponding to this xid */
1240 spin_lock(&xprt->transport_lock);
1241 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1242 if (!req) {
1243 dprintk("RPC: XID %08x request not found!\n",
1244 ntohl(transport->tcp_xid));
1245 spin_unlock(&xprt->transport_lock);
1246 return -1;
1247 }
1248
1249 xs_tcp_read_common(xprt, desc, req);
1250
Chuck Levere136d092006-12-05 16:35:23 -05001251 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001252 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001253
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001254 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001255 return 0;
1256}
1257
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001258#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001259/*
1260 * Obtains an rpc_rqst previously allocated and invokes the common
1261 * tcp read code to read the data. The result is placed in the callback
1262 * queue.
1263 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1264 * connection and return -1.
1265 */
1266static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1267 struct xdr_skb_reader *desc)
1268{
1269 struct sock_xprt *transport =
1270 container_of(xprt, struct sock_xprt, xprt);
1271 struct rpc_rqst *req;
1272
1273 req = xprt_alloc_bc_request(xprt);
1274 if (req == NULL) {
1275 printk(KERN_WARNING "Callback slot table overflowed\n");
1276 xprt_force_disconnect(xprt);
1277 return -1;
1278 }
1279
1280 req->rq_xid = transport->tcp_xid;
1281 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1282 xs_tcp_read_common(xprt, desc, req);
1283
1284 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1285 struct svc_serv *bc_serv = xprt->bc_serv;
1286
1287 /*
1288 * Add callback request to callback list. The callback
1289 * service sleeps on the sv_cb_waitq waiting for new
1290 * requests. Wake it up after adding enqueing the
1291 * request.
1292 */
1293 dprintk("RPC: add callback request to list\n");
1294 spin_lock(&bc_serv->sv_cb_lock);
1295 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1296 spin_unlock(&bc_serv->sv_cb_lock);
1297 wake_up(&bc_serv->sv_cb_waitq);
1298 }
1299
1300 req->rq_private_buf.len = transport->tcp_copied;
1301
1302 return 0;
1303}
1304
1305static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1306 struct xdr_skb_reader *desc)
1307{
1308 struct sock_xprt *transport =
1309 container_of(xprt, struct sock_xprt, xprt);
1310
1311 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1312 xs_tcp_read_reply(xprt, desc) :
1313 xs_tcp_read_callback(xprt, desc);
1314}
1315#else
1316static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1317 struct xdr_skb_reader *desc)
1318{
1319 return xs_tcp_read_reply(xprt, desc);
1320}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001321#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001322
1323/*
1324 * Read data off the transport. This can be either an RPC_CALL or an
1325 * RPC_REPLY. Relay the processing to helper functions.
1326 */
1327static void xs_tcp_read_data(struct rpc_xprt *xprt,
1328 struct xdr_skb_reader *desc)
1329{
1330 struct sock_xprt *transport =
1331 container_of(xprt, struct sock_xprt, xprt);
1332
1333 if (_xs_tcp_read_data(xprt, desc) == 0)
1334 xs_tcp_check_fraghdr(transport);
1335 else {
1336 /*
1337 * The transport_lock protects the request handling.
1338 * There's no need to hold it to update the tcp_flags.
1339 */
1340 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1341 }
Chuck Levera246b012005-08-11 16:25:23 -04001342}
1343
Chuck Leverdd456472006-12-05 16:35:44 -05001344static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001345{
1346 size_t len;
1347
Chuck Lever51971132006-12-05 16:35:19 -05001348 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001349 if (len > desc->count)
1350 len = desc->count;
1351 desc->count -= len;
1352 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001353 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001354 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001355 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001356}
1357
Chuck Lever9903cd12005-08-11 16:25:26 -04001358static 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 -04001359{
1360 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001361 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001362 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001363 .skb = skb,
1364 .offset = offset,
1365 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001366 };
Chuck Levera246b012005-08-11 16:25:23 -04001367
Chuck Lever46121cf2007-01-31 12:14:08 -05001368 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001369 do {
1370 /* Read in a new fragment marker if necessary */
1371 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001372 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001373 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001374 continue;
1375 }
1376 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001377 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001378 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001379 continue;
1380 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001381 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001382 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001383 xs_tcp_read_calldir(transport, &desc);
1384 continue;
1385 }
Chuck Levera246b012005-08-11 16:25:23 -04001386 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001387 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001388 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001389 continue;
1390 }
1391 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001392 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001393 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001394 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001395 return len - desc.count;
1396}
1397
Chuck Lever9903cd12005-08-11 16:25:26 -04001398/**
1399 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1400 * @sk: socket with data to read
1401 * @bytes: how much data to read
1402 *
1403 */
1404static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001405{
1406 struct rpc_xprt *xprt;
1407 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001408 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001409
Chuck Lever46121cf2007-01-31 12:14:08 -05001410 dprintk("RPC: xs_tcp_data_ready...\n");
1411
Eric Dumazetf064af12010-09-22 12:43:39 +00001412 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001413 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001414 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001415 if (xprt->shutdown)
1416 goto out;
1417
Neil Brown61d0a8e2009-09-23 14:36:37 -04001418 /* Any data means we had a useful conversation, so
1419 * the we don't need to delay the next reconnect
1420 */
1421 if (xprt->reestablish_timeout)
1422 xprt->reestablish_timeout = 0;
1423
Chuck Lever9903cd12005-08-11 16:25:26 -04001424 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001425 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001426 do {
1427 rd_desc.count = 65536;
1428 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1429 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001430out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001431 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001432}
1433
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001434/*
1435 * Do the equivalent of linger/linger2 handling for dealing with
1436 * broken servers that don't close the socket in a timely
1437 * fashion
1438 */
1439static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1440 unsigned long timeout)
1441{
1442 struct sock_xprt *transport;
1443
1444 if (xprt_test_and_set_connecting(xprt))
1445 return;
1446 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1447 transport = container_of(xprt, struct sock_xprt, xprt);
1448 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1449 timeout);
1450}
1451
1452static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1453{
1454 struct sock_xprt *transport;
1455
1456 transport = container_of(xprt, struct sock_xprt, xprt);
1457
1458 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1459 !cancel_delayed_work(&transport->connect_worker))
1460 return;
1461 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1462 xprt_clear_connecting(xprt);
1463}
1464
1465static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1466{
1467 smp_mb__before_clear_bit();
Trond Myklebuste23c80e2012-09-12 16:49:15 -04001468 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1469 clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001470 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1471 clear_bit(XPRT_CLOSING, &xprt->state);
1472 smp_mb__after_clear_bit();
1473 /* Mark transport as closed and wake up all pending tasks */
1474 xprt_disconnect_done(xprt);
1475}
1476
Chuck Lever9903cd12005-08-11 16:25:26 -04001477/**
1478 * xs_tcp_state_change - callback to handle TCP socket state changes
1479 * @sk: socket whose state has changed
1480 *
1481 */
1482static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001483{
Chuck Lever9903cd12005-08-11 16:25:26 -04001484 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001485
Eric Dumazetf064af12010-09-22 12:43:39 +00001486 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001487 if (!(xprt = xprt_from_sock(sk)))
1488 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001489 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001490 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001491 sk->sk_state, xprt_connected(xprt),
1492 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001493 sock_flag(sk, SOCK_ZAPPED),
1494 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001495
1496 switch (sk->sk_state) {
1497 case TCP_ESTABLISHED:
Eric Dumazetf064af12010-09-22 12:43:39 +00001498 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001499 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001500 struct sock_xprt *transport = container_of(xprt,
1501 struct sock_xprt, xprt);
1502
Chuck Levera246b012005-08-11 16:25:23 -04001503 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001504 transport->tcp_offset = 0;
1505 transport->tcp_reclen = 0;
1506 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001507 transport->tcp_flags =
1508 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001509
Trond Myklebust2a491992009-03-11 14:38:00 -04001510 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001511 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001512 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001513 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001514 case TCP_FIN_WAIT1:
1515 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001516 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001517 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001518 set_bit(XPRT_CLOSING, &xprt->state);
1519 smp_mb__before_clear_bit();
1520 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001521 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001522 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001523 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001524 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001525 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001526 /* The server initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001527 xprt->connect_cookie++;
Trond Myklebust362eb452012-10-23 11:35:47 -04001528 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebuste23c80e2012-09-12 16:49:15 -04001529 xs_tcp_force_close(xprt);
Trond Myklebust663b8852008-01-01 18:42:12 -05001530 case TCP_CLOSING:
1531 /*
1532 * If the server closed down the connection, make sure that
1533 * we back off before reconnecting
1534 */
1535 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1536 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001537 break;
1538 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001539 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001540 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001541 smp_mb__before_clear_bit();
1542 clear_bit(XPRT_CONNECTED, &xprt->state);
1543 smp_mb__after_clear_bit();
1544 break;
1545 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001546 xs_tcp_cancel_linger_timeout(xprt);
1547 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001548 }
1549 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001550 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001551}
1552
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001553static void xs_write_space(struct sock *sk)
1554{
1555 struct socket *sock;
1556 struct rpc_xprt *xprt;
1557
1558 if (unlikely(!(sock = sk->sk_socket)))
1559 return;
1560 clear_bit(SOCK_NOSPACE, &sock->flags);
1561
1562 if (unlikely(!(xprt = xprt_from_sock(sk))))
1563 return;
1564 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1565 return;
1566
1567 xprt_write_space(xprt);
1568}
1569
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001570/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001571 * xs_udp_write_space - callback invoked when socket buffer space
1572 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001573 * @sk: socket whose state has changed
1574 *
Chuck Levera246b012005-08-11 16:25:23 -04001575 * Called when more output buffer space is available for this socket.
1576 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001577 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001578 * with a bunch of small requests.
1579 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001580static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001581{
Eric Dumazetf064af12010-09-22 12:43:39 +00001582 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001583
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001584 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001585 if (sock_writeable(sk))
1586 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001587
Eric Dumazetf064af12010-09-22 12:43:39 +00001588 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001589}
Chuck Levera246b012005-08-11 16:25:23 -04001590
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001591/**
1592 * xs_tcp_write_space - callback invoked when socket buffer space
1593 * becomes available
1594 * @sk: socket whose state has changed
1595 *
1596 * Called when more output buffer space is available for this socket.
1597 * We try not to wake our writers until they can make "significant"
1598 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1599 * with a bunch of small requests.
1600 */
1601static void xs_tcp_write_space(struct sock *sk)
1602{
Eric Dumazetf064af12010-09-22 12:43:39 +00001603 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001604
1605 /* from net/core/stream.c:sk_stream_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001606 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1607 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001608
Eric Dumazetf064af12010-09-22 12:43:39 +00001609 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001610}
1611
Chuck Lever470056c2005-08-25 16:25:56 -07001612static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001613{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001614 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1615 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001616
Chuck Lever7c6e0662006-12-05 16:35:30 -05001617 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001618 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001619 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001620 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001621 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001622 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001623 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001624 sk->sk_write_space(sk);
1625 }
1626}
1627
Chuck Lever43118c22005-08-25 16:25:49 -07001628/**
Chuck Lever470056c2005-08-25 16:25:56 -07001629 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001630 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001631 * @sndsize: requested size of send buffer, in bytes
1632 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001633 *
Chuck Lever470056c2005-08-25 16:25:56 -07001634 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001635 */
Chuck Lever470056c2005-08-25 16:25:56 -07001636static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001637{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001638 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1639
1640 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001641 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001642 transport->sndsize = sndsize + 1024;
1643 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001644 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001645 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001646
1647 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001648}
1649
Chuck Lever46c0ee82005-08-25 16:25:52 -07001650/**
1651 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1652 * @task: task that timed out
1653 *
1654 * Adjust the congestion window after a retransmit timeout has occurred.
1655 */
1656static void xs_udp_timer(struct rpc_task *task)
1657{
1658 xprt_adjust_cwnd(task, -ETIMEDOUT);
1659}
1660
Chuck Leverb85d8802006-05-25 01:40:49 -04001661static unsigned short xs_get_random_port(void)
1662{
1663 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1664 unsigned short rand = (unsigned short) net_random() % range;
1665 return rand + xprt_min_resvport;
1666}
1667
Chuck Lever92200412006-01-03 09:55:51 +01001668/**
1669 * xs_set_port - reset the port number in the remote endpoint address
1670 * @xprt: generic transport
1671 * @port: new port number
1672 *
1673 */
1674static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1675{
Chuck Lever46121cf2007-01-31 12:14:08 -05001676 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001677
Chuck Lever9dc3b092009-08-09 15:09:46 -04001678 rpc_set_port(xs_addr(xprt), port);
1679 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001680}
1681
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001682static unsigned short xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001683{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001684 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001685
1686 if (port == 0 && transport->xprt.resvport)
1687 port = xs_get_random_port();
1688 return port;
1689}
1690
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001691static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001692{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001693 if (transport->srcport != 0)
1694 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001695 if (!transport->xprt.resvport)
1696 return 0;
1697 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1698 return xprt_max_resvport;
1699 return --port;
1700}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001701static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001702{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001703 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001704 int err, nloop = 0;
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001705 unsigned short port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001706 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001707
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001708 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001709 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001710 rpc_set_port((struct sockaddr *)&myaddr, port);
1711 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1712 transport->xprt.addrlen);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001713 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001714 break;
Chuck Levera246b012005-08-11 16:25:23 -04001715 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001716 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001717 break;
Chuck Levera246b012005-08-11 16:25:23 -04001718 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001719 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001720 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001721 if (port > last)
1722 nloop++;
1723 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001724
Chuck Lever4232e862010-10-20 11:52:51 -04001725 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001726 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1727 &((struct sockaddr_in *)&myaddr)->sin_addr,
1728 port, err ? "failed" : "ok", err);
1729 else
1730 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1731 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1732 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001733 return err;
1734}
1735
Chuck Lever176e21e2011-05-09 15:22:44 -04001736/*
1737 * We don't support autobind on AF_LOCAL sockets
1738 */
1739static void xs_local_rpcbind(struct rpc_task *task)
1740{
1741 xprt_set_bound(task->tk_xprt);
1742}
1743
1744static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1745{
1746}
Chuck Levera246b012005-08-11 16:25:23 -04001747
Peter Zijlstraed075362006-12-06 20:35:24 -08001748#ifdef CONFIG_DEBUG_LOCK_ALLOC
1749static struct lock_class_key xs_key[2];
1750static struct lock_class_key xs_slock_key[2];
1751
Chuck Lever176e21e2011-05-09 15:22:44 -04001752static inline void xs_reclassify_socketu(struct socket *sock)
1753{
1754 struct sock *sk = sock->sk;
1755
1756 BUG_ON(sock_owned_by_user(sk));
1757 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1758 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1759}
1760
Chuck Lever8945ee52007-08-06 11:58:04 -04001761static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001762{
1763 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001764
John Heffner02b3d342007-09-12 10:42:12 +02001765 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001766 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1767 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1768}
Peter Zijlstraed075362006-12-06 20:35:24 -08001769
Chuck Lever8945ee52007-08-06 11:58:04 -04001770static inline void xs_reclassify_socket6(struct socket *sock)
1771{
1772 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001773
Linus Torvaldsf4921af2007-10-15 10:46:05 -07001774 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001775 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1776 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001777}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001778
1779static inline void xs_reclassify_socket(int family, struct socket *sock)
1780{
Chuck Lever4232e862010-10-20 11:52:51 -04001781 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001782 case AF_LOCAL:
1783 xs_reclassify_socketu(sock);
1784 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001785 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001786 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001787 break;
1788 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001789 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001790 break;
1791 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001792}
Peter Zijlstraed075362006-12-06 20:35:24 -08001793#else
Chuck Lever176e21e2011-05-09 15:22:44 -04001794static inline void xs_reclassify_socketu(struct socket *sock)
1795{
1796}
1797
Chuck Lever8945ee52007-08-06 11:58:04 -04001798static inline void xs_reclassify_socket4(struct socket *sock)
1799{
1800}
1801
1802static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001803{
1804}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001805
1806static inline void xs_reclassify_socket(int family, struct socket *sock)
1807{
1808}
Peter Zijlstraed075362006-12-06 20:35:24 -08001809#endif
1810
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001811static struct socket *xs_create_sock(struct rpc_xprt *xprt,
1812 struct sock_xprt *transport, int family, int type, int protocol)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001813{
1814 struct socket *sock;
1815 int err;
1816
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001817 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001818 if (err < 0) {
1819 dprintk("RPC: can't create %d transport socket (%d).\n",
1820 protocol, -err);
1821 goto out;
1822 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001823 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001824
Ben Hutchings4cea2882011-02-22 21:54:34 +00001825 err = xs_bind(transport, sock);
1826 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001827 sock_release(sock);
1828 goto out;
1829 }
1830
1831 return sock;
1832out:
1833 return ERR_PTR(err);
1834}
1835
Chuck Lever176e21e2011-05-09 15:22:44 -04001836static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1837 struct socket *sock)
1838{
1839 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1840 xprt);
1841
1842 if (!transport->inet) {
1843 struct sock *sk = sock->sk;
1844
1845 write_lock_bh(&sk->sk_callback_lock);
1846
1847 xs_save_old_callbacks(transport, sk);
1848
1849 sk->sk_user_data = xprt;
1850 sk->sk_data_ready = xs_local_data_ready;
1851 sk->sk_write_space = xs_udp_write_space;
Chuck Lever176e21e2011-05-09 15:22:44 -04001852 sk->sk_allocation = GFP_ATOMIC;
1853
1854 xprt_clear_connected(xprt);
1855
1856 /* Reset to new socket */
1857 transport->sock = sock;
1858 transport->inet = sk;
1859
1860 write_unlock_bh(&sk->sk_callback_lock);
1861 }
1862
1863 /* Tell the socket layer to start connecting... */
1864 xprt->stat.connect_count++;
1865 xprt->stat.connect_start = jiffies;
1866 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1867}
1868
1869/**
1870 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1871 * @xprt: RPC transport to connect
1872 * @transport: socket transport to connect
1873 * @create_sock: function to create a socket of the correct type
1874 *
1875 * Invoked by a work queue tasklet.
1876 */
1877static void xs_local_setup_socket(struct work_struct *work)
1878{
1879 struct sock_xprt *transport =
1880 container_of(work, struct sock_xprt, connect_worker.work);
1881 struct rpc_xprt *xprt = &transport->xprt;
1882 struct socket *sock;
1883 int status = -EIO;
1884
1885 if (xprt->shutdown)
1886 goto out;
1887
Jeff Layton05630012012-07-23 13:58:51 -04001888 current->flags |= PF_FSTRANS;
1889
Chuck Lever176e21e2011-05-09 15:22:44 -04001890 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1891 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1892 SOCK_STREAM, 0, &sock, 1);
1893 if (status < 0) {
1894 dprintk("RPC: can't create AF_LOCAL "
1895 "transport socket (%d).\n", -status);
1896 goto out;
1897 }
1898 xs_reclassify_socketu(sock);
1899
1900 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1901 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1902
1903 status = xs_local_finish_connecting(xprt, sock);
1904 switch (status) {
1905 case 0:
1906 dprintk("RPC: xprt %p connected to %s\n",
1907 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1908 xprt_set_connected(xprt);
1909 break;
1910 case -ENOENT:
1911 dprintk("RPC: xprt %p: socket %s does not exist\n",
1912 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1913 break;
1914 default:
1915 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1916 __func__, -status,
1917 xprt->address_strings[RPC_DISPLAY_ADDR]);
1918 }
1919
1920out:
1921 xprt_clear_connecting(xprt);
1922 xprt_wake_pending_tasks(xprt, status);
Jeff Layton05630012012-07-23 13:58:51 -04001923 current->flags &= ~PF_FSTRANS;
Chuck Lever176e21e2011-05-09 15:22:44 -04001924}
1925
Chuck Lever16be2d22007-08-06 11:57:38 -04001926static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001927{
Chuck Lever16be2d22007-08-06 11:57:38 -04001928 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001929
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001930 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001931 struct sock *sk = sock->sk;
1932
1933 write_lock_bh(&sk->sk_callback_lock);
1934
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001935 xs_save_old_callbacks(transport, sk);
1936
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001937 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001938 sk->sk_data_ready = xs_udp_data_ready;
1939 sk->sk_write_space = xs_udp_write_space;
1940 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa72005-12-13 16:13:52 -05001941 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001942
1943 xprt_set_connected(xprt);
1944
1945 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001946 transport->sock = sock;
1947 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001948
1949 write_unlock_bh(&sk->sk_callback_lock);
1950 }
Chuck Lever470056c2005-08-25 16:25:56 -07001951 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04001952}
1953
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04001954static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04001955{
1956 struct sock_xprt *transport =
1957 container_of(work, struct sock_xprt, connect_worker.work);
1958 struct rpc_xprt *xprt = &transport->xprt;
1959 struct socket *sock = transport->sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04001960 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04001961
Trond Myklebust01d37c42009-03-11 14:09:39 -04001962 if (xprt->shutdown)
Chuck Lever9903cd12005-08-11 16:25:26 -04001963 goto out;
1964
Jeff Layton05630012012-07-23 13:58:51 -04001965 current->flags |= PF_FSTRANS;
1966
Chuck Levera246b012005-08-11 16:25:23 -04001967 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001968 xs_reset_transport(transport);
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04001969 sock = xs_create_sock(xprt, transport,
1970 xs_addr(xprt)->sa_family, SOCK_DGRAM, IPPROTO_UDP);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04001971 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04001972 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04001973
Chuck Leverc740eff2009-08-09 15:09:46 -04001974 dprintk("RPC: worker connecting xprt %p via %s to "
1975 "%s (port %s)\n", xprt,
1976 xprt->address_strings[RPC_DISPLAY_PROTO],
1977 xprt->address_strings[RPC_DISPLAY_ADDR],
1978 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04001979
1980 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001981 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001982out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001983 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001984 xprt_wake_pending_tasks(xprt, status);
Jeff Layton05630012012-07-23 13:58:51 -04001985 current->flags &= ~PF_FSTRANS;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001986}
1987
Chuck Lever3167e122005-08-25 16:25:55 -07001988/*
1989 * We need to preserve the port number so the reply cache on the server can
1990 * find our cached RPC replies when we get around to reconnecting.
1991 */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04001992static void xs_abort_connection(struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07001993{
1994 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07001995 struct sockaddr any;
1996
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04001997 dprintk("RPC: disconnecting xprt %p to reuse port\n", transport);
Chuck Lever3167e122005-08-25 16:25:55 -07001998
1999 /*
2000 * Disconnect the transport socket by doing a connect operation
2001 * with AF_UNSPEC. This should return immediately...
2002 */
2003 memset(&any, 0, sizeof(any));
2004 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002005 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002006 if (!result)
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002007 xs_sock_mark_closed(&transport->xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002008 else
Chuck Lever46121cf2007-01-31 12:14:08 -05002009 dprintk("RPC: AF_UNSPEC connect return code %d\n",
Chuck Lever3167e122005-08-25 16:25:55 -07002010 result);
2011}
2012
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002013static void xs_tcp_reuse_connection(struct sock_xprt *transport)
Trond Myklebust40d25492009-03-11 14:37:58 -04002014{
2015 unsigned int state = transport->inet->sk_state;
2016
Andy Chittenden669502f2010-08-10 10:19:53 -04002017 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) {
2018 /* we don't need to abort the connection if the socket
2019 * hasn't undergone a shutdown
2020 */
2021 if (transport->inet->sk_shutdown == 0)
2022 return;
2023 dprintk("RPC: %s: TCP_CLOSEd and sk_shutdown set to %d\n",
2024 __func__, transport->inet->sk_shutdown);
2025 }
2026 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) {
2027 /* we don't need to abort the connection if the socket
2028 * hasn't undergone a shutdown
2029 */
2030 if (transport->inet->sk_shutdown == 0)
2031 return;
2032 dprintk("RPC: %s: ESTABLISHED/SYN_SENT "
2033 "sk_shutdown set to %d\n",
2034 __func__, transport->inet->sk_shutdown);
2035 }
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002036 xs_abort_connection(transport);
Trond Myklebust40d25492009-03-11 14:37:58 -04002037}
2038
Chuck Lever16be2d22007-08-06 11:57:38 -04002039static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002040{
Chuck Lever16be2d22007-08-06 11:57:38 -04002041 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002042 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002043
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002044 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002045 struct sock *sk = sock->sk;
2046
2047 write_lock_bh(&sk->sk_callback_lock);
2048
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002049 xs_save_old_callbacks(transport, sk);
2050
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002051 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002052 sk->sk_data_ready = xs_tcp_data_ready;
2053 sk->sk_state_change = xs_tcp_state_change;
2054 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebustb079fa72005-12-13 16:13:52 -05002055 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07002056
2057 /* socket options */
2058 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
2059 sock_reset_flag(sk, SOCK_LINGER);
2060 tcp_sk(sk)->linger2 = 0;
2061 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002062
2063 xprt_clear_connected(xprt);
2064
2065 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002066 transport->sock = sock;
2067 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002068
2069 write_unlock_bh(&sk->sk_callback_lock);
2070 }
2071
Trond Myklebust01d37c42009-03-11 14:09:39 -04002072 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002073 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002074
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002075 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05002076 xprt->stat.connect_count++;
2077 xprt->stat.connect_start = jiffies;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002078 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2079 switch (ret) {
2080 case 0:
2081 case -EINPROGRESS:
2082 /* SYN_SENT! */
2083 xprt->connect_cookie++;
2084 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2085 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2086 }
2087out:
2088 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002089}
2090
2091/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002092 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2093 * @xprt: RPC transport to connect
2094 * @transport: socket transport to connect
2095 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04002096 *
2097 * Invoked by a work queue tasklet.
2098 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002099static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002100{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002101 struct sock_xprt *transport =
2102 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002103 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002104 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002105 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002106
Trond Myklebust01d37c42009-03-11 14:09:39 -04002107 if (xprt->shutdown)
Chuck Lever16be2d22007-08-06 11:57:38 -04002108 goto out;
2109
Jeff Layton05630012012-07-23 13:58:51 -04002110 current->flags |= PF_FSTRANS;
2111
Chuck Lever16be2d22007-08-06 11:57:38 -04002112 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002113 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002114 sock = xs_create_sock(xprt, transport,
2115 xs_addr(xprt)->sa_family, SOCK_STREAM, IPPROTO_TCP);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002116 if (IS_ERR(sock)) {
2117 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002118 goto out;
2119 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002120 } else {
2121 int abort_and_exit;
2122
2123 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
2124 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04002125 /* "close" the socket, preserving the local port */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002126 xs_tcp_reuse_connection(transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04002127
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002128 if (abort_and_exit)
2129 goto out_eagain;
2130 }
2131
Chuck Leverc740eff2009-08-09 15:09:46 -04002132 dprintk("RPC: worker connecting xprt %p via %s to "
2133 "%s (port %s)\n", xprt,
2134 xprt->address_strings[RPC_DISPLAY_PROTO],
2135 xprt->address_strings[RPC_DISPLAY_ADDR],
2136 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002137
2138 status = xs_tcp_finish_connecting(xprt, sock);
Chuck Lever46121cf2007-01-31 12:14:08 -05002139 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2140 xprt, -status, xprt_connected(xprt),
2141 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002142 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002143 default:
2144 printk("%s: connect returned unhandled error %d\n",
2145 __func__, status);
2146 case -EADDRNOTAVAIL:
2147 /* We're probably in TIME_WAIT. Get rid of existing socket,
2148 * and retry
2149 */
Trond Myklebuste23c80e2012-09-12 16:49:15 -04002150 xs_tcp_force_close(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002151 break;
Trond Myklebust8a2cec22009-03-11 14:38:01 -04002152 case -ECONNREFUSED:
2153 case -ECONNRESET:
2154 case -ENETUNREACH:
2155 /* retry with existing socket, after a delay */
Trond Myklebust2a491992009-03-11 14:38:00 -04002156 case 0:
2157 case -EINPROGRESS:
2158 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002159 xprt_clear_connecting(xprt);
Jeff Layton05630012012-07-23 13:58:51 -04002160 current->flags &= ~PF_FSTRANS;
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002161 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002162 case -EINVAL:
2163 /* Happens, for instance, if the user specified a link
2164 * local IPv6 address without a scope-id.
2165 */
2166 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002167 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002168out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04002169 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002170out:
Chuck Lever2226feb2005-08-11 16:25:38 -04002171 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002172 xprt_wake_pending_tasks(xprt, status);
Jeff Layton05630012012-07-23 13:58:51 -04002173 current->flags &= ~PF_FSTRANS;
Chuck Levera246b012005-08-11 16:25:23 -04002174}
2175
Chuck Lever68e220b2007-08-06 11:57:48 -04002176/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002177 * xs_connect - connect a socket to a remote endpoint
2178 * @task: address of RPC task that manages state of connect request
2179 *
2180 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002181 *
2182 * UDP socket connects are synchronous, but we use a work queue anyway
2183 * to guarantee that even unprivileged user processes can set up a
2184 * socket on a privileged port.
2185 *
2186 * If a UDP socket connect fails, the delay behavior here prevents
2187 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002188 */
2189static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002190{
2191 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002192 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002193
Chuck Lever09a21c42009-12-03 15:58:56 -05002194 if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002195 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2196 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002197 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002198 queue_delayed_work(rpciod_workqueue,
2199 &transport->connect_worker,
2200 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07002201 xprt->reestablish_timeout <<= 1;
Neil Brown61d0a8e2009-09-23 14:36:37 -04002202 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2203 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002204 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2205 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002206 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05002207 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002208 queue_delayed_work(rpciod_workqueue,
2209 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002210 }
2211}
2212
Chuck Lever262ca072006-03-20 13:44:16 -05002213/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002214 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2215 * @xprt: rpc_xprt struct containing statistics
2216 * @seq: output file
2217 *
2218 */
2219static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2220{
2221 long idle_time = 0;
2222
2223 if (xprt_connected(xprt))
2224 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2225
2226 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
Andy Adamson15a45202012-02-14 16:19:18 -05002227 "%llu %llu %lu %llu %llu\n",
Chuck Lever176e21e2011-05-09 15:22:44 -04002228 xprt->stat.bind_count,
2229 xprt->stat.connect_count,
2230 xprt->stat.connect_time,
2231 idle_time,
2232 xprt->stat.sends,
2233 xprt->stat.recvs,
2234 xprt->stat.bad_xids,
2235 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002236 xprt->stat.bklog_u,
2237 xprt->stat.max_slots,
2238 xprt->stat.sending_u,
2239 xprt->stat.pending_u);
Chuck Lever176e21e2011-05-09 15:22:44 -04002240}
2241
2242/**
Chuck Lever262ca072006-03-20 13:44:16 -05002243 * xs_udp_print_stats - display UDP socket-specifc stats
2244 * @xprt: rpc_xprt struct containing statistics
2245 * @seq: output file
2246 *
2247 */
2248static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2249{
Chuck Leverc8475462006-12-05 16:35:26 -05002250 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2251
Andy Adamson15a45202012-02-14 16:19:18 -05002252 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2253 "%lu %llu %llu\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,
Andy Adamson15a45202012-02-14 16:19:18 -05002260 xprt->stat.bklog_u,
2261 xprt->stat.max_slots,
2262 xprt->stat.sending_u,
2263 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002264}
2265
2266/**
2267 * xs_tcp_print_stats - display TCP socket-specifc stats
2268 * @xprt: rpc_xprt struct containing statistics
2269 * @seq: output file
2270 *
2271 */
2272static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2273{
Chuck Leverc8475462006-12-05 16:35:26 -05002274 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002275 long idle_time = 0;
2276
2277 if (xprt_connected(xprt))
2278 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2279
Andy Adamson15a45202012-02-14 16:19:18 -05002280 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2281 "%llu %llu %lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002282 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002283 xprt->stat.bind_count,
2284 xprt->stat.connect_count,
2285 xprt->stat.connect_time,
2286 idle_time,
2287 xprt->stat.sends,
2288 xprt->stat.recvs,
2289 xprt->stat.bad_xids,
2290 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002291 xprt->stat.bklog_u,
2292 xprt->stat.max_slots,
2293 xprt->stat.sending_u,
2294 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002295}
2296
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002297/*
2298 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2299 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2300 * to use the server side send routines.
2301 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002302static void *bc_malloc(struct rpc_task *task, size_t size)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002303{
2304 struct page *page;
2305 struct rpc_buffer *buf;
2306
2307 BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2308 page = alloc_page(GFP_KERNEL);
2309
2310 if (!page)
2311 return NULL;
2312
2313 buf = page_address(page);
2314 buf->len = PAGE_SIZE;
2315
2316 return buf->data;
2317}
2318
2319/*
2320 * Free the space allocated in the bc_alloc routine
2321 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002322static void bc_free(void *buffer)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002323{
2324 struct rpc_buffer *buf;
2325
2326 if (!buffer)
2327 return;
2328
2329 buf = container_of(buffer, struct rpc_buffer, data);
2330 free_page((unsigned long)buf);
2331}
2332
2333/*
2334 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2335 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2336 */
2337static int bc_sendto(struct rpc_rqst *req)
2338{
2339 int len;
2340 struct xdr_buf *xbufp = &req->rq_snd_buf;
2341 struct rpc_xprt *xprt = req->rq_xprt;
2342 struct sock_xprt *transport =
2343 container_of(xprt, struct sock_xprt, xprt);
2344 struct socket *sock = transport->sock;
2345 unsigned long headoff;
2346 unsigned long tailoff;
2347
Chuck Lever61677ee2011-05-09 15:22:34 -04002348 xs_encode_stream_record_marker(xbufp);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002349
2350 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2351 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2352 len = svc_send_common(sock, xbufp,
2353 virt_to_page(xbufp->head[0].iov_base), headoff,
2354 xbufp->tail[0].iov_base, tailoff);
2355
2356 if (len != xbufp->len) {
2357 printk(KERN_NOTICE "Error sending entire callback!\n");
2358 len = -EAGAIN;
2359 }
2360
2361 return len;
2362}
2363
2364/*
2365 * The send routine. Borrows from svc_send
2366 */
2367static int bc_send_request(struct rpc_task *task)
2368{
2369 struct rpc_rqst *req = task->tk_rqstp;
2370 struct svc_xprt *xprt;
2371 struct svc_sock *svsk;
2372 u32 len;
2373
2374 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2375 /*
2376 * Get the server socket associated with this callback xprt
2377 */
2378 xprt = req->rq_xprt->bc_xprt;
2379 svsk = container_of(xprt, struct svc_sock, sk_xprt);
2380
2381 /*
2382 * Grab the mutex to serialize data as the connection is shared
2383 * with the fore channel
2384 */
2385 if (!mutex_trylock(&xprt->xpt_mutex)) {
2386 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2387 if (!mutex_trylock(&xprt->xpt_mutex))
2388 return -EAGAIN;
2389 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2390 }
2391 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2392 len = -ENOTCONN;
2393 else
2394 len = bc_sendto(req);
2395 mutex_unlock(&xprt->xpt_mutex);
2396
2397 if (len > 0)
2398 len = 0;
2399
2400 return len;
2401}
2402
2403/*
2404 * The close routine. Since this is client initiated, we do nothing
2405 */
2406
2407static void bc_close(struct rpc_xprt *xprt)
2408{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002409}
2410
2411/*
2412 * The xprt destroy routine. Again, because this connection is client
2413 * initiated, we do nothing
2414 */
2415
2416static void bc_destroy(struct rpc_xprt *xprt)
2417{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002418}
2419
Chuck Lever176e21e2011-05-09 15:22:44 -04002420static struct rpc_xprt_ops xs_local_ops = {
2421 .reserve_xprt = xprt_reserve_xprt,
2422 .release_xprt = xs_tcp_release_xprt,
Trond Myklebust49c26da2012-10-22 12:56:58 -04002423 .alloc_slot = xprt_alloc_slot,
Chuck Lever176e21e2011-05-09 15:22:44 -04002424 .rpcbind = xs_local_rpcbind,
2425 .set_port = xs_local_set_port,
2426 .connect = xs_connect,
2427 .buf_alloc = rpc_malloc,
2428 .buf_free = rpc_free,
2429 .send_request = xs_local_send_request,
2430 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2431 .close = xs_close,
2432 .destroy = xs_destroy,
2433 .print_stats = xs_local_print_stats,
2434};
2435
Chuck Lever262965f2005-08-11 16:25:56 -04002436static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002437 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002438 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002439 .release_xprt = xprt_release_xprt_cong,
Trond Myklebust49c26da2012-10-22 12:56:58 -04002440 .alloc_slot = xprt_alloc_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002441 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002442 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002443 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002444 .buf_alloc = rpc_malloc,
2445 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002446 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002447 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002448 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002449 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002450 .close = xs_close,
2451 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002452 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04002453};
2454
2455static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002456 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002457 .release_xprt = xs_tcp_release_xprt,
Trond Myklebust49c26da2012-10-22 12:56:58 -04002458 .alloc_slot = xprt_lock_and_alloc_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002459 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002460 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002461 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002462 .buf_alloc = rpc_malloc,
2463 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002464 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002465 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustf75e6742009-04-21 17:18:20 -04002466 .close = xs_tcp_close,
Chuck Lever9903cd12005-08-11 16:25:26 -04002467 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002468 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002469};
2470
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002471/*
2472 * The rpc_xprt_ops for the server backchannel
2473 */
2474
2475static struct rpc_xprt_ops bc_tcp_ops = {
2476 .reserve_xprt = xprt_reserve_xprt,
2477 .release_xprt = xprt_release_xprt,
Trond Myklebust49c26da2012-10-22 12:56:58 -04002478 .alloc_slot = xprt_alloc_slot,
J. Bruce Fields92769102012-03-23 15:25:03 -04002479 .rpcbind = xs_local_rpcbind,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002480 .buf_alloc = bc_malloc,
2481 .buf_free = bc_free,
2482 .send_request = bc_send_request,
2483 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2484 .close = bc_close,
2485 .destroy = bc_destroy,
2486 .print_stats = xs_tcp_print_stats,
2487};
2488
Chuck Lever92476852010-10-20 11:53:01 -04002489static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2490{
2491 static const struct sockaddr_in sin = {
2492 .sin_family = AF_INET,
2493 .sin_addr.s_addr = htonl(INADDR_ANY),
2494 };
2495 static const struct sockaddr_in6 sin6 = {
2496 .sin6_family = AF_INET6,
2497 .sin6_addr = IN6ADDR_ANY_INIT,
2498 };
2499
2500 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002501 case AF_LOCAL:
2502 break;
Chuck Lever92476852010-10-20 11:53:01 -04002503 case AF_INET:
2504 memcpy(sap, &sin, sizeof(sin));
2505 break;
2506 case AF_INET6:
2507 memcpy(sap, &sin6, sizeof(sin6));
2508 break;
2509 default:
2510 dprintk("RPC: %s: Bad address family\n", __func__);
2511 return -EAFNOSUPPORT;
2512 }
2513 return 0;
2514}
2515
\"Talpey, Thomas\3c341b02007-09-10 13:47:07 -04002516static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002517 unsigned int slot_table_size,
2518 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002519{
2520 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002521 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002522
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002523 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002524 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002525 return ERR_PTR(-EBADF);
2526 }
2527
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002528 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2529 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002530 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002531 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2532 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002533 return ERR_PTR(-ENOMEM);
2534 }
2535
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002536 new = container_of(xprt, struct sock_xprt, xprt);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002537 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2538 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002539 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002540 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002541 else {
2542 int err;
2543 err = xs_init_anyaddr(args->dstaddr->sa_family,
2544 (struct sockaddr *)&new->srcaddr);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002545 if (err != 0) {
2546 xprt_free(xprt);
Chuck Lever92476852010-10-20 11:53:01 -04002547 return ERR_PTR(err);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002548 }
Chuck Lever92476852010-10-20 11:53:01 -04002549 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002550
2551 return xprt;
2552}
2553
Chuck Lever176e21e2011-05-09 15:22:44 -04002554static const struct rpc_timeout xs_local_default_timeout = {
2555 .to_initval = 10 * HZ,
2556 .to_maxval = 10 * HZ,
2557 .to_retries = 2,
2558};
2559
2560/**
2561 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2562 * @args: rpc transport creation arguments
2563 *
2564 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2565 */
2566static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2567{
2568 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2569 struct sock_xprt *transport;
2570 struct rpc_xprt *xprt;
2571 struct rpc_xprt *ret;
2572
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002573 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2574 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002575 if (IS_ERR(xprt))
2576 return xprt;
2577 transport = container_of(xprt, struct sock_xprt, xprt);
2578
2579 xprt->prot = 0;
2580 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2581 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2582
2583 xprt->bind_timeout = XS_BIND_TO;
2584 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2585 xprt->idle_timeout = XS_IDLE_DISC_TO;
2586
2587 xprt->ops = &xs_local_ops;
2588 xprt->timeout = &xs_local_default_timeout;
2589
2590 switch (sun->sun_family) {
2591 case AF_LOCAL:
2592 if (sun->sun_path[0] != '/') {
2593 dprintk("RPC: bad AF_LOCAL address: %s\n",
2594 sun->sun_path);
2595 ret = ERR_PTR(-EINVAL);
2596 goto out_err;
2597 }
2598 xprt_set_bound(xprt);
2599 INIT_DELAYED_WORK(&transport->connect_worker,
2600 xs_local_setup_socket);
2601 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
2602 break;
2603 default:
2604 ret = ERR_PTR(-EAFNOSUPPORT);
2605 goto out_err;
2606 }
2607
2608 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2609 xprt->address_strings[RPC_DISPLAY_ADDR]);
2610
2611 if (try_module_get(THIS_MODULE))
2612 return xprt;
2613 ret = ERR_PTR(-EINVAL);
2614out_err:
2615 xprt_free(xprt);
2616 return ret;
2617}
2618
Trond Myklebust2881ae72007-12-20 16:03:54 -05002619static const struct rpc_timeout xs_udp_default_timeout = {
2620 .to_initval = 5 * HZ,
2621 .to_maxval = 30 * HZ,
2622 .to_increment = 5 * HZ,
2623 .to_retries = 5,
2624};
2625
Chuck Lever9903cd12005-08-11 16:25:26 -04002626/**
2627 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002628 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002629 *
2630 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002631static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002632{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002633 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002634 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002635 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002636 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002637
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002638 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2639 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002640 if (IS_ERR(xprt))
2641 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002642 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002643
Chuck Leverec739ef2006-08-22 20:06:15 -04002644 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002645 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002646 /* XXX: header size can vary due to auth type, IPv6, etc. */
2647 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2648
Chuck Lever03bf4b72005-08-25 16:25:55 -07002649 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002650 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2651 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002652
Chuck Lever262965f2005-08-11 16:25:56 -04002653 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002654
Trond Myklebustba7392b2007-12-20 16:03:55 -05002655 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002656
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002657 switch (addr->sa_family) {
2658 case AF_INET:
2659 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2660 xprt_set_bound(xprt);
2661
2662 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002663 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002664 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002665 break;
2666 case AF_INET6:
2667 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2668 xprt_set_bound(xprt);
2669
2670 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002671 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002672 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002673 break;
2674 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002675 ret = ERR_PTR(-EAFNOSUPPORT);
2676 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002677 }
2678
Chuck Leverc740eff2009-08-09 15:09:46 -04002679 if (xprt_bound(xprt))
2680 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2681 xprt->address_strings[RPC_DISPLAY_ADDR],
2682 xprt->address_strings[RPC_DISPLAY_PORT],
2683 xprt->address_strings[RPC_DISPLAY_PROTO]);
2684 else
2685 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2686 xprt->address_strings[RPC_DISPLAY_ADDR],
2687 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002688
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002689 if (try_module_get(THIS_MODULE))
2690 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002691 ret = ERR_PTR(-EINVAL);
2692out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002693 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002694 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002695}
2696
Trond Myklebust2881ae72007-12-20 16:03:54 -05002697static const struct rpc_timeout xs_tcp_default_timeout = {
2698 .to_initval = 60 * HZ,
2699 .to_maxval = 60 * HZ,
2700 .to_retries = 2,
2701};
2702
Chuck Lever9903cd12005-08-11 16:25:26 -04002703/**
2704 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002705 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002706 *
2707 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002708static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002709{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002710 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002711 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002712 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002713 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002714
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002715 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2716 xprt_max_tcp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002717 if (IS_ERR(xprt))
2718 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002719 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002720
Chuck Leverec739ef2006-08-22 20:06:15 -04002721 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002722 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002723 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002724
Chuck Lever03bf4b72005-08-25 16:25:55 -07002725 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002726 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2727 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002728
Chuck Lever262965f2005-08-11 16:25:56 -04002729 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002730 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002731
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002732 switch (addr->sa_family) {
2733 case AF_INET:
2734 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2735 xprt_set_bound(xprt);
2736
Chuck Lever9dc3b092009-08-09 15:09:46 -04002737 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002738 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002739 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002740 break;
2741 case AF_INET6:
2742 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2743 xprt_set_bound(xprt);
2744
Chuck Lever9dc3b092009-08-09 15:09:46 -04002745 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002746 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002747 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002748 break;
2749 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002750 ret = ERR_PTR(-EAFNOSUPPORT);
2751 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002752 }
2753
Chuck Leverc740eff2009-08-09 15:09:46 -04002754 if (xprt_bound(xprt))
2755 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2756 xprt->address_strings[RPC_DISPLAY_ADDR],
2757 xprt->address_strings[RPC_DISPLAY_PORT],
2758 xprt->address_strings[RPC_DISPLAY_PROTO]);
2759 else
2760 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2761 xprt->address_strings[RPC_DISPLAY_ADDR],
2762 xprt->address_strings[RPC_DISPLAY_PROTO]);
2763
Chuck Leveredb267a2006-08-22 20:06:18 -04002764
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002765 if (try_module_get(THIS_MODULE))
2766 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002767 ret = ERR_PTR(-EINVAL);
2768out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002769 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002770 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002771}
Chuck Lever282b32e2006-12-05 16:35:51 -05002772
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002773/**
2774 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2775 * @args: rpc transport creation arguments
2776 *
2777 */
2778static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2779{
2780 struct sockaddr *addr = args->dstaddr;
2781 struct rpc_xprt *xprt;
2782 struct sock_xprt *transport;
2783 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002784 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002785
J. Bruce Fieldsf0418aa2010-12-08 13:48:19 -05002786 if (args->bc_xprt->xpt_bc_xprt) {
2787 /*
2788 * This server connection already has a backchannel
2789 * export; we can't create a new one, as we wouldn't be
2790 * able to match replies based on xid any more. So,
2791 * reuse the already-existing one:
2792 */
2793 return args->bc_xprt->xpt_bc_xprt;
2794 }
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002795 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2796 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002797 if (IS_ERR(xprt))
2798 return xprt;
2799 transport = container_of(xprt, struct sock_xprt, xprt);
2800
2801 xprt->prot = IPPROTO_TCP;
2802 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2803 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2804 xprt->timeout = &xs_tcp_default_timeout;
2805
2806 /* backchannel */
2807 xprt_set_bound(xprt);
2808 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002809 xprt->reestablish_timeout = 0;
2810 xprt->idle_timeout = 0;
2811
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002812 xprt->ops = &bc_tcp_ops;
2813
2814 switch (addr->sa_family) {
2815 case AF_INET:
2816 xs_format_peer_addresses(xprt, "tcp",
2817 RPCBIND_NETID_TCP);
2818 break;
2819 case AF_INET6:
2820 xs_format_peer_addresses(xprt, "tcp",
2821 RPCBIND_NETID_TCP6);
2822 break;
2823 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002824 ret = ERR_PTR(-EAFNOSUPPORT);
2825 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002826 }
2827
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04002828 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2829 xprt->address_strings[RPC_DISPLAY_ADDR],
2830 xprt->address_strings[RPC_DISPLAY_PORT],
2831 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002832
2833 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002834 * Once we've associated a backchannel xprt with a connection,
2835 * we want to keep it around as long as long as the connection
2836 * lasts, in case we need to start using it for a backchannel
2837 * again; this reference won't be dropped until bc_xprt is
2838 * destroyed.
2839 */
2840 xprt_get(xprt);
2841 args->bc_xprt->xpt_bc_xprt = xprt;
2842 xprt->bc_xprt = args->bc_xprt;
2843 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2844 transport->sock = bc_sock->sk_sock;
2845 transport->inet = bc_sock->sk_sk;
2846
2847 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002848 * Since we don't want connections for the backchannel, we set
2849 * the xprt status to connected
2850 */
2851 xprt_set_connected(xprt);
2852
2853
2854 if (try_module_get(THIS_MODULE))
2855 return xprt;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002856 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002857 ret = ERR_PTR(-EINVAL);
2858out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002859 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002860 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002861}
2862
Chuck Lever176e21e2011-05-09 15:22:44 -04002863static struct xprt_class xs_local_transport = {
2864 .list = LIST_HEAD_INIT(xs_local_transport.list),
2865 .name = "named UNIX socket",
2866 .owner = THIS_MODULE,
2867 .ident = XPRT_TRANSPORT_LOCAL,
2868 .setup = xs_setup_local,
2869};
2870
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002871static struct xprt_class xs_udp_transport = {
2872 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2873 .name = "udp",
2874 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002875 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002876 .setup = xs_setup_udp,
2877};
2878
2879static struct xprt_class xs_tcp_transport = {
2880 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2881 .name = "tcp",
2882 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002883 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002884 .setup = xs_setup_tcp,
2885};
2886
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002887static struct xprt_class xs_bc_tcp_transport = {
2888 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2889 .name = "tcp NFSv4.1 backchannel",
2890 .owner = THIS_MODULE,
2891 .ident = XPRT_TRANSPORT_BC_TCP,
2892 .setup = xs_setup_bc_tcp,
2893};
2894
Chuck Lever282b32e2006-12-05 16:35:51 -05002895/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002896 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002897 *
2898 */
2899int init_socket_xprt(void)
2900{
Chuck Leverfbf76682006-12-05 16:35:54 -05002901#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002902 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002903 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002904#endif
2905
Chuck Lever176e21e2011-05-09 15:22:44 -04002906 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002907 xprt_register_transport(&xs_udp_transport);
2908 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002909 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002910
Chuck Lever282b32e2006-12-05 16:35:51 -05002911 return 0;
2912}
2913
2914/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002915 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05002916 *
2917 */
2918void cleanup_socket_xprt(void)
2919{
Chuck Leverfbf76682006-12-05 16:35:54 -05002920#ifdef RPC_DEBUG
2921 if (sunrpc_table_header) {
2922 unregister_sysctl_table(sunrpc_table_header);
2923 sunrpc_table_header = NULL;
2924 }
2925#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002926
Chuck Lever176e21e2011-05-09 15:22:44 -04002927 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002928 xprt_unregister_transport(&xs_udp_transport);
2929 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002930 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05002931}
Trond Myklebustcbf11072009-08-09 15:06:19 -04002932
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002933static int param_set_uint_minmax(const char *val,
2934 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04002935 unsigned int min, unsigned int max)
2936{
2937 unsigned long num;
2938 int ret;
2939
2940 if (!val)
2941 return -EINVAL;
2942 ret = strict_strtoul(val, 0, &num);
2943 if (ret == -EINVAL || num < min || num > max)
2944 return -EINVAL;
2945 *((unsigned int *)kp->arg) = num;
2946 return 0;
2947}
2948
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002949static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04002950{
2951 return param_set_uint_minmax(val, kp,
2952 RPC_MIN_RESVPORT,
2953 RPC_MAX_RESVPORT);
2954}
2955
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002956static struct kernel_param_ops param_ops_portnr = {
2957 .set = param_set_portnr,
2958 .get = param_get_uint,
2959};
2960
Trond Myklebustcbf11072009-08-09 15:06:19 -04002961#define param_check_portnr(name, p) \
2962 __param_check(name, p, unsigned int);
2963
2964module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
2965module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
2966
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002967static int param_set_slot_table_size(const char *val,
2968 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04002969{
2970 return param_set_uint_minmax(val, kp,
2971 RPC_MIN_SLOT_TABLE,
2972 RPC_MAX_SLOT_TABLE);
2973}
2974
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002975static struct kernel_param_ops param_ops_slot_table_size = {
2976 .set = param_set_slot_table_size,
2977 .get = param_get_uint,
2978};
2979
Trond Myklebustcbf11072009-08-09 15:06:19 -04002980#define param_check_slot_table_size(name, p) \
2981 __param_check(name, p, unsigned int);
2982
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002983static int param_set_max_slot_table_size(const char *val,
2984 const struct kernel_param *kp)
2985{
2986 return param_set_uint_minmax(val, kp,
2987 RPC_MIN_SLOT_TABLE,
2988 RPC_MAX_SLOT_TABLE_LIMIT);
2989}
2990
2991static struct kernel_param_ops param_ops_max_slot_table_size = {
2992 .set = param_set_max_slot_table_size,
2993 .get = param_get_uint,
2994};
2995
2996#define param_check_max_slot_table_size(name, p) \
2997 __param_check(name, p, unsigned int);
2998
Trond Myklebustcbf11072009-08-09 15:06:19 -04002999module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3000 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003001module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3002 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003003module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3004 slot_table_size, 0644);
3005