blob: b98bce5461b59e9d4a62aa5459249fa6b2d463bf [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>
Jeff Layton59766872013-02-04 12:50:00 -050036#include <linux/sunrpc/addr.h>
Chuck Lever02107142006-01-03 09:55:49 +010037#include <linux/sunrpc/sched.h>
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030038#include <linux/sunrpc/svcsock.h>
\"Talpey, Thomas\49c36fc2007-09-10 13:47:31 -040039#include <linux/sunrpc/xprtsock.h>
Chuck Levera246b012005-08-11 16:25:23 -040040#include <linux/file.h>
Trond Myklebust9e00abc2011-07-13 19:20:49 -040041#ifdef CONFIG_SUNRPC_BACKCHANNEL
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -040042#include <linux/sunrpc/bc_xprt.h>
43#endif
Chuck Levera246b012005-08-11 16:25:23 -040044
45#include <net/sock.h>
46#include <net/checksum.h>
47#include <net/udp.h>
48#include <net/tcp.h>
49
Trond Myklebust40b5ea02013-09-04 12:16:23 -040050#include <trace/events/sunrpc.h>
51
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030052#include "sunrpc.h"
Chuck Lever176e21e2011-05-09 15:22:44 -040053
54static void xs_close(struct rpc_xprt *xprt);
55
Chuck Lever9903cd12005-08-11 16:25:26 -040056/*
Chuck Leverc556b752005-11-01 12:24:48 -050057 * xprtsock tunables
58 */
Trond Myklebust09acfea2012-03-11 15:22:54 -040059static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
60static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
61static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
Chuck Leverc556b752005-11-01 12:24:48 -050062
Trond Myklebust09acfea2012-03-11 15:22:54 -040063static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
64static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
Chuck Leverc556b752005-11-01 12:24:48 -050065
Trond Myklebust7d1e8252009-03-11 14:38:03 -040066#define XS_TCP_LINGER_TO (15U * HZ)
Trond Myklebust25fe6142009-03-11 14:38:03 -040067static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
Trond Myklebust7d1e8252009-03-11 14:38:03 -040068
Chuck Leverc556b752005-11-01 12:24:48 -050069/*
Chuck Leverfbf76682006-12-05 16:35:54 -050070 * We can register our own files under /proc/sys/sunrpc by
71 * calling register_sysctl_table() again. The files in that
72 * directory become the union of all files registered there.
73 *
74 * We simply need to make sure that we don't collide with
75 * someone else's file names!
76 */
77
78#ifdef RPC_DEBUG
79
80static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
81static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
Trond Myklebustd9ba1312011-07-17 18:11:30 -040082static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Leverfbf76682006-12-05 16:35:54 -050083static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
84static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
85
86static struct ctl_table_header *sunrpc_table_header;
87
88/*
89 * FIXME: changing the UDP slot table size should also resize the UDP
90 * socket buffers for existing UDP transports
91 */
Joe Perchesfe2c6332013-06-11 23:04:25 -070092static struct ctl_table xs_tunables_table[] = {
Chuck Leverfbf76682006-12-05 16:35:54 -050093 {
Chuck Leverfbf76682006-12-05 16:35:54 -050094 .procname = "udp_slot_table_entries",
95 .data = &xprt_udp_slot_table_entries,
96 .maxlen = sizeof(unsigned int),
97 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080098 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -050099 .extra1 = &min_slot_table_size,
100 .extra2 = &max_slot_table_size
101 },
102 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500103 .procname = "tcp_slot_table_entries",
104 .data = &xprt_tcp_slot_table_entries,
105 .maxlen = sizeof(unsigned int),
106 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800107 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500108 .extra1 = &min_slot_table_size,
109 .extra2 = &max_slot_table_size
110 },
111 {
Trond Myklebustd9ba1312011-07-17 18:11:30 -0400112 .procname = "tcp_max_slot_table_entries",
113 .data = &xprt_max_tcp_slot_table_entries,
114 .maxlen = sizeof(unsigned int),
115 .mode = 0644,
116 .proc_handler = proc_dointvec_minmax,
117 .extra1 = &min_slot_table_size,
118 .extra2 = &max_tcp_slot_table_limit
119 },
120 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500121 .procname = "min_resvport",
122 .data = &xprt_min_resvport,
123 .maxlen = sizeof(unsigned int),
124 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800125 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500126 .extra1 = &xprt_min_resvport_limit,
127 .extra2 = &xprt_max_resvport_limit
128 },
129 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500130 .procname = "max_resvport",
131 .data = &xprt_max_resvport,
132 .maxlen = sizeof(unsigned int),
133 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800134 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500135 .extra1 = &xprt_min_resvport_limit,
136 .extra2 = &xprt_max_resvport_limit
137 },
138 {
Trond Myklebust25fe6142009-03-11 14:38:03 -0400139 .procname = "tcp_fin_timeout",
140 .data = &xs_tcp_fin_timeout,
141 .maxlen = sizeof(xs_tcp_fin_timeout),
142 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800143 .proc_handler = proc_dointvec_jiffies,
Trond Myklebust25fe6142009-03-11 14:38:03 -0400144 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800145 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500146};
147
Joe Perchesfe2c6332013-06-11 23:04:25 -0700148static struct ctl_table sunrpc_table[] = {
Chuck Leverfbf76682006-12-05 16:35:54 -0500149 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500150 .procname = "sunrpc",
151 .mode = 0555,
152 .child = xs_tunables_table
153 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800154 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500155};
156
157#endif
158
159/*
Chuck Lever03bf4b72005-08-25 16:25:55 -0700160 * Wait duration for a reply from the RPC portmapper.
161 */
162#define XS_BIND_TO (60U * HZ)
163
164/*
165 * Delay if a UDP socket connect error occurs. This is most likely some
166 * kind of resource problem on the local host.
167 */
168#define XS_UDP_REEST_TO (2U * HZ)
169
170/*
171 * The reestablish timeout allows clients to delay for a bit before attempting
172 * to reconnect to a server that just dropped our connection.
173 *
174 * We implement an exponential backoff when trying to reestablish a TCP
175 * transport connection with the server. Some servers like to drop a TCP
176 * connection when they are overworked, so we start with a short timeout and
177 * increase over time if the server is down or not responding.
178 */
179#define XS_TCP_INIT_REEST_TO (3U * HZ)
180#define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
181
182/*
183 * TCP idle timeout; client drops the transport socket if it is idle
184 * for this long. Note that we also timeout UDP sockets to prevent
185 * holding port numbers when there is no RPC traffic.
186 */
187#define XS_IDLE_DISC_TO (5U * 60 * HZ)
188
Chuck Levera246b012005-08-11 16:25:23 -0400189#ifdef RPC_DEBUG
190# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400191# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400192#endif
193
Chuck Levera246b012005-08-11 16:25:23 -0400194#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400195static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400196{
Chuck Lever9903cd12005-08-11 16:25:26 -0400197 u8 *buf = (u8 *) packet;
198 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400199
Chuck Lever46121cf2007-01-31 12:14:08 -0500200 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400201 for (j = 0; j < count && j < 128; j += 4) {
202 if (!(j & 31)) {
203 if (j)
204 dprintk("\n");
205 dprintk("0x%04x ", j);
206 }
207 dprintk("%02x%02x%02x%02x ",
208 buf[j], buf[j+1], buf[j+2], buf[j+3]);
209 }
210 dprintk("\n");
211}
212#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400213static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400214{
215 /* NOP */
216}
217#endif
218
Chuck Leverffc2e512006-12-05 16:35:11 -0500219struct sock_xprt {
220 struct rpc_xprt xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500221
222 /*
223 * Network layer
224 */
225 struct socket * sock;
226 struct sock * inet;
Chuck Lever51971132006-12-05 16:35:19 -0500227
228 /*
229 * State of TCP reply receive
230 */
231 __be32 tcp_fraghdr,
Trond Myklebustb76ce562010-06-16 13:57:32 -0400232 tcp_xid,
233 tcp_calldir;
Chuck Lever51971132006-12-05 16:35:19 -0500234
235 u32 tcp_offset,
236 tcp_reclen;
237
238 unsigned long tcp_copied,
239 tcp_flags;
Chuck Leverc8475462006-12-05 16:35:26 -0500240
241 /*
242 * Connection of transports
243 */
Trond Myklebust34161db2006-12-07 15:48:15 -0500244 struct delayed_work connect_worker;
Chuck Leverfbfffbd2009-08-09 15:09:46 -0400245 struct sockaddr_storage srcaddr;
246 unsigned short srcport;
Chuck Lever7c6e0662006-12-05 16:35:30 -0500247
248 /*
249 * UDP socket buffer size parameters
250 */
251 size_t rcvsize,
252 sndsize;
Chuck Lever314dfd72006-12-05 16:35:34 -0500253
254 /*
255 * Saved socket callback addresses
256 */
257 void (*old_data_ready)(struct sock *, int);
258 void (*old_state_change)(struct sock *);
259 void (*old_write_space)(struct sock *);
Chuck Leverffc2e512006-12-05 16:35:11 -0500260};
261
Chuck Levere136d092006-12-05 16:35:23 -0500262/*
263 * TCP receive state flags
264 */
265#define TCP_RCV_LAST_FRAG (1UL << 0)
266#define TCP_RCV_COPY_FRAGHDR (1UL << 1)
267#define TCP_RCV_COPY_XID (1UL << 2)
268#define TCP_RCV_COPY_DATA (1UL << 3)
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400269#define TCP_RCV_READ_CALLDIR (1UL << 4)
270#define TCP_RCV_COPY_CALLDIR (1UL << 5)
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400271
272/*
273 * TCP RPC flags
274 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400275#define TCP_RPC_REPLY (1UL << 6)
Chuck Levere136d092006-12-05 16:35:23 -0500276
Chuck Lever95392c52007-08-06 11:57:58 -0400277static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400278{
Chuck Lever95392c52007-08-06 11:57:58 -0400279 return (struct sockaddr *) &xprt->addr;
280}
281
Chuck Lever176e21e2011-05-09 15:22:44 -0400282static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
283{
284 return (struct sockaddr_un *) &xprt->addr;
285}
286
Chuck Lever95392c52007-08-06 11:57:58 -0400287static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
288{
289 return (struct sockaddr_in *) &xprt->addr;
290}
291
292static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
293{
294 return (struct sockaddr_in6 *) &xprt->addr;
295}
296
Chuck Leverc877b842009-08-09 15:09:36 -0400297static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400298{
Chuck Leverc877b842009-08-09 15:09:36 -0400299 struct sockaddr *sap = xs_addr(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400300 struct sockaddr_in6 *sin6;
301 struct sockaddr_in *sin;
Chuck Lever176e21e2011-05-09 15:22:44 -0400302 struct sockaddr_un *sun;
Chuck Leverc877b842009-08-09 15:09:36 -0400303 char buf[128];
Chuck Leveredb267a2006-08-22 20:06:18 -0400304
Chuck Lever9dc3b092009-08-09 15:09:46 -0400305 switch (sap->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400306 case AF_LOCAL:
307 sun = xs_addr_un(xprt);
308 strlcpy(buf, sun->sun_path, sizeof(buf));
309 xprt->address_strings[RPC_DISPLAY_ADDR] =
310 kstrdup(buf, GFP_KERNEL);
311 break;
Chuck Lever9dc3b092009-08-09 15:09:46 -0400312 case AF_INET:
Chuck Lever176e21e2011-05-09 15:22:44 -0400313 (void)rpc_ntop(sap, buf, sizeof(buf));
314 xprt->address_strings[RPC_DISPLAY_ADDR] =
315 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400316 sin = xs_addr_in(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800317 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
Chuck Lever9dc3b092009-08-09 15:09:46 -0400318 break;
319 case AF_INET6:
Chuck Lever176e21e2011-05-09 15:22:44 -0400320 (void)rpc_ntop(sap, buf, sizeof(buf));
321 xprt->address_strings[RPC_DISPLAY_ADDR] =
322 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400323 sin6 = xs_addr_in6(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800324 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400325 break;
326 default:
327 BUG();
Chuck Leveredb267a2006-08-22 20:06:18 -0400328 }
Chuck Lever176e21e2011-05-09 15:22:44 -0400329
Chuck Lever9dc3b092009-08-09 15:09:46 -0400330 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
Chuck Leveredb267a2006-08-22 20:06:18 -0400331}
332
Chuck Lever9dc3b092009-08-09 15:09:46 -0400333static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400334{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400335 struct sockaddr *sap = xs_addr(xprt);
336 char buf[128];
Chuck Lever4b6473f2007-08-06 11:57:12 -0400337
Joe Perches81160e662010-03-08 12:15:59 -0800338 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400339 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400340
Joe Perches81160e662010-03-08 12:15:59 -0800341 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400342 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
343}
Chuck Lever4b6473f2007-08-06 11:57:12 -0400344
Chuck Lever9dc3b092009-08-09 15:09:46 -0400345static void xs_format_peer_addresses(struct rpc_xprt *xprt,
346 const char *protocol,
347 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400348{
Chuck Leverb454ae92008-01-07 18:34:48 -0500349 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leverb454ae92008-01-07 18:34:48 -0500350 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leverc877b842009-08-09 15:09:36 -0400351 xs_format_common_peer_addresses(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400352 xs_format_common_peer_ports(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400353}
354
Chuck Lever9dc3b092009-08-09 15:09:46 -0400355static void xs_update_peer_port(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400356{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400357 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
358 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400359
Chuck Lever9dc3b092009-08-09 15:09:46 -0400360 xs_format_common_peer_ports(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400361}
362
363static void xs_free_peer_addresses(struct rpc_xprt *xprt)
364{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500365 unsigned int i;
366
367 for (i = 0; i < RPC_DISPLAY_MAX; i++)
368 switch (i) {
369 case RPC_DISPLAY_PROTO:
370 case RPC_DISPLAY_NETID:
371 continue;
372 default:
373 kfree(xprt->address_strings[i]);
374 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400375}
376
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400377#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
378
Trond Myklebust24c56842006-10-17 15:06:22 -0400379static 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 -0400380{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400381 struct msghdr msg = {
382 .msg_name = addr,
383 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400384 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
385 };
386 struct kvec iov = {
387 .iov_base = vec->iov_base + base,
388 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400389 };
390
Trond Myklebust24c56842006-10-17 15:06:22 -0400391 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400392 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
393 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
394}
395
Trond Myklebust24c56842006-10-17 15:06:22 -0400396static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400397{
Trond Myklebust24c56842006-10-17 15:06:22 -0400398 struct page **ppage;
399 unsigned int remainder;
400 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400401
Trond Myklebust24c56842006-10-17 15:06:22 -0400402 remainder = xdr->page_len - base;
403 base += xdr->page_base;
404 ppage = xdr->pages + (base >> PAGE_SHIFT);
405 base &= ~PAGE_MASK;
406 for(;;) {
407 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
408 int flags = XS_SENDMSG_FLAGS;
409
410 remainder -= len;
411 if (remainder != 0 || more)
412 flags |= MSG_MORE;
413 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
414 if (remainder == 0 || err != len)
415 break;
416 sent += err;
417 ppage++;
418 base = 0;
419 }
420 if (sent == 0)
421 return err;
422 if (err > 0)
423 sent += err;
424 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400425}
426
Chuck Lever9903cd12005-08-11 16:25:26 -0400427/**
428 * xs_sendpages - write pages directly to a socket
429 * @sock: socket to send on
430 * @addr: UDP only -- address of destination
431 * @addrlen: UDP only -- length of destination address
432 * @xdr: buffer containing this request
433 * @base: starting position in the buffer
434 *
Chuck Levera246b012005-08-11 16:25:23 -0400435 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400436static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
Chuck Levera246b012005-08-11 16:25:23 -0400437{
Trond Myklebust24c56842006-10-17 15:06:22 -0400438 unsigned int remainder = xdr->len - base;
439 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400440
Chuck Lever262965f2005-08-11 16:25:56 -0400441 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400442 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400443
444 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400445 if (base != 0) {
446 addr = NULL;
447 addrlen = 0;
448 }
Chuck Lever262965f2005-08-11 16:25:56 -0400449
Trond Myklebust24c56842006-10-17 15:06:22 -0400450 if (base < xdr->head[0].iov_len || addr != NULL) {
451 unsigned int len = xdr->head[0].iov_len - base;
452 remainder -= len;
453 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
454 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400455 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400456 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400457 base = 0;
458 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400459 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400460
Trond Myklebust24c56842006-10-17 15:06:22 -0400461 if (base < xdr->page_len) {
462 unsigned int len = xdr->page_len - base;
463 remainder -= len;
464 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
465 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400466 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400467 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400468 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400469 } else
470 base -= xdr->page_len;
471
472 if (base >= xdr->tail[0].iov_len)
473 return sent;
474 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400475out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400476 if (sent == 0)
477 return err;
478 if (err > 0)
479 sent += err;
480 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400481}
482
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400483static void xs_nospace_callback(struct rpc_task *task)
484{
485 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
486
487 transport->inet->sk_write_pending--;
488 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
489}
490
Chuck Lever9903cd12005-08-11 16:25:26 -0400491/**
Chuck Lever262965f2005-08-11 16:25:56 -0400492 * xs_nospace - place task on wait queue if transmit was incomplete
493 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400494 *
Chuck Levera246b012005-08-11 16:25:23 -0400495 */
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400496static int xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400497{
Chuck Lever262965f2005-08-11 16:25:56 -0400498 struct rpc_rqst *req = task->tk_rqstp;
499 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500500 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust24ca9a82011-11-22 14:44:28 +0200501 int ret = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400502
Chuck Lever46121cf2007-01-31 12:14:08 -0500503 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400504 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
505 req->rq_slen);
506
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400507 /* Protect against races with write_space */
508 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400509
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400510 /* Don't race with disconnect */
511 if (xprt_connected(xprt)) {
512 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
513 /*
514 * Notify TCP that we're limited by the application
515 * window size
516 */
517 set_bit(SOCK_NOSPACE, &transport->sock->flags);
518 transport->inet->sk_write_pending++;
519 /* ...and wait for more buffer space */
520 xprt_wait_for_buffer_space(task, xs_nospace_callback);
521 }
522 } else {
523 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400524 ret = -ENOTCONN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400525 }
Chuck Lever262965f2005-08-11 16:25:56 -0400526
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400527 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400528 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400529}
530
Chuck Lever61677ee2011-05-09 15:22:34 -0400531/*
532 * Construct a stream transport record marker in @buf.
533 */
534static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
535{
536 u32 reclen = buf->len - sizeof(rpc_fraghdr);
537 rpc_fraghdr *base = buf->head[0].iov_base;
538 *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
539}
540
Chuck Lever262965f2005-08-11 16:25:56 -0400541/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400542 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
543 * @task: RPC task that manages the state of an RPC request
544 *
545 * Return values:
546 * 0: The request has been sent
547 * EAGAIN: The socket was blocked, please call again later to
548 * complete the request
549 * ENOTCONN: Caller needs to invoke connect logic then call again
550 * other: Some other error occured, the request was not sent
551 */
552static int xs_local_send_request(struct rpc_task *task)
553{
554 struct rpc_rqst *req = task->tk_rqstp;
555 struct rpc_xprt *xprt = req->rq_xprt;
556 struct sock_xprt *transport =
557 container_of(xprt, struct sock_xprt, xprt);
558 struct xdr_buf *xdr = &req->rq_snd_buf;
559 int status;
560
561 xs_encode_stream_record_marker(&req->rq_snd_buf);
562
563 xs_pktdump("packet data:",
564 req->rq_svec->iov_base, req->rq_svec->iov_len);
565
566 status = xs_sendpages(transport->sock, NULL, 0,
567 xdr, req->rq_bytes_sent);
568 dprintk("RPC: %s(%u) = %d\n",
569 __func__, xdr->len - req->rq_bytes_sent, status);
570 if (likely(status >= 0)) {
571 req->rq_bytes_sent += status;
572 req->rq_xmit_bytes_sent += status;
573 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
574 req->rq_bytes_sent = 0;
575 return 0;
576 }
577 status = -EAGAIN;
578 }
579
580 switch (status) {
581 case -EAGAIN:
582 status = xs_nospace(task);
583 break;
584 default:
585 dprintk("RPC: sendmsg returned unrecognized error %d\n",
586 -status);
587 case -EPIPE:
588 xs_close(xprt);
589 status = -ENOTCONN;
590 }
591
592 return status;
593}
594
595/**
Chuck Lever262965f2005-08-11 16:25:56 -0400596 * xs_udp_send_request - write an RPC request to a UDP socket
597 * @task: address of RPC task that manages the state of an RPC request
598 *
599 * Return values:
600 * 0: The request has been sent
601 * EAGAIN: The socket was blocked, please call again later to
602 * complete the request
603 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300604 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -0400605 */
606static int xs_udp_send_request(struct rpc_task *task)
607{
608 struct rpc_rqst *req = task->tk_rqstp;
609 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500610 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400611 struct xdr_buf *xdr = &req->rq_snd_buf;
612 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400613
Chuck Lever9903cd12005-08-11 16:25:26 -0400614 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400615 req->rq_svec->iov_base,
616 req->rq_svec->iov_len);
617
Trond Myklebust01d37c42009-03-11 14:09:39 -0400618 if (!xprt_bound(xprt))
619 return -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500620 status = xs_sendpages(transport->sock,
Chuck Lever95392c52007-08-06 11:57:58 -0400621 xs_addr(xprt),
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500622 xprt->addrlen, xdr,
623 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400624
Chuck Lever46121cf2007-01-31 12:14:08 -0500625 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400626 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400627
Trond Myklebust21997002007-10-01 11:43:37 -0400628 if (status >= 0) {
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400629 req->rq_xmit_bytes_sent += status;
Trond Myklebust21997002007-10-01 11:43:37 -0400630 if (status >= req->rq_slen)
631 return 0;
632 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400633 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400634 }
Chuck Levera246b012005-08-11 16:25:23 -0400635
Chuck Lever262965f2005-08-11 16:25:56 -0400636 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400637 case -ENOTSOCK:
638 status = -ENOTCONN;
639 /* Should we call xs_close() here? */
640 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400641 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400642 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400643 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400644 default:
645 dprintk("RPC: sendmsg returned unrecognized error %d\n",
646 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400647 case -ENETUNREACH:
648 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400649 case -ECONNREFUSED:
650 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400651 * prompts ECONNREFUSED. */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400652 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400653 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800654
Chuck Lever262965f2005-08-11 16:25:56 -0400655 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400656}
657
Trond Myklebuste06799f2007-11-05 15:44:12 -0500658/**
659 * xs_tcp_shutdown - gracefully shut down a TCP socket
660 * @xprt: transport
661 *
662 * Initiates a graceful shutdown of the TCP socket by calling the
663 * equivalent of shutdown(SHUT_WR);
664 */
665static void xs_tcp_shutdown(struct rpc_xprt *xprt)
666{
667 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
668 struct socket *sock = transport->sock;
669
Trond Myklebust40b5ea02013-09-04 12:16:23 -0400670 if (sock != NULL) {
Trond Myklebuste06799f2007-11-05 15:44:12 -0500671 kernel_sock_shutdown(sock, SHUT_WR);
Trond Myklebust40b5ea02013-09-04 12:16:23 -0400672 trace_rpc_socket_shutdown(xprt, sock);
673 }
Trond Myklebuste06799f2007-11-05 15:44:12 -0500674}
675
Chuck Lever9903cd12005-08-11 16:25:26 -0400676/**
Chuck Lever262965f2005-08-11 16:25:56 -0400677 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400678 * @task: address of RPC task that manages the state of an RPC request
679 *
680 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400681 * 0: The request has been sent
682 * EAGAIN: The socket was blocked, please call again later to
683 * complete the request
684 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300685 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400686 *
687 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400688 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400689 */
Chuck Lever262965f2005-08-11 16:25:56 -0400690static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400691{
692 struct rpc_rqst *req = task->tk_rqstp;
693 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500694 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400695 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Leverb595bb12007-08-06 11:56:42 -0400696 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400697
Chuck Lever61677ee2011-05-09 15:22:34 -0400698 xs_encode_stream_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400699
Chuck Lever262965f2005-08-11 16:25:56 -0400700 xs_pktdump("packet data:",
701 req->rq_svec->iov_base,
702 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400703
704 /* Continue transmitting the packet/record. We must be careful
705 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400706 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400707 while (1) {
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500708 status = xs_sendpages(transport->sock,
709 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400710
Chuck Lever46121cf2007-01-31 12:14:08 -0500711 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400712 xdr->len - req->rq_bytes_sent, status);
713
714 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400715 break;
716
Chuck Lever262965f2005-08-11 16:25:56 -0400717 /* If we've sent the entire packet, immediately
718 * reset the count of bytes sent. */
719 req->rq_bytes_sent += status;
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400720 req->rq_xmit_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400721 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
722 req->rq_bytes_sent = 0;
723 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400724 }
725
Trond Myklebust06b4b682008-04-16 16:51:38 -0400726 if (status != 0)
727 continue;
Chuck Levera246b012005-08-11 16:25:23 -0400728 status = -EAGAIN;
Trond Myklebust06b4b682008-04-16 16:51:38 -0400729 break;
Chuck Levera246b012005-08-11 16:25:23 -0400730 }
731
Chuck Lever262965f2005-08-11 16:25:56 -0400732 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400733 case -ENOTSOCK:
734 status = -ENOTCONN;
735 /* Should we call xs_close() here? */
736 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400737 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400738 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400739 break;
740 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500741 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400742 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400743 case -ECONNRESET:
Trond Myklebuste06799f2007-11-05 15:44:12 -0500744 xs_tcp_shutdown(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400745 case -ECONNREFUSED:
746 case -ENOTCONN:
Trond Myklebustb9d2bb22012-10-23 11:40:02 -0400747 case -EPIPE:
Chuck Lever262965f2005-08-11 16:25:56 -0400748 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400749 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800750
Chuck Levera246b012005-08-11 16:25:23 -0400751 return status;
752}
753
Chuck Lever9903cd12005-08-11 16:25:26 -0400754/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400755 * xs_tcp_release_xprt - clean up after a tcp transmission
756 * @xprt: transport
757 * @task: rpc task
758 *
759 * This cleans up if an error causes us to abort the transmission of a request.
760 * In this case, the socket may need to be reset in order to avoid confusing
761 * the server.
762 */
763static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
764{
765 struct rpc_rqst *req;
766
767 if (task != xprt->snd_task)
768 return;
769 if (task == NULL)
770 goto out_release;
771 req = task->tk_rqstp;
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400772 if (req == NULL)
773 goto out_release;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400774 if (req->rq_bytes_sent == 0)
775 goto out_release;
776 if (req->rq_bytes_sent == req->rq_snd_buf.len)
777 goto out_release;
Trond Myklebusta4f08352013-01-08 09:10:21 -0500778 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400779out_release:
780 xprt_release_xprt(xprt, task);
781}
782
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400783static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
784{
785 transport->old_data_ready = sk->sk_data_ready;
786 transport->old_state_change = sk->sk_state_change;
787 transport->old_write_space = sk->sk_write_space;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400788}
789
790static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
791{
792 sk->sk_data_ready = transport->old_data_ready;
793 sk->sk_state_change = transport->old_state_change;
794 sk->sk_write_space = transport->old_write_space;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400795}
796
Chuck Leverfe315e762009-03-11 14:10:21 -0400797static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400798{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500799 struct socket *sock = transport->sock;
800 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400801
Chuck Leverfe315e762009-03-11 14:10:21 -0400802 if (sk == NULL)
803 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400804
Trond Myklebust246408d2011-03-22 18:40:10 -0400805 transport->srcport = 0;
806
Chuck Levera246b012005-08-11 16:25:23 -0400807 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500808 transport->inet = NULL;
809 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400810
Chuck Lever9903cd12005-08-11 16:25:26 -0400811 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400812
813 xs_restore_old_callbacks(transport, sk);
Chuck Levera246b012005-08-11 16:25:23 -0400814 write_unlock_bh(&sk->sk_callback_lock);
815
Chuck Lever9903cd12005-08-11 16:25:26 -0400816 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400817
Trond Myklebust40b5ea02013-09-04 12:16:23 -0400818 trace_rpc_socket_close(&transport->xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -0400819 sock_release(sock);
Chuck Leverfe315e762009-03-11 14:10:21 -0400820}
821
822/**
823 * xs_close - close a socket
824 * @xprt: transport
825 *
826 * This is used when all requests are complete; ie, no DRC state remains
827 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400828 *
829 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
830 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e762009-03-11 14:10:21 -0400831 */
832static void xs_close(struct rpc_xprt *xprt)
833{
834 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
835
836 dprintk("RPC: xs_close xprt %p\n", xprt);
837
838 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -0400839 xprt->reestablish_timeout = 0;
Chuck Leverfe315e762009-03-11 14:10:21 -0400840
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100841 smp_mb__before_clear_bit();
Trond Myklebust7d1e8252009-03-11 14:38:03 -0400842 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100843 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -0500844 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100845 smp_mb__after_clear_bit();
Trond Myklebust62da3b22007-11-06 18:44:20 -0500846 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400847}
848
Trond Myklebustf75e6742009-04-21 17:18:20 -0400849static void xs_tcp_close(struct rpc_xprt *xprt)
850{
851 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
852 xs_close(xprt);
853 else
854 xs_tcp_shutdown(xprt);
855}
856
J. Bruce Fields190b1ecf2013-03-08 13:13:08 -0500857static void xs_local_destroy(struct rpc_xprt *xprt)
858{
859 xs_close(xprt);
860 xs_free_peer_addresses(xprt);
861 xprt_free(xprt);
862 module_put(THIS_MODULE);
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
J. Bruce Fields190b1ecf2013-03-08 13:13:08 -0500878 xs_local_destroy(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400879}
880
Chuck Lever9903cd12005-08-11 16:25:26 -0400881static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400882{
Chuck Lever9903cd12005-08-11 16:25:26 -0400883 return (struct rpc_xprt *) sk->sk_user_data;
884}
885
Chuck Lever176e21e2011-05-09 15:22:44 -0400886static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
887{
888 struct xdr_skb_reader desc = {
889 .skb = skb,
890 .offset = sizeof(rpc_fraghdr),
891 .count = skb->len - sizeof(rpc_fraghdr),
892 };
893
894 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
895 return -1;
896 if (desc.count)
897 return -1;
898 return 0;
899}
900
901/**
902 * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets
903 * @sk: socket with data to read
904 * @len: how much data to read
905 *
906 * Currently this assumes we can read the whole reply in a single gulp.
907 */
908static void xs_local_data_ready(struct sock *sk, int len)
909{
910 struct rpc_task *task;
911 struct rpc_xprt *xprt;
912 struct rpc_rqst *rovr;
913 struct sk_buff *skb;
914 int err, repsize, copied;
915 u32 _xid;
916 __be32 *xp;
917
918 read_lock_bh(&sk->sk_callback_lock);
919 dprintk("RPC: %s...\n", __func__);
920 xprt = xprt_from_sock(sk);
921 if (xprt == NULL)
922 goto out;
923
924 skb = skb_recv_datagram(sk, 0, 1, &err);
925 if (skb == NULL)
926 goto out;
927
Chuck Lever176e21e2011-05-09 15:22:44 -0400928 repsize = skb->len - sizeof(rpc_fraghdr);
929 if (repsize < 4) {
930 dprintk("RPC: impossible RPC reply size %d\n", repsize);
931 goto dropit;
932 }
933
934 /* Copy the XID from the skb... */
935 xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
936 if (xp == NULL)
937 goto dropit;
938
939 /* Look up and lock the request corresponding to the given XID */
940 spin_lock(&xprt->transport_lock);
941 rovr = xprt_lookup_rqst(xprt, *xp);
942 if (!rovr)
943 goto out_unlock;
944 task = rovr->rq_task;
945
946 copied = rovr->rq_private_buf.buflen;
947 if (copied > repsize)
948 copied = repsize;
949
950 if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
951 dprintk("RPC: sk_buff copy failed\n");
952 goto out_unlock;
953 }
954
955 xprt_complete_rqst(task, copied);
956
957 out_unlock:
958 spin_unlock(&xprt->transport_lock);
959 dropit:
960 skb_free_datagram(sk, skb);
961 out:
962 read_unlock_bh(&sk->sk_callback_lock);
963}
964
Chuck Lever9903cd12005-08-11 16:25:26 -0400965/**
966 * xs_udp_data_ready - "data ready" callback for UDP sockets
967 * @sk: socket with data to read
968 * @len: how much data to read
969 *
970 */
971static void xs_udp_data_ready(struct sock *sk, int len)
972{
973 struct rpc_task *task;
974 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400975 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400976 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400977 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700978 u32 _xid;
979 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400980
Eric Dumazetf064af12010-09-22 12:43:39 +0000981 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500982 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400983 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400984 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400985
986 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
987 goto out;
988
Chuck Levera246b012005-08-11 16:25:23 -0400989 repsize = skb->len - sizeof(struct udphdr);
990 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500991 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400992 goto dropit;
993 }
994
995 /* Copy the XID from the skb... */
996 xp = skb_header_pointer(skb, sizeof(struct udphdr),
997 sizeof(_xid), &_xid);
998 if (xp == NULL)
999 goto dropit;
1000
1001 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001002 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001003 rovr = xprt_lookup_rqst(xprt, *xp);
1004 if (!rovr)
1005 goto out_unlock;
1006 task = rovr->rq_task;
1007
Chuck Levera246b012005-08-11 16:25:23 -04001008 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1009 copied = repsize;
1010
1011 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001012 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
1013 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -04001014 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001015 }
1016
1017 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -04001018
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001019 xprt_adjust_cwnd(xprt, task, copied);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001020 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -04001021
1022 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001023 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001024 dropit:
1025 skb_free_datagram(sk, skb);
1026 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001027 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001028}
1029
Trond Myklebusta519fc72012-09-12 16:49:15 -04001030/*
1031 * Helper function to force a TCP close if the server is sending
1032 * junk and/or it has put us in CLOSE_WAIT
1033 */
1034static void xs_tcp_force_close(struct rpc_xprt *xprt)
1035{
1036 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1037 xprt_force_disconnect(xprt);
1038}
1039
Chuck Leverdd456472006-12-05 16:35:44 -05001040static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001041{
Chuck Lever51971132006-12-05 16:35:19 -05001042 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001043 size_t len, used;
1044 char *p;
1045
Chuck Lever51971132006-12-05 16:35:19 -05001046 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
1047 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001048 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001049 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001050 if (used != len)
1051 return;
Chuck Lever808012f2005-08-25 16:25:49 -07001052
Chuck Lever51971132006-12-05 16:35:19 -05001053 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
1054 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -05001055 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -04001056 else
Chuck Levere136d092006-12-05 16:35:23 -05001057 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -05001058 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -07001059
Chuck Levere136d092006-12-05 16:35:23 -05001060 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001061 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -07001062
Chuck Levera246b012005-08-11 16:25:23 -04001063 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001064 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001065 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebusta519fc72012-09-12 16:49:15 -04001066 xs_tcp_force_close(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001067 return;
Chuck Levera246b012005-08-11 16:25:23 -04001068 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001069 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -05001070 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001071}
1072
Chuck Lever51971132006-12-05 16:35:19 -05001073static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001074{
Chuck Lever51971132006-12-05 16:35:19 -05001075 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001076 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001077 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001078 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
1079 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1080 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001081 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001082 }
1083 }
1084}
1085
Chuck Leverdd456472006-12-05 16:35:44 -05001086static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001087{
1088 size_t len, used;
1089 char *p;
1090
Chuck Lever51971132006-12-05 16:35:19 -05001091 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -05001092 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001093 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001094 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001095 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001096 if (used != len)
1097 return;
Chuck Levere136d092006-12-05 16:35:23 -05001098 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001099 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -05001100 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001101 dprintk("RPC: reading %s XID %08x\n",
1102 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1103 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -05001104 ntohl(transport->tcp_xid));
1105 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001106}
1107
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001108static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1109 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001110{
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001111 size_t len, used;
1112 u32 offset;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001113 char *p;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001114
1115 /*
1116 * We want transport->tcp_offset to be 8 at the end of this routine
1117 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1118 * When this function is called for the first time,
1119 * transport->tcp_offset is 4 (after having already read the xid).
1120 */
1121 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001122 len = sizeof(transport->tcp_calldir) - offset;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001123 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001124 p = ((char *) &transport->tcp_calldir) + offset;
1125 used = xdr_skb_read_bits(desc, p, len);
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001126 transport->tcp_offset += used;
1127 if (used != len)
1128 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001129 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001130 /*
1131 * We don't yet have the XDR buffer, so we will write the calldir
1132 * out after we get the buffer from the 'struct rpc_rqst'
1133 */
Trond Myklebustb76ce562010-06-16 13:57:32 -04001134 switch (ntohl(transport->tcp_calldir)) {
1135 case RPC_REPLY:
1136 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1137 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001138 transport->tcp_flags |= TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001139 break;
1140 case RPC_CALL:
1141 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1142 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001143 transport->tcp_flags &= ~TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001144 break;
1145 default:
1146 dprintk("RPC: invalid request message type\n");
Trond Myklebusta519fc72012-09-12 16:49:15 -04001147 xs_tcp_force_close(&transport->xprt);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001148 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001149 xs_tcp_check_fraghdr(transport);
1150}
1151
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001152static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
1153 struct xdr_skb_reader *desc,
1154 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001155{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001156 struct sock_xprt *transport =
1157 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001158 struct xdr_buf *rcvbuf;
1159 size_t len;
1160 ssize_t r;
1161
Chuck Levera246b012005-08-11 16:25:23 -04001162 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001163
1164 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1165 /*
1166 * Save the RPC direction in the XDR buffer
1167 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001168 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
Trond Myklebustb76ce562010-06-16 13:57:32 -04001169 &transport->tcp_calldir,
1170 sizeof(transport->tcp_calldir));
1171 transport->tcp_copied += sizeof(transport->tcp_calldir);
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001172 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -04001173 }
1174
Chuck Levera246b012005-08-11 16:25:23 -04001175 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -05001176 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -05001177 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -04001178
Chuck Lever51971132006-12-05 16:35:19 -05001179 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001180 memcpy(&my_desc, desc, sizeof(my_desc));
1181 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -05001182 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001183 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001184 desc->count -= r;
1185 desc->offset += r;
1186 } else
Chuck Lever51971132006-12-05 16:35:19 -05001187 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001188 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001189
1190 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001191 transport->tcp_copied += r;
1192 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001193 }
1194 if (r != len) {
1195 /* Error when copying to the receive buffer,
1196 * usually because we weren't able to allocate
1197 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001198 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001199 * will not receive any additional updates,
1200 * and time out.
1201 * Any remaining data from this record will
1202 * be discarded.
1203 */
Chuck Levere136d092006-12-05 16:35:23 -05001204 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001205 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001206 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001207 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1208 "tcp_offset = %u, tcp_reclen = %u\n",
1209 xprt, transport->tcp_copied,
1210 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001211 return;
Chuck Levera246b012005-08-11 16:25:23 -04001212 }
1213
Chuck Lever46121cf2007-01-31 12:14:08 -05001214 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001215 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001216 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1217 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1218 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001219
Chuck Lever51971132006-12-05 16:35:19 -05001220 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001221 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001222 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001223 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1224 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001225 }
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001226}
1227
1228/*
1229 * Finds the request corresponding to the RPC xid and invokes the common
1230 * tcp read code to read the data.
1231 */
1232static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1233 struct xdr_skb_reader *desc)
1234{
1235 struct sock_xprt *transport =
1236 container_of(xprt, struct sock_xprt, xprt);
1237 struct rpc_rqst *req;
1238
1239 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1240
1241 /* Find and lock the request corresponding to this xid */
1242 spin_lock(&xprt->transport_lock);
1243 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1244 if (!req) {
1245 dprintk("RPC: XID %08x request not found!\n",
1246 ntohl(transport->tcp_xid));
1247 spin_unlock(&xprt->transport_lock);
1248 return -1;
1249 }
1250
1251 xs_tcp_read_common(xprt, desc, req);
1252
Chuck Levere136d092006-12-05 16:35:23 -05001253 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001254 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001255
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001256 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001257 return 0;
1258}
1259
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001260#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001261/*
1262 * Obtains an rpc_rqst previously allocated and invokes the common
1263 * tcp read code to read the data. The result is placed in the callback
1264 * queue.
1265 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1266 * connection and return -1.
1267 */
1268static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1269 struct xdr_skb_reader *desc)
1270{
1271 struct sock_xprt *transport =
1272 container_of(xprt, struct sock_xprt, xprt);
1273 struct rpc_rqst *req;
1274
1275 req = xprt_alloc_bc_request(xprt);
1276 if (req == NULL) {
1277 printk(KERN_WARNING "Callback slot table overflowed\n");
1278 xprt_force_disconnect(xprt);
1279 return -1;
1280 }
1281
1282 req->rq_xid = transport->tcp_xid;
1283 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1284 xs_tcp_read_common(xprt, desc, req);
1285
1286 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1287 struct svc_serv *bc_serv = xprt->bc_serv;
1288
1289 /*
1290 * Add callback request to callback list. The callback
1291 * service sleeps on the sv_cb_waitq waiting for new
1292 * requests. Wake it up after adding enqueing the
1293 * request.
1294 */
1295 dprintk("RPC: add callback request to list\n");
1296 spin_lock(&bc_serv->sv_cb_lock);
1297 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1298 spin_unlock(&bc_serv->sv_cb_lock);
1299 wake_up(&bc_serv->sv_cb_waitq);
1300 }
1301
1302 req->rq_private_buf.len = transport->tcp_copied;
1303
1304 return 0;
1305}
1306
1307static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1308 struct xdr_skb_reader *desc)
1309{
1310 struct sock_xprt *transport =
1311 container_of(xprt, struct sock_xprt, xprt);
1312
1313 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1314 xs_tcp_read_reply(xprt, desc) :
1315 xs_tcp_read_callback(xprt, desc);
1316}
1317#else
1318static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1319 struct xdr_skb_reader *desc)
1320{
1321 return xs_tcp_read_reply(xprt, desc);
1322}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001323#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001324
1325/*
1326 * Read data off the transport. This can be either an RPC_CALL or an
1327 * RPC_REPLY. Relay the processing to helper functions.
1328 */
1329static void xs_tcp_read_data(struct rpc_xprt *xprt,
1330 struct xdr_skb_reader *desc)
1331{
1332 struct sock_xprt *transport =
1333 container_of(xprt, struct sock_xprt, xprt);
1334
1335 if (_xs_tcp_read_data(xprt, desc) == 0)
1336 xs_tcp_check_fraghdr(transport);
1337 else {
1338 /*
1339 * The transport_lock protects the request handling.
1340 * There's no need to hold it to update the tcp_flags.
1341 */
1342 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1343 }
Chuck Levera246b012005-08-11 16:25:23 -04001344}
1345
Chuck Leverdd456472006-12-05 16:35:44 -05001346static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001347{
1348 size_t len;
1349
Chuck Lever51971132006-12-05 16:35:19 -05001350 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001351 if (len > desc->count)
1352 len = desc->count;
1353 desc->count -= len;
1354 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001355 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001356 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001357 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001358}
1359
Chuck Lever9903cd12005-08-11 16:25:26 -04001360static 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 -04001361{
1362 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001363 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001364 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001365 .skb = skb,
1366 .offset = offset,
1367 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001368 };
Chuck Levera246b012005-08-11 16:25:23 -04001369
Chuck Lever46121cf2007-01-31 12:14:08 -05001370 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001371 do {
1372 /* Read in a new fragment marker if necessary */
1373 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001374 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001375 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001376 continue;
1377 }
1378 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001379 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001380 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001381 continue;
1382 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001383 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001384 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001385 xs_tcp_read_calldir(transport, &desc);
1386 continue;
1387 }
Chuck Levera246b012005-08-11 16:25:23 -04001388 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001389 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001390 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001391 continue;
1392 }
1393 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001394 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001395 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001396 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001397 return len - desc.count;
1398}
1399
Chuck Lever9903cd12005-08-11 16:25:26 -04001400/**
1401 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1402 * @sk: socket with data to read
1403 * @bytes: how much data to read
1404 *
1405 */
1406static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001407{
1408 struct rpc_xprt *xprt;
1409 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001410 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001411
Chuck Lever46121cf2007-01-31 12:14:08 -05001412 dprintk("RPC: xs_tcp_data_ready...\n");
1413
Eric Dumazetf064af12010-09-22 12:43:39 +00001414 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001415 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001416 goto out;
Neil Brown61d0a8e2009-09-23 14:36:37 -04001417 /* Any data means we had a useful conversation, so
1418 * the we don't need to delay the next reconnect
1419 */
1420 if (xprt->reestablish_timeout)
1421 xprt->reestablish_timeout = 0;
1422
Chuck Lever9903cd12005-08-11 16:25:26 -04001423 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001424 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001425 do {
1426 rd_desc.count = 65536;
1427 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1428 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001429out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001430 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001431}
1432
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001433/*
1434 * Do the equivalent of linger/linger2 handling for dealing with
1435 * broken servers that don't close the socket in a timely
1436 * fashion
1437 */
1438static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1439 unsigned long timeout)
1440{
1441 struct sock_xprt *transport;
1442
1443 if (xprt_test_and_set_connecting(xprt))
1444 return;
1445 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1446 transport = container_of(xprt, struct sock_xprt, xprt);
1447 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1448 timeout);
1449}
1450
1451static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1452{
1453 struct sock_xprt *transport;
1454
1455 transport = container_of(xprt, struct sock_xprt, xprt);
1456
1457 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1458 !cancel_delayed_work(&transport->connect_worker))
1459 return;
1460 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1461 xprt_clear_connecting(xprt);
1462}
1463
Trond Myklebust4bc1e682012-10-23 17:50:07 -04001464static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001465{
1466 smp_mb__before_clear_bit();
Trond Myklebusta519fc72012-09-12 16:49:15 -04001467 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1468 clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001469 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1470 clear_bit(XPRT_CLOSING, &xprt->state);
1471 smp_mb__after_clear_bit();
Trond Myklebust4bc1e682012-10-23 17:50:07 -04001472}
1473
1474static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1475{
1476 xs_sock_reset_connection_flags(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001477 /* Mark transport as closed and wake up all pending tasks */
1478 xprt_disconnect_done(xprt);
1479}
1480
Chuck Lever9903cd12005-08-11 16:25:26 -04001481/**
1482 * xs_tcp_state_change - callback to handle TCP socket state changes
1483 * @sk: socket whose state has changed
1484 *
1485 */
1486static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001487{
Chuck Lever9903cd12005-08-11 16:25:26 -04001488 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001489
Eric Dumazetf064af12010-09-22 12:43:39 +00001490 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001491 if (!(xprt = xprt_from_sock(sk)))
1492 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001493 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001494 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001495 sk->sk_state, xprt_connected(xprt),
1496 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001497 sock_flag(sk, SOCK_ZAPPED),
1498 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001499
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001500 trace_rpc_socket_state_change(xprt, sk->sk_socket);
Chuck Levera246b012005-08-11 16:25:23 -04001501 switch (sk->sk_state) {
1502 case TCP_ESTABLISHED:
Eric Dumazetf064af12010-09-22 12:43:39 +00001503 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001504 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001505 struct sock_xprt *transport = container_of(xprt,
1506 struct sock_xprt, xprt);
1507
Chuck Levera246b012005-08-11 16:25:23 -04001508 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001509 transport->tcp_offset = 0;
1510 transport->tcp_reclen = 0;
1511 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001512 transport->tcp_flags =
1513 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001514
Trond Myklebust2a491992009-03-11 14:38:00 -04001515 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001516 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001517 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001518 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001519 case TCP_FIN_WAIT1:
1520 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001521 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001522 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001523 set_bit(XPRT_CLOSING, &xprt->state);
1524 smp_mb__before_clear_bit();
1525 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001526 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001527 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001528 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001529 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001530 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001531 /* The server initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001532 xprt->connect_cookie++;
Trond Myklebustd0bea452012-10-23 11:35:47 -04001533 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebusta519fc72012-09-12 16:49:15 -04001534 xs_tcp_force_close(xprt);
Trond Myklebust663b8852008-01-01 18:42:12 -05001535 case TCP_CLOSING:
1536 /*
1537 * If the server closed down the connection, make sure that
1538 * we back off before reconnecting
1539 */
1540 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1541 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001542 break;
1543 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001544 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001545 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001546 smp_mb__before_clear_bit();
1547 clear_bit(XPRT_CONNECTED, &xprt->state);
1548 smp_mb__after_clear_bit();
1549 break;
1550 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001551 xs_tcp_cancel_linger_timeout(xprt);
1552 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001553 }
1554 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001555 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001556}
1557
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001558static void xs_write_space(struct sock *sk)
1559{
1560 struct socket *sock;
1561 struct rpc_xprt *xprt;
1562
1563 if (unlikely(!(sock = sk->sk_socket)))
1564 return;
1565 clear_bit(SOCK_NOSPACE, &sock->flags);
1566
1567 if (unlikely(!(xprt = xprt_from_sock(sk))))
1568 return;
1569 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1570 return;
1571
1572 xprt_write_space(xprt);
1573}
1574
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001575/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001576 * xs_udp_write_space - callback invoked when socket buffer space
1577 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001578 * @sk: socket whose state has changed
1579 *
Chuck Levera246b012005-08-11 16:25:23 -04001580 * Called when more output buffer space is available for this socket.
1581 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001582 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001583 * with a bunch of small requests.
1584 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001585static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001586{
Eric Dumazetf064af12010-09-22 12:43:39 +00001587 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001588
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001589 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001590 if (sock_writeable(sk))
1591 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001592
Eric Dumazetf064af12010-09-22 12:43:39 +00001593 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001594}
Chuck Levera246b012005-08-11 16:25:23 -04001595
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001596/**
1597 * xs_tcp_write_space - callback invoked when socket buffer space
1598 * becomes available
1599 * @sk: socket whose state has changed
1600 *
1601 * Called when more output buffer space is available for this socket.
1602 * We try not to wake our writers until they can make "significant"
1603 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1604 * with a bunch of small requests.
1605 */
1606static void xs_tcp_write_space(struct sock *sk)
1607{
Eric Dumazetf064af12010-09-22 12:43:39 +00001608 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001609
1610 /* from net/core/stream.c:sk_stream_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001611 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1612 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001613
Eric Dumazetf064af12010-09-22 12:43:39 +00001614 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001615}
1616
Chuck Lever470056c2005-08-25 16:25:56 -07001617static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001618{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001619 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1620 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001621
Chuck Lever7c6e0662006-12-05 16:35:30 -05001622 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001623 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001624 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001625 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001626 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001627 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001628 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001629 sk->sk_write_space(sk);
1630 }
1631}
1632
Chuck Lever43118c22005-08-25 16:25:49 -07001633/**
Chuck Lever470056c2005-08-25 16:25:56 -07001634 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001635 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001636 * @sndsize: requested size of send buffer, in bytes
1637 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001638 *
Chuck Lever470056c2005-08-25 16:25:56 -07001639 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001640 */
Chuck Lever470056c2005-08-25 16:25:56 -07001641static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001642{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001643 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1644
1645 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001646 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001647 transport->sndsize = sndsize + 1024;
1648 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001649 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001650 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001651
1652 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001653}
1654
Chuck Lever46c0ee82005-08-25 16:25:52 -07001655/**
1656 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1657 * @task: task that timed out
1658 *
1659 * Adjust the congestion window after a retransmit timeout has occurred.
1660 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001661static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever46c0ee82005-08-25 16:25:52 -07001662{
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001663 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
Chuck Lever46c0ee82005-08-25 16:25:52 -07001664}
1665
Chuck Leverb85d8802006-05-25 01:40:49 -04001666static unsigned short xs_get_random_port(void)
1667{
1668 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1669 unsigned short rand = (unsigned short) net_random() % range;
1670 return rand + xprt_min_resvport;
1671}
1672
Chuck Lever92200412006-01-03 09:55:51 +01001673/**
1674 * xs_set_port - reset the port number in the remote endpoint address
1675 * @xprt: generic transport
1676 * @port: new port number
1677 *
1678 */
1679static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1680{
Chuck Lever46121cf2007-01-31 12:14:08 -05001681 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001682
Chuck Lever9dc3b092009-08-09 15:09:46 -04001683 rpc_set_port(xs_addr(xprt), port);
1684 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001685}
1686
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001687static unsigned short xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001688{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001689 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001690
1691 if (port == 0 && transport->xprt.resvport)
1692 port = xs_get_random_port();
1693 return port;
1694}
1695
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001696static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001697{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001698 if (transport->srcport != 0)
1699 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001700 if (!transport->xprt.resvport)
1701 return 0;
1702 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1703 return xprt_max_resvport;
1704 return --port;
1705}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001706static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001707{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001708 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001709 int err, nloop = 0;
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001710 unsigned short port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001711 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001712
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001713 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001714 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001715 rpc_set_port((struct sockaddr *)&myaddr, port);
1716 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1717 transport->xprt.addrlen);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001718 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001719 break;
Chuck Levera246b012005-08-11 16:25:23 -04001720 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001721 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001722 break;
Chuck Levera246b012005-08-11 16:25:23 -04001723 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001724 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001725 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001726 if (port > last)
1727 nloop++;
1728 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001729
Chuck Lever4232e862010-10-20 11:52:51 -04001730 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001731 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1732 &((struct sockaddr_in *)&myaddr)->sin_addr,
1733 port, err ? "failed" : "ok", err);
1734 else
1735 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1736 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1737 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001738 return err;
1739}
1740
Chuck Lever176e21e2011-05-09 15:22:44 -04001741/*
1742 * We don't support autobind on AF_LOCAL sockets
1743 */
1744static void xs_local_rpcbind(struct rpc_task *task)
1745{
Trond Myklebust3dc0da22013-01-08 09:31:13 -05001746 rcu_read_lock();
1747 xprt_set_bound(rcu_dereference(task->tk_client->cl_xprt));
1748 rcu_read_unlock();
Chuck Lever176e21e2011-05-09 15:22:44 -04001749}
1750
1751static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1752{
1753}
Chuck Levera246b012005-08-11 16:25:23 -04001754
Peter Zijlstraed075362006-12-06 20:35:24 -08001755#ifdef CONFIG_DEBUG_LOCK_ALLOC
1756static struct lock_class_key xs_key[2];
1757static struct lock_class_key xs_slock_key[2];
1758
Chuck Lever176e21e2011-05-09 15:22:44 -04001759static inline void xs_reclassify_socketu(struct socket *sock)
1760{
1761 struct sock *sk = sock->sk;
1762
Chuck Lever176e21e2011-05-09 15:22:44 -04001763 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1764 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1765}
1766
Chuck Lever8945ee52007-08-06 11:58:04 -04001767static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001768{
1769 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001770
Chuck Lever8945ee52007-08-06 11:58:04 -04001771 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1772 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1773}
Peter Zijlstraed075362006-12-06 20:35:24 -08001774
Chuck Lever8945ee52007-08-06 11:58:04 -04001775static inline void xs_reclassify_socket6(struct socket *sock)
1776{
1777 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001778
Chuck Lever8945ee52007-08-06 11:58:04 -04001779 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1780 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001781}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001782
1783static inline void xs_reclassify_socket(int family, struct socket *sock)
1784{
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -04001785 WARN_ON_ONCE(sock_owned_by_user(sock->sk));
1786 if (sock_owned_by_user(sock->sk))
1787 return;
1788
Chuck Lever4232e862010-10-20 11:52:51 -04001789 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001790 case AF_LOCAL:
1791 xs_reclassify_socketu(sock);
1792 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001793 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001794 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001795 break;
1796 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001797 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001798 break;
1799 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001800}
Peter Zijlstraed075362006-12-06 20:35:24 -08001801#else
Chuck Lever176e21e2011-05-09 15:22:44 -04001802static inline void xs_reclassify_socketu(struct socket *sock)
1803{
1804}
1805
Chuck Lever8945ee52007-08-06 11:58:04 -04001806static inline void xs_reclassify_socket4(struct socket *sock)
1807{
1808}
1809
1810static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001811{
1812}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001813
1814static inline void xs_reclassify_socket(int family, struct socket *sock)
1815{
1816}
Peter Zijlstraed075362006-12-06 20:35:24 -08001817#endif
1818
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001819static struct socket *xs_create_sock(struct rpc_xprt *xprt,
1820 struct sock_xprt *transport, int family, int type, int protocol)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001821{
1822 struct socket *sock;
1823 int err;
1824
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001825 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001826 if (err < 0) {
1827 dprintk("RPC: can't create %d transport socket (%d).\n",
1828 protocol, -err);
1829 goto out;
1830 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001831 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001832
Ben Hutchings4cea2882011-02-22 21:54:34 +00001833 err = xs_bind(transport, sock);
1834 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001835 sock_release(sock);
1836 goto out;
1837 }
1838
1839 return sock;
1840out:
1841 return ERR_PTR(err);
1842}
1843
Chuck Lever176e21e2011-05-09 15:22:44 -04001844static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1845 struct socket *sock)
1846{
1847 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1848 xprt);
1849
1850 if (!transport->inet) {
1851 struct sock *sk = sock->sk;
1852
1853 write_lock_bh(&sk->sk_callback_lock);
1854
1855 xs_save_old_callbacks(transport, sk);
1856
1857 sk->sk_user_data = xprt;
1858 sk->sk_data_ready = xs_local_data_ready;
1859 sk->sk_write_space = xs_udp_write_space;
Chuck Lever176e21e2011-05-09 15:22:44 -04001860 sk->sk_allocation = GFP_ATOMIC;
1861
1862 xprt_clear_connected(xprt);
1863
1864 /* Reset to new socket */
1865 transport->sock = sock;
1866 transport->inet = sk;
1867
1868 write_unlock_bh(&sk->sk_callback_lock);
1869 }
1870
1871 /* Tell the socket layer to start connecting... */
1872 xprt->stat.connect_count++;
1873 xprt->stat.connect_start = jiffies;
1874 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1875}
1876
1877/**
1878 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1879 * @xprt: RPC transport to connect
1880 * @transport: socket transport to connect
1881 * @create_sock: function to create a socket of the correct type
Chuck Lever176e21e2011-05-09 15:22:44 -04001882 */
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001883static int xs_local_setup_socket(struct sock_xprt *transport)
Chuck Lever176e21e2011-05-09 15:22:44 -04001884{
Chuck Lever176e21e2011-05-09 15:22:44 -04001885 struct rpc_xprt *xprt = &transport->xprt;
1886 struct socket *sock;
1887 int status = -EIO;
1888
Jeff Layton5cf02d02012-07-23 13:58:51 -04001889 current->flags |= PF_FSTRANS;
1890
Chuck Lever176e21e2011-05-09 15:22:44 -04001891 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1892 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1893 SOCK_STREAM, 0, &sock, 1);
1894 if (status < 0) {
1895 dprintk("RPC: can't create AF_LOCAL "
1896 "transport socket (%d).\n", -status);
1897 goto out;
1898 }
1899 xs_reclassify_socketu(sock);
1900
1901 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1902 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1903
1904 status = xs_local_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001905 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever176e21e2011-05-09 15:22:44 -04001906 switch (status) {
1907 case 0:
1908 dprintk("RPC: xprt %p connected to %s\n",
1909 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1910 xprt_set_connected(xprt);
1911 break;
1912 case -ENOENT:
1913 dprintk("RPC: xprt %p: socket %s does not exist\n",
1914 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1915 break;
Trond Myklebust4a20a982012-12-15 17:02:29 -05001916 case -ECONNREFUSED:
1917 dprintk("RPC: xprt %p: connection refused for %s\n",
1918 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1919 break;
Chuck Lever176e21e2011-05-09 15:22:44 -04001920 default:
1921 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1922 __func__, -status,
1923 xprt->address_strings[RPC_DISPLAY_ADDR]);
1924 }
1925
1926out:
1927 xprt_clear_connecting(xprt);
1928 xprt_wake_pending_tasks(xprt, status);
Jeff Layton5cf02d02012-07-23 13:58:51 -04001929 current->flags &= ~PF_FSTRANS;
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001930 return status;
1931}
1932
Linus Torvaldsb6669732013-02-28 18:02:55 -08001933static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001934{
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001935 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1936 int ret;
1937
1938 if (RPC_IS_ASYNC(task)) {
1939 /*
1940 * We want the AF_LOCAL connect to be resolved in the
1941 * filesystem namespace of the process making the rpc
1942 * call. Thus we connect synchronously.
1943 *
1944 * If we want to support asynchronous AF_LOCAL calls,
1945 * we'll need to figure out how to pass a namespace to
1946 * connect.
1947 */
1948 rpc_exit(task, -ENOTCONN);
1949 return;
1950 }
1951 ret = xs_local_setup_socket(transport);
1952 if (ret && !RPC_IS_SOFTCONN(task))
1953 msleep_interruptible(15000);
Chuck Lever176e21e2011-05-09 15:22:44 -04001954}
1955
Mel Gormana564b8f2012-07-31 16:45:12 -07001956#ifdef CONFIG_SUNRPC_SWAP
1957static void xs_set_memalloc(struct rpc_xprt *xprt)
1958{
1959 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1960 xprt);
1961
1962 if (xprt->swapper)
1963 sk_set_memalloc(transport->inet);
1964}
1965
1966/**
1967 * xs_swapper - Tag this transport as being used for swap.
1968 * @xprt: transport to tag
1969 * @enable: enable/disable
1970 *
1971 */
1972int xs_swapper(struct rpc_xprt *xprt, int enable)
1973{
1974 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1975 xprt);
1976 int err = 0;
1977
1978 if (enable) {
1979 xprt->swapper++;
1980 xs_set_memalloc(xprt);
1981 } else if (xprt->swapper) {
1982 xprt->swapper--;
1983 sk_clear_memalloc(transport->inet);
1984 }
1985
1986 return err;
1987}
1988EXPORT_SYMBOL_GPL(xs_swapper);
1989#else
1990static void xs_set_memalloc(struct rpc_xprt *xprt)
1991{
1992}
1993#endif
1994
Chuck Lever16be2d22007-08-06 11:57:38 -04001995static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001996{
Chuck Lever16be2d22007-08-06 11:57:38 -04001997 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001998
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001999 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002000 struct sock *sk = sock->sk;
2001
2002 write_lock_bh(&sk->sk_callback_lock);
2003
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002004 xs_save_old_callbacks(transport, sk);
2005
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002006 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002007 sk->sk_data_ready = xs_udp_data_ready;
2008 sk->sk_write_space = xs_udp_write_space;
2009 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05002010 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002011
2012 xprt_set_connected(xprt);
2013
2014 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002015 transport->sock = sock;
2016 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002017
Mel Gormana564b8f2012-07-31 16:45:12 -07002018 xs_set_memalloc(xprt);
2019
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002020 write_unlock_bh(&sk->sk_callback_lock);
2021 }
Chuck Lever470056c2005-08-25 16:25:56 -07002022 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04002023}
2024
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002025static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04002026{
2027 struct sock_xprt *transport =
2028 container_of(work, struct sock_xprt, connect_worker.work);
2029 struct rpc_xprt *xprt = &transport->xprt;
2030 struct socket *sock = transport->sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002031 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04002032
Jeff Layton5cf02d02012-07-23 13:58:51 -04002033 current->flags |= PF_FSTRANS;
2034
Chuck Levera246b012005-08-11 16:25:23 -04002035 /* Start by resetting any existing state */
Chuck Leverfe315e762009-03-11 14:10:21 -04002036 xs_reset_transport(transport);
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002037 sock = xs_create_sock(xprt, transport,
2038 xs_addr(xprt)->sa_family, SOCK_DGRAM, IPPROTO_UDP);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002039 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04002040 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04002041
Chuck Leverc740eff2009-08-09 15:09:46 -04002042 dprintk("RPC: worker connecting xprt %p via %s to "
2043 "%s (port %s)\n", xprt,
2044 xprt->address_strings[RPC_DISPLAY_PROTO],
2045 xprt->address_strings[RPC_DISPLAY_ADDR],
2046 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04002047
2048 xs_udp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002049 trace_rpc_socket_connect(xprt, sock, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002050 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002051out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002052 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002053 xprt_wake_pending_tasks(xprt, status);
Jeff Layton5cf02d02012-07-23 13:58:51 -04002054 current->flags &= ~PF_FSTRANS;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002055}
2056
Chuck Lever3167e122005-08-25 16:25:55 -07002057/*
2058 * We need to preserve the port number so the reply cache on the server can
2059 * find our cached RPC replies when we get around to reconnecting.
2060 */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002061static void xs_abort_connection(struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07002062{
2063 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07002064 struct sockaddr any;
2065
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002066 dprintk("RPC: disconnecting xprt %p to reuse port\n", transport);
Chuck Lever3167e122005-08-25 16:25:55 -07002067
2068 /*
2069 * Disconnect the transport socket by doing a connect operation
2070 * with AF_UNSPEC. This should return immediately...
2071 */
2072 memset(&any, 0, sizeof(any));
2073 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002074 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002075 trace_rpc_socket_reset_connection(&transport->xprt,
2076 transport->sock, result);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002077 if (!result)
Trond Myklebust4bc1e682012-10-23 17:50:07 -04002078 xs_sock_reset_connection_flags(&transport->xprt);
2079 dprintk("RPC: AF_UNSPEC connect return code %d\n", result);
Chuck Lever3167e122005-08-25 16:25:55 -07002080}
2081
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002082static void xs_tcp_reuse_connection(struct sock_xprt *transport)
Trond Myklebust40d25492009-03-11 14:37:58 -04002083{
2084 unsigned int state = transport->inet->sk_state;
2085
Andy Chittenden669502f2010-08-10 10:19:53 -04002086 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) {
2087 /* we don't need to abort the connection if the socket
2088 * hasn't undergone a shutdown
2089 */
2090 if (transport->inet->sk_shutdown == 0)
2091 return;
2092 dprintk("RPC: %s: TCP_CLOSEd and sk_shutdown set to %d\n",
2093 __func__, transport->inet->sk_shutdown);
2094 }
2095 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) {
2096 /* we don't need to abort the connection if the socket
2097 * hasn't undergone a shutdown
2098 */
2099 if (transport->inet->sk_shutdown == 0)
2100 return;
2101 dprintk("RPC: %s: ESTABLISHED/SYN_SENT "
2102 "sk_shutdown set to %d\n",
2103 __func__, transport->inet->sk_shutdown);
2104 }
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002105 xs_abort_connection(transport);
Trond Myklebust40d25492009-03-11 14:37:58 -04002106}
2107
Chuck Lever16be2d22007-08-06 11:57:38 -04002108static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002109{
Chuck Lever16be2d22007-08-06 11:57:38 -04002110 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002111 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002112
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002113 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002114 struct sock *sk = sock->sk;
2115
2116 write_lock_bh(&sk->sk_callback_lock);
2117
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002118 xs_save_old_callbacks(transport, sk);
2119
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002120 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002121 sk->sk_data_ready = xs_tcp_data_ready;
2122 sk->sk_state_change = xs_tcp_state_change;
2123 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05002124 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07002125
2126 /* socket options */
2127 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
2128 sock_reset_flag(sk, SOCK_LINGER);
2129 tcp_sk(sk)->linger2 = 0;
2130 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002131
2132 xprt_clear_connected(xprt);
2133
2134 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002135 transport->sock = sock;
2136 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002137
2138 write_unlock_bh(&sk->sk_callback_lock);
2139 }
2140
Trond Myklebust01d37c42009-03-11 14:09:39 -04002141 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002142 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002143
Mel Gormana564b8f2012-07-31 16:45:12 -07002144 xs_set_memalloc(xprt);
2145
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002146 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05002147 xprt->stat.connect_count++;
2148 xprt->stat.connect_start = jiffies;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002149 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2150 switch (ret) {
2151 case 0:
2152 case -EINPROGRESS:
2153 /* SYN_SENT! */
2154 xprt->connect_cookie++;
2155 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2156 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2157 }
2158out:
2159 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002160}
2161
2162/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002163 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2164 * @xprt: RPC transport to connect
2165 * @transport: socket transport to connect
2166 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04002167 *
2168 * Invoked by a work queue tasklet.
2169 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002170static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002171{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002172 struct sock_xprt *transport =
2173 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002174 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002175 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002176 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002177
Jeff Layton5cf02d02012-07-23 13:58:51 -04002178 current->flags |= PF_FSTRANS;
2179
Chuck Lever16be2d22007-08-06 11:57:38 -04002180 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002181 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002182 sock = xs_create_sock(xprt, transport,
2183 xs_addr(xprt)->sa_family, SOCK_STREAM, IPPROTO_TCP);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002184 if (IS_ERR(sock)) {
2185 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002186 goto out;
2187 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002188 } else {
2189 int abort_and_exit;
2190
2191 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
2192 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04002193 /* "close" the socket, preserving the local port */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002194 xs_tcp_reuse_connection(transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04002195
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002196 if (abort_and_exit)
2197 goto out_eagain;
2198 }
2199
Chuck Leverc740eff2009-08-09 15:09:46 -04002200 dprintk("RPC: worker connecting xprt %p via %s to "
2201 "%s (port %s)\n", xprt,
2202 xprt->address_strings[RPC_DISPLAY_PROTO],
2203 xprt->address_strings[RPC_DISPLAY_ADDR],
2204 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002205
2206 status = xs_tcp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002207 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever46121cf2007-01-31 12:14:08 -05002208 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2209 xprt, -status, xprt_connected(xprt),
2210 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002211 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002212 default:
2213 printk("%s: connect returned unhandled error %d\n",
2214 __func__, status);
2215 case -EADDRNOTAVAIL:
2216 /* We're probably in TIME_WAIT. Get rid of existing socket,
2217 * and retry
2218 */
Trond Myklebusta519fc72012-09-12 16:49:15 -04002219 xs_tcp_force_close(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002220 break;
Trond Myklebust2a491992009-03-11 14:38:00 -04002221 case 0:
2222 case -EINPROGRESS:
2223 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002224 xprt_clear_connecting(xprt);
Jeff Layton5cf02d02012-07-23 13:58:51 -04002225 current->flags &= ~PF_FSTRANS;
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002226 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002227 case -EINVAL:
2228 /* Happens, for instance, if the user specified a link
2229 * local IPv6 address without a scope-id.
2230 */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002231 case -ECONNREFUSED:
2232 case -ECONNRESET:
2233 case -ENETUNREACH:
2234 /* retry with existing socket, after a delay */
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002235 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002236 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002237out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04002238 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002239out:
Chuck Lever2226feb2005-08-11 16:25:38 -04002240 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002241 xprt_wake_pending_tasks(xprt, status);
Jeff Layton5cf02d02012-07-23 13:58:51 -04002242 current->flags &= ~PF_FSTRANS;
Chuck Levera246b012005-08-11 16:25:23 -04002243}
2244
Chuck Lever68e220b2007-08-06 11:57:48 -04002245/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002246 * xs_connect - connect a socket to a remote endpoint
Trond Myklebust1b092092013-01-08 09:26:49 -05002247 * @xprt: pointer to transport structure
Chuck Lever9903cd12005-08-11 16:25:26 -04002248 * @task: address of RPC task that manages state of connect request
2249 *
2250 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002251 *
2252 * UDP socket connects are synchronous, but we use a work queue anyway
2253 * to guarantee that even unprivileged user processes can set up a
2254 * socket on a privileged port.
2255 *
2256 * If a UDP socket connect fails, the delay behavior here prevents
2257 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002258 */
Trond Myklebust1b092092013-01-08 09:26:49 -05002259static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002260{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002261 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002262
Chuck Lever09a21c42009-12-03 15:58:56 -05002263 if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002264 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2265 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002266 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002267 queue_delayed_work(rpciod_workqueue,
2268 &transport->connect_worker,
2269 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07002270 xprt->reestablish_timeout <<= 1;
Neil Brown61d0a8e2009-09-23 14:36:37 -04002271 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2272 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002273 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2274 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002275 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05002276 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002277 queue_delayed_work(rpciod_workqueue,
2278 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002279 }
2280}
2281
Chuck Lever262ca072006-03-20 13:44:16 -05002282/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002283 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2284 * @xprt: rpc_xprt struct containing statistics
2285 * @seq: output file
2286 *
2287 */
2288static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2289{
2290 long idle_time = 0;
2291
2292 if (xprt_connected(xprt))
2293 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2294
2295 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
Andy Adamson15a45202012-02-14 16:19:18 -05002296 "%llu %llu %lu %llu %llu\n",
Chuck Lever176e21e2011-05-09 15:22:44 -04002297 xprt->stat.bind_count,
2298 xprt->stat.connect_count,
2299 xprt->stat.connect_time,
2300 idle_time,
2301 xprt->stat.sends,
2302 xprt->stat.recvs,
2303 xprt->stat.bad_xids,
2304 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002305 xprt->stat.bklog_u,
2306 xprt->stat.max_slots,
2307 xprt->stat.sending_u,
2308 xprt->stat.pending_u);
Chuck Lever176e21e2011-05-09 15:22:44 -04002309}
2310
2311/**
Chuck Lever262ca072006-03-20 13:44:16 -05002312 * xs_udp_print_stats - display UDP socket-specifc stats
2313 * @xprt: rpc_xprt struct containing statistics
2314 * @seq: output file
2315 *
2316 */
2317static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2318{
Chuck Leverc8475462006-12-05 16:35:26 -05002319 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2320
Andy Adamson15a45202012-02-14 16:19:18 -05002321 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2322 "%lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002323 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002324 xprt->stat.bind_count,
2325 xprt->stat.sends,
2326 xprt->stat.recvs,
2327 xprt->stat.bad_xids,
2328 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002329 xprt->stat.bklog_u,
2330 xprt->stat.max_slots,
2331 xprt->stat.sending_u,
2332 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002333}
2334
2335/**
2336 * xs_tcp_print_stats - display TCP socket-specifc stats
2337 * @xprt: rpc_xprt struct containing statistics
2338 * @seq: output file
2339 *
2340 */
2341static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2342{
Chuck Leverc8475462006-12-05 16:35:26 -05002343 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002344 long idle_time = 0;
2345
2346 if (xprt_connected(xprt))
2347 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2348
Andy Adamson15a45202012-02-14 16:19:18 -05002349 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2350 "%llu %llu %lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002351 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002352 xprt->stat.bind_count,
2353 xprt->stat.connect_count,
2354 xprt->stat.connect_time,
2355 idle_time,
2356 xprt->stat.sends,
2357 xprt->stat.recvs,
2358 xprt->stat.bad_xids,
2359 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002360 xprt->stat.bklog_u,
2361 xprt->stat.max_slots,
2362 xprt->stat.sending_u,
2363 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002364}
2365
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002366/*
2367 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2368 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2369 * to use the server side send routines.
2370 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002371static void *bc_malloc(struct rpc_task *task, size_t size)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002372{
2373 struct page *page;
2374 struct rpc_buffer *buf;
2375
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002376 WARN_ON_ONCE(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2377 if (size > PAGE_SIZE - sizeof(struct rpc_buffer))
2378 return NULL;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002379
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002380 page = alloc_page(GFP_KERNEL);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002381 if (!page)
2382 return NULL;
2383
2384 buf = page_address(page);
2385 buf->len = PAGE_SIZE;
2386
2387 return buf->data;
2388}
2389
2390/*
2391 * Free the space allocated in the bc_alloc routine
2392 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002393static void bc_free(void *buffer)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002394{
2395 struct rpc_buffer *buf;
2396
2397 if (!buffer)
2398 return;
2399
2400 buf = container_of(buffer, struct rpc_buffer, data);
2401 free_page((unsigned long)buf);
2402}
2403
2404/*
2405 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2406 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2407 */
2408static int bc_sendto(struct rpc_rqst *req)
2409{
2410 int len;
2411 struct xdr_buf *xbufp = &req->rq_snd_buf;
2412 struct rpc_xprt *xprt = req->rq_xprt;
2413 struct sock_xprt *transport =
2414 container_of(xprt, struct sock_xprt, xprt);
2415 struct socket *sock = transport->sock;
2416 unsigned long headoff;
2417 unsigned long tailoff;
2418
Chuck Lever61677ee2011-05-09 15:22:34 -04002419 xs_encode_stream_record_marker(xbufp);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002420
2421 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2422 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2423 len = svc_send_common(sock, xbufp,
2424 virt_to_page(xbufp->head[0].iov_base), headoff,
2425 xbufp->tail[0].iov_base, tailoff);
2426
2427 if (len != xbufp->len) {
2428 printk(KERN_NOTICE "Error sending entire callback!\n");
2429 len = -EAGAIN;
2430 }
2431
2432 return len;
2433}
2434
2435/*
2436 * The send routine. Borrows from svc_send
2437 */
2438static int bc_send_request(struct rpc_task *task)
2439{
2440 struct rpc_rqst *req = task->tk_rqstp;
2441 struct svc_xprt *xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002442 u32 len;
2443
2444 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2445 /*
2446 * Get the server socket associated with this callback xprt
2447 */
2448 xprt = req->rq_xprt->bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002449
2450 /*
2451 * Grab the mutex to serialize data as the connection is shared
2452 * with the fore channel
2453 */
2454 if (!mutex_trylock(&xprt->xpt_mutex)) {
2455 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2456 if (!mutex_trylock(&xprt->xpt_mutex))
2457 return -EAGAIN;
2458 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2459 }
2460 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2461 len = -ENOTCONN;
2462 else
2463 len = bc_sendto(req);
2464 mutex_unlock(&xprt->xpt_mutex);
2465
2466 if (len > 0)
2467 len = 0;
2468
2469 return len;
2470}
2471
2472/*
2473 * The close routine. Since this is client initiated, we do nothing
2474 */
2475
2476static void bc_close(struct rpc_xprt *xprt)
2477{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002478}
2479
2480/*
2481 * The xprt destroy routine. Again, because this connection is client
2482 * initiated, we do nothing
2483 */
2484
2485static void bc_destroy(struct rpc_xprt *xprt)
2486{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002487}
2488
Chuck Lever176e21e2011-05-09 15:22:44 -04002489static struct rpc_xprt_ops xs_local_ops = {
2490 .reserve_xprt = xprt_reserve_xprt,
2491 .release_xprt = xs_tcp_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002492 .alloc_slot = xprt_alloc_slot,
Chuck Lever176e21e2011-05-09 15:22:44 -04002493 .rpcbind = xs_local_rpcbind,
2494 .set_port = xs_local_set_port,
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002495 .connect = xs_local_connect,
Chuck Lever176e21e2011-05-09 15:22:44 -04002496 .buf_alloc = rpc_malloc,
2497 .buf_free = rpc_free,
2498 .send_request = xs_local_send_request,
2499 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2500 .close = xs_close,
J. Bruce Fields190b1ecf2013-03-08 13:13:08 -05002501 .destroy = xs_local_destroy,
Chuck Lever176e21e2011-05-09 15:22:44 -04002502 .print_stats = xs_local_print_stats,
2503};
2504
Chuck Lever262965f2005-08-11 16:25:56 -04002505static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002506 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002507 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002508 .release_xprt = xprt_release_xprt_cong,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002509 .alloc_slot = xprt_alloc_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002510 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002511 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002512 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002513 .buf_alloc = rpc_malloc,
2514 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002515 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002516 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002517 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002518 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002519 .close = xs_close,
2520 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002521 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04002522};
2523
2524static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002525 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002526 .release_xprt = xs_tcp_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002527 .alloc_slot = xprt_lock_and_alloc_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002528 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002529 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002530 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002531 .buf_alloc = rpc_malloc,
2532 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002533 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002534 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustf75e6742009-04-21 17:18:20 -04002535 .close = xs_tcp_close,
Chuck Lever9903cd12005-08-11 16:25:26 -04002536 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002537 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002538};
2539
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002540/*
2541 * The rpc_xprt_ops for the server backchannel
2542 */
2543
2544static struct rpc_xprt_ops bc_tcp_ops = {
2545 .reserve_xprt = xprt_reserve_xprt,
2546 .release_xprt = xprt_release_xprt,
Bryan Schumaker84e28a32012-09-24 13:39:01 -04002547 .alloc_slot = xprt_alloc_slot,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002548 .buf_alloc = bc_malloc,
2549 .buf_free = bc_free,
2550 .send_request = bc_send_request,
2551 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2552 .close = bc_close,
2553 .destroy = bc_destroy,
2554 .print_stats = xs_tcp_print_stats,
2555};
2556
Chuck Lever92476852010-10-20 11:53:01 -04002557static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2558{
2559 static const struct sockaddr_in sin = {
2560 .sin_family = AF_INET,
2561 .sin_addr.s_addr = htonl(INADDR_ANY),
2562 };
2563 static const struct sockaddr_in6 sin6 = {
2564 .sin6_family = AF_INET6,
2565 .sin6_addr = IN6ADDR_ANY_INIT,
2566 };
2567
2568 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002569 case AF_LOCAL:
2570 break;
Chuck Lever92476852010-10-20 11:53:01 -04002571 case AF_INET:
2572 memcpy(sap, &sin, sizeof(sin));
2573 break;
2574 case AF_INET6:
2575 memcpy(sap, &sin6, sizeof(sin6));
2576 break;
2577 default:
2578 dprintk("RPC: %s: Bad address family\n", __func__);
2579 return -EAFNOSUPPORT;
2580 }
2581 return 0;
2582}
2583
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002584static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002585 unsigned int slot_table_size,
2586 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002587{
2588 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002589 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002590
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002591 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002592 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002593 return ERR_PTR(-EBADF);
2594 }
2595
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002596 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2597 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002598 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002599 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2600 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002601 return ERR_PTR(-ENOMEM);
2602 }
2603
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002604 new = container_of(xprt, struct sock_xprt, xprt);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002605 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2606 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002607 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002608 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002609 else {
2610 int err;
2611 err = xs_init_anyaddr(args->dstaddr->sa_family,
2612 (struct sockaddr *)&new->srcaddr);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002613 if (err != 0) {
2614 xprt_free(xprt);
Chuck Lever92476852010-10-20 11:53:01 -04002615 return ERR_PTR(err);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002616 }
Chuck Lever92476852010-10-20 11:53:01 -04002617 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002618
2619 return xprt;
2620}
2621
Chuck Lever176e21e2011-05-09 15:22:44 -04002622static const struct rpc_timeout xs_local_default_timeout = {
2623 .to_initval = 10 * HZ,
2624 .to_maxval = 10 * HZ,
2625 .to_retries = 2,
2626};
2627
2628/**
2629 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2630 * @args: rpc transport creation arguments
2631 *
2632 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2633 */
2634static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2635{
2636 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2637 struct sock_xprt *transport;
2638 struct rpc_xprt *xprt;
2639 struct rpc_xprt *ret;
2640
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002641 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2642 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002643 if (IS_ERR(xprt))
2644 return xprt;
2645 transport = container_of(xprt, struct sock_xprt, xprt);
2646
2647 xprt->prot = 0;
2648 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2649 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2650
2651 xprt->bind_timeout = XS_BIND_TO;
2652 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2653 xprt->idle_timeout = XS_IDLE_DISC_TO;
2654
2655 xprt->ops = &xs_local_ops;
2656 xprt->timeout = &xs_local_default_timeout;
2657
2658 switch (sun->sun_family) {
2659 case AF_LOCAL:
2660 if (sun->sun_path[0] != '/') {
2661 dprintk("RPC: bad AF_LOCAL address: %s\n",
2662 sun->sun_path);
2663 ret = ERR_PTR(-EINVAL);
2664 goto out_err;
2665 }
2666 xprt_set_bound(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002667 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
J. Bruce Fields7073ea82013-02-21 10:14:22 -05002668 ret = ERR_PTR(xs_local_setup_socket(transport));
2669 if (ret)
2670 goto out_err;
Chuck Lever176e21e2011-05-09 15:22:44 -04002671 break;
2672 default:
2673 ret = ERR_PTR(-EAFNOSUPPORT);
2674 goto out_err;
2675 }
2676
2677 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2678 xprt->address_strings[RPC_DISPLAY_ADDR]);
2679
2680 if (try_module_get(THIS_MODULE))
2681 return xprt;
2682 ret = ERR_PTR(-EINVAL);
2683out_err:
2684 xprt_free(xprt);
2685 return ret;
2686}
2687
Trond Myklebust2881ae72007-12-20 16:03:54 -05002688static const struct rpc_timeout xs_udp_default_timeout = {
2689 .to_initval = 5 * HZ,
2690 .to_maxval = 30 * HZ,
2691 .to_increment = 5 * HZ,
2692 .to_retries = 5,
2693};
2694
Chuck Lever9903cd12005-08-11 16:25:26 -04002695/**
2696 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002697 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002698 *
2699 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002700static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002701{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002702 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002703 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002704 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002705 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002706
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002707 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2708 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002709 if (IS_ERR(xprt))
2710 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002711 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002712
Chuck Leverec739ef2006-08-22 20:06:15 -04002713 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002714 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002715 /* XXX: header size can vary due to auth type, IPv6, etc. */
2716 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2717
Chuck Lever03bf4b72005-08-25 16:25:55 -07002718 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002719 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2720 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002721
Chuck Lever262965f2005-08-11 16:25:56 -04002722 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002723
Trond Myklebustba7392b2007-12-20 16:03:55 -05002724 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002725
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002726 switch (addr->sa_family) {
2727 case AF_INET:
2728 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2729 xprt_set_bound(xprt);
2730
2731 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002732 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002733 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002734 break;
2735 case AF_INET6:
2736 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2737 xprt_set_bound(xprt);
2738
2739 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002740 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002741 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002742 break;
2743 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002744 ret = ERR_PTR(-EAFNOSUPPORT);
2745 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002746 }
2747
Chuck Leverc740eff2009-08-09 15:09:46 -04002748 if (xprt_bound(xprt))
2749 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2750 xprt->address_strings[RPC_DISPLAY_ADDR],
2751 xprt->address_strings[RPC_DISPLAY_PORT],
2752 xprt->address_strings[RPC_DISPLAY_PROTO]);
2753 else
2754 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2755 xprt->address_strings[RPC_DISPLAY_ADDR],
2756 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002757
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002758 if (try_module_get(THIS_MODULE))
2759 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002760 ret = ERR_PTR(-EINVAL);
2761out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002762 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002763 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002764}
2765
Trond Myklebust2881ae72007-12-20 16:03:54 -05002766static const struct rpc_timeout xs_tcp_default_timeout = {
2767 .to_initval = 60 * HZ,
2768 .to_maxval = 60 * HZ,
2769 .to_retries = 2,
2770};
2771
Chuck Lever9903cd12005-08-11 16:25:26 -04002772/**
2773 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002774 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002775 *
2776 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002777static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002778{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002779 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002780 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002781 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002782 struct rpc_xprt *ret;
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002783 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2784
2785 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2786 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Levera246b012005-08-11 16:25:23 -04002787
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002788 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002789 max_slot_table_size);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002790 if (IS_ERR(xprt))
2791 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002792 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002793
Chuck Leverec739ef2006-08-22 20:06:15 -04002794 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002795 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002796 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002797
Chuck Lever03bf4b72005-08-25 16:25:55 -07002798 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002799 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2800 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002801
Chuck Lever262965f2005-08-11 16:25:56 -04002802 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002803 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002804
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002805 switch (addr->sa_family) {
2806 case AF_INET:
2807 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2808 xprt_set_bound(xprt);
2809
Chuck Lever9dc3b092009-08-09 15:09:46 -04002810 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002811 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002812 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002813 break;
2814 case AF_INET6:
2815 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2816 xprt_set_bound(xprt);
2817
Chuck Lever9dc3b092009-08-09 15:09:46 -04002818 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002819 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002820 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002821 break;
2822 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002823 ret = ERR_PTR(-EAFNOSUPPORT);
2824 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002825 }
2826
Chuck Leverc740eff2009-08-09 15:09:46 -04002827 if (xprt_bound(xprt))
2828 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2829 xprt->address_strings[RPC_DISPLAY_ADDR],
2830 xprt->address_strings[RPC_DISPLAY_PORT],
2831 xprt->address_strings[RPC_DISPLAY_PROTO]);
2832 else
2833 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2834 xprt->address_strings[RPC_DISPLAY_ADDR],
2835 xprt->address_strings[RPC_DISPLAY_PROTO]);
2836
Chuck Leveredb267a2006-08-22 20:06:18 -04002837
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002838 if (try_module_get(THIS_MODULE))
2839 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002840 ret = ERR_PTR(-EINVAL);
2841out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002842 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002843 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002844}
Chuck Lever282b32e2006-12-05 16:35:51 -05002845
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002846/**
2847 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2848 * @args: rpc transport creation arguments
2849 *
2850 */
2851static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2852{
2853 struct sockaddr *addr = args->dstaddr;
2854 struct rpc_xprt *xprt;
2855 struct sock_xprt *transport;
2856 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002857 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002858
J. Bruce Fieldsf0418aa2010-12-08 13:48:19 -05002859 if (args->bc_xprt->xpt_bc_xprt) {
2860 /*
2861 * This server connection already has a backchannel
2862 * export; we can't create a new one, as we wouldn't be
2863 * able to match replies based on xid any more. So,
2864 * reuse the already-existing one:
2865 */
2866 return args->bc_xprt->xpt_bc_xprt;
2867 }
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002868 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2869 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002870 if (IS_ERR(xprt))
2871 return xprt;
2872 transport = container_of(xprt, struct sock_xprt, xprt);
2873
2874 xprt->prot = IPPROTO_TCP;
2875 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2876 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2877 xprt->timeout = &xs_tcp_default_timeout;
2878
2879 /* backchannel */
2880 xprt_set_bound(xprt);
2881 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002882 xprt->reestablish_timeout = 0;
2883 xprt->idle_timeout = 0;
2884
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002885 xprt->ops = &bc_tcp_ops;
2886
2887 switch (addr->sa_family) {
2888 case AF_INET:
2889 xs_format_peer_addresses(xprt, "tcp",
2890 RPCBIND_NETID_TCP);
2891 break;
2892 case AF_INET6:
2893 xs_format_peer_addresses(xprt, "tcp",
2894 RPCBIND_NETID_TCP6);
2895 break;
2896 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002897 ret = ERR_PTR(-EAFNOSUPPORT);
2898 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002899 }
2900
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04002901 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2902 xprt->address_strings[RPC_DISPLAY_ADDR],
2903 xprt->address_strings[RPC_DISPLAY_PORT],
2904 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002905
2906 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002907 * Once we've associated a backchannel xprt with a connection,
2908 * we want to keep it around as long as long as the connection
2909 * lasts, in case we need to start using it for a backchannel
2910 * again; this reference won't be dropped until bc_xprt is
2911 * destroyed.
2912 */
2913 xprt_get(xprt);
2914 args->bc_xprt->xpt_bc_xprt = xprt;
2915 xprt->bc_xprt = args->bc_xprt;
2916 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2917 transport->sock = bc_sock->sk_sock;
2918 transport->inet = bc_sock->sk_sk;
2919
2920 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002921 * Since we don't want connections for the backchannel, we set
2922 * the xprt status to connected
2923 */
2924 xprt_set_connected(xprt);
2925
2926
2927 if (try_module_get(THIS_MODULE))
2928 return xprt;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002929 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002930 ret = ERR_PTR(-EINVAL);
2931out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002932 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002933 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002934}
2935
Chuck Lever176e21e2011-05-09 15:22:44 -04002936static struct xprt_class xs_local_transport = {
2937 .list = LIST_HEAD_INIT(xs_local_transport.list),
2938 .name = "named UNIX socket",
2939 .owner = THIS_MODULE,
2940 .ident = XPRT_TRANSPORT_LOCAL,
2941 .setup = xs_setup_local,
2942};
2943
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002944static struct xprt_class xs_udp_transport = {
2945 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2946 .name = "udp",
2947 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002948 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002949 .setup = xs_setup_udp,
2950};
2951
2952static struct xprt_class xs_tcp_transport = {
2953 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2954 .name = "tcp",
2955 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002956 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002957 .setup = xs_setup_tcp,
2958};
2959
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002960static struct xprt_class xs_bc_tcp_transport = {
2961 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2962 .name = "tcp NFSv4.1 backchannel",
2963 .owner = THIS_MODULE,
2964 .ident = XPRT_TRANSPORT_BC_TCP,
2965 .setup = xs_setup_bc_tcp,
2966};
2967
Chuck Lever282b32e2006-12-05 16:35:51 -05002968/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002969 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002970 *
2971 */
2972int init_socket_xprt(void)
2973{
Chuck Leverfbf76682006-12-05 16:35:54 -05002974#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002975 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002976 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002977#endif
2978
Chuck Lever176e21e2011-05-09 15:22:44 -04002979 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002980 xprt_register_transport(&xs_udp_transport);
2981 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002982 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002983
Chuck Lever282b32e2006-12-05 16:35:51 -05002984 return 0;
2985}
2986
2987/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002988 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05002989 *
2990 */
2991void cleanup_socket_xprt(void)
2992{
Chuck Leverfbf76682006-12-05 16:35:54 -05002993#ifdef RPC_DEBUG
2994 if (sunrpc_table_header) {
2995 unregister_sysctl_table(sunrpc_table_header);
2996 sunrpc_table_header = NULL;
2997 }
2998#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002999
Chuck Lever176e21e2011-05-09 15:22:44 -04003000 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003001 xprt_unregister_transport(&xs_udp_transport);
3002 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003003 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05003004}
Trond Myklebustcbf11072009-08-09 15:06:19 -04003005
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003006static int param_set_uint_minmax(const char *val,
3007 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003008 unsigned int min, unsigned int max)
3009{
3010 unsigned long num;
3011 int ret;
3012
3013 if (!val)
3014 return -EINVAL;
3015 ret = strict_strtoul(val, 0, &num);
3016 if (ret == -EINVAL || num < min || num > max)
3017 return -EINVAL;
3018 *((unsigned int *)kp->arg) = num;
3019 return 0;
3020}
3021
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003022static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003023{
3024 return param_set_uint_minmax(val, kp,
3025 RPC_MIN_RESVPORT,
3026 RPC_MAX_RESVPORT);
3027}
3028
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003029static struct kernel_param_ops param_ops_portnr = {
3030 .set = param_set_portnr,
3031 .get = param_get_uint,
3032};
3033
Trond Myklebustcbf11072009-08-09 15:06:19 -04003034#define param_check_portnr(name, p) \
3035 __param_check(name, p, unsigned int);
3036
3037module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3038module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3039
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003040static int param_set_slot_table_size(const char *val,
3041 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003042{
3043 return param_set_uint_minmax(val, kp,
3044 RPC_MIN_SLOT_TABLE,
3045 RPC_MAX_SLOT_TABLE);
3046}
3047
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003048static struct kernel_param_ops param_ops_slot_table_size = {
3049 .set = param_set_slot_table_size,
3050 .get = param_get_uint,
3051};
3052
Trond Myklebustcbf11072009-08-09 15:06:19 -04003053#define param_check_slot_table_size(name, p) \
3054 __param_check(name, p, unsigned int);
3055
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003056static int param_set_max_slot_table_size(const char *val,
3057 const struct kernel_param *kp)
3058{
3059 return param_set_uint_minmax(val, kp,
3060 RPC_MIN_SLOT_TABLE,
3061 RPC_MAX_SLOT_TABLE_LIMIT);
3062}
3063
3064static struct kernel_param_ops param_ops_max_slot_table_size = {
3065 .set = param_set_max_slot_table_size,
3066 .get = param_get_uint,
3067};
3068
3069#define param_check_max_slot_table_size(name, p) \
3070 __param_check(name, p, unsigned int);
3071
Trond Myklebustcbf11072009-08-09 15:06:19 -04003072module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3073 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003074module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3075 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003076module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3077 slot_table_size, 0644);
3078