blob: 31f981d700a38e86a3d00d866c6cf76f0327cc65 [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 Myklebust53c620e2013-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 Myklebust53c620e2013-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 Myklebust53c620e2013-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 Myklebust53c620e2013-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 Myklebust53c620e2013-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 Myklebust53c620e2013-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 Myklebust53c620e2013-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 Myklebust24ca9a82011-11-22 14:44:28 +0200504 int ret = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400505
Chuck Lever46121cf2007-01-31 12:14:08 -0500506 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400507 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
508 req->rq_slen);
509
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400510 /* Protect against races with write_space */
511 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400512
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400513 /* Don't race with disconnect */
514 if (xprt_connected(xprt)) {
515 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
516 /*
517 * Notify TCP that we're limited by the application
518 * window size
519 */
520 set_bit(SOCK_NOSPACE, &transport->sock->flags);
521 transport->inet->sk_write_pending++;
522 /* ...and wait for more buffer space */
523 xprt_wait_for_buffer_space(task, xs_nospace_callback);
524 }
525 } else {
526 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400527 ret = -ENOTCONN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400528 }
Chuck Lever262965f2005-08-11 16:25:56 -0400529
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400530 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400531 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400532}
533
Chuck Lever61677ee2011-05-09 15:22:34 -0400534/*
535 * Construct a stream transport record marker in @buf.
536 */
537static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
538{
539 u32 reclen = buf->len - sizeof(rpc_fraghdr);
540 rpc_fraghdr *base = buf->head[0].iov_base;
541 *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
542}
543
Chuck Lever262965f2005-08-11 16:25:56 -0400544/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400545 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
546 * @task: RPC task that manages the state of an RPC request
547 *
548 * Return values:
549 * 0: The request has been sent
550 * EAGAIN: The socket was blocked, please call again later to
551 * complete the request
552 * ENOTCONN: Caller needs to invoke connect logic then call again
553 * other: Some other error occured, the request was not sent
554 */
555static int xs_local_send_request(struct rpc_task *task)
556{
557 struct rpc_rqst *req = task->tk_rqstp;
558 struct rpc_xprt *xprt = req->rq_xprt;
559 struct sock_xprt *transport =
560 container_of(xprt, struct sock_xprt, xprt);
561 struct xdr_buf *xdr = &req->rq_snd_buf;
562 int status;
563
564 xs_encode_stream_record_marker(&req->rq_snd_buf);
565
566 xs_pktdump("packet data:",
567 req->rq_svec->iov_base, req->rq_svec->iov_len);
568
569 status = xs_sendpages(transport->sock, NULL, 0,
Trond Myklebust53c620e2013-11-08 16:03:50 -0500570 xdr, req->rq_bytes_sent, true);
Chuck Lever176e21e2011-05-09 15:22:44 -0400571 dprintk("RPC: %s(%u) = %d\n",
572 __func__, xdr->len - req->rq_bytes_sent, status);
573 if (likely(status >= 0)) {
574 req->rq_bytes_sent += status;
575 req->rq_xmit_bytes_sent += status;
576 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
577 req->rq_bytes_sent = 0;
578 return 0;
579 }
580 status = -EAGAIN;
581 }
582
583 switch (status) {
584 case -EAGAIN:
585 status = xs_nospace(task);
586 break;
587 default:
588 dprintk("RPC: sendmsg returned unrecognized error %d\n",
589 -status);
590 case -EPIPE:
591 xs_close(xprt);
592 status = -ENOTCONN;
593 }
594
595 return status;
596}
597
598/**
Chuck Lever262965f2005-08-11 16:25:56 -0400599 * xs_udp_send_request - write an RPC request to a UDP socket
600 * @task: address of RPC task that manages the state of an RPC request
601 *
602 * Return values:
603 * 0: The request has been sent
604 * EAGAIN: The socket was blocked, please call again later to
605 * complete the request
606 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300607 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -0400608 */
609static int xs_udp_send_request(struct rpc_task *task)
610{
611 struct rpc_rqst *req = task->tk_rqstp;
612 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500613 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400614 struct xdr_buf *xdr = &req->rq_snd_buf;
615 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400616
Chuck Lever9903cd12005-08-11 16:25:26 -0400617 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400618 req->rq_svec->iov_base,
619 req->rq_svec->iov_len);
620
Trond Myklebust01d37c42009-03-11 14:09:39 -0400621 if (!xprt_bound(xprt))
622 return -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500623 status = xs_sendpages(transport->sock,
Chuck Lever95392c52007-08-06 11:57:58 -0400624 xs_addr(xprt),
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500625 xprt->addrlen, xdr,
Trond Myklebust53c620e2013-11-08 16:03:50 -0500626 req->rq_bytes_sent, true);
Chuck Levera246b012005-08-11 16:25:23 -0400627
Chuck Lever46121cf2007-01-31 12:14:08 -0500628 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400629 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400630
Trond Myklebust21997002007-10-01 11:43:37 -0400631 if (status >= 0) {
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400632 req->rq_xmit_bytes_sent += status;
Trond Myklebust21997002007-10-01 11:43:37 -0400633 if (status >= req->rq_slen)
634 return 0;
635 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400636 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400637 }
Chuck Levera246b012005-08-11 16:25:23 -0400638
Chuck Lever262965f2005-08-11 16:25:56 -0400639 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400640 case -ENOTSOCK:
641 status = -ENOTCONN;
642 /* Should we call xs_close() here? */
643 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400644 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400645 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400646 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400647 default:
648 dprintk("RPC: sendmsg returned unrecognized error %d\n",
649 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400650 case -ENETUNREACH:
651 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400652 case -ECONNREFUSED:
653 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400654 * prompts ECONNREFUSED. */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400655 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400656 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800657
Chuck Lever262965f2005-08-11 16:25:56 -0400658 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400659}
660
Trond Myklebuste06799f2007-11-05 15:44:12 -0500661/**
662 * xs_tcp_shutdown - gracefully shut down a TCP socket
663 * @xprt: transport
664 *
665 * Initiates a graceful shutdown of the TCP socket by calling the
666 * equivalent of shutdown(SHUT_WR);
667 */
668static void xs_tcp_shutdown(struct rpc_xprt *xprt)
669{
670 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
671 struct socket *sock = transport->sock;
672
673 if (sock != NULL)
674 kernel_sock_shutdown(sock, SHUT_WR);
675}
676
Chuck Lever9903cd12005-08-11 16:25:26 -0400677/**
Chuck Lever262965f2005-08-11 16:25:56 -0400678 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400679 * @task: address of RPC task that manages the state of an RPC request
680 *
681 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400682 * 0: The request has been sent
683 * EAGAIN: The socket was blocked, please call again later to
684 * complete the request
685 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300686 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400687 *
688 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400689 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400690 */
Chuck Lever262965f2005-08-11 16:25:56 -0400691static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400692{
693 struct rpc_rqst *req = task->tk_rqstp;
694 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500695 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400696 struct xdr_buf *xdr = &req->rq_snd_buf;
Trond Myklebust53c620e2013-11-08 16:03:50 -0500697 bool zerocopy = true;
Chuck Leverb595bb12007-08-06 11:56:42 -0400698 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400699
Chuck Lever61677ee2011-05-09 15:22:34 -0400700 xs_encode_stream_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400701
Chuck Lever262965f2005-08-11 16:25:56 -0400702 xs_pktdump("packet data:",
703 req->rq_svec->iov_base,
704 req->rq_svec->iov_len);
Trond Myklebust53c620e2013-11-08 16:03:50 -0500705 /* Don't use zero copy if this is a resend. If the RPC call
706 * completes while the socket holds a reference to the pages,
707 * then we may end up resending corrupted data.
708 */
709 if (task->tk_flags & RPC_TASK_SENT)
710 zerocopy = false;
Chuck Levera246b012005-08-11 16:25:23 -0400711
712 /* Continue transmitting the packet/record. We must be careful
713 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400714 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400715 while (1) {
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500716 status = xs_sendpages(transport->sock,
Trond Myklebust53c620e2013-11-08 16:03:50 -0500717 NULL, 0, xdr, req->rq_bytes_sent,
718 zerocopy);
Chuck Levera246b012005-08-11 16:25:23 -0400719
Chuck Lever46121cf2007-01-31 12:14:08 -0500720 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400721 xdr->len - req->rq_bytes_sent, status);
722
723 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400724 break;
725
Chuck Lever262965f2005-08-11 16:25:56 -0400726 /* If we've sent the entire packet, immediately
727 * reset the count of bytes sent. */
728 req->rq_bytes_sent += status;
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400729 req->rq_xmit_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400730 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
731 req->rq_bytes_sent = 0;
732 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400733 }
734
Trond Myklebust06b4b682008-04-16 16:51:38 -0400735 if (status != 0)
736 continue;
Chuck Levera246b012005-08-11 16:25:23 -0400737 status = -EAGAIN;
Trond Myklebust06b4b682008-04-16 16:51:38 -0400738 break;
Chuck Levera246b012005-08-11 16:25:23 -0400739 }
740
Chuck Lever262965f2005-08-11 16:25:56 -0400741 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400742 case -ENOTSOCK:
743 status = -ENOTCONN;
744 /* Should we call xs_close() here? */
745 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400746 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400747 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400748 break;
749 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500750 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400751 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400752 case -ECONNRESET:
Trond Myklebuste06799f2007-11-05 15:44:12 -0500753 xs_tcp_shutdown(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400754 case -ECONNREFUSED:
755 case -ENOTCONN:
Trond Myklebustdedf1c22012-10-23 11:40:02 -0400756 case -EPIPE:
Chuck Lever262965f2005-08-11 16:25:56 -0400757 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400758 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800759
Chuck Levera246b012005-08-11 16:25:23 -0400760 return status;
761}
762
Chuck Lever9903cd12005-08-11 16:25:26 -0400763/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400764 * xs_tcp_release_xprt - clean up after a tcp transmission
765 * @xprt: transport
766 * @task: rpc task
767 *
768 * This cleans up if an error causes us to abort the transmission of a request.
769 * In this case, the socket may need to be reset in order to avoid confusing
770 * the server.
771 */
772static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
773{
774 struct rpc_rqst *req;
775
776 if (task != xprt->snd_task)
777 return;
778 if (task == NULL)
779 goto out_release;
780 req = task->tk_rqstp;
Trond Myklebust43cedbf2011-07-17 16:01:03 -0400781 if (req == NULL)
782 goto out_release;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400783 if (req->rq_bytes_sent == 0)
784 goto out_release;
785 if (req->rq_bytes_sent == req->rq_snd_buf.len)
786 goto out_release;
787 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
788out_release:
789 xprt_release_xprt(xprt, task);
790}
791
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400792static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
793{
794 transport->old_data_ready = sk->sk_data_ready;
795 transport->old_state_change = sk->sk_state_change;
796 transport->old_write_space = sk->sk_write_space;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400797}
798
799static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
800{
801 sk->sk_data_ready = transport->old_data_ready;
802 sk->sk_state_change = transport->old_state_change;
803 sk->sk_write_space = transport->old_write_space;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400804}
805
Chuck Leverfe315e72009-03-11 14:10:21 -0400806static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400807{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500808 struct socket *sock = transport->sock;
809 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400810
Chuck Leverfe315e72009-03-11 14:10:21 -0400811 if (sk == NULL)
812 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400813
Trond Myklebust246408d2011-03-22 18:40:10 -0400814 transport->srcport = 0;
815
Chuck Levera246b012005-08-11 16:25:23 -0400816 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500817 transport->inet = NULL;
818 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400819
Chuck Lever9903cd12005-08-11 16:25:26 -0400820 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400821
822 xs_restore_old_callbacks(transport, sk);
Chuck Levera246b012005-08-11 16:25:23 -0400823 write_unlock_bh(&sk->sk_callback_lock);
824
Chuck Lever9903cd12005-08-11 16:25:26 -0400825 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400826
827 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -0400828}
829
830/**
831 * xs_close - close a socket
832 * @xprt: transport
833 *
834 * This is used when all requests are complete; ie, no DRC state remains
835 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400836 *
837 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
838 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e72009-03-11 14:10:21 -0400839 */
840static void xs_close(struct rpc_xprt *xprt)
841{
842 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
843
844 dprintk("RPC: xs_close xprt %p\n", xprt);
845
846 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -0400847 xprt->reestablish_timeout = 0;
Chuck Leverfe315e72009-03-11 14:10:21 -0400848
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100849 smp_mb__before_clear_bit();
Trond Myklebust7d1e8252009-03-11 14:38:03 -0400850 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100851 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -0500852 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100853 smp_mb__after_clear_bit();
Trond Myklebust62da3b22007-11-06 18:44:20 -0500854 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400855}
856
Trond Myklebustf75e6742009-04-21 17:18:20 -0400857static void xs_tcp_close(struct rpc_xprt *xprt)
858{
859 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
860 xs_close(xprt);
861 else
862 xs_tcp_shutdown(xprt);
863}
864
Chuck Lever9903cd12005-08-11 16:25:26 -0400865/**
866 * xs_destroy - prepare to shutdown a transport
867 * @xprt: doomed transport
868 *
869 */
870static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400871{
Chuck Leverc8475462006-12-05 16:35:26 -0500872 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
873
Chuck Lever46121cf2007-01-31 12:14:08 -0500874 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400875
Tejun Heoafe2c512010-12-14 16:21:17 +0100876 cancel_delayed_work_sync(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400877
Chuck Lever9903cd12005-08-11 16:25:26 -0400878 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400879 xs_free_peer_addresses(xprt);
Pavel Emelyanove204e622010-09-29 16:03:13 +0400880 xprt_free(xprt);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -0400881 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400882}
883
Chuck Lever9903cd12005-08-11 16:25:26 -0400884static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400885{
Chuck Lever9903cd12005-08-11 16:25:26 -0400886 return (struct rpc_xprt *) sk->sk_user_data;
887}
888
Chuck Lever176e21e2011-05-09 15:22:44 -0400889static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
890{
891 struct xdr_skb_reader desc = {
892 .skb = skb,
893 .offset = sizeof(rpc_fraghdr),
894 .count = skb->len - sizeof(rpc_fraghdr),
895 };
896
897 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
898 return -1;
899 if (desc.count)
900 return -1;
901 return 0;
902}
903
904/**
905 * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets
906 * @sk: socket with data to read
907 * @len: how much data to read
908 *
909 * Currently this assumes we can read the whole reply in a single gulp.
910 */
911static void xs_local_data_ready(struct sock *sk, int len)
912{
913 struct rpc_task *task;
914 struct rpc_xprt *xprt;
915 struct rpc_rqst *rovr;
916 struct sk_buff *skb;
917 int err, repsize, copied;
918 u32 _xid;
919 __be32 *xp;
920
921 read_lock_bh(&sk->sk_callback_lock);
922 dprintk("RPC: %s...\n", __func__);
923 xprt = xprt_from_sock(sk);
924 if (xprt == NULL)
925 goto out;
926
927 skb = skb_recv_datagram(sk, 0, 1, &err);
928 if (skb == NULL)
929 goto out;
930
931 if (xprt->shutdown)
932 goto dropit;
933
934 repsize = skb->len - sizeof(rpc_fraghdr);
935 if (repsize < 4) {
936 dprintk("RPC: impossible RPC reply size %d\n", repsize);
937 goto dropit;
938 }
939
940 /* Copy the XID from the skb... */
941 xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
942 if (xp == NULL)
943 goto dropit;
944
945 /* Look up and lock the request corresponding to the given XID */
946 spin_lock(&xprt->transport_lock);
947 rovr = xprt_lookup_rqst(xprt, *xp);
948 if (!rovr)
949 goto out_unlock;
950 task = rovr->rq_task;
951
952 copied = rovr->rq_private_buf.buflen;
953 if (copied > repsize)
954 copied = repsize;
955
956 if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
957 dprintk("RPC: sk_buff copy failed\n");
958 goto out_unlock;
959 }
960
961 xprt_complete_rqst(task, copied);
962
963 out_unlock:
964 spin_unlock(&xprt->transport_lock);
965 dropit:
966 skb_free_datagram(sk, skb);
967 out:
968 read_unlock_bh(&sk->sk_callback_lock);
969}
970
Chuck Lever9903cd12005-08-11 16:25:26 -0400971/**
972 * xs_udp_data_ready - "data ready" callback for UDP sockets
973 * @sk: socket with data to read
974 * @len: how much data to read
975 *
976 */
977static void xs_udp_data_ready(struct sock *sk, int len)
978{
979 struct rpc_task *task;
980 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400981 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400982 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400983 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700984 u32 _xid;
985 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400986
Eric Dumazetf064af12010-09-22 12:43:39 +0000987 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500988 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400989 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400990 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400991
992 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
993 goto out;
994
995 if (xprt->shutdown)
996 goto dropit;
997
998 repsize = skb->len - sizeof(struct udphdr);
999 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001000 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -04001001 goto dropit;
1002 }
1003
1004 /* Copy the XID from the skb... */
1005 xp = skb_header_pointer(skb, sizeof(struct udphdr),
1006 sizeof(_xid), &_xid);
1007 if (xp == NULL)
1008 goto dropit;
1009
1010 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001011 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001012 rovr = xprt_lookup_rqst(xprt, *xp);
1013 if (!rovr)
1014 goto out_unlock;
1015 task = rovr->rq_task;
1016
Chuck Levera246b012005-08-11 16:25:23 -04001017 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1018 copied = repsize;
1019
1020 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001021 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
1022 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -04001023 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001024 }
1025
1026 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -04001027
1028 /* Something worked... */
Eric Dumazetadf30902009-06-02 05:19:30 +00001029 dst_confirm(skb_dst(skb));
Chuck Levera246b012005-08-11 16:25:23 -04001030
Chuck Lever1570c1e2005-08-25 16:25:52 -07001031 xprt_adjust_cwnd(task, copied);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001032 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -04001033
1034 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001035 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001036 dropit:
1037 skb_free_datagram(sk, skb);
1038 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001039 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001040}
1041
Trond Myklebustb77a7a02012-09-12 16:49:15 -04001042/*
1043 * Helper function to force a TCP close if the server is sending
1044 * junk and/or it has put us in CLOSE_WAIT
1045 */
1046static void xs_tcp_force_close(struct rpc_xprt *xprt)
1047{
1048 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1049 xprt_force_disconnect(xprt);
1050}
1051
Chuck Leverdd456472006-12-05 16:35:44 -05001052static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001053{
Chuck Lever51971132006-12-05 16:35:19 -05001054 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001055 size_t len, used;
1056 char *p;
1057
Chuck Lever51971132006-12-05 16:35:19 -05001058 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
1059 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001060 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001061 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001062 if (used != len)
1063 return;
Chuck Lever808012f2005-08-25 16:25:49 -07001064
Chuck Lever51971132006-12-05 16:35:19 -05001065 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
1066 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -05001067 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -04001068 else
Chuck Levere136d092006-12-05 16:35:23 -05001069 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -05001070 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -07001071
Chuck Levere136d092006-12-05 16:35:23 -05001072 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001073 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -07001074
Chuck Levera246b012005-08-11 16:25:23 -04001075 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001076 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001077 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebustb77a7a02012-09-12 16:49:15 -04001078 xs_tcp_force_close(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001079 return;
Chuck Levera246b012005-08-11 16:25:23 -04001080 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001081 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -05001082 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001083}
1084
Chuck Lever51971132006-12-05 16:35:19 -05001085static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001086{
Chuck Lever51971132006-12-05 16:35:19 -05001087 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001088 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001089 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001090 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
1091 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1092 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001093 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001094 }
1095 }
1096}
1097
Chuck Leverdd456472006-12-05 16:35:44 -05001098static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001099{
1100 size_t len, used;
1101 char *p;
1102
Chuck Lever51971132006-12-05 16:35:19 -05001103 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -05001104 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001105 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001106 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001107 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001108 if (used != len)
1109 return;
Chuck Levere136d092006-12-05 16:35:23 -05001110 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001111 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -05001112 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001113 dprintk("RPC: reading %s XID %08x\n",
1114 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1115 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -05001116 ntohl(transport->tcp_xid));
1117 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001118}
1119
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001120static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1121 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001122{
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001123 size_t len, used;
1124 u32 offset;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001125 char *p;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001126
1127 /*
1128 * We want transport->tcp_offset to be 8 at the end of this routine
1129 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1130 * When this function is called for the first time,
1131 * transport->tcp_offset is 4 (after having already read the xid).
1132 */
1133 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001134 len = sizeof(transport->tcp_calldir) - offset;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001135 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001136 p = ((char *) &transport->tcp_calldir) + offset;
1137 used = xdr_skb_read_bits(desc, p, len);
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001138 transport->tcp_offset += used;
1139 if (used != len)
1140 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001141 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001142 /*
1143 * We don't yet have the XDR buffer, so we will write the calldir
1144 * out after we get the buffer from the 'struct rpc_rqst'
1145 */
Trond Myklebustb76ce562010-06-16 13:57:32 -04001146 switch (ntohl(transport->tcp_calldir)) {
1147 case RPC_REPLY:
1148 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1149 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001150 transport->tcp_flags |= TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001151 break;
1152 case RPC_CALL:
1153 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1154 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001155 transport->tcp_flags &= ~TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001156 break;
1157 default:
1158 dprintk("RPC: invalid request message type\n");
Trond Myklebustb77a7a02012-09-12 16:49:15 -04001159 xs_tcp_force_close(&transport->xprt);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001160 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001161 xs_tcp_check_fraghdr(transport);
1162}
1163
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001164static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
1165 struct xdr_skb_reader *desc,
1166 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001167{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001168 struct sock_xprt *transport =
1169 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001170 struct xdr_buf *rcvbuf;
1171 size_t len;
1172 ssize_t r;
1173
Chuck Levera246b012005-08-11 16:25:23 -04001174 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001175
1176 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1177 /*
1178 * Save the RPC direction in the XDR buffer
1179 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001180 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
Trond Myklebustb76ce562010-06-16 13:57:32 -04001181 &transport->tcp_calldir,
1182 sizeof(transport->tcp_calldir));
1183 transport->tcp_copied += sizeof(transport->tcp_calldir);
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001184 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -04001185 }
1186
Chuck Levera246b012005-08-11 16:25:23 -04001187 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -05001188 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -05001189 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -04001190
Chuck Lever51971132006-12-05 16:35:19 -05001191 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001192 memcpy(&my_desc, desc, sizeof(my_desc));
1193 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -05001194 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001195 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001196 desc->count -= r;
1197 desc->offset += r;
1198 } else
Chuck Lever51971132006-12-05 16:35:19 -05001199 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001200 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001201
1202 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001203 transport->tcp_copied += r;
1204 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001205 }
1206 if (r != len) {
1207 /* Error when copying to the receive buffer,
1208 * usually because we weren't able to allocate
1209 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001210 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001211 * will not receive any additional updates,
1212 * and time out.
1213 * Any remaining data from this record will
1214 * be discarded.
1215 */
Chuck Levere136d092006-12-05 16:35:23 -05001216 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001217 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001218 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001219 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1220 "tcp_offset = %u, tcp_reclen = %u\n",
1221 xprt, transport->tcp_copied,
1222 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001223 return;
Chuck Levera246b012005-08-11 16:25:23 -04001224 }
1225
Chuck Lever46121cf2007-01-31 12:14:08 -05001226 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001227 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001228 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1229 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1230 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001231
Chuck Lever51971132006-12-05 16:35:19 -05001232 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001233 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001234 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001235 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1236 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001237 }
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001238}
1239
1240/*
1241 * Finds the request corresponding to the RPC xid and invokes the common
1242 * tcp read code to read the data.
1243 */
1244static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1245 struct xdr_skb_reader *desc)
1246{
1247 struct sock_xprt *transport =
1248 container_of(xprt, struct sock_xprt, xprt);
1249 struct rpc_rqst *req;
1250
1251 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1252
1253 /* Find and lock the request corresponding to this xid */
1254 spin_lock(&xprt->transport_lock);
1255 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1256 if (!req) {
1257 dprintk("RPC: XID %08x request not found!\n",
1258 ntohl(transport->tcp_xid));
1259 spin_unlock(&xprt->transport_lock);
1260 return -1;
1261 }
1262
1263 xs_tcp_read_common(xprt, desc, req);
1264
Chuck Levere136d092006-12-05 16:35:23 -05001265 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001266 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001267
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001268 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001269 return 0;
1270}
1271
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001272#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001273/*
1274 * Obtains an rpc_rqst previously allocated and invokes the common
1275 * tcp read code to read the data. The result is placed in the callback
1276 * queue.
1277 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1278 * connection and return -1.
1279 */
1280static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1281 struct xdr_skb_reader *desc)
1282{
1283 struct sock_xprt *transport =
1284 container_of(xprt, struct sock_xprt, xprt);
1285 struct rpc_rqst *req;
1286
1287 req = xprt_alloc_bc_request(xprt);
1288 if (req == NULL) {
1289 printk(KERN_WARNING "Callback slot table overflowed\n");
1290 xprt_force_disconnect(xprt);
1291 return -1;
1292 }
1293
1294 req->rq_xid = transport->tcp_xid;
1295 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1296 xs_tcp_read_common(xprt, desc, req);
1297
1298 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1299 struct svc_serv *bc_serv = xprt->bc_serv;
1300
1301 /*
1302 * Add callback request to callback list. The callback
1303 * service sleeps on the sv_cb_waitq waiting for new
1304 * requests. Wake it up after adding enqueing the
1305 * request.
1306 */
1307 dprintk("RPC: add callback request to list\n");
1308 spin_lock(&bc_serv->sv_cb_lock);
1309 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1310 spin_unlock(&bc_serv->sv_cb_lock);
1311 wake_up(&bc_serv->sv_cb_waitq);
1312 }
1313
1314 req->rq_private_buf.len = transport->tcp_copied;
1315
1316 return 0;
1317}
1318
1319static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1320 struct xdr_skb_reader *desc)
1321{
1322 struct sock_xprt *transport =
1323 container_of(xprt, struct sock_xprt, xprt);
1324
1325 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1326 xs_tcp_read_reply(xprt, desc) :
1327 xs_tcp_read_callback(xprt, desc);
1328}
1329#else
1330static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1331 struct xdr_skb_reader *desc)
1332{
1333 return xs_tcp_read_reply(xprt, desc);
1334}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001335#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001336
1337/*
1338 * Read data off the transport. This can be either an RPC_CALL or an
1339 * RPC_REPLY. Relay the processing to helper functions.
1340 */
1341static void xs_tcp_read_data(struct rpc_xprt *xprt,
1342 struct xdr_skb_reader *desc)
1343{
1344 struct sock_xprt *transport =
1345 container_of(xprt, struct sock_xprt, xprt);
1346
1347 if (_xs_tcp_read_data(xprt, desc) == 0)
1348 xs_tcp_check_fraghdr(transport);
1349 else {
1350 /*
1351 * The transport_lock protects the request handling.
1352 * There's no need to hold it to update the tcp_flags.
1353 */
1354 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1355 }
Chuck Levera246b012005-08-11 16:25:23 -04001356}
1357
Chuck Leverdd456472006-12-05 16:35:44 -05001358static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001359{
1360 size_t len;
1361
Chuck Lever51971132006-12-05 16:35:19 -05001362 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001363 if (len > desc->count)
1364 len = desc->count;
1365 desc->count -= len;
1366 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001367 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001368 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001369 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001370}
1371
Chuck Lever9903cd12005-08-11 16:25:26 -04001372static 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 -04001373{
1374 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001375 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001376 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001377 .skb = skb,
1378 .offset = offset,
1379 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001380 };
Chuck Levera246b012005-08-11 16:25:23 -04001381
Chuck Lever46121cf2007-01-31 12:14:08 -05001382 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001383 do {
1384 /* Read in a new fragment marker if necessary */
1385 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001386 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001387 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001388 continue;
1389 }
1390 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001391 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001392 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001393 continue;
1394 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001395 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001396 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001397 xs_tcp_read_calldir(transport, &desc);
1398 continue;
1399 }
Chuck Levera246b012005-08-11 16:25:23 -04001400 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001401 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001402 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001403 continue;
1404 }
1405 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001406 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001407 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001408 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001409 return len - desc.count;
1410}
1411
Chuck Lever9903cd12005-08-11 16:25:26 -04001412/**
1413 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1414 * @sk: socket with data to read
1415 * @bytes: how much data to read
1416 *
1417 */
1418static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001419{
1420 struct rpc_xprt *xprt;
1421 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001422 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001423
Chuck Lever46121cf2007-01-31 12:14:08 -05001424 dprintk("RPC: xs_tcp_data_ready...\n");
1425
Eric Dumazetf064af12010-09-22 12:43:39 +00001426 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001427 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001428 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001429 if (xprt->shutdown)
1430 goto out;
1431
Neil Brown61d0a8e2009-09-23 14:36:37 -04001432 /* Any data means we had a useful conversation, so
1433 * the we don't need to delay the next reconnect
1434 */
1435 if (xprt->reestablish_timeout)
1436 xprt->reestablish_timeout = 0;
1437
Chuck Lever9903cd12005-08-11 16:25:26 -04001438 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001439 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001440 do {
1441 rd_desc.count = 65536;
1442 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1443 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001444out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001445 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001446}
1447
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001448/*
1449 * Do the equivalent of linger/linger2 handling for dealing with
1450 * broken servers that don't close the socket in a timely
1451 * fashion
1452 */
1453static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1454 unsigned long timeout)
1455{
1456 struct sock_xprt *transport;
1457
1458 if (xprt_test_and_set_connecting(xprt))
1459 return;
1460 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1461 transport = container_of(xprt, struct sock_xprt, xprt);
1462 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1463 timeout);
1464}
1465
1466static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1467{
1468 struct sock_xprt *transport;
1469
1470 transport = container_of(xprt, struct sock_xprt, xprt);
1471
1472 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1473 !cancel_delayed_work(&transport->connect_worker))
1474 return;
1475 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1476 xprt_clear_connecting(xprt);
1477}
1478
Trond Myklebust6c6dff62012-10-23 17:50:07 -04001479static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001480{
1481 smp_mb__before_clear_bit();
Trond Myklebustb77a7a02012-09-12 16:49:15 -04001482 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1483 clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001484 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1485 clear_bit(XPRT_CLOSING, &xprt->state);
1486 smp_mb__after_clear_bit();
Trond Myklebust6c6dff62012-10-23 17:50:07 -04001487}
1488
1489static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1490{
1491 xs_sock_reset_connection_flags(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001492 /* Mark transport as closed and wake up all pending tasks */
1493 xprt_disconnect_done(xprt);
1494}
1495
Chuck Lever9903cd12005-08-11 16:25:26 -04001496/**
1497 * xs_tcp_state_change - callback to handle TCP socket state changes
1498 * @sk: socket whose state has changed
1499 *
1500 */
1501static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001502{
Chuck Lever9903cd12005-08-11 16:25:26 -04001503 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001504
Eric Dumazetf064af12010-09-22 12:43:39 +00001505 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001506 if (!(xprt = xprt_from_sock(sk)))
1507 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001508 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001509 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001510 sk->sk_state, xprt_connected(xprt),
1511 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001512 sock_flag(sk, SOCK_ZAPPED),
1513 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001514
1515 switch (sk->sk_state) {
1516 case TCP_ESTABLISHED:
Eric Dumazetf064af12010-09-22 12:43:39 +00001517 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001518 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001519 struct sock_xprt *transport = container_of(xprt,
1520 struct sock_xprt, xprt);
1521
Chuck Levera246b012005-08-11 16:25:23 -04001522 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001523 transport->tcp_offset = 0;
1524 transport->tcp_reclen = 0;
1525 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001526 transport->tcp_flags =
1527 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001528
Trond Myklebust2a491992009-03-11 14:38:00 -04001529 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001530 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001531 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001532 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001533 case TCP_FIN_WAIT1:
1534 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001535 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001536 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001537 set_bit(XPRT_CLOSING, &xprt->state);
1538 smp_mb__before_clear_bit();
1539 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001540 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001541 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001542 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001543 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001544 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001545 /* The server initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001546 xprt->connect_cookie++;
Trond Myklebustea288722012-10-23 11:35:47 -04001547 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustb77a7a02012-09-12 16:49:15 -04001548 xs_tcp_force_close(xprt);
Trond Myklebust663b8852008-01-01 18:42:12 -05001549 case TCP_CLOSING:
1550 /*
1551 * If the server closed down the connection, make sure that
1552 * we back off before reconnecting
1553 */
1554 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1555 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001556 break;
1557 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001558 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001559 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001560 smp_mb__before_clear_bit();
1561 clear_bit(XPRT_CONNECTED, &xprt->state);
1562 smp_mb__after_clear_bit();
1563 break;
1564 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001565 xs_tcp_cancel_linger_timeout(xprt);
1566 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001567 }
1568 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001569 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001570}
1571
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001572static void xs_write_space(struct sock *sk)
1573{
1574 struct socket *sock;
1575 struct rpc_xprt *xprt;
1576
1577 if (unlikely(!(sock = sk->sk_socket)))
1578 return;
1579 clear_bit(SOCK_NOSPACE, &sock->flags);
1580
1581 if (unlikely(!(xprt = xprt_from_sock(sk))))
1582 return;
1583 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1584 return;
1585
1586 xprt_write_space(xprt);
1587}
1588
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001589/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001590 * xs_udp_write_space - callback invoked when socket buffer space
1591 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001592 * @sk: socket whose state has changed
1593 *
Chuck Levera246b012005-08-11 16:25:23 -04001594 * Called when more output buffer space is available for this socket.
1595 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001596 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001597 * with a bunch of small requests.
1598 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001599static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001600{
Eric Dumazetf064af12010-09-22 12:43:39 +00001601 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001602
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001603 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001604 if (sock_writeable(sk))
1605 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001606
Eric Dumazetf064af12010-09-22 12:43:39 +00001607 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001608}
Chuck Levera246b012005-08-11 16:25:23 -04001609
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001610/**
1611 * xs_tcp_write_space - callback invoked when socket buffer space
1612 * becomes available
1613 * @sk: socket whose state has changed
1614 *
1615 * Called when more output buffer space is available for this socket.
1616 * We try not to wake our writers until they can make "significant"
1617 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1618 * with a bunch of small requests.
1619 */
1620static void xs_tcp_write_space(struct sock *sk)
1621{
Eric Dumazetf064af12010-09-22 12:43:39 +00001622 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001623
1624 /* from net/core/stream.c:sk_stream_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001625 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1626 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001627
Eric Dumazetf064af12010-09-22 12:43:39 +00001628 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001629}
1630
Chuck Lever470056c2005-08-25 16:25:56 -07001631static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001632{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001633 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1634 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001635
Chuck Lever7c6e0662006-12-05 16:35:30 -05001636 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001637 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001638 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001639 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001640 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001641 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001642 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001643 sk->sk_write_space(sk);
1644 }
1645}
1646
Chuck Lever43118c22005-08-25 16:25:49 -07001647/**
Chuck Lever470056c2005-08-25 16:25:56 -07001648 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001649 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001650 * @sndsize: requested size of send buffer, in bytes
1651 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001652 *
Chuck Lever470056c2005-08-25 16:25:56 -07001653 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001654 */
Chuck Lever470056c2005-08-25 16:25:56 -07001655static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001656{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001657 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1658
1659 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001660 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001661 transport->sndsize = sndsize + 1024;
1662 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001663 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001664 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001665
1666 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001667}
1668
Chuck Lever46c0ee82005-08-25 16:25:52 -07001669/**
1670 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1671 * @task: task that timed out
1672 *
1673 * Adjust the congestion window after a retransmit timeout has occurred.
1674 */
1675static void xs_udp_timer(struct rpc_task *task)
1676{
1677 xprt_adjust_cwnd(task, -ETIMEDOUT);
1678}
1679
Chuck Leverb85d8802006-05-25 01:40:49 -04001680static unsigned short xs_get_random_port(void)
1681{
1682 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1683 unsigned short rand = (unsigned short) net_random() % range;
1684 return rand + xprt_min_resvport;
1685}
1686
Chuck Lever92200412006-01-03 09:55:51 +01001687/**
1688 * xs_set_port - reset the port number in the remote endpoint address
1689 * @xprt: generic transport
1690 * @port: new port number
1691 *
1692 */
1693static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1694{
Chuck Lever46121cf2007-01-31 12:14:08 -05001695 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001696
Chuck Lever9dc3b092009-08-09 15:09:46 -04001697 rpc_set_port(xs_addr(xprt), port);
1698 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001699}
1700
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001701static unsigned short xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001702{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001703 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001704
1705 if (port == 0 && transport->xprt.resvport)
1706 port = xs_get_random_port();
1707 return port;
1708}
1709
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001710static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001711{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001712 if (transport->srcport != 0)
1713 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001714 if (!transport->xprt.resvport)
1715 return 0;
1716 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1717 return xprt_max_resvport;
1718 return --port;
1719}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001720static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001721{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001722 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001723 int err, nloop = 0;
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001724 unsigned short port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001725 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001726
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001727 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001728 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001729 rpc_set_port((struct sockaddr *)&myaddr, port);
1730 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1731 transport->xprt.addrlen);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001732 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001733 break;
Chuck Levera246b012005-08-11 16:25:23 -04001734 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001735 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001736 break;
Chuck Levera246b012005-08-11 16:25:23 -04001737 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001738 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001739 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001740 if (port > last)
1741 nloop++;
1742 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001743
Chuck Lever4232e862010-10-20 11:52:51 -04001744 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001745 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1746 &((struct sockaddr_in *)&myaddr)->sin_addr,
1747 port, err ? "failed" : "ok", err);
1748 else
1749 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1750 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1751 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001752 return err;
1753}
1754
Chuck Lever176e21e2011-05-09 15:22:44 -04001755/*
1756 * We don't support autobind on AF_LOCAL sockets
1757 */
1758static void xs_local_rpcbind(struct rpc_task *task)
1759{
1760 xprt_set_bound(task->tk_xprt);
1761}
1762
1763static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1764{
1765}
Chuck Levera246b012005-08-11 16:25:23 -04001766
Peter Zijlstraed075362006-12-06 20:35:24 -08001767#ifdef CONFIG_DEBUG_LOCK_ALLOC
1768static struct lock_class_key xs_key[2];
1769static struct lock_class_key xs_slock_key[2];
1770
Chuck Lever176e21e2011-05-09 15:22:44 -04001771static inline void xs_reclassify_socketu(struct socket *sock)
1772{
1773 struct sock *sk = sock->sk;
1774
1775 BUG_ON(sock_owned_by_user(sk));
1776 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1777 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1778}
1779
Chuck Lever8945ee52007-08-06 11:58:04 -04001780static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001781{
1782 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001783
John Heffner02b3d342007-09-12 10:42:12 +02001784 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001785 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1786 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1787}
Peter Zijlstraed075362006-12-06 20:35:24 -08001788
Chuck Lever8945ee52007-08-06 11:58:04 -04001789static inline void xs_reclassify_socket6(struct socket *sock)
1790{
1791 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001792
Linus Torvaldsf4921af2007-10-15 10:46:05 -07001793 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001794 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1795 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001796}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001797
1798static inline void xs_reclassify_socket(int family, struct socket *sock)
1799{
Chuck Lever4232e862010-10-20 11:52:51 -04001800 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001801 case AF_LOCAL:
1802 xs_reclassify_socketu(sock);
1803 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001804 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001805 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001806 break;
1807 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001808 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001809 break;
1810 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001811}
Peter Zijlstraed075362006-12-06 20:35:24 -08001812#else
Chuck Lever176e21e2011-05-09 15:22:44 -04001813static inline void xs_reclassify_socketu(struct socket *sock)
1814{
1815}
1816
Chuck Lever8945ee52007-08-06 11:58:04 -04001817static inline void xs_reclassify_socket4(struct socket *sock)
1818{
1819}
1820
1821static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001822{
1823}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001824
1825static inline void xs_reclassify_socket(int family, struct socket *sock)
1826{
1827}
Peter Zijlstraed075362006-12-06 20:35:24 -08001828#endif
1829
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001830static struct socket *xs_create_sock(struct rpc_xprt *xprt,
1831 struct sock_xprt *transport, int family, int type, int protocol)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001832{
1833 struct socket *sock;
1834 int err;
1835
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001836 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001837 if (err < 0) {
1838 dprintk("RPC: can't create %d transport socket (%d).\n",
1839 protocol, -err);
1840 goto out;
1841 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001842 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001843
Ben Hutchings4cea2882011-02-22 21:54:34 +00001844 err = xs_bind(transport, sock);
1845 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001846 sock_release(sock);
1847 goto out;
1848 }
1849
1850 return sock;
1851out:
1852 return ERR_PTR(err);
1853}
1854
Chuck Lever176e21e2011-05-09 15:22:44 -04001855static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1856 struct socket *sock)
1857{
1858 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1859 xprt);
1860
1861 if (!transport->inet) {
1862 struct sock *sk = sock->sk;
1863
1864 write_lock_bh(&sk->sk_callback_lock);
1865
1866 xs_save_old_callbacks(transport, sk);
1867
1868 sk->sk_user_data = xprt;
1869 sk->sk_data_ready = xs_local_data_ready;
1870 sk->sk_write_space = xs_udp_write_space;
Chuck Lever176e21e2011-05-09 15:22:44 -04001871 sk->sk_allocation = GFP_ATOMIC;
1872
1873 xprt_clear_connected(xprt);
1874
1875 /* Reset to new socket */
1876 transport->sock = sock;
1877 transport->inet = sk;
1878
1879 write_unlock_bh(&sk->sk_callback_lock);
1880 }
1881
1882 /* Tell the socket layer to start connecting... */
1883 xprt->stat.connect_count++;
1884 xprt->stat.connect_start = jiffies;
1885 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1886}
1887
1888/**
1889 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1890 * @xprt: RPC transport to connect
1891 * @transport: socket transport to connect
1892 * @create_sock: function to create a socket of the correct type
1893 *
1894 * Invoked by a work queue tasklet.
1895 */
1896static void xs_local_setup_socket(struct work_struct *work)
1897{
1898 struct sock_xprt *transport =
1899 container_of(work, struct sock_xprt, connect_worker.work);
1900 struct rpc_xprt *xprt = &transport->xprt;
1901 struct socket *sock;
1902 int status = -EIO;
1903
1904 if (xprt->shutdown)
1905 goto out;
1906
Jeff Layton032da6a2012-07-23 13:58:51 -04001907 current->flags |= PF_FSTRANS;
1908
Chuck Lever176e21e2011-05-09 15:22:44 -04001909 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1910 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1911 SOCK_STREAM, 0, &sock, 1);
1912 if (status < 0) {
1913 dprintk("RPC: can't create AF_LOCAL "
1914 "transport socket (%d).\n", -status);
1915 goto out;
1916 }
1917 xs_reclassify_socketu(sock);
1918
1919 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1920 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1921
1922 status = xs_local_finish_connecting(xprt, sock);
1923 switch (status) {
1924 case 0:
1925 dprintk("RPC: xprt %p connected to %s\n",
1926 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1927 xprt_set_connected(xprt);
1928 break;
1929 case -ENOENT:
1930 dprintk("RPC: xprt %p: socket %s does not exist\n",
1931 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1932 break;
1933 default:
1934 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1935 __func__, -status,
1936 xprt->address_strings[RPC_DISPLAY_ADDR]);
1937 }
1938
1939out:
1940 xprt_clear_connecting(xprt);
1941 xprt_wake_pending_tasks(xprt, status);
Jeff Layton032da6a2012-07-23 13:58:51 -04001942 current->flags &= ~PF_FSTRANS;
Chuck Lever176e21e2011-05-09 15:22:44 -04001943}
1944
Chuck Lever16be2d22007-08-06 11:57:38 -04001945static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001946{
Chuck Lever16be2d22007-08-06 11:57:38 -04001947 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001948
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001949 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001950 struct sock *sk = sock->sk;
1951
1952 write_lock_bh(&sk->sk_callback_lock);
1953
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001954 xs_save_old_callbacks(transport, sk);
1955
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001956 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001957 sk->sk_data_ready = xs_udp_data_ready;
1958 sk->sk_write_space = xs_udp_write_space;
1959 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa72005-12-13 16:13:52 -05001960 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001961
1962 xprt_set_connected(xprt);
1963
1964 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001965 transport->sock = sock;
1966 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001967
1968 write_unlock_bh(&sk->sk_callback_lock);
1969 }
Chuck Lever470056c2005-08-25 16:25:56 -07001970 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04001971}
1972
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04001973static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04001974{
1975 struct sock_xprt *transport =
1976 container_of(work, struct sock_xprt, connect_worker.work);
1977 struct rpc_xprt *xprt = &transport->xprt;
1978 struct socket *sock = transport->sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04001979 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04001980
Trond Myklebust01d37c42009-03-11 14:09:39 -04001981 if (xprt->shutdown)
Chuck Lever9903cd12005-08-11 16:25:26 -04001982 goto out;
1983
Jeff Layton032da6a2012-07-23 13:58:51 -04001984 current->flags |= PF_FSTRANS;
1985
Chuck Levera246b012005-08-11 16:25:23 -04001986 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001987 xs_reset_transport(transport);
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04001988 sock = xs_create_sock(xprt, transport,
1989 xs_addr(xprt)->sa_family, SOCK_DGRAM, IPPROTO_UDP);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04001990 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04001991 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04001992
Chuck Leverc740eff2009-08-09 15:09:46 -04001993 dprintk("RPC: worker connecting xprt %p via %s to "
1994 "%s (port %s)\n", xprt,
1995 xprt->address_strings[RPC_DISPLAY_PROTO],
1996 xprt->address_strings[RPC_DISPLAY_ADDR],
1997 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04001998
1999 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04002000 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002001out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002002 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002003 xprt_wake_pending_tasks(xprt, status);
Jeff Layton032da6a2012-07-23 13:58:51 -04002004 current->flags &= ~PF_FSTRANS;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002005}
2006
Chuck Lever3167e122005-08-25 16:25:55 -07002007/*
2008 * We need to preserve the port number so the reply cache on the server can
2009 * find our cached RPC replies when we get around to reconnecting.
2010 */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002011static void xs_abort_connection(struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07002012{
2013 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07002014 struct sockaddr any;
2015
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002016 dprintk("RPC: disconnecting xprt %p to reuse port\n", transport);
Chuck Lever3167e122005-08-25 16:25:55 -07002017
2018 /*
2019 * Disconnect the transport socket by doing a connect operation
2020 * with AF_UNSPEC. This should return immediately...
2021 */
2022 memset(&any, 0, sizeof(any));
2023 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002024 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002025 if (!result)
Trond Myklebust6c6dff62012-10-23 17:50:07 -04002026 xs_sock_reset_connection_flags(&transport->xprt);
2027 dprintk("RPC: AF_UNSPEC connect return code %d\n", result);
Chuck Lever3167e122005-08-25 16:25:55 -07002028}
2029
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002030static void xs_tcp_reuse_connection(struct sock_xprt *transport)
Trond Myklebust40d25492009-03-11 14:37:58 -04002031{
2032 unsigned int state = transport->inet->sk_state;
2033
Andy Chittenden669502f2010-08-10 10:19:53 -04002034 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) {
2035 /* we don't need to abort the connection if the socket
2036 * hasn't undergone a shutdown
2037 */
2038 if (transport->inet->sk_shutdown == 0)
2039 return;
2040 dprintk("RPC: %s: TCP_CLOSEd and sk_shutdown set to %d\n",
2041 __func__, transport->inet->sk_shutdown);
2042 }
2043 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) {
2044 /* we don't need to abort the connection if the socket
2045 * hasn't undergone a shutdown
2046 */
2047 if (transport->inet->sk_shutdown == 0)
2048 return;
2049 dprintk("RPC: %s: ESTABLISHED/SYN_SENT "
2050 "sk_shutdown set to %d\n",
2051 __func__, transport->inet->sk_shutdown);
2052 }
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002053 xs_abort_connection(transport);
Trond Myklebust40d25492009-03-11 14:37:58 -04002054}
2055
Chuck Lever16be2d22007-08-06 11:57:38 -04002056static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002057{
Chuck Lever16be2d22007-08-06 11:57:38 -04002058 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002059 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002060
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002061 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002062 struct sock *sk = sock->sk;
2063
2064 write_lock_bh(&sk->sk_callback_lock);
2065
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002066 xs_save_old_callbacks(transport, sk);
2067
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002068 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002069 sk->sk_data_ready = xs_tcp_data_ready;
2070 sk->sk_state_change = xs_tcp_state_change;
2071 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebustb079fa72005-12-13 16:13:52 -05002072 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07002073
2074 /* socket options */
2075 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
2076 sock_reset_flag(sk, SOCK_LINGER);
2077 tcp_sk(sk)->linger2 = 0;
2078 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002079
2080 xprt_clear_connected(xprt);
2081
2082 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002083 transport->sock = sock;
2084 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002085
2086 write_unlock_bh(&sk->sk_callback_lock);
2087 }
2088
Trond Myklebust01d37c42009-03-11 14:09:39 -04002089 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002090 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002091
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002092 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05002093 xprt->stat.connect_count++;
2094 xprt->stat.connect_start = jiffies;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002095 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2096 switch (ret) {
2097 case 0:
2098 case -EINPROGRESS:
2099 /* SYN_SENT! */
2100 xprt->connect_cookie++;
2101 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2102 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2103 }
2104out:
2105 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002106}
2107
2108/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002109 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2110 * @xprt: RPC transport to connect
2111 * @transport: socket transport to connect
2112 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04002113 *
2114 * Invoked by a work queue tasklet.
2115 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002116static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002117{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002118 struct sock_xprt *transport =
2119 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002120 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002121 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002122 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002123
Trond Myklebust01d37c42009-03-11 14:09:39 -04002124 if (xprt->shutdown)
Chuck Lever16be2d22007-08-06 11:57:38 -04002125 goto out;
2126
Jeff Layton032da6a2012-07-23 13:58:51 -04002127 current->flags |= PF_FSTRANS;
2128
Chuck Lever16be2d22007-08-06 11:57:38 -04002129 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002130 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002131 sock = xs_create_sock(xprt, transport,
2132 xs_addr(xprt)->sa_family, SOCK_STREAM, IPPROTO_TCP);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002133 if (IS_ERR(sock)) {
2134 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002135 goto out;
2136 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002137 } else {
2138 int abort_and_exit;
2139
2140 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
2141 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04002142 /* "close" the socket, preserving the local port */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002143 xs_tcp_reuse_connection(transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04002144
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002145 if (abort_and_exit)
2146 goto out_eagain;
2147 }
2148
Chuck Leverc740eff2009-08-09 15:09:46 -04002149 dprintk("RPC: worker connecting xprt %p via %s to "
2150 "%s (port %s)\n", xprt,
2151 xprt->address_strings[RPC_DISPLAY_PROTO],
2152 xprt->address_strings[RPC_DISPLAY_ADDR],
2153 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002154
2155 status = xs_tcp_finish_connecting(xprt, sock);
Chuck Lever46121cf2007-01-31 12:14:08 -05002156 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2157 xprt, -status, xprt_connected(xprt),
2158 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002159 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002160 default:
2161 printk("%s: connect returned unhandled error %d\n",
2162 __func__, status);
2163 case -EADDRNOTAVAIL:
2164 /* We're probably in TIME_WAIT. Get rid of existing socket,
2165 * and retry
2166 */
Trond Myklebustb77a7a02012-09-12 16:49:15 -04002167 xs_tcp_force_close(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002168 break;
Trond Myklebust8a2cec22009-03-11 14:38:01 -04002169 case -ECONNREFUSED:
2170 case -ECONNRESET:
2171 case -ENETUNREACH:
2172 /* retry with existing socket, after a delay */
Trond Myklebust2a491992009-03-11 14:38:00 -04002173 case 0:
2174 case -EINPROGRESS:
2175 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002176 xprt_clear_connecting(xprt);
Jeff Layton032da6a2012-07-23 13:58:51 -04002177 current->flags &= ~PF_FSTRANS;
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002178 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002179 case -EINVAL:
2180 /* Happens, for instance, if the user specified a link
2181 * local IPv6 address without a scope-id.
2182 */
2183 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002184 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002185out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04002186 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002187out:
Chuck Lever2226feb2005-08-11 16:25:38 -04002188 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002189 xprt_wake_pending_tasks(xprt, status);
Jeff Layton032da6a2012-07-23 13:58:51 -04002190 current->flags &= ~PF_FSTRANS;
Chuck Levera246b012005-08-11 16:25:23 -04002191}
2192
Chuck Lever68e220b2007-08-06 11:57:48 -04002193/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002194 * xs_connect - connect a socket to a remote endpoint
2195 * @task: address of RPC task that manages state of connect request
2196 *
2197 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002198 *
2199 * UDP socket connects are synchronous, but we use a work queue anyway
2200 * to guarantee that even unprivileged user processes can set up a
2201 * socket on a privileged port.
2202 *
2203 * If a UDP socket connect fails, the delay behavior here prevents
2204 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002205 */
2206static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002207{
2208 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002209 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002210
Chuck Lever09a21c42009-12-03 15:58:56 -05002211 if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002212 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2213 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002214 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002215 queue_delayed_work(rpciod_workqueue,
2216 &transport->connect_worker,
2217 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07002218 xprt->reestablish_timeout <<= 1;
Neil Brown61d0a8e2009-09-23 14:36:37 -04002219 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2220 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002221 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2222 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002223 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05002224 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002225 queue_delayed_work(rpciod_workqueue,
2226 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002227 }
2228}
2229
Chuck Lever262ca072006-03-20 13:44:16 -05002230/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002231 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2232 * @xprt: rpc_xprt struct containing statistics
2233 * @seq: output file
2234 *
2235 */
2236static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2237{
2238 long idle_time = 0;
2239
2240 if (xprt_connected(xprt))
2241 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2242
2243 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
Andy Adamson15a45202012-02-14 16:19:18 -05002244 "%llu %llu %lu %llu %llu\n",
Chuck Lever176e21e2011-05-09 15:22:44 -04002245 xprt->stat.bind_count,
2246 xprt->stat.connect_count,
2247 xprt->stat.connect_time,
2248 idle_time,
2249 xprt->stat.sends,
2250 xprt->stat.recvs,
2251 xprt->stat.bad_xids,
2252 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002253 xprt->stat.bklog_u,
2254 xprt->stat.max_slots,
2255 xprt->stat.sending_u,
2256 xprt->stat.pending_u);
Chuck Lever176e21e2011-05-09 15:22:44 -04002257}
2258
2259/**
Chuck Lever262ca072006-03-20 13:44:16 -05002260 * xs_udp_print_stats - display UDP socket-specifc stats
2261 * @xprt: rpc_xprt struct containing statistics
2262 * @seq: output file
2263 *
2264 */
2265static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2266{
Chuck Leverc8475462006-12-05 16:35:26 -05002267 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2268
Andy Adamson15a45202012-02-14 16:19:18 -05002269 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2270 "%lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002271 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002272 xprt->stat.bind_count,
2273 xprt->stat.sends,
2274 xprt->stat.recvs,
2275 xprt->stat.bad_xids,
2276 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002277 xprt->stat.bklog_u,
2278 xprt->stat.max_slots,
2279 xprt->stat.sending_u,
2280 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002281}
2282
2283/**
2284 * xs_tcp_print_stats - display TCP socket-specifc stats
2285 * @xprt: rpc_xprt struct containing statistics
2286 * @seq: output file
2287 *
2288 */
2289static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2290{
Chuck Leverc8475462006-12-05 16:35:26 -05002291 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002292 long idle_time = 0;
2293
2294 if (xprt_connected(xprt))
2295 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2296
Andy Adamson15a45202012-02-14 16:19:18 -05002297 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2298 "%llu %llu %lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002299 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002300 xprt->stat.bind_count,
2301 xprt->stat.connect_count,
2302 xprt->stat.connect_time,
2303 idle_time,
2304 xprt->stat.sends,
2305 xprt->stat.recvs,
2306 xprt->stat.bad_xids,
2307 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002308 xprt->stat.bklog_u,
2309 xprt->stat.max_slots,
2310 xprt->stat.sending_u,
2311 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002312}
2313
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002314/*
2315 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2316 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2317 * to use the server side send routines.
2318 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002319static void *bc_malloc(struct rpc_task *task, size_t size)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002320{
2321 struct page *page;
2322 struct rpc_buffer *buf;
2323
2324 BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2325 page = alloc_page(GFP_KERNEL);
2326
2327 if (!page)
2328 return NULL;
2329
2330 buf = page_address(page);
2331 buf->len = PAGE_SIZE;
2332
2333 return buf->data;
2334}
2335
2336/*
2337 * Free the space allocated in the bc_alloc routine
2338 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002339static void bc_free(void *buffer)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002340{
2341 struct rpc_buffer *buf;
2342
2343 if (!buffer)
2344 return;
2345
2346 buf = container_of(buffer, struct rpc_buffer, data);
2347 free_page((unsigned long)buf);
2348}
2349
2350/*
2351 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2352 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2353 */
2354static int bc_sendto(struct rpc_rqst *req)
2355{
2356 int len;
2357 struct xdr_buf *xbufp = &req->rq_snd_buf;
2358 struct rpc_xprt *xprt = req->rq_xprt;
2359 struct sock_xprt *transport =
2360 container_of(xprt, struct sock_xprt, xprt);
2361 struct socket *sock = transport->sock;
2362 unsigned long headoff;
2363 unsigned long tailoff;
2364
Chuck Lever61677ee2011-05-09 15:22:34 -04002365 xs_encode_stream_record_marker(xbufp);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002366
2367 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2368 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2369 len = svc_send_common(sock, xbufp,
2370 virt_to_page(xbufp->head[0].iov_base), headoff,
2371 xbufp->tail[0].iov_base, tailoff);
2372
2373 if (len != xbufp->len) {
2374 printk(KERN_NOTICE "Error sending entire callback!\n");
2375 len = -EAGAIN;
2376 }
2377
2378 return len;
2379}
2380
2381/*
2382 * The send routine. Borrows from svc_send
2383 */
2384static int bc_send_request(struct rpc_task *task)
2385{
2386 struct rpc_rqst *req = task->tk_rqstp;
2387 struct svc_xprt *xprt;
2388 struct svc_sock *svsk;
2389 u32 len;
2390
2391 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2392 /*
2393 * Get the server socket associated with this callback xprt
2394 */
2395 xprt = req->rq_xprt->bc_xprt;
2396 svsk = container_of(xprt, struct svc_sock, sk_xprt);
2397
2398 /*
2399 * Grab the mutex to serialize data as the connection is shared
2400 * with the fore channel
2401 */
2402 if (!mutex_trylock(&xprt->xpt_mutex)) {
2403 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2404 if (!mutex_trylock(&xprt->xpt_mutex))
2405 return -EAGAIN;
2406 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2407 }
2408 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2409 len = -ENOTCONN;
2410 else
2411 len = bc_sendto(req);
2412 mutex_unlock(&xprt->xpt_mutex);
2413
2414 if (len > 0)
2415 len = 0;
2416
2417 return len;
2418}
2419
2420/*
2421 * The close routine. Since this is client initiated, we do nothing
2422 */
2423
2424static void bc_close(struct rpc_xprt *xprt)
2425{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002426}
2427
2428/*
2429 * The xprt destroy routine. Again, because this connection is client
2430 * initiated, we do nothing
2431 */
2432
2433static void bc_destroy(struct rpc_xprt *xprt)
2434{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002435}
2436
Chuck Lever176e21e2011-05-09 15:22:44 -04002437static struct rpc_xprt_ops xs_local_ops = {
2438 .reserve_xprt = xprt_reserve_xprt,
2439 .release_xprt = xs_tcp_release_xprt,
Trond Myklebust9b623552012-10-22 12:56:58 -04002440 .alloc_slot = xprt_alloc_slot,
Chuck Lever176e21e2011-05-09 15:22:44 -04002441 .rpcbind = xs_local_rpcbind,
2442 .set_port = xs_local_set_port,
2443 .connect = xs_connect,
2444 .buf_alloc = rpc_malloc,
2445 .buf_free = rpc_free,
2446 .send_request = xs_local_send_request,
2447 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2448 .close = xs_close,
2449 .destroy = xs_destroy,
2450 .print_stats = xs_local_print_stats,
2451};
2452
Chuck Lever262965f2005-08-11 16:25:56 -04002453static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002454 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002455 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002456 .release_xprt = xprt_release_xprt_cong,
Trond Myklebust9b623552012-10-22 12:56:58 -04002457 .alloc_slot = xprt_alloc_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002458 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002459 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002460 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002461 .buf_alloc = rpc_malloc,
2462 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002463 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002464 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002465 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002466 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002467 .close = xs_close,
2468 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002469 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04002470};
2471
2472static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002473 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002474 .release_xprt = xs_tcp_release_xprt,
Trond Myklebust9b623552012-10-22 12:56:58 -04002475 .alloc_slot = xprt_lock_and_alloc_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002476 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002477 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002478 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002479 .buf_alloc = rpc_malloc,
2480 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002481 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002482 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustf75e6742009-04-21 17:18:20 -04002483 .close = xs_tcp_close,
Chuck Lever9903cd12005-08-11 16:25:26 -04002484 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002485 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002486};
2487
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002488/*
2489 * The rpc_xprt_ops for the server backchannel
2490 */
2491
2492static struct rpc_xprt_ops bc_tcp_ops = {
2493 .reserve_xprt = xprt_reserve_xprt,
2494 .release_xprt = xprt_release_xprt,
Trond Myklebust9b623552012-10-22 12:56:58 -04002495 .alloc_slot = xprt_alloc_slot,
J. Bruce Fields92769102012-03-23 15:25:03 -04002496 .rpcbind = xs_local_rpcbind,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002497 .buf_alloc = bc_malloc,
2498 .buf_free = bc_free,
2499 .send_request = bc_send_request,
2500 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2501 .close = bc_close,
2502 .destroy = bc_destroy,
2503 .print_stats = xs_tcp_print_stats,
2504};
2505
Chuck Lever92476852010-10-20 11:53:01 -04002506static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2507{
2508 static const struct sockaddr_in sin = {
2509 .sin_family = AF_INET,
2510 .sin_addr.s_addr = htonl(INADDR_ANY),
2511 };
2512 static const struct sockaddr_in6 sin6 = {
2513 .sin6_family = AF_INET6,
2514 .sin6_addr = IN6ADDR_ANY_INIT,
2515 };
2516
2517 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002518 case AF_LOCAL:
2519 break;
Chuck Lever92476852010-10-20 11:53:01 -04002520 case AF_INET:
2521 memcpy(sap, &sin, sizeof(sin));
2522 break;
2523 case AF_INET6:
2524 memcpy(sap, &sin6, sizeof(sin6));
2525 break;
2526 default:
2527 dprintk("RPC: %s: Bad address family\n", __func__);
2528 return -EAFNOSUPPORT;
2529 }
2530 return 0;
2531}
2532
\"Talpey, Thomas\3c341b02007-09-10 13:47:07 -04002533static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002534 unsigned int slot_table_size,
2535 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002536{
2537 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002538 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002539
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002540 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002541 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002542 return ERR_PTR(-EBADF);
2543 }
2544
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002545 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2546 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002547 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002548 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2549 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002550 return ERR_PTR(-ENOMEM);
2551 }
2552
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002553 new = container_of(xprt, struct sock_xprt, xprt);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002554 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2555 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002556 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002557 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002558 else {
2559 int err;
2560 err = xs_init_anyaddr(args->dstaddr->sa_family,
2561 (struct sockaddr *)&new->srcaddr);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002562 if (err != 0) {
2563 xprt_free(xprt);
Chuck Lever92476852010-10-20 11:53:01 -04002564 return ERR_PTR(err);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002565 }
Chuck Lever92476852010-10-20 11:53:01 -04002566 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002567
2568 return xprt;
2569}
2570
Chuck Lever176e21e2011-05-09 15:22:44 -04002571static const struct rpc_timeout xs_local_default_timeout = {
2572 .to_initval = 10 * HZ,
2573 .to_maxval = 10 * HZ,
2574 .to_retries = 2,
2575};
2576
2577/**
2578 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2579 * @args: rpc transport creation arguments
2580 *
2581 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2582 */
2583static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2584{
2585 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2586 struct sock_xprt *transport;
2587 struct rpc_xprt *xprt;
2588 struct rpc_xprt *ret;
2589
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002590 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2591 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002592 if (IS_ERR(xprt))
2593 return xprt;
2594 transport = container_of(xprt, struct sock_xprt, xprt);
2595
2596 xprt->prot = 0;
2597 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2598 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2599
2600 xprt->bind_timeout = XS_BIND_TO;
2601 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2602 xprt->idle_timeout = XS_IDLE_DISC_TO;
2603
2604 xprt->ops = &xs_local_ops;
2605 xprt->timeout = &xs_local_default_timeout;
2606
2607 switch (sun->sun_family) {
2608 case AF_LOCAL:
2609 if (sun->sun_path[0] != '/') {
2610 dprintk("RPC: bad AF_LOCAL address: %s\n",
2611 sun->sun_path);
2612 ret = ERR_PTR(-EINVAL);
2613 goto out_err;
2614 }
2615 xprt_set_bound(xprt);
2616 INIT_DELAYED_WORK(&transport->connect_worker,
2617 xs_local_setup_socket);
2618 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
2619 break;
2620 default:
2621 ret = ERR_PTR(-EAFNOSUPPORT);
2622 goto out_err;
2623 }
2624
2625 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2626 xprt->address_strings[RPC_DISPLAY_ADDR]);
2627
2628 if (try_module_get(THIS_MODULE))
2629 return xprt;
2630 ret = ERR_PTR(-EINVAL);
2631out_err:
2632 xprt_free(xprt);
2633 return ret;
2634}
2635
Trond Myklebust2881ae72007-12-20 16:03:54 -05002636static const struct rpc_timeout xs_udp_default_timeout = {
2637 .to_initval = 5 * HZ,
2638 .to_maxval = 30 * HZ,
2639 .to_increment = 5 * HZ,
2640 .to_retries = 5,
2641};
2642
Chuck Lever9903cd12005-08-11 16:25:26 -04002643/**
2644 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002645 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002646 *
2647 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002648static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002649{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002650 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002651 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002652 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002653 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002654
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002655 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2656 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002657 if (IS_ERR(xprt))
2658 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002659 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002660
Chuck Leverec739ef2006-08-22 20:06:15 -04002661 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002662 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002663 /* XXX: header size can vary due to auth type, IPv6, etc. */
2664 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2665
Chuck Lever03bf4b72005-08-25 16:25:55 -07002666 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002667 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2668 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002669
Chuck Lever262965f2005-08-11 16:25:56 -04002670 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002671
Trond Myklebustba7392b2007-12-20 16:03:55 -05002672 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002673
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002674 switch (addr->sa_family) {
2675 case AF_INET:
2676 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2677 xprt_set_bound(xprt);
2678
2679 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002680 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002681 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002682 break;
2683 case AF_INET6:
2684 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2685 xprt_set_bound(xprt);
2686
2687 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002688 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002689 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002690 break;
2691 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002692 ret = ERR_PTR(-EAFNOSUPPORT);
2693 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002694 }
2695
Chuck Leverc740eff2009-08-09 15:09:46 -04002696 if (xprt_bound(xprt))
2697 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2698 xprt->address_strings[RPC_DISPLAY_ADDR],
2699 xprt->address_strings[RPC_DISPLAY_PORT],
2700 xprt->address_strings[RPC_DISPLAY_PROTO]);
2701 else
2702 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2703 xprt->address_strings[RPC_DISPLAY_ADDR],
2704 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002705
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002706 if (try_module_get(THIS_MODULE))
2707 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002708 ret = ERR_PTR(-EINVAL);
2709out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002710 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002711 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002712}
2713
Trond Myklebust2881ae72007-12-20 16:03:54 -05002714static const struct rpc_timeout xs_tcp_default_timeout = {
2715 .to_initval = 60 * HZ,
2716 .to_maxval = 60 * HZ,
2717 .to_retries = 2,
2718};
2719
Chuck Lever9903cd12005-08-11 16:25:26 -04002720/**
2721 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002722 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002723 *
2724 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002725static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002726{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002727 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002728 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002729 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002730 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002731
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002732 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2733 xprt_max_tcp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002734 if (IS_ERR(xprt))
2735 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002736 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002737
Chuck Leverec739ef2006-08-22 20:06:15 -04002738 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002739 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002740 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002741
Chuck Lever03bf4b72005-08-25 16:25:55 -07002742 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002743 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2744 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002745
Chuck Lever262965f2005-08-11 16:25:56 -04002746 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002747 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002748
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002749 switch (addr->sa_family) {
2750 case AF_INET:
2751 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2752 xprt_set_bound(xprt);
2753
Chuck Lever9dc3b092009-08-09 15:09:46 -04002754 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002755 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002756 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002757 break;
2758 case AF_INET6:
2759 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2760 xprt_set_bound(xprt);
2761
Chuck Lever9dc3b092009-08-09 15:09:46 -04002762 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002763 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002764 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002765 break;
2766 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002767 ret = ERR_PTR(-EAFNOSUPPORT);
2768 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002769 }
2770
Chuck Leverc740eff2009-08-09 15:09:46 -04002771 if (xprt_bound(xprt))
2772 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2773 xprt->address_strings[RPC_DISPLAY_ADDR],
2774 xprt->address_strings[RPC_DISPLAY_PORT],
2775 xprt->address_strings[RPC_DISPLAY_PROTO]);
2776 else
2777 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2778 xprt->address_strings[RPC_DISPLAY_ADDR],
2779 xprt->address_strings[RPC_DISPLAY_PROTO]);
2780
Chuck Leveredb267a2006-08-22 20:06:18 -04002781
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002782 if (try_module_get(THIS_MODULE))
2783 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002784 ret = ERR_PTR(-EINVAL);
2785out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002786 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002787 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002788}
Chuck Lever282b32e2006-12-05 16:35:51 -05002789
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002790/**
2791 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2792 * @args: rpc transport creation arguments
2793 *
2794 */
2795static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2796{
2797 struct sockaddr *addr = args->dstaddr;
2798 struct rpc_xprt *xprt;
2799 struct sock_xprt *transport;
2800 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002801 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002802
J. Bruce Fieldsf0418aa2010-12-08 13:48:19 -05002803 if (args->bc_xprt->xpt_bc_xprt) {
2804 /*
2805 * This server connection already has a backchannel
2806 * export; we can't create a new one, as we wouldn't be
2807 * able to match replies based on xid any more. So,
2808 * reuse the already-existing one:
2809 */
2810 return args->bc_xprt->xpt_bc_xprt;
2811 }
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002812 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2813 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002814 if (IS_ERR(xprt))
2815 return xprt;
2816 transport = container_of(xprt, struct sock_xprt, xprt);
2817
2818 xprt->prot = IPPROTO_TCP;
2819 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2820 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2821 xprt->timeout = &xs_tcp_default_timeout;
2822
2823 /* backchannel */
2824 xprt_set_bound(xprt);
2825 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002826 xprt->reestablish_timeout = 0;
2827 xprt->idle_timeout = 0;
2828
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002829 xprt->ops = &bc_tcp_ops;
2830
2831 switch (addr->sa_family) {
2832 case AF_INET:
2833 xs_format_peer_addresses(xprt, "tcp",
2834 RPCBIND_NETID_TCP);
2835 break;
2836 case AF_INET6:
2837 xs_format_peer_addresses(xprt, "tcp",
2838 RPCBIND_NETID_TCP6);
2839 break;
2840 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002841 ret = ERR_PTR(-EAFNOSUPPORT);
2842 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002843 }
2844
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04002845 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2846 xprt->address_strings[RPC_DISPLAY_ADDR],
2847 xprt->address_strings[RPC_DISPLAY_PORT],
2848 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002849
2850 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002851 * Once we've associated a backchannel xprt with a connection,
2852 * we want to keep it around as long as long as the connection
2853 * lasts, in case we need to start using it for a backchannel
2854 * again; this reference won't be dropped until bc_xprt is
2855 * destroyed.
2856 */
2857 xprt_get(xprt);
2858 args->bc_xprt->xpt_bc_xprt = xprt;
2859 xprt->bc_xprt = args->bc_xprt;
2860 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2861 transport->sock = bc_sock->sk_sock;
2862 transport->inet = bc_sock->sk_sk;
2863
2864 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002865 * Since we don't want connections for the backchannel, we set
2866 * the xprt status to connected
2867 */
2868 xprt_set_connected(xprt);
2869
2870
2871 if (try_module_get(THIS_MODULE))
2872 return xprt;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002873 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002874 ret = ERR_PTR(-EINVAL);
2875out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002876 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002877 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002878}
2879
Chuck Lever176e21e2011-05-09 15:22:44 -04002880static struct xprt_class xs_local_transport = {
2881 .list = LIST_HEAD_INIT(xs_local_transport.list),
2882 .name = "named UNIX socket",
2883 .owner = THIS_MODULE,
2884 .ident = XPRT_TRANSPORT_LOCAL,
2885 .setup = xs_setup_local,
2886};
2887
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002888static struct xprt_class xs_udp_transport = {
2889 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2890 .name = "udp",
2891 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002892 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002893 .setup = xs_setup_udp,
2894};
2895
2896static struct xprt_class xs_tcp_transport = {
2897 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2898 .name = "tcp",
2899 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002900 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002901 .setup = xs_setup_tcp,
2902};
2903
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002904static struct xprt_class xs_bc_tcp_transport = {
2905 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2906 .name = "tcp NFSv4.1 backchannel",
2907 .owner = THIS_MODULE,
2908 .ident = XPRT_TRANSPORT_BC_TCP,
2909 .setup = xs_setup_bc_tcp,
2910};
2911
Chuck Lever282b32e2006-12-05 16:35:51 -05002912/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002913 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002914 *
2915 */
2916int init_socket_xprt(void)
2917{
Chuck Leverfbf76682006-12-05 16:35:54 -05002918#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002919 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002920 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002921#endif
2922
Chuck Lever176e21e2011-05-09 15:22:44 -04002923 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002924 xprt_register_transport(&xs_udp_transport);
2925 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002926 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002927
Chuck Lever282b32e2006-12-05 16:35:51 -05002928 return 0;
2929}
2930
2931/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002932 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05002933 *
2934 */
2935void cleanup_socket_xprt(void)
2936{
Chuck Leverfbf76682006-12-05 16:35:54 -05002937#ifdef RPC_DEBUG
2938 if (sunrpc_table_header) {
2939 unregister_sysctl_table(sunrpc_table_header);
2940 sunrpc_table_header = NULL;
2941 }
2942#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002943
Chuck Lever176e21e2011-05-09 15:22:44 -04002944 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002945 xprt_unregister_transport(&xs_udp_transport);
2946 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002947 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05002948}
Trond Myklebustcbf11072009-08-09 15:06:19 -04002949
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002950static int param_set_uint_minmax(const char *val,
2951 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04002952 unsigned int min, unsigned int max)
2953{
2954 unsigned long num;
2955 int ret;
2956
2957 if (!val)
2958 return -EINVAL;
2959 ret = strict_strtoul(val, 0, &num);
2960 if (ret == -EINVAL || num < min || num > max)
2961 return -EINVAL;
2962 *((unsigned int *)kp->arg) = num;
2963 return 0;
2964}
2965
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002966static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04002967{
2968 return param_set_uint_minmax(val, kp,
2969 RPC_MIN_RESVPORT,
2970 RPC_MAX_RESVPORT);
2971}
2972
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002973static struct kernel_param_ops param_ops_portnr = {
2974 .set = param_set_portnr,
2975 .get = param_get_uint,
2976};
2977
Trond Myklebustcbf11072009-08-09 15:06:19 -04002978#define param_check_portnr(name, p) \
2979 __param_check(name, p, unsigned int);
2980
2981module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
2982module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
2983
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002984static int param_set_slot_table_size(const char *val,
2985 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04002986{
2987 return param_set_uint_minmax(val, kp,
2988 RPC_MIN_SLOT_TABLE,
2989 RPC_MAX_SLOT_TABLE);
2990}
2991
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002992static struct kernel_param_ops param_ops_slot_table_size = {
2993 .set = param_set_slot_table_size,
2994 .get = param_get_uint,
2995};
2996
Trond Myklebustcbf11072009-08-09 15:06:19 -04002997#define param_check_slot_table_size(name, p) \
2998 __param_check(name, p, unsigned int);
2999
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003000static int param_set_max_slot_table_size(const char *val,
3001 const struct kernel_param *kp)
3002{
3003 return param_set_uint_minmax(val, kp,
3004 RPC_MIN_SLOT_TABLE,
3005 RPC_MAX_SLOT_TABLE_LIMIT);
3006}
3007
3008static struct kernel_param_ops param_ops_max_slot_table_size = {
3009 .set = param_set_max_slot_table_size,
3010 .get = param_get_uint,
3011};
3012
3013#define param_check_max_slot_table_size(name, p) \
3014 __param_check(name, p, unsigned int);
3015
Trond Myklebustcbf11072009-08-09 15:06:19 -04003016module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3017 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003018module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3019 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003020module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3021 slot_table_size, 0644);
3022