blob: 31275e52c6670f1fc616df4e8ecab6155a45153f [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 Myklebust6d912992013-11-08 16:03:50 -0500393static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more, bool zerocopy)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400394{
Trond Myklebust6d912992013-11-08 16:03:50 -0500395 ssize_t (*do_sendpage)(struct socket *sock, struct page *page,
396 int offset, size_t size, int flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400397 struct page **ppage;
398 unsigned int remainder;
399 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400400
Trond Myklebust24c56842006-10-17 15:06:22 -0400401 remainder = xdr->page_len - base;
402 base += xdr->page_base;
403 ppage = xdr->pages + (base >> PAGE_SHIFT);
404 base &= ~PAGE_MASK;
Trond Myklebust6d912992013-11-08 16:03:50 -0500405 do_sendpage = sock->ops->sendpage;
406 if (!zerocopy)
407 do_sendpage = sock_no_sendpage;
Trond Myklebust24c56842006-10-17 15:06:22 -0400408 for(;;) {
409 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
410 int flags = XS_SENDMSG_FLAGS;
411
412 remainder -= len;
413 if (remainder != 0 || more)
414 flags |= MSG_MORE;
Trond Myklebust6d912992013-11-08 16:03:50 -0500415 err = do_sendpage(sock, *ppage, base, len, flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400416 if (remainder == 0 || err != len)
417 break;
418 sent += err;
419 ppage++;
420 base = 0;
421 }
422 if (sent == 0)
423 return err;
424 if (err > 0)
425 sent += err;
426 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400427}
428
Chuck Lever9903cd12005-08-11 16:25:26 -0400429/**
430 * xs_sendpages - write pages directly to a socket
431 * @sock: socket to send on
432 * @addr: UDP only -- address of destination
433 * @addrlen: UDP only -- length of destination address
434 * @xdr: buffer containing this request
435 * @base: starting position in the buffer
Trond Myklebust6d912992013-11-08 16:03:50 -0500436 * @zerocopy: true if it is safe to use sendpage()
Chuck Lever9903cd12005-08-11 16:25:26 -0400437 *
Chuck Levera246b012005-08-11 16:25:23 -0400438 */
Trond Myklebust6d912992013-11-08 16:03:50 -0500439static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, bool zerocopy)
Chuck Levera246b012005-08-11 16:25:23 -0400440{
Trond Myklebust24c56842006-10-17 15:06:22 -0400441 unsigned int remainder = xdr->len - base;
442 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400443
Chuck Lever262965f2005-08-11 16:25:56 -0400444 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400445 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400446
447 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400448 if (base != 0) {
449 addr = NULL;
450 addrlen = 0;
451 }
Chuck Lever262965f2005-08-11 16:25:56 -0400452
Trond Myklebust24c56842006-10-17 15:06:22 -0400453 if (base < xdr->head[0].iov_len || addr != NULL) {
454 unsigned int len = xdr->head[0].iov_len - base;
455 remainder -= len;
456 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
457 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400458 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400459 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400460 base = 0;
461 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400462 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400463
Trond Myklebust24c56842006-10-17 15:06:22 -0400464 if (base < xdr->page_len) {
465 unsigned int len = xdr->page_len - base;
466 remainder -= len;
Trond Myklebust6d912992013-11-08 16:03:50 -0500467 err = xs_send_pagedata(sock, xdr, base, remainder != 0, zerocopy);
Trond Myklebust24c56842006-10-17 15:06:22 -0400468 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400469 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400470 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400471 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400472 } else
473 base -= xdr->page_len;
474
475 if (base >= xdr->tail[0].iov_len)
476 return sent;
477 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400478out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400479 if (sent == 0)
480 return err;
481 if (err > 0)
482 sent += err;
483 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400484}
485
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400486static void xs_nospace_callback(struct rpc_task *task)
487{
488 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
489
490 transport->inet->sk_write_pending--;
491 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
492}
493
Chuck Lever9903cd12005-08-11 16:25:26 -0400494/**
Chuck Lever262965f2005-08-11 16:25:56 -0400495 * xs_nospace - place task on wait queue if transmit was incomplete
496 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400497 *
Chuck Levera246b012005-08-11 16:25:23 -0400498 */
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400499static int xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400500{
Chuck Lever262965f2005-08-11 16:25:56 -0400501 struct rpc_rqst *req = task->tk_rqstp;
502 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500503 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust1fccff62014-02-11 09:15:54 -0500504 struct sock *sk = transport->inet;
Trond Myklebust24ca9a82011-11-22 14:44:28 +0200505 int ret = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400506
Chuck Lever46121cf2007-01-31 12:14:08 -0500507 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400508 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
509 req->rq_slen);
510
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400511 /* Protect against races with write_space */
512 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400513
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400514 /* Don't race with disconnect */
515 if (xprt_connected(xprt)) {
516 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
517 /*
518 * Notify TCP that we're limited by the application
519 * window size
520 */
521 set_bit(SOCK_NOSPACE, &transport->sock->flags);
Trond Myklebust1fccff62014-02-11 09:15:54 -0500522 sk->sk_write_pending++;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400523 /* ...and wait for more buffer space */
524 xprt_wait_for_buffer_space(task, xs_nospace_callback);
525 }
526 } else {
527 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400528 ret = -ENOTCONN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400529 }
Chuck Lever262965f2005-08-11 16:25:56 -0400530
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400531 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust1fccff62014-02-11 09:15:54 -0500532
533 /* Race breaker in case memory is freed before above code is called */
534 sk->sk_write_space(sk);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400535 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400536}
537
Chuck Lever61677ee2011-05-09 15:22:34 -0400538/*
539 * Construct a stream transport record marker in @buf.
540 */
541static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
542{
543 u32 reclen = buf->len - sizeof(rpc_fraghdr);
544 rpc_fraghdr *base = buf->head[0].iov_base;
545 *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
546}
547
Chuck Lever262965f2005-08-11 16:25:56 -0400548/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400549 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
550 * @task: RPC task that manages the state of an RPC request
551 *
552 * Return values:
553 * 0: The request has been sent
554 * EAGAIN: The socket was blocked, please call again later to
555 * complete the request
556 * ENOTCONN: Caller needs to invoke connect logic then call again
557 * other: Some other error occured, the request was not sent
558 */
559static int xs_local_send_request(struct rpc_task *task)
560{
561 struct rpc_rqst *req = task->tk_rqstp;
562 struct rpc_xprt *xprt = req->rq_xprt;
563 struct sock_xprt *transport =
564 container_of(xprt, struct sock_xprt, xprt);
565 struct xdr_buf *xdr = &req->rq_snd_buf;
566 int status;
567
568 xs_encode_stream_record_marker(&req->rq_snd_buf);
569
570 xs_pktdump("packet data:",
571 req->rq_svec->iov_base, req->rq_svec->iov_len);
572
573 status = xs_sendpages(transport->sock, NULL, 0,
Trond Myklebust6d912992013-11-08 16:03:50 -0500574 xdr, req->rq_bytes_sent, true);
Chuck Lever176e21e2011-05-09 15:22:44 -0400575 dprintk("RPC: %s(%u) = %d\n",
576 __func__, xdr->len - req->rq_bytes_sent, status);
577 if (likely(status >= 0)) {
578 req->rq_bytes_sent += status;
579 req->rq_xmit_bytes_sent += status;
580 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
581 req->rq_bytes_sent = 0;
582 return 0;
583 }
584 status = -EAGAIN;
585 }
586
587 switch (status) {
588 case -EAGAIN:
589 status = xs_nospace(task);
590 break;
591 default:
592 dprintk("RPC: sendmsg returned unrecognized error %d\n",
593 -status);
594 case -EPIPE:
595 xs_close(xprt);
596 status = -ENOTCONN;
597 }
598
599 return status;
600}
601
602/**
Chuck Lever262965f2005-08-11 16:25:56 -0400603 * xs_udp_send_request - write an RPC request to a UDP socket
604 * @task: address of RPC task that manages the state of an RPC request
605 *
606 * Return values:
607 * 0: The request has been sent
608 * EAGAIN: The socket was blocked, please call again later to
609 * complete the request
610 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300611 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -0400612 */
613static int xs_udp_send_request(struct rpc_task *task)
614{
615 struct rpc_rqst *req = task->tk_rqstp;
616 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500617 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400618 struct xdr_buf *xdr = &req->rq_snd_buf;
619 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400620
Chuck Lever9903cd12005-08-11 16:25:26 -0400621 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400622 req->rq_svec->iov_base,
623 req->rq_svec->iov_len);
624
Trond Myklebust01d37c42009-03-11 14:09:39 -0400625 if (!xprt_bound(xprt))
626 return -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500627 status = xs_sendpages(transport->sock,
Chuck Lever95392c52007-08-06 11:57:58 -0400628 xs_addr(xprt),
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500629 xprt->addrlen, xdr,
Trond Myklebust6d912992013-11-08 16:03:50 -0500630 req->rq_bytes_sent, true);
Chuck Levera246b012005-08-11 16:25:23 -0400631
Chuck Lever46121cf2007-01-31 12:14:08 -0500632 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400633 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400634
Trond Myklebust21997002007-10-01 11:43:37 -0400635 if (status >= 0) {
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400636 req->rq_xmit_bytes_sent += status;
Trond Myklebust21997002007-10-01 11:43:37 -0400637 if (status >= req->rq_slen)
638 return 0;
639 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400640 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400641 }
Chuck Levera246b012005-08-11 16:25:23 -0400642
Chuck Lever262965f2005-08-11 16:25:56 -0400643 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400644 case -ENOTSOCK:
645 status = -ENOTCONN;
646 /* Should we call xs_close() here? */
647 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400648 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400649 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400650 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400651 default:
652 dprintk("RPC: sendmsg returned unrecognized error %d\n",
653 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400654 case -ENETUNREACH:
655 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400656 case -ECONNREFUSED:
657 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400658 * prompts ECONNREFUSED. */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400659 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400660 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800661
Chuck Lever262965f2005-08-11 16:25:56 -0400662 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400663}
664
Trond Myklebuste06799f2007-11-05 15:44:12 -0500665/**
666 * xs_tcp_shutdown - gracefully shut down a TCP socket
667 * @xprt: transport
668 *
669 * Initiates a graceful shutdown of the TCP socket by calling the
670 * equivalent of shutdown(SHUT_WR);
671 */
672static void xs_tcp_shutdown(struct rpc_xprt *xprt)
673{
674 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
675 struct socket *sock = transport->sock;
676
677 if (sock != NULL)
678 kernel_sock_shutdown(sock, SHUT_WR);
679}
680
Chuck Lever9903cd12005-08-11 16:25:26 -0400681/**
Chuck Lever262965f2005-08-11 16:25:56 -0400682 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400683 * @task: address of RPC task that manages the state of an RPC request
684 *
685 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400686 * 0: The request has been sent
687 * EAGAIN: The socket was blocked, please call again later to
688 * complete the request
689 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300690 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400691 *
692 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400693 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400694 */
Chuck Lever262965f2005-08-11 16:25:56 -0400695static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400696{
697 struct rpc_rqst *req = task->tk_rqstp;
698 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500699 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400700 struct xdr_buf *xdr = &req->rq_snd_buf;
Trond Myklebust6d912992013-11-08 16:03:50 -0500701 bool zerocopy = true;
Chuck Leverb595bb12007-08-06 11:56:42 -0400702 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400703
Chuck Lever61677ee2011-05-09 15:22:34 -0400704 xs_encode_stream_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400705
Chuck Lever262965f2005-08-11 16:25:56 -0400706 xs_pktdump("packet data:",
707 req->rq_svec->iov_base,
708 req->rq_svec->iov_len);
Trond Myklebust6d912992013-11-08 16:03:50 -0500709 /* Don't use zero copy if this is a resend. If the RPC call
710 * completes while the socket holds a reference to the pages,
711 * then we may end up resending corrupted data.
712 */
713 if (task->tk_flags & RPC_TASK_SENT)
714 zerocopy = false;
Chuck Levera246b012005-08-11 16:25:23 -0400715
716 /* Continue transmitting the packet/record. We must be careful
717 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400718 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400719 while (1) {
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500720 status = xs_sendpages(transport->sock,
Trond Myklebust6d912992013-11-08 16:03:50 -0500721 NULL, 0, xdr, req->rq_bytes_sent,
722 zerocopy);
Chuck Levera246b012005-08-11 16:25:23 -0400723
Chuck Lever46121cf2007-01-31 12:14:08 -0500724 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400725 xdr->len - req->rq_bytes_sent, status);
726
727 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400728 break;
729
Chuck Lever262965f2005-08-11 16:25:56 -0400730 /* If we've sent the entire packet, immediately
731 * reset the count of bytes sent. */
732 req->rq_bytes_sent += status;
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400733 req->rq_xmit_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400734 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
735 req->rq_bytes_sent = 0;
736 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400737 }
738
Trond Myklebust06b4b682008-04-16 16:51:38 -0400739 if (status != 0)
740 continue;
Chuck Levera246b012005-08-11 16:25:23 -0400741 status = -EAGAIN;
Trond Myklebust06b4b682008-04-16 16:51:38 -0400742 break;
Chuck Levera246b012005-08-11 16:25:23 -0400743 }
744
Chuck Lever262965f2005-08-11 16:25:56 -0400745 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400746 case -ENOTSOCK:
747 status = -ENOTCONN;
748 /* Should we call xs_close() here? */
749 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400750 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400751 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400752 break;
753 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500754 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400755 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400756 case -ECONNRESET:
Trond Myklebuste06799f2007-11-05 15:44:12 -0500757 xs_tcp_shutdown(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400758 case -ECONNREFUSED:
759 case -ENOTCONN:
Trond Myklebust6b2c5532012-10-23 11:40:02 -0400760 case -EPIPE:
Chuck Lever262965f2005-08-11 16:25:56 -0400761 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400762 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800763
Chuck Levera246b012005-08-11 16:25:23 -0400764 return status;
765}
766
Chuck Lever9903cd12005-08-11 16:25:26 -0400767/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400768 * xs_tcp_release_xprt - clean up after a tcp transmission
769 * @xprt: transport
770 * @task: rpc task
771 *
772 * This cleans up if an error causes us to abort the transmission of a request.
773 * In this case, the socket may need to be reset in order to avoid confusing
774 * the server.
775 */
776static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
777{
778 struct rpc_rqst *req;
779
780 if (task != xprt->snd_task)
781 return;
782 if (task == NULL)
783 goto out_release;
784 req = task->tk_rqstp;
Trond Myklebust43cedbf2011-07-17 16:01:03 -0400785 if (req == NULL)
786 goto out_release;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400787 if (req->rq_bytes_sent == 0)
788 goto out_release;
789 if (req->rq_bytes_sent == req->rq_snd_buf.len)
790 goto out_release;
791 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
792out_release:
793 xprt_release_xprt(xprt, task);
794}
795
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400796static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
797{
798 transport->old_data_ready = sk->sk_data_ready;
799 transport->old_state_change = sk->sk_state_change;
800 transport->old_write_space = sk->sk_write_space;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400801}
802
803static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
804{
805 sk->sk_data_ready = transport->old_data_ready;
806 sk->sk_state_change = transport->old_state_change;
807 sk->sk_write_space = transport->old_write_space;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400808}
809
Chuck Leverfe315e72009-03-11 14:10:21 -0400810static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400811{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500812 struct socket *sock = transport->sock;
813 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400814
Chuck Leverfe315e72009-03-11 14:10:21 -0400815 if (sk == NULL)
816 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400817
Trond Myklebust246408d2011-03-22 18:40:10 -0400818 transport->srcport = 0;
819
Chuck Levera246b012005-08-11 16:25:23 -0400820 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500821 transport->inet = NULL;
822 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400823
Chuck Lever9903cd12005-08-11 16:25:26 -0400824 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400825
826 xs_restore_old_callbacks(transport, sk);
Chuck Levera246b012005-08-11 16:25:23 -0400827 write_unlock_bh(&sk->sk_callback_lock);
828
Chuck Lever9903cd12005-08-11 16:25:26 -0400829 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400830
831 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -0400832}
833
834/**
835 * xs_close - close a socket
836 * @xprt: transport
837 *
838 * This is used when all requests are complete; ie, no DRC state remains
839 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400840 *
841 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
842 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e72009-03-11 14:10:21 -0400843 */
844static void xs_close(struct rpc_xprt *xprt)
845{
846 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
847
848 dprintk("RPC: xs_close xprt %p\n", xprt);
849
850 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -0400851 xprt->reestablish_timeout = 0;
Chuck Leverfe315e72009-03-11 14:10:21 -0400852
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100853 smp_mb__before_clear_bit();
Trond Myklebust7d1e8252009-03-11 14:38:03 -0400854 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100855 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -0500856 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100857 smp_mb__after_clear_bit();
Trond Myklebust62da3b22007-11-06 18:44:20 -0500858 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400859}
860
Trond Myklebustf75e6742009-04-21 17:18:20 -0400861static void xs_tcp_close(struct rpc_xprt *xprt)
862{
863 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
864 xs_close(xprt);
865 else
866 xs_tcp_shutdown(xprt);
867}
868
Chuck Lever9903cd12005-08-11 16:25:26 -0400869/**
870 * xs_destroy - prepare to shutdown a transport
871 * @xprt: doomed transport
872 *
873 */
874static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400875{
Chuck Leverc8475462006-12-05 16:35:26 -0500876 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
877
Chuck Lever46121cf2007-01-31 12:14:08 -0500878 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400879
Tejun Heoafe2c512010-12-14 16:21:17 +0100880 cancel_delayed_work_sync(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400881
Chuck Lever9903cd12005-08-11 16:25:26 -0400882 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400883 xs_free_peer_addresses(xprt);
Pavel Emelyanove204e622010-09-29 16:03:13 +0400884 xprt_free(xprt);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -0400885 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400886}
887
Chuck Lever9903cd12005-08-11 16:25:26 -0400888static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400889{
Chuck Lever9903cd12005-08-11 16:25:26 -0400890 return (struct rpc_xprt *) sk->sk_user_data;
891}
892
Chuck Lever176e21e2011-05-09 15:22:44 -0400893static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
894{
895 struct xdr_skb_reader desc = {
896 .skb = skb,
897 .offset = sizeof(rpc_fraghdr),
898 .count = skb->len - sizeof(rpc_fraghdr),
899 };
900
901 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
902 return -1;
903 if (desc.count)
904 return -1;
905 return 0;
906}
907
908/**
909 * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets
910 * @sk: socket with data to read
911 * @len: how much data to read
912 *
913 * Currently this assumes we can read the whole reply in a single gulp.
914 */
915static void xs_local_data_ready(struct sock *sk, int len)
916{
917 struct rpc_task *task;
918 struct rpc_xprt *xprt;
919 struct rpc_rqst *rovr;
920 struct sk_buff *skb;
921 int err, repsize, copied;
922 u32 _xid;
923 __be32 *xp;
924
925 read_lock_bh(&sk->sk_callback_lock);
926 dprintk("RPC: %s...\n", __func__);
927 xprt = xprt_from_sock(sk);
928 if (xprt == NULL)
929 goto out;
930
931 skb = skb_recv_datagram(sk, 0, 1, &err);
932 if (skb == NULL)
933 goto out;
934
935 if (xprt->shutdown)
936 goto dropit;
937
938 repsize = skb->len - sizeof(rpc_fraghdr);
939 if (repsize < 4) {
940 dprintk("RPC: impossible RPC reply size %d\n", repsize);
941 goto dropit;
942 }
943
944 /* Copy the XID from the skb... */
945 xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
946 if (xp == NULL)
947 goto dropit;
948
949 /* Look up and lock the request corresponding to the given XID */
950 spin_lock(&xprt->transport_lock);
951 rovr = xprt_lookup_rqst(xprt, *xp);
952 if (!rovr)
953 goto out_unlock;
954 task = rovr->rq_task;
955
956 copied = rovr->rq_private_buf.buflen;
957 if (copied > repsize)
958 copied = repsize;
959
960 if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
961 dprintk("RPC: sk_buff copy failed\n");
962 goto out_unlock;
963 }
964
965 xprt_complete_rqst(task, copied);
966
967 out_unlock:
968 spin_unlock(&xprt->transport_lock);
969 dropit:
970 skb_free_datagram(sk, skb);
971 out:
972 read_unlock_bh(&sk->sk_callback_lock);
973}
974
Chuck Lever9903cd12005-08-11 16:25:26 -0400975/**
976 * xs_udp_data_ready - "data ready" callback for UDP sockets
977 * @sk: socket with data to read
978 * @len: how much data to read
979 *
980 */
981static void xs_udp_data_ready(struct sock *sk, int len)
982{
983 struct rpc_task *task;
984 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400985 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400986 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400987 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700988 u32 _xid;
989 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400990
Eric Dumazetf064af12010-09-22 12:43:39 +0000991 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500992 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400993 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400994 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400995
996 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
997 goto out;
998
999 if (xprt->shutdown)
1000 goto dropit;
1001
1002 repsize = skb->len - sizeof(struct udphdr);
1003 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001004 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -04001005 goto dropit;
1006 }
1007
1008 /* Copy the XID from the skb... */
1009 xp = skb_header_pointer(skb, sizeof(struct udphdr),
1010 sizeof(_xid), &_xid);
1011 if (xp == NULL)
1012 goto dropit;
1013
1014 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001015 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001016 rovr = xprt_lookup_rqst(xprt, *xp);
1017 if (!rovr)
1018 goto out_unlock;
1019 task = rovr->rq_task;
1020
Chuck Levera246b012005-08-11 16:25:23 -04001021 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1022 copied = repsize;
1023
1024 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001025 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
1026 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -04001027 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001028 }
1029
1030 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -04001031
1032 /* Something worked... */
Eric Dumazetadf30902009-06-02 05:19:30 +00001033 dst_confirm(skb_dst(skb));
Chuck Levera246b012005-08-11 16:25:23 -04001034
Chuck Lever1570c1e2005-08-25 16:25:52 -07001035 xprt_adjust_cwnd(task, copied);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001036 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -04001037
1038 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001039 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001040 dropit:
1041 skb_free_datagram(sk, skb);
1042 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001043 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001044}
1045
Trond Myklebuste23c80e2012-09-12 16:49:15 -04001046/*
1047 * Helper function to force a TCP close if the server is sending
1048 * junk and/or it has put us in CLOSE_WAIT
1049 */
1050static void xs_tcp_force_close(struct rpc_xprt *xprt)
1051{
1052 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1053 xprt_force_disconnect(xprt);
1054}
1055
Chuck Leverdd456472006-12-05 16:35:44 -05001056static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001057{
Chuck Lever51971132006-12-05 16:35:19 -05001058 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001059 size_t len, used;
1060 char *p;
1061
Chuck Lever51971132006-12-05 16:35:19 -05001062 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
1063 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001064 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001065 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001066 if (used != len)
1067 return;
Chuck Lever808012f2005-08-25 16:25:49 -07001068
Chuck Lever51971132006-12-05 16:35:19 -05001069 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
1070 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -05001071 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -04001072 else
Chuck Levere136d092006-12-05 16:35:23 -05001073 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -05001074 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -07001075
Chuck Levere136d092006-12-05 16:35:23 -05001076 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001077 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -07001078
Chuck Levera246b012005-08-11 16:25:23 -04001079 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001080 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001081 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebuste23c80e2012-09-12 16:49:15 -04001082 xs_tcp_force_close(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001083 return;
Chuck Levera246b012005-08-11 16:25:23 -04001084 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001085 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -05001086 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001087}
1088
Chuck Lever51971132006-12-05 16:35:19 -05001089static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001090{
Chuck Lever51971132006-12-05 16:35:19 -05001091 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001092 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001093 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001094 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
1095 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1096 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001097 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001098 }
1099 }
1100}
1101
Chuck Leverdd456472006-12-05 16:35:44 -05001102static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001103{
1104 size_t len, used;
1105 char *p;
1106
Chuck Lever51971132006-12-05 16:35:19 -05001107 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -05001108 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001109 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001110 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001111 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001112 if (used != len)
1113 return;
Chuck Levere136d092006-12-05 16:35:23 -05001114 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001115 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -05001116 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001117 dprintk("RPC: reading %s XID %08x\n",
1118 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1119 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -05001120 ntohl(transport->tcp_xid));
1121 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001122}
1123
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001124static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1125 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001126{
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001127 size_t len, used;
1128 u32 offset;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001129 char *p;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001130
1131 /*
1132 * We want transport->tcp_offset to be 8 at the end of this routine
1133 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1134 * When this function is called for the first time,
1135 * transport->tcp_offset is 4 (after having already read the xid).
1136 */
1137 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001138 len = sizeof(transport->tcp_calldir) - offset;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001139 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001140 p = ((char *) &transport->tcp_calldir) + offset;
1141 used = xdr_skb_read_bits(desc, p, len);
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001142 transport->tcp_offset += used;
1143 if (used != len)
1144 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001145 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001146 /*
1147 * We don't yet have the XDR buffer, so we will write the calldir
1148 * out after we get the buffer from the 'struct rpc_rqst'
1149 */
Trond Myklebustb76ce562010-06-16 13:57:32 -04001150 switch (ntohl(transport->tcp_calldir)) {
1151 case RPC_REPLY:
1152 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1153 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001154 transport->tcp_flags |= TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001155 break;
1156 case RPC_CALL:
1157 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1158 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001159 transport->tcp_flags &= ~TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001160 break;
1161 default:
1162 dprintk("RPC: invalid request message type\n");
Trond Myklebuste23c80e2012-09-12 16:49:15 -04001163 xs_tcp_force_close(&transport->xprt);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001164 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001165 xs_tcp_check_fraghdr(transport);
1166}
1167
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001168static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
1169 struct xdr_skb_reader *desc,
1170 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001171{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001172 struct sock_xprt *transport =
1173 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001174 struct xdr_buf *rcvbuf;
1175 size_t len;
1176 ssize_t r;
1177
Chuck Levera246b012005-08-11 16:25:23 -04001178 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001179
1180 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1181 /*
1182 * Save the RPC direction in the XDR buffer
1183 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001184 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
Trond Myklebustb76ce562010-06-16 13:57:32 -04001185 &transport->tcp_calldir,
1186 sizeof(transport->tcp_calldir));
1187 transport->tcp_copied += sizeof(transport->tcp_calldir);
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001188 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -04001189 }
1190
Chuck Levera246b012005-08-11 16:25:23 -04001191 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -05001192 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -05001193 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -04001194
Chuck Lever51971132006-12-05 16:35:19 -05001195 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001196 memcpy(&my_desc, desc, sizeof(my_desc));
1197 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -05001198 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001199 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001200 desc->count -= r;
1201 desc->offset += r;
1202 } else
Chuck Lever51971132006-12-05 16:35:19 -05001203 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001204 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001205
1206 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001207 transport->tcp_copied += r;
1208 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001209 }
1210 if (r != len) {
1211 /* Error when copying to the receive buffer,
1212 * usually because we weren't able to allocate
1213 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001214 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001215 * will not receive any additional updates,
1216 * and time out.
1217 * Any remaining data from this record will
1218 * be discarded.
1219 */
Chuck Levere136d092006-12-05 16:35:23 -05001220 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001221 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001222 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001223 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1224 "tcp_offset = %u, tcp_reclen = %u\n",
1225 xprt, transport->tcp_copied,
1226 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001227 return;
Chuck Levera246b012005-08-11 16:25:23 -04001228 }
1229
Chuck Lever46121cf2007-01-31 12:14:08 -05001230 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001231 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001232 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1233 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1234 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001235
Chuck Lever51971132006-12-05 16:35:19 -05001236 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001237 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001238 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001239 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1240 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001241 }
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001242}
1243
1244/*
1245 * Finds the request corresponding to the RPC xid and invokes the common
1246 * tcp read code to read the data.
1247 */
1248static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1249 struct xdr_skb_reader *desc)
1250{
1251 struct sock_xprt *transport =
1252 container_of(xprt, struct sock_xprt, xprt);
1253 struct rpc_rqst *req;
1254
1255 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1256
1257 /* Find and lock the request corresponding to this xid */
1258 spin_lock(&xprt->transport_lock);
1259 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1260 if (!req) {
1261 dprintk("RPC: XID %08x request not found!\n",
1262 ntohl(transport->tcp_xid));
1263 spin_unlock(&xprt->transport_lock);
1264 return -1;
1265 }
1266
1267 xs_tcp_read_common(xprt, desc, req);
1268
Chuck Levere136d092006-12-05 16:35:23 -05001269 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001270 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001271
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001272 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001273 return 0;
1274}
1275
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001276#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001277/*
1278 * Obtains an rpc_rqst previously allocated and invokes the common
1279 * tcp read code to read the data. The result is placed in the callback
1280 * queue.
1281 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1282 * connection and return -1.
1283 */
1284static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1285 struct xdr_skb_reader *desc)
1286{
1287 struct sock_xprt *transport =
1288 container_of(xprt, struct sock_xprt, xprt);
1289 struct rpc_rqst *req;
1290
1291 req = xprt_alloc_bc_request(xprt);
1292 if (req == NULL) {
1293 printk(KERN_WARNING "Callback slot table overflowed\n");
1294 xprt_force_disconnect(xprt);
1295 return -1;
1296 }
1297
1298 req->rq_xid = transport->tcp_xid;
1299 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1300 xs_tcp_read_common(xprt, desc, req);
1301
1302 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1303 struct svc_serv *bc_serv = xprt->bc_serv;
1304
1305 /*
1306 * Add callback request to callback list. The callback
1307 * service sleeps on the sv_cb_waitq waiting for new
1308 * requests. Wake it up after adding enqueing the
1309 * request.
1310 */
1311 dprintk("RPC: add callback request to list\n");
1312 spin_lock(&bc_serv->sv_cb_lock);
1313 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1314 spin_unlock(&bc_serv->sv_cb_lock);
1315 wake_up(&bc_serv->sv_cb_waitq);
1316 }
1317
1318 req->rq_private_buf.len = transport->tcp_copied;
1319
1320 return 0;
1321}
1322
1323static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1324 struct xdr_skb_reader *desc)
1325{
1326 struct sock_xprt *transport =
1327 container_of(xprt, struct sock_xprt, xprt);
1328
1329 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1330 xs_tcp_read_reply(xprt, desc) :
1331 xs_tcp_read_callback(xprt, desc);
1332}
1333#else
1334static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1335 struct xdr_skb_reader *desc)
1336{
1337 return xs_tcp_read_reply(xprt, desc);
1338}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001339#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001340
1341/*
1342 * Read data off the transport. This can be either an RPC_CALL or an
1343 * RPC_REPLY. Relay the processing to helper functions.
1344 */
1345static void xs_tcp_read_data(struct rpc_xprt *xprt,
1346 struct xdr_skb_reader *desc)
1347{
1348 struct sock_xprt *transport =
1349 container_of(xprt, struct sock_xprt, xprt);
1350
1351 if (_xs_tcp_read_data(xprt, desc) == 0)
1352 xs_tcp_check_fraghdr(transport);
1353 else {
1354 /*
1355 * The transport_lock protects the request handling.
1356 * There's no need to hold it to update the tcp_flags.
1357 */
1358 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1359 }
Chuck Levera246b012005-08-11 16:25:23 -04001360}
1361
Chuck Leverdd456472006-12-05 16:35:44 -05001362static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001363{
1364 size_t len;
1365
Chuck Lever51971132006-12-05 16:35:19 -05001366 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001367 if (len > desc->count)
1368 len = desc->count;
1369 desc->count -= len;
1370 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001371 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001372 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001373 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001374}
1375
Chuck Lever9903cd12005-08-11 16:25:26 -04001376static 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 -04001377{
1378 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001379 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001380 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001381 .skb = skb,
1382 .offset = offset,
1383 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001384 };
Chuck Levera246b012005-08-11 16:25:23 -04001385
Chuck Lever46121cf2007-01-31 12:14:08 -05001386 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001387 do {
1388 /* Read in a new fragment marker if necessary */
1389 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001390 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001391 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001392 continue;
1393 }
1394 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001395 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001396 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001397 continue;
1398 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001399 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001400 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001401 xs_tcp_read_calldir(transport, &desc);
1402 continue;
1403 }
Chuck Levera246b012005-08-11 16:25:23 -04001404 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001405 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001406 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001407 continue;
1408 }
1409 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001410 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001411 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001412 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001413 return len - desc.count;
1414}
1415
Chuck Lever9903cd12005-08-11 16:25:26 -04001416/**
1417 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1418 * @sk: socket with data to read
1419 * @bytes: how much data to read
1420 *
1421 */
1422static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001423{
1424 struct rpc_xprt *xprt;
1425 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001426 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001427
Chuck Lever46121cf2007-01-31 12:14:08 -05001428 dprintk("RPC: xs_tcp_data_ready...\n");
1429
Eric Dumazetf064af12010-09-22 12:43:39 +00001430 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001431 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001432 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001433 if (xprt->shutdown)
1434 goto out;
1435
Neil Brown61d0a8e2009-09-23 14:36:37 -04001436 /* Any data means we had a useful conversation, so
1437 * the we don't need to delay the next reconnect
1438 */
1439 if (xprt->reestablish_timeout)
1440 xprt->reestablish_timeout = 0;
1441
Chuck Lever9903cd12005-08-11 16:25:26 -04001442 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001443 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001444 do {
1445 rd_desc.count = 65536;
1446 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1447 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001448out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001449 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001450}
1451
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001452/*
1453 * Do the equivalent of linger/linger2 handling for dealing with
1454 * broken servers that don't close the socket in a timely
1455 * fashion
1456 */
1457static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1458 unsigned long timeout)
1459{
1460 struct sock_xprt *transport;
1461
1462 if (xprt_test_and_set_connecting(xprt))
1463 return;
1464 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1465 transport = container_of(xprt, struct sock_xprt, xprt);
1466 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1467 timeout);
1468}
1469
1470static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1471{
1472 struct sock_xprt *transport;
1473
1474 transport = container_of(xprt, struct sock_xprt, xprt);
1475
1476 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1477 !cancel_delayed_work(&transport->connect_worker))
1478 return;
1479 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1480 xprt_clear_connecting(xprt);
1481}
1482
Trond Myklebustf5f65f12012-10-23 17:50:07 -04001483static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001484{
1485 smp_mb__before_clear_bit();
Trond Myklebuste23c80e2012-09-12 16:49:15 -04001486 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1487 clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001488 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1489 clear_bit(XPRT_CLOSING, &xprt->state);
1490 smp_mb__after_clear_bit();
Trond Myklebustf5f65f12012-10-23 17:50:07 -04001491}
1492
1493static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1494{
1495 xs_sock_reset_connection_flags(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001496 /* Mark transport as closed and wake up all pending tasks */
1497 xprt_disconnect_done(xprt);
1498}
1499
Chuck Lever9903cd12005-08-11 16:25:26 -04001500/**
1501 * xs_tcp_state_change - callback to handle TCP socket state changes
1502 * @sk: socket whose state has changed
1503 *
1504 */
1505static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001506{
Chuck Lever9903cd12005-08-11 16:25:26 -04001507 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001508
Eric Dumazetf064af12010-09-22 12:43:39 +00001509 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001510 if (!(xprt = xprt_from_sock(sk)))
1511 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001512 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001513 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001514 sk->sk_state, xprt_connected(xprt),
1515 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001516 sock_flag(sk, SOCK_ZAPPED),
1517 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001518
1519 switch (sk->sk_state) {
1520 case TCP_ESTABLISHED:
Eric Dumazetf064af12010-09-22 12:43:39 +00001521 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001522 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001523 struct sock_xprt *transport = container_of(xprt,
1524 struct sock_xprt, xprt);
1525
Chuck Levera246b012005-08-11 16:25:23 -04001526 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001527 transport->tcp_offset = 0;
1528 transport->tcp_reclen = 0;
1529 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001530 transport->tcp_flags =
1531 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001532
Trond Myklebust2a491992009-03-11 14:38:00 -04001533 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001534 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001535 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001536 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001537 case TCP_FIN_WAIT1:
1538 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001539 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001540 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001541 set_bit(XPRT_CLOSING, &xprt->state);
1542 smp_mb__before_clear_bit();
1543 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001544 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001545 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001546 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001547 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001548 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001549 /* The server initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001550 xprt->connect_cookie++;
Trond Myklebust362eb452012-10-23 11:35:47 -04001551 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebuste23c80e2012-09-12 16:49:15 -04001552 xs_tcp_force_close(xprt);
Trond Myklebust663b8852008-01-01 18:42:12 -05001553 case TCP_CLOSING:
1554 /*
1555 * If the server closed down the connection, make sure that
1556 * we back off before reconnecting
1557 */
1558 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1559 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001560 break;
1561 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001562 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001563 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001564 smp_mb__before_clear_bit();
1565 clear_bit(XPRT_CONNECTED, &xprt->state);
1566 smp_mb__after_clear_bit();
1567 break;
1568 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001569 xs_tcp_cancel_linger_timeout(xprt);
1570 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001571 }
1572 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001573 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001574}
1575
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001576static void xs_write_space(struct sock *sk)
1577{
1578 struct socket *sock;
1579 struct rpc_xprt *xprt;
1580
1581 if (unlikely(!(sock = sk->sk_socket)))
1582 return;
1583 clear_bit(SOCK_NOSPACE, &sock->flags);
1584
1585 if (unlikely(!(xprt = xprt_from_sock(sk))))
1586 return;
1587 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1588 return;
1589
1590 xprt_write_space(xprt);
1591}
1592
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001593/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001594 * xs_udp_write_space - callback invoked when socket buffer space
1595 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001596 * @sk: socket whose state has changed
1597 *
Chuck Levera246b012005-08-11 16:25:23 -04001598 * Called when more output buffer space is available for this socket.
1599 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001600 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001601 * with a bunch of small requests.
1602 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001603static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001604{
Eric Dumazetf064af12010-09-22 12:43:39 +00001605 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001606
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001607 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001608 if (sock_writeable(sk))
1609 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001610
Eric Dumazetf064af12010-09-22 12:43:39 +00001611 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001612}
Chuck Levera246b012005-08-11 16:25:23 -04001613
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001614/**
1615 * xs_tcp_write_space - callback invoked when socket buffer space
1616 * becomes available
1617 * @sk: socket whose state has changed
1618 *
1619 * Called when more output buffer space is available for this socket.
1620 * We try not to wake our writers until they can make "significant"
1621 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1622 * with a bunch of small requests.
1623 */
1624static void xs_tcp_write_space(struct sock *sk)
1625{
Eric Dumazetf064af12010-09-22 12:43:39 +00001626 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001627
1628 /* from net/core/stream.c:sk_stream_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001629 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1630 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001631
Eric Dumazetf064af12010-09-22 12:43:39 +00001632 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001633}
1634
Chuck Lever470056c2005-08-25 16:25:56 -07001635static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001636{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001637 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1638 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001639
Chuck Lever7c6e0662006-12-05 16:35:30 -05001640 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001641 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001642 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001643 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001644 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001645 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001646 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001647 sk->sk_write_space(sk);
1648 }
1649}
1650
Chuck Lever43118c22005-08-25 16:25:49 -07001651/**
Chuck Lever470056c2005-08-25 16:25:56 -07001652 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001653 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001654 * @sndsize: requested size of send buffer, in bytes
1655 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001656 *
Chuck Lever470056c2005-08-25 16:25:56 -07001657 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001658 */
Chuck Lever470056c2005-08-25 16:25:56 -07001659static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001660{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001661 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1662
1663 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001664 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001665 transport->sndsize = sndsize + 1024;
1666 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001667 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001668 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001669
1670 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001671}
1672
Chuck Lever46c0ee82005-08-25 16:25:52 -07001673/**
1674 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1675 * @task: task that timed out
1676 *
1677 * Adjust the congestion window after a retransmit timeout has occurred.
1678 */
1679static void xs_udp_timer(struct rpc_task *task)
1680{
1681 xprt_adjust_cwnd(task, -ETIMEDOUT);
1682}
1683
Chuck Leverb85d8802006-05-25 01:40:49 -04001684static unsigned short xs_get_random_port(void)
1685{
1686 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1687 unsigned short rand = (unsigned short) net_random() % range;
1688 return rand + xprt_min_resvport;
1689}
1690
Chuck Lever92200412006-01-03 09:55:51 +01001691/**
1692 * xs_set_port - reset the port number in the remote endpoint address
1693 * @xprt: generic transport
1694 * @port: new port number
1695 *
1696 */
1697static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1698{
Chuck Lever46121cf2007-01-31 12:14:08 -05001699 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001700
Chuck Lever9dc3b092009-08-09 15:09:46 -04001701 rpc_set_port(xs_addr(xprt), port);
1702 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001703}
1704
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001705static unsigned short xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001706{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001707 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001708
1709 if (port == 0 && transport->xprt.resvport)
1710 port = xs_get_random_port();
1711 return port;
1712}
1713
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001714static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001715{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001716 if (transport->srcport != 0)
1717 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001718 if (!transport->xprt.resvport)
1719 return 0;
1720 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1721 return xprt_max_resvport;
1722 return --port;
1723}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001724static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001725{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001726 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001727 int err, nloop = 0;
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001728 unsigned short port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001729 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001730
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001731 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001732 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001733 rpc_set_port((struct sockaddr *)&myaddr, port);
1734 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1735 transport->xprt.addrlen);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001736 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001737 break;
Chuck Levera246b012005-08-11 16:25:23 -04001738 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001739 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001740 break;
Chuck Levera246b012005-08-11 16:25:23 -04001741 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001742 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001743 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001744 if (port > last)
1745 nloop++;
1746 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001747
Chuck Lever4232e862010-10-20 11:52:51 -04001748 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001749 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1750 &((struct sockaddr_in *)&myaddr)->sin_addr,
1751 port, err ? "failed" : "ok", err);
1752 else
1753 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1754 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1755 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001756 return err;
1757}
1758
Chuck Lever176e21e2011-05-09 15:22:44 -04001759/*
1760 * We don't support autobind on AF_LOCAL sockets
1761 */
1762static void xs_local_rpcbind(struct rpc_task *task)
1763{
1764 xprt_set_bound(task->tk_xprt);
1765}
1766
1767static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1768{
1769}
Chuck Levera246b012005-08-11 16:25:23 -04001770
Peter Zijlstraed075362006-12-06 20:35:24 -08001771#ifdef CONFIG_DEBUG_LOCK_ALLOC
1772static struct lock_class_key xs_key[2];
1773static struct lock_class_key xs_slock_key[2];
1774
Chuck Lever176e21e2011-05-09 15:22:44 -04001775static inline void xs_reclassify_socketu(struct socket *sock)
1776{
1777 struct sock *sk = sock->sk;
1778
1779 BUG_ON(sock_owned_by_user(sk));
1780 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1781 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1782}
1783
Chuck Lever8945ee52007-08-06 11:58:04 -04001784static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001785{
1786 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001787
John Heffner02b3d342007-09-12 10:42:12 +02001788 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001789 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1790 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1791}
Peter Zijlstraed075362006-12-06 20:35:24 -08001792
Chuck Lever8945ee52007-08-06 11:58:04 -04001793static inline void xs_reclassify_socket6(struct socket *sock)
1794{
1795 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001796
Linus Torvaldsf4921af2007-10-15 10:46:05 -07001797 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001798 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1799 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001800}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001801
1802static inline void xs_reclassify_socket(int family, struct socket *sock)
1803{
Chuck Lever4232e862010-10-20 11:52:51 -04001804 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001805 case AF_LOCAL:
1806 xs_reclassify_socketu(sock);
1807 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001808 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001809 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001810 break;
1811 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001812 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001813 break;
1814 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001815}
Peter Zijlstraed075362006-12-06 20:35:24 -08001816#else
Chuck Lever176e21e2011-05-09 15:22:44 -04001817static inline void xs_reclassify_socketu(struct socket *sock)
1818{
1819}
1820
Chuck Lever8945ee52007-08-06 11:58:04 -04001821static inline void xs_reclassify_socket4(struct socket *sock)
1822{
1823}
1824
1825static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001826{
1827}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001828
1829static inline void xs_reclassify_socket(int family, struct socket *sock)
1830{
1831}
Peter Zijlstraed075362006-12-06 20:35:24 -08001832#endif
1833
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001834static struct socket *xs_create_sock(struct rpc_xprt *xprt,
1835 struct sock_xprt *transport, int family, int type, int protocol)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001836{
1837 struct socket *sock;
1838 int err;
1839
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001840 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001841 if (err < 0) {
1842 dprintk("RPC: can't create %d transport socket (%d).\n",
1843 protocol, -err);
1844 goto out;
1845 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001846 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001847
Ben Hutchings4cea2882011-02-22 21:54:34 +00001848 err = xs_bind(transport, sock);
1849 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001850 sock_release(sock);
1851 goto out;
1852 }
1853
1854 return sock;
1855out:
1856 return ERR_PTR(err);
1857}
1858
Chuck Lever176e21e2011-05-09 15:22:44 -04001859static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1860 struct socket *sock)
1861{
1862 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1863 xprt);
1864
1865 if (!transport->inet) {
1866 struct sock *sk = sock->sk;
1867
1868 write_lock_bh(&sk->sk_callback_lock);
1869
1870 xs_save_old_callbacks(transport, sk);
1871
1872 sk->sk_user_data = xprt;
1873 sk->sk_data_ready = xs_local_data_ready;
1874 sk->sk_write_space = xs_udp_write_space;
Chuck Lever176e21e2011-05-09 15:22:44 -04001875 sk->sk_allocation = GFP_ATOMIC;
1876
1877 xprt_clear_connected(xprt);
1878
1879 /* Reset to new socket */
1880 transport->sock = sock;
1881 transport->inet = sk;
1882
1883 write_unlock_bh(&sk->sk_callback_lock);
1884 }
1885
1886 /* Tell the socket layer to start connecting... */
1887 xprt->stat.connect_count++;
1888 xprt->stat.connect_start = jiffies;
1889 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1890}
1891
1892/**
1893 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1894 * @xprt: RPC transport to connect
1895 * @transport: socket transport to connect
1896 * @create_sock: function to create a socket of the correct type
1897 *
1898 * Invoked by a work queue tasklet.
1899 */
1900static void xs_local_setup_socket(struct work_struct *work)
1901{
1902 struct sock_xprt *transport =
1903 container_of(work, struct sock_xprt, connect_worker.work);
1904 struct rpc_xprt *xprt = &transport->xprt;
1905 struct socket *sock;
1906 int status = -EIO;
1907
1908 if (xprt->shutdown)
1909 goto out;
1910
Jeff Layton05630012012-07-23 13:58:51 -04001911 current->flags |= PF_FSTRANS;
1912
Chuck Lever176e21e2011-05-09 15:22:44 -04001913 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1914 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1915 SOCK_STREAM, 0, &sock, 1);
1916 if (status < 0) {
1917 dprintk("RPC: can't create AF_LOCAL "
1918 "transport socket (%d).\n", -status);
1919 goto out;
1920 }
1921 xs_reclassify_socketu(sock);
1922
1923 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1924 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1925
1926 status = xs_local_finish_connecting(xprt, sock);
1927 switch (status) {
1928 case 0:
1929 dprintk("RPC: xprt %p connected to %s\n",
1930 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1931 xprt_set_connected(xprt);
1932 break;
1933 case -ENOENT:
1934 dprintk("RPC: xprt %p: socket %s does not exist\n",
1935 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1936 break;
1937 default:
1938 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1939 __func__, -status,
1940 xprt->address_strings[RPC_DISPLAY_ADDR]);
1941 }
1942
1943out:
1944 xprt_clear_connecting(xprt);
1945 xprt_wake_pending_tasks(xprt, status);
Jeff Layton05630012012-07-23 13:58:51 -04001946 current->flags &= ~PF_FSTRANS;
Chuck Lever176e21e2011-05-09 15:22:44 -04001947}
1948
Chuck Lever16be2d22007-08-06 11:57:38 -04001949static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001950{
Chuck Lever16be2d22007-08-06 11:57:38 -04001951 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001952
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001953 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001954 struct sock *sk = sock->sk;
1955
1956 write_lock_bh(&sk->sk_callback_lock);
1957
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001958 xs_save_old_callbacks(transport, sk);
1959
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001960 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001961 sk->sk_data_ready = xs_udp_data_ready;
1962 sk->sk_write_space = xs_udp_write_space;
1963 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa72005-12-13 16:13:52 -05001964 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001965
1966 xprt_set_connected(xprt);
1967
1968 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001969 transport->sock = sock;
1970 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001971
1972 write_unlock_bh(&sk->sk_callback_lock);
1973 }
Chuck Lever470056c2005-08-25 16:25:56 -07001974 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04001975}
1976
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04001977static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04001978{
1979 struct sock_xprt *transport =
1980 container_of(work, struct sock_xprt, connect_worker.work);
1981 struct rpc_xprt *xprt = &transport->xprt;
1982 struct socket *sock = transport->sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04001983 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04001984
Trond Myklebust01d37c42009-03-11 14:09:39 -04001985 if (xprt->shutdown)
Chuck Lever9903cd12005-08-11 16:25:26 -04001986 goto out;
1987
Jeff Layton05630012012-07-23 13:58:51 -04001988 current->flags |= PF_FSTRANS;
1989
Chuck Levera246b012005-08-11 16:25:23 -04001990 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001991 xs_reset_transport(transport);
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04001992 sock = xs_create_sock(xprt, transport,
1993 xs_addr(xprt)->sa_family, SOCK_DGRAM, IPPROTO_UDP);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04001994 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04001995 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04001996
Chuck Leverc740eff2009-08-09 15:09:46 -04001997 dprintk("RPC: worker connecting xprt %p via %s to "
1998 "%s (port %s)\n", xprt,
1999 xprt->address_strings[RPC_DISPLAY_PROTO],
2000 xprt->address_strings[RPC_DISPLAY_ADDR],
2001 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04002002
2003 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04002004 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002005out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002006 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002007 xprt_wake_pending_tasks(xprt, status);
Jeff Layton05630012012-07-23 13:58:51 -04002008 current->flags &= ~PF_FSTRANS;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002009}
2010
Chuck Lever3167e122005-08-25 16:25:55 -07002011/*
2012 * We need to preserve the port number so the reply cache on the server can
2013 * find our cached RPC replies when we get around to reconnecting.
2014 */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002015static void xs_abort_connection(struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07002016{
2017 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07002018 struct sockaddr any;
2019
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002020 dprintk("RPC: disconnecting xprt %p to reuse port\n", transport);
Chuck Lever3167e122005-08-25 16:25:55 -07002021
2022 /*
2023 * Disconnect the transport socket by doing a connect operation
2024 * with AF_UNSPEC. This should return immediately...
2025 */
2026 memset(&any, 0, sizeof(any));
2027 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002028 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002029 if (!result)
Trond Myklebustf5f65f12012-10-23 17:50:07 -04002030 xs_sock_reset_connection_flags(&transport->xprt);
2031 dprintk("RPC: AF_UNSPEC connect return code %d\n", result);
Chuck Lever3167e122005-08-25 16:25:55 -07002032}
2033
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002034static void xs_tcp_reuse_connection(struct sock_xprt *transport)
Trond Myklebust40d25492009-03-11 14:37:58 -04002035{
2036 unsigned int state = transport->inet->sk_state;
2037
Andy Chittenden669502f2010-08-10 10:19:53 -04002038 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) {
2039 /* we don't need to abort the connection if the socket
2040 * hasn't undergone a shutdown
2041 */
2042 if (transport->inet->sk_shutdown == 0)
2043 return;
2044 dprintk("RPC: %s: TCP_CLOSEd and sk_shutdown set to %d\n",
2045 __func__, transport->inet->sk_shutdown);
2046 }
2047 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) {
2048 /* we don't need to abort the connection if the socket
2049 * hasn't undergone a shutdown
2050 */
2051 if (transport->inet->sk_shutdown == 0)
2052 return;
2053 dprintk("RPC: %s: ESTABLISHED/SYN_SENT "
2054 "sk_shutdown set to %d\n",
2055 __func__, transport->inet->sk_shutdown);
2056 }
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002057 xs_abort_connection(transport);
Trond Myklebust40d25492009-03-11 14:37:58 -04002058}
2059
Chuck Lever16be2d22007-08-06 11:57:38 -04002060static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002061{
Chuck Lever16be2d22007-08-06 11:57:38 -04002062 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002063 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002064
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002065 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002066 struct sock *sk = sock->sk;
2067
2068 write_lock_bh(&sk->sk_callback_lock);
2069
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002070 xs_save_old_callbacks(transport, sk);
2071
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002072 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002073 sk->sk_data_ready = xs_tcp_data_ready;
2074 sk->sk_state_change = xs_tcp_state_change;
2075 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebustb079fa72005-12-13 16:13:52 -05002076 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07002077
2078 /* socket options */
2079 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
2080 sock_reset_flag(sk, SOCK_LINGER);
2081 tcp_sk(sk)->linger2 = 0;
2082 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002083
2084 xprt_clear_connected(xprt);
2085
2086 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002087 transport->sock = sock;
2088 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002089
2090 write_unlock_bh(&sk->sk_callback_lock);
2091 }
2092
Trond Myklebust01d37c42009-03-11 14:09:39 -04002093 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002094 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002095
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002096 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05002097 xprt->stat.connect_count++;
2098 xprt->stat.connect_start = jiffies;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002099 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2100 switch (ret) {
2101 case 0:
2102 case -EINPROGRESS:
2103 /* SYN_SENT! */
2104 xprt->connect_cookie++;
2105 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2106 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2107 }
2108out:
2109 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002110}
2111
2112/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002113 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2114 * @xprt: RPC transport to connect
2115 * @transport: socket transport to connect
2116 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04002117 *
2118 * Invoked by a work queue tasklet.
2119 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002120static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002121{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002122 struct sock_xprt *transport =
2123 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002124 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002125 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002126 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002127
Trond Myklebust01d37c42009-03-11 14:09:39 -04002128 if (xprt->shutdown)
Chuck Lever16be2d22007-08-06 11:57:38 -04002129 goto out;
2130
Jeff Layton05630012012-07-23 13:58:51 -04002131 current->flags |= PF_FSTRANS;
2132
Chuck Lever16be2d22007-08-06 11:57:38 -04002133 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002134 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002135 sock = xs_create_sock(xprt, transport,
2136 xs_addr(xprt)->sa_family, SOCK_STREAM, IPPROTO_TCP);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002137 if (IS_ERR(sock)) {
2138 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002139 goto out;
2140 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002141 } else {
2142 int abort_and_exit;
2143
2144 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
2145 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04002146 /* "close" the socket, preserving the local port */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002147 xs_tcp_reuse_connection(transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04002148
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002149 if (abort_and_exit)
2150 goto out_eagain;
2151 }
2152
Chuck Leverc740eff2009-08-09 15:09:46 -04002153 dprintk("RPC: worker connecting xprt %p via %s to "
2154 "%s (port %s)\n", xprt,
2155 xprt->address_strings[RPC_DISPLAY_PROTO],
2156 xprt->address_strings[RPC_DISPLAY_ADDR],
2157 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002158
2159 status = xs_tcp_finish_connecting(xprt, sock);
Chuck Lever46121cf2007-01-31 12:14:08 -05002160 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2161 xprt, -status, xprt_connected(xprt),
2162 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002163 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002164 default:
2165 printk("%s: connect returned unhandled error %d\n",
2166 __func__, status);
2167 case -EADDRNOTAVAIL:
2168 /* We're probably in TIME_WAIT. Get rid of existing socket,
2169 * and retry
2170 */
Trond Myklebuste23c80e2012-09-12 16:49:15 -04002171 xs_tcp_force_close(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002172 break;
Trond Myklebust8a2cec22009-03-11 14:38:01 -04002173 case -ECONNREFUSED:
2174 case -ECONNRESET:
2175 case -ENETUNREACH:
2176 /* retry with existing socket, after a delay */
Trond Myklebust2a491992009-03-11 14:38:00 -04002177 case 0:
2178 case -EINPROGRESS:
2179 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002180 xprt_clear_connecting(xprt);
Jeff Layton05630012012-07-23 13:58:51 -04002181 current->flags &= ~PF_FSTRANS;
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002182 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002183 case -EINVAL:
2184 /* Happens, for instance, if the user specified a link
2185 * local IPv6 address without a scope-id.
2186 */
2187 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002188 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002189out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04002190 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002191out:
Chuck Lever2226feb2005-08-11 16:25:38 -04002192 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002193 xprt_wake_pending_tasks(xprt, status);
Jeff Layton05630012012-07-23 13:58:51 -04002194 current->flags &= ~PF_FSTRANS;
Chuck Levera246b012005-08-11 16:25:23 -04002195}
2196
Chuck Lever68e220b2007-08-06 11:57:48 -04002197/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002198 * xs_connect - connect a socket to a remote endpoint
2199 * @task: address of RPC task that manages state of connect request
2200 *
2201 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002202 *
2203 * UDP socket connects are synchronous, but we use a work queue anyway
2204 * to guarantee that even unprivileged user processes can set up a
2205 * socket on a privileged port.
2206 *
2207 * If a UDP socket connect fails, the delay behavior here prevents
2208 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002209 */
2210static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002211{
2212 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002213 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002214
Chuck Lever09a21c42009-12-03 15:58:56 -05002215 if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002216 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2217 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002218 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002219 queue_delayed_work(rpciod_workqueue,
2220 &transport->connect_worker,
2221 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07002222 xprt->reestablish_timeout <<= 1;
Neil Brown61d0a8e2009-09-23 14:36:37 -04002223 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2224 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002225 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2226 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002227 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05002228 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002229 queue_delayed_work(rpciod_workqueue,
2230 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002231 }
2232}
2233
Chuck Lever262ca072006-03-20 13:44:16 -05002234/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002235 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2236 * @xprt: rpc_xprt struct containing statistics
2237 * @seq: output file
2238 *
2239 */
2240static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2241{
2242 long idle_time = 0;
2243
2244 if (xprt_connected(xprt))
2245 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2246
2247 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
Andy Adamson15a45202012-02-14 16:19:18 -05002248 "%llu %llu %lu %llu %llu\n",
Chuck Lever176e21e2011-05-09 15:22:44 -04002249 xprt->stat.bind_count,
2250 xprt->stat.connect_count,
2251 xprt->stat.connect_time,
2252 idle_time,
2253 xprt->stat.sends,
2254 xprt->stat.recvs,
2255 xprt->stat.bad_xids,
2256 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002257 xprt->stat.bklog_u,
2258 xprt->stat.max_slots,
2259 xprt->stat.sending_u,
2260 xprt->stat.pending_u);
Chuck Lever176e21e2011-05-09 15:22:44 -04002261}
2262
2263/**
Chuck Lever262ca072006-03-20 13:44:16 -05002264 * xs_udp_print_stats - display UDP socket-specifc stats
2265 * @xprt: rpc_xprt struct containing statistics
2266 * @seq: output file
2267 *
2268 */
2269static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2270{
Chuck Leverc8475462006-12-05 16:35:26 -05002271 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2272
Andy Adamson15a45202012-02-14 16:19:18 -05002273 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2274 "%lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002275 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002276 xprt->stat.bind_count,
2277 xprt->stat.sends,
2278 xprt->stat.recvs,
2279 xprt->stat.bad_xids,
2280 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002281 xprt->stat.bklog_u,
2282 xprt->stat.max_slots,
2283 xprt->stat.sending_u,
2284 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002285}
2286
2287/**
2288 * xs_tcp_print_stats - display TCP socket-specifc stats
2289 * @xprt: rpc_xprt struct containing statistics
2290 * @seq: output file
2291 *
2292 */
2293static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2294{
Chuck Leverc8475462006-12-05 16:35:26 -05002295 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002296 long idle_time = 0;
2297
2298 if (xprt_connected(xprt))
2299 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2300
Andy Adamson15a45202012-02-14 16:19:18 -05002301 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2302 "%llu %llu %lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002303 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002304 xprt->stat.bind_count,
2305 xprt->stat.connect_count,
2306 xprt->stat.connect_time,
2307 idle_time,
2308 xprt->stat.sends,
2309 xprt->stat.recvs,
2310 xprt->stat.bad_xids,
2311 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002312 xprt->stat.bklog_u,
2313 xprt->stat.max_slots,
2314 xprt->stat.sending_u,
2315 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002316}
2317
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002318/*
2319 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2320 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2321 * to use the server side send routines.
2322 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002323static void *bc_malloc(struct rpc_task *task, size_t size)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002324{
2325 struct page *page;
2326 struct rpc_buffer *buf;
2327
2328 BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2329 page = alloc_page(GFP_KERNEL);
2330
2331 if (!page)
2332 return NULL;
2333
2334 buf = page_address(page);
2335 buf->len = PAGE_SIZE;
2336
2337 return buf->data;
2338}
2339
2340/*
2341 * Free the space allocated in the bc_alloc routine
2342 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002343static void bc_free(void *buffer)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002344{
2345 struct rpc_buffer *buf;
2346
2347 if (!buffer)
2348 return;
2349
2350 buf = container_of(buffer, struct rpc_buffer, data);
2351 free_page((unsigned long)buf);
2352}
2353
2354/*
2355 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2356 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2357 */
2358static int bc_sendto(struct rpc_rqst *req)
2359{
2360 int len;
2361 struct xdr_buf *xbufp = &req->rq_snd_buf;
2362 struct rpc_xprt *xprt = req->rq_xprt;
2363 struct sock_xprt *transport =
2364 container_of(xprt, struct sock_xprt, xprt);
2365 struct socket *sock = transport->sock;
2366 unsigned long headoff;
2367 unsigned long tailoff;
2368
Chuck Lever61677ee2011-05-09 15:22:34 -04002369 xs_encode_stream_record_marker(xbufp);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002370
2371 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2372 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2373 len = svc_send_common(sock, xbufp,
2374 virt_to_page(xbufp->head[0].iov_base), headoff,
2375 xbufp->tail[0].iov_base, tailoff);
2376
2377 if (len != xbufp->len) {
2378 printk(KERN_NOTICE "Error sending entire callback!\n");
2379 len = -EAGAIN;
2380 }
2381
2382 return len;
2383}
2384
2385/*
2386 * The send routine. Borrows from svc_send
2387 */
2388static int bc_send_request(struct rpc_task *task)
2389{
2390 struct rpc_rqst *req = task->tk_rqstp;
2391 struct svc_xprt *xprt;
2392 struct svc_sock *svsk;
2393 u32 len;
2394
2395 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2396 /*
2397 * Get the server socket associated with this callback xprt
2398 */
2399 xprt = req->rq_xprt->bc_xprt;
2400 svsk = container_of(xprt, struct svc_sock, sk_xprt);
2401
2402 /*
2403 * Grab the mutex to serialize data as the connection is shared
2404 * with the fore channel
2405 */
2406 if (!mutex_trylock(&xprt->xpt_mutex)) {
2407 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2408 if (!mutex_trylock(&xprt->xpt_mutex))
2409 return -EAGAIN;
2410 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2411 }
2412 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2413 len = -ENOTCONN;
2414 else
2415 len = bc_sendto(req);
2416 mutex_unlock(&xprt->xpt_mutex);
2417
2418 if (len > 0)
2419 len = 0;
2420
2421 return len;
2422}
2423
2424/*
2425 * The close routine. Since this is client initiated, we do nothing
2426 */
2427
2428static void bc_close(struct rpc_xprt *xprt)
2429{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002430}
2431
2432/*
2433 * The xprt destroy routine. Again, because this connection is client
2434 * initiated, we do nothing
2435 */
2436
2437static void bc_destroy(struct rpc_xprt *xprt)
2438{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002439}
2440
Chuck Lever176e21e2011-05-09 15:22:44 -04002441static struct rpc_xprt_ops xs_local_ops = {
2442 .reserve_xprt = xprt_reserve_xprt,
2443 .release_xprt = xs_tcp_release_xprt,
Trond Myklebust49c26da2012-10-22 12:56:58 -04002444 .alloc_slot = xprt_alloc_slot,
Chuck Lever176e21e2011-05-09 15:22:44 -04002445 .rpcbind = xs_local_rpcbind,
2446 .set_port = xs_local_set_port,
2447 .connect = xs_connect,
2448 .buf_alloc = rpc_malloc,
2449 .buf_free = rpc_free,
2450 .send_request = xs_local_send_request,
2451 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2452 .close = xs_close,
2453 .destroy = xs_destroy,
2454 .print_stats = xs_local_print_stats,
2455};
2456
Chuck Lever262965f2005-08-11 16:25:56 -04002457static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002458 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002459 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002460 .release_xprt = xprt_release_xprt_cong,
Trond Myklebust49c26da2012-10-22 12:56:58 -04002461 .alloc_slot = xprt_alloc_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002462 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002463 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002464 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002465 .buf_alloc = rpc_malloc,
2466 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002467 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002468 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002469 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002470 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002471 .close = xs_close,
2472 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002473 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04002474};
2475
2476static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002477 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002478 .release_xprt = xs_tcp_release_xprt,
Trond Myklebust49c26da2012-10-22 12:56:58 -04002479 .alloc_slot = xprt_lock_and_alloc_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002480 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002481 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002482 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002483 .buf_alloc = rpc_malloc,
2484 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002485 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002486 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustf75e6742009-04-21 17:18:20 -04002487 .close = xs_tcp_close,
Chuck Lever9903cd12005-08-11 16:25:26 -04002488 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002489 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002490};
2491
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002492/*
2493 * The rpc_xprt_ops for the server backchannel
2494 */
2495
2496static struct rpc_xprt_ops bc_tcp_ops = {
2497 .reserve_xprt = xprt_reserve_xprt,
2498 .release_xprt = xprt_release_xprt,
Trond Myklebust49c26da2012-10-22 12:56:58 -04002499 .alloc_slot = xprt_alloc_slot,
J. Bruce Fields92769102012-03-23 15:25:03 -04002500 .rpcbind = xs_local_rpcbind,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002501 .buf_alloc = bc_malloc,
2502 .buf_free = bc_free,
2503 .send_request = bc_send_request,
2504 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2505 .close = bc_close,
2506 .destroy = bc_destroy,
2507 .print_stats = xs_tcp_print_stats,
2508};
2509
Chuck Lever92476852010-10-20 11:53:01 -04002510static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2511{
2512 static const struct sockaddr_in sin = {
2513 .sin_family = AF_INET,
2514 .sin_addr.s_addr = htonl(INADDR_ANY),
2515 };
2516 static const struct sockaddr_in6 sin6 = {
2517 .sin6_family = AF_INET6,
2518 .sin6_addr = IN6ADDR_ANY_INIT,
2519 };
2520
2521 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002522 case AF_LOCAL:
2523 break;
Chuck Lever92476852010-10-20 11:53:01 -04002524 case AF_INET:
2525 memcpy(sap, &sin, sizeof(sin));
2526 break;
2527 case AF_INET6:
2528 memcpy(sap, &sin6, sizeof(sin6));
2529 break;
2530 default:
2531 dprintk("RPC: %s: Bad address family\n", __func__);
2532 return -EAFNOSUPPORT;
2533 }
2534 return 0;
2535}
2536
\"Talpey, Thomas\3c341b02007-09-10 13:47:07 -04002537static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002538 unsigned int slot_table_size,
2539 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002540{
2541 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002542 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002543
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002544 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002545 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002546 return ERR_PTR(-EBADF);
2547 }
2548
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002549 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2550 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002551 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002552 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2553 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002554 return ERR_PTR(-ENOMEM);
2555 }
2556
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002557 new = container_of(xprt, struct sock_xprt, xprt);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002558 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2559 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002560 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002561 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002562 else {
2563 int err;
2564 err = xs_init_anyaddr(args->dstaddr->sa_family,
2565 (struct sockaddr *)&new->srcaddr);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002566 if (err != 0) {
2567 xprt_free(xprt);
Chuck Lever92476852010-10-20 11:53:01 -04002568 return ERR_PTR(err);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002569 }
Chuck Lever92476852010-10-20 11:53:01 -04002570 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002571
2572 return xprt;
2573}
2574
Chuck Lever176e21e2011-05-09 15:22:44 -04002575static const struct rpc_timeout xs_local_default_timeout = {
2576 .to_initval = 10 * HZ,
2577 .to_maxval = 10 * HZ,
2578 .to_retries = 2,
2579};
2580
2581/**
2582 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2583 * @args: rpc transport creation arguments
2584 *
2585 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2586 */
2587static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2588{
2589 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2590 struct sock_xprt *transport;
2591 struct rpc_xprt *xprt;
2592 struct rpc_xprt *ret;
2593
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002594 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2595 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002596 if (IS_ERR(xprt))
2597 return xprt;
2598 transport = container_of(xprt, struct sock_xprt, xprt);
2599
2600 xprt->prot = 0;
2601 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2602 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2603
2604 xprt->bind_timeout = XS_BIND_TO;
2605 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2606 xprt->idle_timeout = XS_IDLE_DISC_TO;
2607
2608 xprt->ops = &xs_local_ops;
2609 xprt->timeout = &xs_local_default_timeout;
2610
2611 switch (sun->sun_family) {
2612 case AF_LOCAL:
2613 if (sun->sun_path[0] != '/') {
2614 dprintk("RPC: bad AF_LOCAL address: %s\n",
2615 sun->sun_path);
2616 ret = ERR_PTR(-EINVAL);
2617 goto out_err;
2618 }
2619 xprt_set_bound(xprt);
2620 INIT_DELAYED_WORK(&transport->connect_worker,
2621 xs_local_setup_socket);
2622 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
2623 break;
2624 default:
2625 ret = ERR_PTR(-EAFNOSUPPORT);
2626 goto out_err;
2627 }
2628
2629 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2630 xprt->address_strings[RPC_DISPLAY_ADDR]);
2631
2632 if (try_module_get(THIS_MODULE))
2633 return xprt;
2634 ret = ERR_PTR(-EINVAL);
2635out_err:
2636 xprt_free(xprt);
2637 return ret;
2638}
2639
Trond Myklebust2881ae72007-12-20 16:03:54 -05002640static const struct rpc_timeout xs_udp_default_timeout = {
2641 .to_initval = 5 * HZ,
2642 .to_maxval = 30 * HZ,
2643 .to_increment = 5 * HZ,
2644 .to_retries = 5,
2645};
2646
Chuck Lever9903cd12005-08-11 16:25:26 -04002647/**
2648 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002649 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002650 *
2651 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002652static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002653{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002654 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002655 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002656 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002657 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002658
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002659 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2660 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002661 if (IS_ERR(xprt))
2662 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002663 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002664
Chuck Leverec739ef2006-08-22 20:06:15 -04002665 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002666 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002667 /* XXX: header size can vary due to auth type, IPv6, etc. */
2668 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2669
Chuck Lever03bf4b72005-08-25 16:25:55 -07002670 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002671 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2672 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002673
Chuck Lever262965f2005-08-11 16:25:56 -04002674 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002675
Trond Myklebustba7392b2007-12-20 16:03:55 -05002676 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002677
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002678 switch (addr->sa_family) {
2679 case AF_INET:
2680 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2681 xprt_set_bound(xprt);
2682
2683 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002684 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002685 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002686 break;
2687 case AF_INET6:
2688 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2689 xprt_set_bound(xprt);
2690
2691 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002692 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002693 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002694 break;
2695 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002696 ret = ERR_PTR(-EAFNOSUPPORT);
2697 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002698 }
2699
Chuck Leverc740eff2009-08-09 15:09:46 -04002700 if (xprt_bound(xprt))
2701 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2702 xprt->address_strings[RPC_DISPLAY_ADDR],
2703 xprt->address_strings[RPC_DISPLAY_PORT],
2704 xprt->address_strings[RPC_DISPLAY_PROTO]);
2705 else
2706 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2707 xprt->address_strings[RPC_DISPLAY_ADDR],
2708 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002709
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002710 if (try_module_get(THIS_MODULE))
2711 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002712 ret = ERR_PTR(-EINVAL);
2713out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002714 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002715 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002716}
2717
Trond Myklebust2881ae72007-12-20 16:03:54 -05002718static const struct rpc_timeout xs_tcp_default_timeout = {
2719 .to_initval = 60 * HZ,
2720 .to_maxval = 60 * HZ,
2721 .to_retries = 2,
2722};
2723
Chuck Lever9903cd12005-08-11 16:25:26 -04002724/**
2725 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002726 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002727 *
2728 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002729static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002730{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002731 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002732 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002733 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002734 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002735
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002736 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2737 xprt_max_tcp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002738 if (IS_ERR(xprt))
2739 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002740 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002741
Chuck Leverec739ef2006-08-22 20:06:15 -04002742 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002743 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002744 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002745
Chuck Lever03bf4b72005-08-25 16:25:55 -07002746 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002747 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2748 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002749
Chuck Lever262965f2005-08-11 16:25:56 -04002750 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002751 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002752
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002753 switch (addr->sa_family) {
2754 case AF_INET:
2755 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2756 xprt_set_bound(xprt);
2757
Chuck Lever9dc3b092009-08-09 15:09:46 -04002758 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002759 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002760 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002761 break;
2762 case AF_INET6:
2763 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2764 xprt_set_bound(xprt);
2765
Chuck Lever9dc3b092009-08-09 15:09:46 -04002766 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002767 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002768 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002769 break;
2770 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002771 ret = ERR_PTR(-EAFNOSUPPORT);
2772 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002773 }
2774
Chuck Leverc740eff2009-08-09 15:09:46 -04002775 if (xprt_bound(xprt))
2776 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2777 xprt->address_strings[RPC_DISPLAY_ADDR],
2778 xprt->address_strings[RPC_DISPLAY_PORT],
2779 xprt->address_strings[RPC_DISPLAY_PROTO]);
2780 else
2781 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2782 xprt->address_strings[RPC_DISPLAY_ADDR],
2783 xprt->address_strings[RPC_DISPLAY_PROTO]);
2784
Chuck Leveredb267a2006-08-22 20:06:18 -04002785
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002786 if (try_module_get(THIS_MODULE))
2787 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002788 ret = ERR_PTR(-EINVAL);
2789out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002790 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002791 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002792}
Chuck Lever282b32e2006-12-05 16:35:51 -05002793
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002794/**
2795 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2796 * @args: rpc transport creation arguments
2797 *
2798 */
2799static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2800{
2801 struct sockaddr *addr = args->dstaddr;
2802 struct rpc_xprt *xprt;
2803 struct sock_xprt *transport;
2804 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002805 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002806
J. Bruce Fieldsf0418aa2010-12-08 13:48:19 -05002807 if (args->bc_xprt->xpt_bc_xprt) {
2808 /*
2809 * This server connection already has a backchannel
2810 * export; we can't create a new one, as we wouldn't be
2811 * able to match replies based on xid any more. So,
2812 * reuse the already-existing one:
2813 */
2814 return args->bc_xprt->xpt_bc_xprt;
2815 }
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002816 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2817 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002818 if (IS_ERR(xprt))
2819 return xprt;
2820 transport = container_of(xprt, struct sock_xprt, xprt);
2821
2822 xprt->prot = IPPROTO_TCP;
2823 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2824 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2825 xprt->timeout = &xs_tcp_default_timeout;
2826
2827 /* backchannel */
2828 xprt_set_bound(xprt);
2829 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002830 xprt->reestablish_timeout = 0;
2831 xprt->idle_timeout = 0;
2832
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002833 xprt->ops = &bc_tcp_ops;
2834
2835 switch (addr->sa_family) {
2836 case AF_INET:
2837 xs_format_peer_addresses(xprt, "tcp",
2838 RPCBIND_NETID_TCP);
2839 break;
2840 case AF_INET6:
2841 xs_format_peer_addresses(xprt, "tcp",
2842 RPCBIND_NETID_TCP6);
2843 break;
2844 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002845 ret = ERR_PTR(-EAFNOSUPPORT);
2846 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002847 }
2848
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04002849 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2850 xprt->address_strings[RPC_DISPLAY_ADDR],
2851 xprt->address_strings[RPC_DISPLAY_PORT],
2852 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002853
2854 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002855 * Once we've associated a backchannel xprt with a connection,
2856 * we want to keep it around as long as long as the connection
2857 * lasts, in case we need to start using it for a backchannel
2858 * again; this reference won't be dropped until bc_xprt is
2859 * destroyed.
2860 */
2861 xprt_get(xprt);
2862 args->bc_xprt->xpt_bc_xprt = xprt;
2863 xprt->bc_xprt = args->bc_xprt;
2864 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2865 transport->sock = bc_sock->sk_sock;
2866 transport->inet = bc_sock->sk_sk;
2867
2868 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002869 * Since we don't want connections for the backchannel, we set
2870 * the xprt status to connected
2871 */
2872 xprt_set_connected(xprt);
2873
2874
2875 if (try_module_get(THIS_MODULE))
2876 return xprt;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002877 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002878 ret = ERR_PTR(-EINVAL);
2879out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002880 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002881 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002882}
2883
Chuck Lever176e21e2011-05-09 15:22:44 -04002884static struct xprt_class xs_local_transport = {
2885 .list = LIST_HEAD_INIT(xs_local_transport.list),
2886 .name = "named UNIX socket",
2887 .owner = THIS_MODULE,
2888 .ident = XPRT_TRANSPORT_LOCAL,
2889 .setup = xs_setup_local,
2890};
2891
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002892static struct xprt_class xs_udp_transport = {
2893 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2894 .name = "udp",
2895 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002896 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002897 .setup = xs_setup_udp,
2898};
2899
2900static struct xprt_class xs_tcp_transport = {
2901 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2902 .name = "tcp",
2903 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002904 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002905 .setup = xs_setup_tcp,
2906};
2907
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002908static struct xprt_class xs_bc_tcp_transport = {
2909 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2910 .name = "tcp NFSv4.1 backchannel",
2911 .owner = THIS_MODULE,
2912 .ident = XPRT_TRANSPORT_BC_TCP,
2913 .setup = xs_setup_bc_tcp,
2914};
2915
Chuck Lever282b32e2006-12-05 16:35:51 -05002916/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002917 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002918 *
2919 */
2920int init_socket_xprt(void)
2921{
Chuck Leverfbf76682006-12-05 16:35:54 -05002922#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002923 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002924 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002925#endif
2926
Chuck Lever176e21e2011-05-09 15:22:44 -04002927 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002928 xprt_register_transport(&xs_udp_transport);
2929 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002930 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002931
Chuck Lever282b32e2006-12-05 16:35:51 -05002932 return 0;
2933}
2934
2935/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002936 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05002937 *
2938 */
2939void cleanup_socket_xprt(void)
2940{
Chuck Leverfbf76682006-12-05 16:35:54 -05002941#ifdef RPC_DEBUG
2942 if (sunrpc_table_header) {
2943 unregister_sysctl_table(sunrpc_table_header);
2944 sunrpc_table_header = NULL;
2945 }
2946#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002947
Chuck Lever176e21e2011-05-09 15:22:44 -04002948 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002949 xprt_unregister_transport(&xs_udp_transport);
2950 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002951 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05002952}
Trond Myklebustcbf11072009-08-09 15:06:19 -04002953
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002954static int param_set_uint_minmax(const char *val,
2955 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04002956 unsigned int min, unsigned int max)
2957{
2958 unsigned long num;
2959 int ret;
2960
2961 if (!val)
2962 return -EINVAL;
2963 ret = strict_strtoul(val, 0, &num);
2964 if (ret == -EINVAL || num < min || num > max)
2965 return -EINVAL;
2966 *((unsigned int *)kp->arg) = num;
2967 return 0;
2968}
2969
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002970static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04002971{
2972 return param_set_uint_minmax(val, kp,
2973 RPC_MIN_RESVPORT,
2974 RPC_MAX_RESVPORT);
2975}
2976
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002977static struct kernel_param_ops param_ops_portnr = {
2978 .set = param_set_portnr,
2979 .get = param_get_uint,
2980};
2981
Trond Myklebustcbf11072009-08-09 15:06:19 -04002982#define param_check_portnr(name, p) \
2983 __param_check(name, p, unsigned int);
2984
2985module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
2986module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
2987
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002988static int param_set_slot_table_size(const char *val,
2989 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04002990{
2991 return param_set_uint_minmax(val, kp,
2992 RPC_MIN_SLOT_TABLE,
2993 RPC_MAX_SLOT_TABLE);
2994}
2995
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002996static struct kernel_param_ops param_ops_slot_table_size = {
2997 .set = param_set_slot_table_size,
2998 .get = param_get_uint,
2999};
3000
Trond Myklebustcbf11072009-08-09 15:06:19 -04003001#define param_check_slot_table_size(name, p) \
3002 __param_check(name, p, unsigned int);
3003
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003004static int param_set_max_slot_table_size(const char *val,
3005 const struct kernel_param *kp)
3006{
3007 return param_set_uint_minmax(val, kp,
3008 RPC_MIN_SLOT_TABLE,
3009 RPC_MAX_SLOT_TABLE_LIMIT);
3010}
3011
3012static struct kernel_param_ops param_ops_max_slot_table_size = {
3013 .set = param_set_max_slot_table_size,
3014 .get = param_get_uint,
3015};
3016
3017#define param_check_max_slot_table_size(name, p) \
3018 __param_check(name, p, unsigned int);
3019
Trond Myklebustcbf11072009-08-09 15:06:19 -04003020module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3021 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003022module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3023 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003024module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3025 slot_table_size, 0644);
3026