blob: 17c88928b7db0abe001697867ead944bf6621712 [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 Myklebust43cedbf2011-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 Leverfe315e72009-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 Leverfe315e72009-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 Leverfe315e72009-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 Leverfe315e72009-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
NeilBrown93dc41b2013-10-31 16:14:36 +1100838 cancel_delayed_work_sync(&transport->connect_worker);
839
Chuck Leverfe315e72009-03-11 14:10:21 -0400840 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -0400841 xprt->reestablish_timeout = 0;
Chuck Leverfe315e72009-03-11 14:10:21 -0400842
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100843 smp_mb__before_clear_bit();
Trond Myklebust7d1e8252009-03-11 14:38:03 -0400844 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100845 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -0500846 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100847 smp_mb__after_clear_bit();
Trond Myklebust62da3b22007-11-06 18:44:20 -0500848 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400849}
850
Trond Myklebustf75e6742009-04-21 17:18:20 -0400851static void xs_tcp_close(struct rpc_xprt *xprt)
852{
853 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
854 xs_close(xprt);
855 else
856 xs_tcp_shutdown(xprt);
857}
858
Chuck Lever9903cd12005-08-11 16:25:26 -0400859/**
860 * xs_destroy - prepare to shutdown a transport
861 * @xprt: doomed transport
862 *
863 */
864static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400865{
Chuck Lever46121cf2007-01-31 12:14:08 -0500866 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400867
Trond Myklebusta1311d82013-10-31 09:18:49 -0400868 xs_close(xprt);
869 xs_free_peer_addresses(xprt);
870 xprt_free(xprt);
871 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400872}
873
Chuck Lever9903cd12005-08-11 16:25:26 -0400874static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400875{
Chuck Lever9903cd12005-08-11 16:25:26 -0400876 return (struct rpc_xprt *) sk->sk_user_data;
877}
878
Chuck Lever176e21e2011-05-09 15:22:44 -0400879static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
880{
881 struct xdr_skb_reader desc = {
882 .skb = skb,
883 .offset = sizeof(rpc_fraghdr),
884 .count = skb->len - sizeof(rpc_fraghdr),
885 };
886
887 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
888 return -1;
889 if (desc.count)
890 return -1;
891 return 0;
892}
893
894/**
895 * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets
896 * @sk: socket with data to read
897 * @len: how much data to read
898 *
899 * Currently this assumes we can read the whole reply in a single gulp.
900 */
901static void xs_local_data_ready(struct sock *sk, int len)
902{
903 struct rpc_task *task;
904 struct rpc_xprt *xprt;
905 struct rpc_rqst *rovr;
906 struct sk_buff *skb;
907 int err, repsize, copied;
908 u32 _xid;
909 __be32 *xp;
910
911 read_lock_bh(&sk->sk_callback_lock);
912 dprintk("RPC: %s...\n", __func__);
913 xprt = xprt_from_sock(sk);
914 if (xprt == NULL)
915 goto out;
916
917 skb = skb_recv_datagram(sk, 0, 1, &err);
918 if (skb == NULL)
919 goto out;
920
Chuck Lever176e21e2011-05-09 15:22:44 -0400921 repsize = skb->len - sizeof(rpc_fraghdr);
922 if (repsize < 4) {
923 dprintk("RPC: impossible RPC reply size %d\n", repsize);
924 goto dropit;
925 }
926
927 /* Copy the XID from the skb... */
928 xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
929 if (xp == NULL)
930 goto dropit;
931
932 /* Look up and lock the request corresponding to the given XID */
933 spin_lock(&xprt->transport_lock);
934 rovr = xprt_lookup_rqst(xprt, *xp);
935 if (!rovr)
936 goto out_unlock;
937 task = rovr->rq_task;
938
939 copied = rovr->rq_private_buf.buflen;
940 if (copied > repsize)
941 copied = repsize;
942
943 if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
944 dprintk("RPC: sk_buff copy failed\n");
945 goto out_unlock;
946 }
947
948 xprt_complete_rqst(task, copied);
949
950 out_unlock:
951 spin_unlock(&xprt->transport_lock);
952 dropit:
953 skb_free_datagram(sk, skb);
954 out:
955 read_unlock_bh(&sk->sk_callback_lock);
956}
957
Chuck Lever9903cd12005-08-11 16:25:26 -0400958/**
959 * xs_udp_data_ready - "data ready" callback for UDP sockets
960 * @sk: socket with data to read
961 * @len: how much data to read
962 *
963 */
964static void xs_udp_data_ready(struct sock *sk, int len)
965{
966 struct rpc_task *task;
967 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400968 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400969 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400970 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700971 u32 _xid;
972 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400973
Eric Dumazetf064af12010-09-22 12:43:39 +0000974 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500975 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400976 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400977 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400978
979 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
980 goto out;
981
Chuck Levera246b012005-08-11 16:25:23 -0400982 repsize = skb->len - sizeof(struct udphdr);
983 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500984 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400985 goto dropit;
986 }
987
988 /* Copy the XID from the skb... */
989 xp = skb_header_pointer(skb, sizeof(struct udphdr),
990 sizeof(_xid), &_xid);
991 if (xp == NULL)
992 goto dropit;
993
994 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400995 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400996 rovr = xprt_lookup_rqst(xprt, *xp);
997 if (!rovr)
998 goto out_unlock;
999 task = rovr->rq_task;
1000
Chuck Levera246b012005-08-11 16:25:23 -04001001 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1002 copied = repsize;
1003
1004 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001005 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
1006 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -04001007 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001008 }
1009
1010 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -04001011
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001012 xprt_adjust_cwnd(xprt, task, copied);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001013 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -04001014
1015 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001016 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001017 dropit:
1018 skb_free_datagram(sk, skb);
1019 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001020 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001021}
1022
Trond Myklebusta519fc72012-09-12 16:49:15 -04001023/*
1024 * Helper function to force a TCP close if the server is sending
1025 * junk and/or it has put us in CLOSE_WAIT
1026 */
1027static void xs_tcp_force_close(struct rpc_xprt *xprt)
1028{
1029 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1030 xprt_force_disconnect(xprt);
1031}
1032
Chuck Leverdd456472006-12-05 16:35:44 -05001033static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001034{
Chuck Lever51971132006-12-05 16:35:19 -05001035 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001036 size_t len, used;
1037 char *p;
1038
Chuck Lever51971132006-12-05 16:35:19 -05001039 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
1040 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001041 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001042 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001043 if (used != len)
1044 return;
Chuck Lever808012f2005-08-25 16:25:49 -07001045
Chuck Lever51971132006-12-05 16:35:19 -05001046 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
1047 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -05001048 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -04001049 else
Chuck Levere136d092006-12-05 16:35:23 -05001050 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -05001051 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -07001052
Chuck Levere136d092006-12-05 16:35:23 -05001053 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001054 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -07001055
Chuck Levera246b012005-08-11 16:25:23 -04001056 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001057 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001058 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebusta519fc72012-09-12 16:49:15 -04001059 xs_tcp_force_close(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001060 return;
Chuck Levera246b012005-08-11 16:25:23 -04001061 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001062 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -05001063 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001064}
1065
Chuck Lever51971132006-12-05 16:35:19 -05001066static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001067{
Chuck Lever51971132006-12-05 16:35:19 -05001068 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001069 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001070 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001071 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
1072 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1073 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001074 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001075 }
1076 }
1077}
1078
Chuck Leverdd456472006-12-05 16:35:44 -05001079static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001080{
1081 size_t len, used;
1082 char *p;
1083
Chuck Lever51971132006-12-05 16:35:19 -05001084 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -05001085 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001086 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001087 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001088 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001089 if (used != len)
1090 return;
Chuck Levere136d092006-12-05 16:35:23 -05001091 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001092 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -05001093 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001094 dprintk("RPC: reading %s XID %08x\n",
1095 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1096 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -05001097 ntohl(transport->tcp_xid));
1098 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001099}
1100
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001101static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1102 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001103{
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001104 size_t len, used;
1105 u32 offset;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001106 char *p;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001107
1108 /*
1109 * We want transport->tcp_offset to be 8 at the end of this routine
1110 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1111 * When this function is called for the first time,
1112 * transport->tcp_offset is 4 (after having already read the xid).
1113 */
1114 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001115 len = sizeof(transport->tcp_calldir) - offset;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001116 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001117 p = ((char *) &transport->tcp_calldir) + offset;
1118 used = xdr_skb_read_bits(desc, p, len);
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001119 transport->tcp_offset += used;
1120 if (used != len)
1121 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001122 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001123 /*
1124 * We don't yet have the XDR buffer, so we will write the calldir
1125 * out after we get the buffer from the 'struct rpc_rqst'
1126 */
Trond Myklebustb76ce562010-06-16 13:57:32 -04001127 switch (ntohl(transport->tcp_calldir)) {
1128 case RPC_REPLY:
1129 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1130 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001131 transport->tcp_flags |= TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001132 break;
1133 case RPC_CALL:
1134 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1135 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001136 transport->tcp_flags &= ~TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001137 break;
1138 default:
1139 dprintk("RPC: invalid request message type\n");
Trond Myklebusta519fc72012-09-12 16:49:15 -04001140 xs_tcp_force_close(&transport->xprt);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001141 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001142 xs_tcp_check_fraghdr(transport);
1143}
1144
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001145static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
1146 struct xdr_skb_reader *desc,
1147 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001148{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001149 struct sock_xprt *transport =
1150 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001151 struct xdr_buf *rcvbuf;
1152 size_t len;
1153 ssize_t r;
1154
Chuck Levera246b012005-08-11 16:25:23 -04001155 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001156
1157 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1158 /*
1159 * Save the RPC direction in the XDR buffer
1160 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001161 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
Trond Myklebustb76ce562010-06-16 13:57:32 -04001162 &transport->tcp_calldir,
1163 sizeof(transport->tcp_calldir));
1164 transport->tcp_copied += sizeof(transport->tcp_calldir);
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001165 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -04001166 }
1167
Chuck Levera246b012005-08-11 16:25:23 -04001168 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -05001169 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -05001170 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -04001171
Chuck Lever51971132006-12-05 16:35:19 -05001172 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001173 memcpy(&my_desc, desc, sizeof(my_desc));
1174 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -05001175 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001176 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001177 desc->count -= r;
1178 desc->offset += r;
1179 } else
Chuck Lever51971132006-12-05 16:35:19 -05001180 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001181 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001182
1183 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001184 transport->tcp_copied += r;
1185 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001186 }
1187 if (r != len) {
1188 /* Error when copying to the receive buffer,
1189 * usually because we weren't able to allocate
1190 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001191 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001192 * will not receive any additional updates,
1193 * and time out.
1194 * Any remaining data from this record will
1195 * be discarded.
1196 */
Chuck Levere136d092006-12-05 16:35:23 -05001197 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001198 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001199 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001200 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1201 "tcp_offset = %u, tcp_reclen = %u\n",
1202 xprt, transport->tcp_copied,
1203 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001204 return;
Chuck Levera246b012005-08-11 16:25:23 -04001205 }
1206
Chuck Lever46121cf2007-01-31 12:14:08 -05001207 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001208 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001209 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1210 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1211 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001212
Chuck Lever51971132006-12-05 16:35:19 -05001213 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001214 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001215 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001216 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1217 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001218 }
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001219}
1220
1221/*
1222 * Finds the request corresponding to the RPC xid and invokes the common
1223 * tcp read code to read the data.
1224 */
1225static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1226 struct xdr_skb_reader *desc)
1227{
1228 struct sock_xprt *transport =
1229 container_of(xprt, struct sock_xprt, xprt);
1230 struct rpc_rqst *req;
1231
1232 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1233
1234 /* Find and lock the request corresponding to this xid */
1235 spin_lock(&xprt->transport_lock);
1236 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1237 if (!req) {
1238 dprintk("RPC: XID %08x request not found!\n",
1239 ntohl(transport->tcp_xid));
1240 spin_unlock(&xprt->transport_lock);
1241 return -1;
1242 }
1243
1244 xs_tcp_read_common(xprt, desc, req);
1245
Chuck Levere136d092006-12-05 16:35:23 -05001246 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001247 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001248
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001249 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001250 return 0;
1251}
1252
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001253#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001254/*
1255 * Obtains an rpc_rqst previously allocated and invokes the common
1256 * tcp read code to read the data. The result is placed in the callback
1257 * queue.
1258 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1259 * connection and return -1.
1260 */
1261static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1262 struct xdr_skb_reader *desc)
1263{
1264 struct sock_xprt *transport =
1265 container_of(xprt, struct sock_xprt, xprt);
1266 struct rpc_rqst *req;
1267
1268 req = xprt_alloc_bc_request(xprt);
1269 if (req == NULL) {
1270 printk(KERN_WARNING "Callback slot table overflowed\n");
1271 xprt_force_disconnect(xprt);
1272 return -1;
1273 }
1274
1275 req->rq_xid = transport->tcp_xid;
1276 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1277 xs_tcp_read_common(xprt, desc, req);
1278
1279 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1280 struct svc_serv *bc_serv = xprt->bc_serv;
1281
1282 /*
1283 * Add callback request to callback list. The callback
1284 * service sleeps on the sv_cb_waitq waiting for new
1285 * requests. Wake it up after adding enqueing the
1286 * request.
1287 */
1288 dprintk("RPC: add callback request to list\n");
1289 spin_lock(&bc_serv->sv_cb_lock);
1290 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1291 spin_unlock(&bc_serv->sv_cb_lock);
1292 wake_up(&bc_serv->sv_cb_waitq);
1293 }
1294
1295 req->rq_private_buf.len = transport->tcp_copied;
1296
1297 return 0;
1298}
1299
1300static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1301 struct xdr_skb_reader *desc)
1302{
1303 struct sock_xprt *transport =
1304 container_of(xprt, struct sock_xprt, xprt);
1305
1306 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1307 xs_tcp_read_reply(xprt, desc) :
1308 xs_tcp_read_callback(xprt, desc);
1309}
1310#else
1311static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1312 struct xdr_skb_reader *desc)
1313{
1314 return xs_tcp_read_reply(xprt, desc);
1315}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001316#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001317
1318/*
1319 * Read data off the transport. This can be either an RPC_CALL or an
1320 * RPC_REPLY. Relay the processing to helper functions.
1321 */
1322static void xs_tcp_read_data(struct rpc_xprt *xprt,
1323 struct xdr_skb_reader *desc)
1324{
1325 struct sock_xprt *transport =
1326 container_of(xprt, struct sock_xprt, xprt);
1327
1328 if (_xs_tcp_read_data(xprt, desc) == 0)
1329 xs_tcp_check_fraghdr(transport);
1330 else {
1331 /*
1332 * The transport_lock protects the request handling.
1333 * There's no need to hold it to update the tcp_flags.
1334 */
1335 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1336 }
Chuck Levera246b012005-08-11 16:25:23 -04001337}
1338
Chuck Leverdd456472006-12-05 16:35:44 -05001339static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001340{
1341 size_t len;
1342
Chuck Lever51971132006-12-05 16:35:19 -05001343 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001344 if (len > desc->count)
1345 len = desc->count;
1346 desc->count -= len;
1347 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001348 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001349 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001350 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001351}
1352
Chuck Lever9903cd12005-08-11 16:25:26 -04001353static 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 -04001354{
1355 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001356 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001357 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001358 .skb = skb,
1359 .offset = offset,
1360 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001361 };
Chuck Levera246b012005-08-11 16:25:23 -04001362
Chuck Lever46121cf2007-01-31 12:14:08 -05001363 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001364 do {
1365 /* Read in a new fragment marker if necessary */
1366 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001367 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001368 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001369 continue;
1370 }
1371 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001372 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001373 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001374 continue;
1375 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001376 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001377 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001378 xs_tcp_read_calldir(transport, &desc);
1379 continue;
1380 }
Chuck Levera246b012005-08-11 16:25:23 -04001381 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001382 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001383 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001384 continue;
1385 }
1386 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001387 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001388 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001389 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001390 return len - desc.count;
1391}
1392
Chuck Lever9903cd12005-08-11 16:25:26 -04001393/**
1394 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1395 * @sk: socket with data to read
1396 * @bytes: how much data to read
1397 *
1398 */
1399static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001400{
1401 struct rpc_xprt *xprt;
1402 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001403 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001404
Chuck Lever46121cf2007-01-31 12:14:08 -05001405 dprintk("RPC: xs_tcp_data_ready...\n");
1406
Eric Dumazetf064af12010-09-22 12:43:39 +00001407 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001408 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001409 goto out;
Neil Brown61d0a8e2009-09-23 14:36:37 -04001410 /* Any data means we had a useful conversation, so
1411 * the we don't need to delay the next reconnect
1412 */
1413 if (xprt->reestablish_timeout)
1414 xprt->reestablish_timeout = 0;
1415
Chuck Lever9903cd12005-08-11 16:25:26 -04001416 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001417 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001418 do {
1419 rd_desc.count = 65536;
1420 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1421 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001422out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001423 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001424}
1425
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001426/*
1427 * Do the equivalent of linger/linger2 handling for dealing with
1428 * broken servers that don't close the socket in a timely
1429 * fashion
1430 */
1431static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1432 unsigned long timeout)
1433{
1434 struct sock_xprt *transport;
1435
1436 if (xprt_test_and_set_connecting(xprt))
1437 return;
1438 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1439 transport = container_of(xprt, struct sock_xprt, xprt);
1440 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1441 timeout);
1442}
1443
1444static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1445{
1446 struct sock_xprt *transport;
1447
1448 transport = container_of(xprt, struct sock_xprt, xprt);
1449
1450 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1451 !cancel_delayed_work(&transport->connect_worker))
1452 return;
1453 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1454 xprt_clear_connecting(xprt);
1455}
1456
Trond Myklebust4bc1e682012-10-23 17:50:07 -04001457static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001458{
1459 smp_mb__before_clear_bit();
Trond Myklebusta519fc72012-09-12 16:49:15 -04001460 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1461 clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001462 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1463 clear_bit(XPRT_CLOSING, &xprt->state);
1464 smp_mb__after_clear_bit();
Trond Myklebust4bc1e682012-10-23 17:50:07 -04001465}
1466
1467static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1468{
1469 xs_sock_reset_connection_flags(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001470 /* Mark transport as closed and wake up all pending tasks */
1471 xprt_disconnect_done(xprt);
1472}
1473
Chuck Lever9903cd12005-08-11 16:25:26 -04001474/**
1475 * xs_tcp_state_change - callback to handle TCP socket state changes
1476 * @sk: socket whose state has changed
1477 *
1478 */
1479static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001480{
Chuck Lever9903cd12005-08-11 16:25:26 -04001481 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001482
Eric Dumazetf064af12010-09-22 12:43:39 +00001483 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001484 if (!(xprt = xprt_from_sock(sk)))
1485 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001486 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001487 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001488 sk->sk_state, xprt_connected(xprt),
1489 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001490 sock_flag(sk, SOCK_ZAPPED),
1491 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001492
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001493 trace_rpc_socket_state_change(xprt, sk->sk_socket);
Chuck Levera246b012005-08-11 16:25:23 -04001494 switch (sk->sk_state) {
1495 case TCP_ESTABLISHED:
Eric Dumazetf064af12010-09-22 12:43:39 +00001496 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001497 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001498 struct sock_xprt *transport = container_of(xprt,
1499 struct sock_xprt, xprt);
1500
Chuck Levera246b012005-08-11 16:25:23 -04001501 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001502 transport->tcp_offset = 0;
1503 transport->tcp_reclen = 0;
1504 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001505 transport->tcp_flags =
1506 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Trond Myklebust8b717982013-09-26 10:18:04 -04001507 xprt->connect_cookie++;
Chuck Lever51971132006-12-05 16:35:19 -05001508
Trond Myklebust2a491992009-03-11 14:38:00 -04001509 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001510 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001511 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001512 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001513 case TCP_FIN_WAIT1:
1514 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001515 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001516 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001517 set_bit(XPRT_CLOSING, &xprt->state);
1518 smp_mb__before_clear_bit();
1519 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001520 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001521 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001522 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001523 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001524 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001525 /* The server initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001526 xprt->connect_cookie++;
Trond Myklebustd0bea452012-10-23 11:35:47 -04001527 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebusta519fc72012-09-12 16:49:15 -04001528 xs_tcp_force_close(xprt);
Trond Myklebust663b8852008-01-01 18:42:12 -05001529 case TCP_CLOSING:
1530 /*
1531 * If the server closed down the connection, make sure that
1532 * we back off before reconnecting
1533 */
1534 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1535 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001536 break;
1537 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001538 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001539 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001540 smp_mb__before_clear_bit();
1541 clear_bit(XPRT_CONNECTED, &xprt->state);
1542 smp_mb__after_clear_bit();
1543 break;
1544 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001545 xs_tcp_cancel_linger_timeout(xprt);
1546 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001547 }
1548 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001549 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001550}
1551
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001552static void xs_write_space(struct sock *sk)
1553{
1554 struct socket *sock;
1555 struct rpc_xprt *xprt;
1556
1557 if (unlikely(!(sock = sk->sk_socket)))
1558 return;
1559 clear_bit(SOCK_NOSPACE, &sock->flags);
1560
1561 if (unlikely(!(xprt = xprt_from_sock(sk))))
1562 return;
1563 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1564 return;
1565
1566 xprt_write_space(xprt);
1567}
1568
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001569/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001570 * xs_udp_write_space - callback invoked when socket buffer space
1571 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001572 * @sk: socket whose state has changed
1573 *
Chuck Levera246b012005-08-11 16:25:23 -04001574 * Called when more output buffer space is available for this socket.
1575 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001576 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001577 * with a bunch of small requests.
1578 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001579static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001580{
Eric Dumazetf064af12010-09-22 12:43:39 +00001581 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001582
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001583 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001584 if (sock_writeable(sk))
1585 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001586
Eric Dumazetf064af12010-09-22 12:43:39 +00001587 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001588}
Chuck Levera246b012005-08-11 16:25:23 -04001589
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001590/**
1591 * xs_tcp_write_space - callback invoked when socket buffer space
1592 * becomes available
1593 * @sk: socket whose state has changed
1594 *
1595 * Called when more output buffer space is available for this socket.
1596 * We try not to wake our writers until they can make "significant"
1597 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1598 * with a bunch of small requests.
1599 */
1600static void xs_tcp_write_space(struct sock *sk)
1601{
Eric Dumazetf064af12010-09-22 12:43:39 +00001602 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001603
1604 /* from net/core/stream.c:sk_stream_write_space */
Eric Dumazet64dc6132013-07-22 20:26:31 -07001605 if (sk_stream_is_writeable(sk))
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001606 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001607
Eric Dumazetf064af12010-09-22 12:43:39 +00001608 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001609}
1610
Chuck Lever470056c2005-08-25 16:25:56 -07001611static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001612{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001613 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1614 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001615
Chuck Lever7c6e0662006-12-05 16:35:30 -05001616 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001617 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001618 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001619 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001620 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001621 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001622 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001623 sk->sk_write_space(sk);
1624 }
1625}
1626
Chuck Lever43118c22005-08-25 16:25:49 -07001627/**
Chuck Lever470056c2005-08-25 16:25:56 -07001628 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001629 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001630 * @sndsize: requested size of send buffer, in bytes
1631 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001632 *
Chuck Lever470056c2005-08-25 16:25:56 -07001633 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001634 */
Chuck Lever470056c2005-08-25 16:25:56 -07001635static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001636{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001637 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1638
1639 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001640 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001641 transport->sndsize = sndsize + 1024;
1642 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001643 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001644 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001645
1646 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001647}
1648
Chuck Lever46c0ee82005-08-25 16:25:52 -07001649/**
1650 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1651 * @task: task that timed out
1652 *
1653 * Adjust the congestion window after a retransmit timeout has occurred.
1654 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001655static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever46c0ee82005-08-25 16:25:52 -07001656{
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001657 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
Chuck Lever46c0ee82005-08-25 16:25:52 -07001658}
1659
Chuck Leverb85d8802006-05-25 01:40:49 -04001660static unsigned short xs_get_random_port(void)
1661{
1662 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1663 unsigned short rand = (unsigned short) net_random() % range;
1664 return rand + xprt_min_resvport;
1665}
1666
Chuck Lever92200412006-01-03 09:55:51 +01001667/**
1668 * xs_set_port - reset the port number in the remote endpoint address
1669 * @xprt: generic transport
1670 * @port: new port number
1671 *
1672 */
1673static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1674{
Chuck Lever46121cf2007-01-31 12:14:08 -05001675 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001676
Chuck Lever9dc3b092009-08-09 15:09:46 -04001677 rpc_set_port(xs_addr(xprt), port);
1678 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001679}
1680
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001681static unsigned short xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001682{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001683 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001684
1685 if (port == 0 && transport->xprt.resvport)
1686 port = xs_get_random_port();
1687 return port;
1688}
1689
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001690static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001691{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001692 if (transport->srcport != 0)
1693 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001694 if (!transport->xprt.resvport)
1695 return 0;
1696 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1697 return xprt_max_resvport;
1698 return --port;
1699}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001700static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001701{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001702 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001703 int err, nloop = 0;
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001704 unsigned short port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001705 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001706
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001707 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001708 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001709 rpc_set_port((struct sockaddr *)&myaddr, port);
1710 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1711 transport->xprt.addrlen);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001712 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001713 break;
Chuck Levera246b012005-08-11 16:25:23 -04001714 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001715 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001716 break;
Chuck Levera246b012005-08-11 16:25:23 -04001717 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001718 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001719 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001720 if (port > last)
1721 nloop++;
1722 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001723
Chuck Lever4232e862010-10-20 11:52:51 -04001724 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001725 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1726 &((struct sockaddr_in *)&myaddr)->sin_addr,
1727 port, err ? "failed" : "ok", err);
1728 else
1729 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1730 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1731 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001732 return err;
1733}
1734
Chuck Lever176e21e2011-05-09 15:22:44 -04001735/*
1736 * We don't support autobind on AF_LOCAL sockets
1737 */
1738static void xs_local_rpcbind(struct rpc_task *task)
1739{
Trond Myklebust3dc0da22013-01-08 09:31:13 -05001740 rcu_read_lock();
1741 xprt_set_bound(rcu_dereference(task->tk_client->cl_xprt));
1742 rcu_read_unlock();
Chuck Lever176e21e2011-05-09 15:22:44 -04001743}
1744
1745static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1746{
1747}
Chuck Levera246b012005-08-11 16:25:23 -04001748
Peter Zijlstraed075362006-12-06 20:35:24 -08001749#ifdef CONFIG_DEBUG_LOCK_ALLOC
1750static struct lock_class_key xs_key[2];
1751static struct lock_class_key xs_slock_key[2];
1752
Chuck Lever176e21e2011-05-09 15:22:44 -04001753static inline void xs_reclassify_socketu(struct socket *sock)
1754{
1755 struct sock *sk = sock->sk;
1756
Chuck Lever176e21e2011-05-09 15:22:44 -04001757 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1758 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1759}
1760
Chuck Lever8945ee52007-08-06 11:58:04 -04001761static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001762{
1763 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001764
Chuck Lever8945ee52007-08-06 11:58:04 -04001765 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1766 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1767}
Peter Zijlstraed075362006-12-06 20:35:24 -08001768
Chuck Lever8945ee52007-08-06 11:58:04 -04001769static inline void xs_reclassify_socket6(struct socket *sock)
1770{
1771 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001772
Chuck Lever8945ee52007-08-06 11:58:04 -04001773 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1774 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001775}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001776
1777static inline void xs_reclassify_socket(int family, struct socket *sock)
1778{
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -04001779 WARN_ON_ONCE(sock_owned_by_user(sock->sk));
1780 if (sock_owned_by_user(sock->sk))
1781 return;
1782
Chuck Lever4232e862010-10-20 11:52:51 -04001783 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001784 case AF_LOCAL:
1785 xs_reclassify_socketu(sock);
1786 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001787 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001788 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001789 break;
1790 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001791 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001792 break;
1793 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001794}
Peter Zijlstraed075362006-12-06 20:35:24 -08001795#else
Chuck Lever176e21e2011-05-09 15:22:44 -04001796static inline void xs_reclassify_socketu(struct socket *sock)
1797{
1798}
1799
Chuck Lever8945ee52007-08-06 11:58:04 -04001800static inline void xs_reclassify_socket4(struct socket *sock)
1801{
1802}
1803
1804static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001805{
1806}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001807
1808static inline void xs_reclassify_socket(int family, struct socket *sock)
1809{
1810}
Peter Zijlstraed075362006-12-06 20:35:24 -08001811#endif
1812
NeilBrown93dc41b2013-10-31 16:14:36 +11001813static void xs_dummy_setup_socket(struct work_struct *work)
1814{
1815}
1816
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001817static struct socket *xs_create_sock(struct rpc_xprt *xprt,
1818 struct sock_xprt *transport, int family, int type, int protocol)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001819{
1820 struct socket *sock;
1821 int err;
1822
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001823 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001824 if (err < 0) {
1825 dprintk("RPC: can't create %d transport socket (%d).\n",
1826 protocol, -err);
1827 goto out;
1828 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001829 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001830
Ben Hutchings4cea2882011-02-22 21:54:34 +00001831 err = xs_bind(transport, sock);
1832 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001833 sock_release(sock);
1834 goto out;
1835 }
1836
1837 return sock;
1838out:
1839 return ERR_PTR(err);
1840}
1841
Chuck Lever176e21e2011-05-09 15:22:44 -04001842static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1843 struct socket *sock)
1844{
1845 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1846 xprt);
1847
1848 if (!transport->inet) {
1849 struct sock *sk = sock->sk;
1850
1851 write_lock_bh(&sk->sk_callback_lock);
1852
1853 xs_save_old_callbacks(transport, sk);
1854
1855 sk->sk_user_data = xprt;
1856 sk->sk_data_ready = xs_local_data_ready;
1857 sk->sk_write_space = xs_udp_write_space;
Chuck Lever176e21e2011-05-09 15:22:44 -04001858 sk->sk_allocation = GFP_ATOMIC;
1859
1860 xprt_clear_connected(xprt);
1861
1862 /* Reset to new socket */
1863 transport->sock = sock;
1864 transport->inet = sk;
1865
1866 write_unlock_bh(&sk->sk_callback_lock);
1867 }
1868
1869 /* Tell the socket layer to start connecting... */
1870 xprt->stat.connect_count++;
1871 xprt->stat.connect_start = jiffies;
1872 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1873}
1874
1875/**
1876 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1877 * @xprt: RPC transport to connect
1878 * @transport: socket transport to connect
1879 * @create_sock: function to create a socket of the correct type
Chuck Lever176e21e2011-05-09 15:22:44 -04001880 */
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001881static int xs_local_setup_socket(struct sock_xprt *transport)
Chuck Lever176e21e2011-05-09 15:22:44 -04001882{
Chuck Lever176e21e2011-05-09 15:22:44 -04001883 struct rpc_xprt *xprt = &transport->xprt;
1884 struct socket *sock;
1885 int status = -EIO;
1886
Jeff Layton5cf02d02012-07-23 13:58:51 -04001887 current->flags |= PF_FSTRANS;
1888
Chuck Lever176e21e2011-05-09 15:22:44 -04001889 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1890 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1891 SOCK_STREAM, 0, &sock, 1);
1892 if (status < 0) {
1893 dprintk("RPC: can't create AF_LOCAL "
1894 "transport socket (%d).\n", -status);
1895 goto out;
1896 }
1897 xs_reclassify_socketu(sock);
1898
1899 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1900 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1901
1902 status = xs_local_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001903 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever176e21e2011-05-09 15:22:44 -04001904 switch (status) {
1905 case 0:
1906 dprintk("RPC: xprt %p connected to %s\n",
1907 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1908 xprt_set_connected(xprt);
1909 break;
1910 case -ENOENT:
1911 dprintk("RPC: xprt %p: socket %s does not exist\n",
1912 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1913 break;
Trond Myklebust4a20a982012-12-15 17:02:29 -05001914 case -ECONNREFUSED:
1915 dprintk("RPC: xprt %p: connection refused for %s\n",
1916 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1917 break;
Chuck Lever176e21e2011-05-09 15:22:44 -04001918 default:
1919 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1920 __func__, -status,
1921 xprt->address_strings[RPC_DISPLAY_ADDR]);
1922 }
1923
1924out:
1925 xprt_clear_connecting(xprt);
1926 xprt_wake_pending_tasks(xprt, status);
Jeff Layton5cf02d02012-07-23 13:58:51 -04001927 current->flags &= ~PF_FSTRANS;
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001928 return status;
1929}
1930
Linus Torvaldsb6669732013-02-28 18:02:55 -08001931static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001932{
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001933 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1934 int ret;
1935
1936 if (RPC_IS_ASYNC(task)) {
1937 /*
1938 * We want the AF_LOCAL connect to be resolved in the
1939 * filesystem namespace of the process making the rpc
1940 * call. Thus we connect synchronously.
1941 *
1942 * If we want to support asynchronous AF_LOCAL calls,
1943 * we'll need to figure out how to pass a namespace to
1944 * connect.
1945 */
1946 rpc_exit(task, -ENOTCONN);
1947 return;
1948 }
1949 ret = xs_local_setup_socket(transport);
1950 if (ret && !RPC_IS_SOFTCONN(task))
1951 msleep_interruptible(15000);
Chuck Lever176e21e2011-05-09 15:22:44 -04001952}
1953
Mel Gormana564b8f2012-07-31 16:45:12 -07001954#ifdef CONFIG_SUNRPC_SWAP
1955static void xs_set_memalloc(struct rpc_xprt *xprt)
1956{
1957 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1958 xprt);
1959
1960 if (xprt->swapper)
1961 sk_set_memalloc(transport->inet);
1962}
1963
1964/**
1965 * xs_swapper - Tag this transport as being used for swap.
1966 * @xprt: transport to tag
1967 * @enable: enable/disable
1968 *
1969 */
1970int xs_swapper(struct rpc_xprt *xprt, int enable)
1971{
1972 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1973 xprt);
1974 int err = 0;
1975
1976 if (enable) {
1977 xprt->swapper++;
1978 xs_set_memalloc(xprt);
1979 } else if (xprt->swapper) {
1980 xprt->swapper--;
1981 sk_clear_memalloc(transport->inet);
1982 }
1983
1984 return err;
1985}
1986EXPORT_SYMBOL_GPL(xs_swapper);
1987#else
1988static void xs_set_memalloc(struct rpc_xprt *xprt)
1989{
1990}
1991#endif
1992
Chuck Lever16be2d22007-08-06 11:57:38 -04001993static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001994{
Chuck Lever16be2d22007-08-06 11:57:38 -04001995 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001996
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001997 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001998 struct sock *sk = sock->sk;
1999
2000 write_lock_bh(&sk->sk_callback_lock);
2001
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002002 xs_save_old_callbacks(transport, sk);
2003
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002004 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002005 sk->sk_data_ready = xs_udp_data_ready;
2006 sk->sk_write_space = xs_udp_write_space;
2007 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05002008 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002009
2010 xprt_set_connected(xprt);
2011
2012 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002013 transport->sock = sock;
2014 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002015
Mel Gormana564b8f2012-07-31 16:45:12 -07002016 xs_set_memalloc(xprt);
2017
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002018 write_unlock_bh(&sk->sk_callback_lock);
2019 }
Chuck Lever470056c2005-08-25 16:25:56 -07002020 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04002021}
2022
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002023static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04002024{
2025 struct sock_xprt *transport =
2026 container_of(work, struct sock_xprt, connect_worker.work);
2027 struct rpc_xprt *xprt = &transport->xprt;
2028 struct socket *sock = transport->sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002029 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04002030
Jeff Layton5cf02d02012-07-23 13:58:51 -04002031 current->flags |= PF_FSTRANS;
2032
Chuck Levera246b012005-08-11 16:25:23 -04002033 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04002034 xs_reset_transport(transport);
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002035 sock = xs_create_sock(xprt, transport,
2036 xs_addr(xprt)->sa_family, SOCK_DGRAM, IPPROTO_UDP);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002037 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04002038 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04002039
Chuck Leverc740eff2009-08-09 15:09:46 -04002040 dprintk("RPC: worker connecting xprt %p via %s to "
2041 "%s (port %s)\n", xprt,
2042 xprt->address_strings[RPC_DISPLAY_PROTO],
2043 xprt->address_strings[RPC_DISPLAY_ADDR],
2044 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04002045
2046 xs_udp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002047 trace_rpc_socket_connect(xprt, sock, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002048 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002049out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002050 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002051 xprt_wake_pending_tasks(xprt, status);
Jeff Layton5cf02d02012-07-23 13:58:51 -04002052 current->flags &= ~PF_FSTRANS;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002053}
2054
Chuck Lever3167e122005-08-25 16:25:55 -07002055/*
2056 * We need to preserve the port number so the reply cache on the server can
2057 * find our cached RPC replies when we get around to reconnecting.
2058 */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002059static void xs_abort_connection(struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07002060{
2061 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07002062 struct sockaddr any;
2063
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002064 dprintk("RPC: disconnecting xprt %p to reuse port\n", transport);
Chuck Lever3167e122005-08-25 16:25:55 -07002065
2066 /*
2067 * Disconnect the transport socket by doing a connect operation
2068 * with AF_UNSPEC. This should return immediately...
2069 */
2070 memset(&any, 0, sizeof(any));
2071 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002072 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002073 trace_rpc_socket_reset_connection(&transport->xprt,
2074 transport->sock, result);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002075 if (!result)
Trond Myklebust4bc1e682012-10-23 17:50:07 -04002076 xs_sock_reset_connection_flags(&transport->xprt);
2077 dprintk("RPC: AF_UNSPEC connect return code %d\n", result);
Chuck Lever3167e122005-08-25 16:25:55 -07002078}
2079
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002080static void xs_tcp_reuse_connection(struct sock_xprt *transport)
Trond Myklebust40d25492009-03-11 14:37:58 -04002081{
2082 unsigned int state = transport->inet->sk_state;
2083
Andy Chittenden669502f2010-08-10 10:19:53 -04002084 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) {
2085 /* we don't need to abort the connection if the socket
2086 * hasn't undergone a shutdown
2087 */
2088 if (transport->inet->sk_shutdown == 0)
2089 return;
2090 dprintk("RPC: %s: TCP_CLOSEd and sk_shutdown set to %d\n",
2091 __func__, transport->inet->sk_shutdown);
2092 }
2093 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) {
2094 /* we don't need to abort the connection if the socket
2095 * hasn't undergone a shutdown
2096 */
2097 if (transport->inet->sk_shutdown == 0)
2098 return;
2099 dprintk("RPC: %s: ESTABLISHED/SYN_SENT "
2100 "sk_shutdown set to %d\n",
2101 __func__, transport->inet->sk_shutdown);
2102 }
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002103 xs_abort_connection(transport);
Trond Myklebust40d25492009-03-11 14:37:58 -04002104}
2105
Chuck Lever16be2d22007-08-06 11:57:38 -04002106static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002107{
Chuck Lever16be2d22007-08-06 11:57:38 -04002108 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002109 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002110
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002111 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002112 struct sock *sk = sock->sk;
Trond Myklebust7f260e82013-09-24 11:25:22 -04002113 unsigned int keepidle = xprt->timeout->to_initval / HZ;
2114 unsigned int keepcnt = xprt->timeout->to_retries + 1;
2115 unsigned int opt_on = 1;
2116
2117 /* TCP Keepalive options */
2118 kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
2119 (char *)&opt_on, sizeof(opt_on));
2120 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
2121 (char *)&keepidle, sizeof(keepidle));
2122 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
2123 (char *)&keepidle, sizeof(keepidle));
2124 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
2125 (char *)&keepcnt, sizeof(keepcnt));
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002126
2127 write_lock_bh(&sk->sk_callback_lock);
2128
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002129 xs_save_old_callbacks(transport, sk);
2130
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002131 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002132 sk->sk_data_ready = xs_tcp_data_ready;
2133 sk->sk_state_change = xs_tcp_state_change;
2134 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05002135 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07002136
2137 /* socket options */
2138 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
2139 sock_reset_flag(sk, SOCK_LINGER);
2140 tcp_sk(sk)->linger2 = 0;
2141 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002142
2143 xprt_clear_connected(xprt);
2144
2145 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002146 transport->sock = sock;
2147 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002148
2149 write_unlock_bh(&sk->sk_callback_lock);
2150 }
2151
Trond Myklebust01d37c42009-03-11 14:09:39 -04002152 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002153 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002154
Mel Gormana564b8f2012-07-31 16:45:12 -07002155 xs_set_memalloc(xprt);
2156
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002157 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05002158 xprt->stat.connect_count++;
2159 xprt->stat.connect_start = jiffies;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002160 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2161 switch (ret) {
2162 case 0:
2163 case -EINPROGRESS:
2164 /* SYN_SENT! */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002165 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2166 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2167 }
2168out:
2169 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002170}
2171
2172/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002173 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2174 * @xprt: RPC transport to connect
2175 * @transport: socket transport to connect
2176 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04002177 *
2178 * Invoked by a work queue tasklet.
2179 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002180static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002181{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002182 struct sock_xprt *transport =
2183 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002184 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002185 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002186 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002187
Jeff Layton5cf02d02012-07-23 13:58:51 -04002188 current->flags |= PF_FSTRANS;
2189
Chuck Lever16be2d22007-08-06 11:57:38 -04002190 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002191 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002192 sock = xs_create_sock(xprt, transport,
2193 xs_addr(xprt)->sa_family, SOCK_STREAM, IPPROTO_TCP);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002194 if (IS_ERR(sock)) {
2195 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002196 goto out;
2197 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002198 } else {
2199 int abort_and_exit;
2200
2201 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
2202 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04002203 /* "close" the socket, preserving the local port */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002204 xs_tcp_reuse_connection(transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04002205
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002206 if (abort_and_exit)
2207 goto out_eagain;
2208 }
2209
Chuck Leverc740eff2009-08-09 15:09:46 -04002210 dprintk("RPC: worker connecting xprt %p via %s to "
2211 "%s (port %s)\n", xprt,
2212 xprt->address_strings[RPC_DISPLAY_PROTO],
2213 xprt->address_strings[RPC_DISPLAY_ADDR],
2214 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002215
2216 status = xs_tcp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002217 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever46121cf2007-01-31 12:14:08 -05002218 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2219 xprt, -status, xprt_connected(xprt),
2220 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002221 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002222 default:
2223 printk("%s: connect returned unhandled error %d\n",
2224 __func__, status);
2225 case -EADDRNOTAVAIL:
2226 /* We're probably in TIME_WAIT. Get rid of existing socket,
2227 * and retry
2228 */
Trond Myklebusta519fc72012-09-12 16:49:15 -04002229 xs_tcp_force_close(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002230 break;
Trond Myklebust2a491992009-03-11 14:38:00 -04002231 case 0:
2232 case -EINPROGRESS:
2233 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002234 xprt_clear_connecting(xprt);
Jeff Layton5cf02d02012-07-23 13:58:51 -04002235 current->flags &= ~PF_FSTRANS;
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002236 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002237 case -EINVAL:
2238 /* Happens, for instance, if the user specified a link
2239 * local IPv6 address without a scope-id.
2240 */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002241 case -ECONNREFUSED:
2242 case -ECONNRESET:
2243 case -ENETUNREACH:
2244 /* retry with existing socket, after a delay */
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002245 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002246 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002247out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04002248 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002249out:
Chuck Lever2226feb2005-08-11 16:25:38 -04002250 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002251 xprt_wake_pending_tasks(xprt, status);
Jeff Layton5cf02d02012-07-23 13:58:51 -04002252 current->flags &= ~PF_FSTRANS;
Chuck Levera246b012005-08-11 16:25:23 -04002253}
2254
Chuck Lever68e220b2007-08-06 11:57:48 -04002255/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002256 * xs_connect - connect a socket to a remote endpoint
Trond Myklebust1b092092013-01-08 09:26:49 -05002257 * @xprt: pointer to transport structure
Chuck Lever9903cd12005-08-11 16:25:26 -04002258 * @task: address of RPC task that manages state of connect request
2259 *
2260 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002261 *
2262 * UDP socket connects are synchronous, but we use a work queue anyway
2263 * to guarantee that even unprivileged user processes can set up a
2264 * socket on a privileged port.
2265 *
2266 * If a UDP socket connect fails, the delay behavior here prevents
2267 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002268 */
Trond Myklebust1b092092013-01-08 09:26:49 -05002269static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002270{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002271 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002272
Chuck Lever09a21c42009-12-03 15:58:56 -05002273 if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002274 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2275 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002276 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002277 queue_delayed_work(rpciod_workqueue,
2278 &transport->connect_worker,
2279 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07002280 xprt->reestablish_timeout <<= 1;
Neil Brown61d0a8e2009-09-23 14:36:37 -04002281 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2282 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002283 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2284 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002285 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05002286 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002287 queue_delayed_work(rpciod_workqueue,
2288 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002289 }
2290}
2291
Chuck Lever262ca072006-03-20 13:44:16 -05002292/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002293 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2294 * @xprt: rpc_xprt struct containing statistics
2295 * @seq: output file
2296 *
2297 */
2298static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2299{
2300 long idle_time = 0;
2301
2302 if (xprt_connected(xprt))
2303 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2304
2305 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
Andy Adamson15a45202012-02-14 16:19:18 -05002306 "%llu %llu %lu %llu %llu\n",
Chuck Lever176e21e2011-05-09 15:22:44 -04002307 xprt->stat.bind_count,
2308 xprt->stat.connect_count,
2309 xprt->stat.connect_time,
2310 idle_time,
2311 xprt->stat.sends,
2312 xprt->stat.recvs,
2313 xprt->stat.bad_xids,
2314 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002315 xprt->stat.bklog_u,
2316 xprt->stat.max_slots,
2317 xprt->stat.sending_u,
2318 xprt->stat.pending_u);
Chuck Lever176e21e2011-05-09 15:22:44 -04002319}
2320
2321/**
Chuck Lever262ca072006-03-20 13:44:16 -05002322 * xs_udp_print_stats - display UDP socket-specifc stats
2323 * @xprt: rpc_xprt struct containing statistics
2324 * @seq: output file
2325 *
2326 */
2327static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2328{
Chuck Leverc8475462006-12-05 16:35:26 -05002329 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2330
Andy Adamson15a45202012-02-14 16:19:18 -05002331 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2332 "%lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002333 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002334 xprt->stat.bind_count,
2335 xprt->stat.sends,
2336 xprt->stat.recvs,
2337 xprt->stat.bad_xids,
2338 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002339 xprt->stat.bklog_u,
2340 xprt->stat.max_slots,
2341 xprt->stat.sending_u,
2342 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002343}
2344
2345/**
2346 * xs_tcp_print_stats - display TCP socket-specifc stats
2347 * @xprt: rpc_xprt struct containing statistics
2348 * @seq: output file
2349 *
2350 */
2351static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2352{
Chuck Leverc8475462006-12-05 16:35:26 -05002353 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002354 long idle_time = 0;
2355
2356 if (xprt_connected(xprt))
2357 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2358
Andy Adamson15a45202012-02-14 16:19:18 -05002359 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2360 "%llu %llu %lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002361 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002362 xprt->stat.bind_count,
2363 xprt->stat.connect_count,
2364 xprt->stat.connect_time,
2365 idle_time,
2366 xprt->stat.sends,
2367 xprt->stat.recvs,
2368 xprt->stat.bad_xids,
2369 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002370 xprt->stat.bklog_u,
2371 xprt->stat.max_slots,
2372 xprt->stat.sending_u,
2373 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002374}
2375
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002376/*
2377 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2378 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2379 * to use the server side send routines.
2380 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002381static void *bc_malloc(struct rpc_task *task, size_t size)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002382{
2383 struct page *page;
2384 struct rpc_buffer *buf;
2385
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002386 WARN_ON_ONCE(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2387 if (size > PAGE_SIZE - sizeof(struct rpc_buffer))
2388 return NULL;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002389
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002390 page = alloc_page(GFP_KERNEL);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002391 if (!page)
2392 return NULL;
2393
2394 buf = page_address(page);
2395 buf->len = PAGE_SIZE;
2396
2397 return buf->data;
2398}
2399
2400/*
2401 * Free the space allocated in the bc_alloc routine
2402 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002403static void bc_free(void *buffer)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002404{
2405 struct rpc_buffer *buf;
2406
2407 if (!buffer)
2408 return;
2409
2410 buf = container_of(buffer, struct rpc_buffer, data);
2411 free_page((unsigned long)buf);
2412}
2413
2414/*
2415 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2416 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2417 */
2418static int bc_sendto(struct rpc_rqst *req)
2419{
2420 int len;
2421 struct xdr_buf *xbufp = &req->rq_snd_buf;
2422 struct rpc_xprt *xprt = req->rq_xprt;
2423 struct sock_xprt *transport =
2424 container_of(xprt, struct sock_xprt, xprt);
2425 struct socket *sock = transport->sock;
2426 unsigned long headoff;
2427 unsigned long tailoff;
2428
Chuck Lever61677ee2011-05-09 15:22:34 -04002429 xs_encode_stream_record_marker(xbufp);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002430
2431 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2432 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2433 len = svc_send_common(sock, xbufp,
2434 virt_to_page(xbufp->head[0].iov_base), headoff,
2435 xbufp->tail[0].iov_base, tailoff);
2436
2437 if (len != xbufp->len) {
2438 printk(KERN_NOTICE "Error sending entire callback!\n");
2439 len = -EAGAIN;
2440 }
2441
2442 return len;
2443}
2444
2445/*
2446 * The send routine. Borrows from svc_send
2447 */
2448static int bc_send_request(struct rpc_task *task)
2449{
2450 struct rpc_rqst *req = task->tk_rqstp;
2451 struct svc_xprt *xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002452 u32 len;
2453
2454 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2455 /*
2456 * Get the server socket associated with this callback xprt
2457 */
2458 xprt = req->rq_xprt->bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002459
2460 /*
2461 * Grab the mutex to serialize data as the connection is shared
2462 * with the fore channel
2463 */
2464 if (!mutex_trylock(&xprt->xpt_mutex)) {
2465 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2466 if (!mutex_trylock(&xprt->xpt_mutex))
2467 return -EAGAIN;
2468 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2469 }
2470 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2471 len = -ENOTCONN;
2472 else
2473 len = bc_sendto(req);
2474 mutex_unlock(&xprt->xpt_mutex);
2475
2476 if (len > 0)
2477 len = 0;
2478
2479 return len;
2480}
2481
2482/*
2483 * The close routine. Since this is client initiated, we do nothing
2484 */
2485
2486static void bc_close(struct rpc_xprt *xprt)
2487{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002488}
2489
2490/*
2491 * The xprt destroy routine. Again, because this connection is client
2492 * initiated, we do nothing
2493 */
2494
2495static void bc_destroy(struct rpc_xprt *xprt)
2496{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002497}
2498
Chuck Lever176e21e2011-05-09 15:22:44 -04002499static struct rpc_xprt_ops xs_local_ops = {
2500 .reserve_xprt = xprt_reserve_xprt,
2501 .release_xprt = xs_tcp_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002502 .alloc_slot = xprt_alloc_slot,
Chuck Lever176e21e2011-05-09 15:22:44 -04002503 .rpcbind = xs_local_rpcbind,
2504 .set_port = xs_local_set_port,
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002505 .connect = xs_local_connect,
Chuck Lever176e21e2011-05-09 15:22:44 -04002506 .buf_alloc = rpc_malloc,
2507 .buf_free = rpc_free,
2508 .send_request = xs_local_send_request,
2509 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2510 .close = xs_close,
Trond Myklebusta1311d82013-10-31 09:18:49 -04002511 .destroy = xs_destroy,
Chuck Lever176e21e2011-05-09 15:22:44 -04002512 .print_stats = xs_local_print_stats,
2513};
2514
Chuck Lever262965f2005-08-11 16:25:56 -04002515static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002516 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002517 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002518 .release_xprt = xprt_release_xprt_cong,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002519 .alloc_slot = xprt_alloc_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002520 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002521 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002522 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002523 .buf_alloc = rpc_malloc,
2524 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002525 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002526 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002527 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002528 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002529 .close = xs_close,
2530 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002531 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04002532};
2533
2534static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002535 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002536 .release_xprt = xs_tcp_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002537 .alloc_slot = xprt_lock_and_alloc_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002538 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002539 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002540 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002541 .buf_alloc = rpc_malloc,
2542 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002543 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002544 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustf75e6742009-04-21 17:18:20 -04002545 .close = xs_tcp_close,
Chuck Lever9903cd12005-08-11 16:25:26 -04002546 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002547 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002548};
2549
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002550/*
2551 * The rpc_xprt_ops for the server backchannel
2552 */
2553
2554static struct rpc_xprt_ops bc_tcp_ops = {
2555 .reserve_xprt = xprt_reserve_xprt,
2556 .release_xprt = xprt_release_xprt,
Bryan Schumaker84e28a32012-09-24 13:39:01 -04002557 .alloc_slot = xprt_alloc_slot,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002558 .buf_alloc = bc_malloc,
2559 .buf_free = bc_free,
2560 .send_request = bc_send_request,
2561 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2562 .close = bc_close,
2563 .destroy = bc_destroy,
2564 .print_stats = xs_tcp_print_stats,
2565};
2566
Chuck Lever92476852010-10-20 11:53:01 -04002567static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2568{
2569 static const struct sockaddr_in sin = {
2570 .sin_family = AF_INET,
2571 .sin_addr.s_addr = htonl(INADDR_ANY),
2572 };
2573 static const struct sockaddr_in6 sin6 = {
2574 .sin6_family = AF_INET6,
2575 .sin6_addr = IN6ADDR_ANY_INIT,
2576 };
2577
2578 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002579 case AF_LOCAL:
2580 break;
Chuck Lever92476852010-10-20 11:53:01 -04002581 case AF_INET:
2582 memcpy(sap, &sin, sizeof(sin));
2583 break;
2584 case AF_INET6:
2585 memcpy(sap, &sin6, sizeof(sin6));
2586 break;
2587 default:
2588 dprintk("RPC: %s: Bad address family\n", __func__);
2589 return -EAFNOSUPPORT;
2590 }
2591 return 0;
2592}
2593
\"Talpey, Thomas\3c341b02007-09-10 13:47:07 -04002594static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002595 unsigned int slot_table_size,
2596 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002597{
2598 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002599 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002600
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002601 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002602 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002603 return ERR_PTR(-EBADF);
2604 }
2605
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002606 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2607 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002608 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002609 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2610 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002611 return ERR_PTR(-ENOMEM);
2612 }
2613
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002614 new = container_of(xprt, struct sock_xprt, xprt);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002615 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2616 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002617 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002618 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002619 else {
2620 int err;
2621 err = xs_init_anyaddr(args->dstaddr->sa_family,
2622 (struct sockaddr *)&new->srcaddr);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002623 if (err != 0) {
2624 xprt_free(xprt);
Chuck Lever92476852010-10-20 11:53:01 -04002625 return ERR_PTR(err);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002626 }
Chuck Lever92476852010-10-20 11:53:01 -04002627 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002628
2629 return xprt;
2630}
2631
Chuck Lever176e21e2011-05-09 15:22:44 -04002632static const struct rpc_timeout xs_local_default_timeout = {
2633 .to_initval = 10 * HZ,
2634 .to_maxval = 10 * HZ,
2635 .to_retries = 2,
2636};
2637
2638/**
2639 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2640 * @args: rpc transport creation arguments
2641 *
2642 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2643 */
2644static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2645{
2646 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2647 struct sock_xprt *transport;
2648 struct rpc_xprt *xprt;
2649 struct rpc_xprt *ret;
2650
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002651 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2652 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002653 if (IS_ERR(xprt))
2654 return xprt;
2655 transport = container_of(xprt, struct sock_xprt, xprt);
2656
2657 xprt->prot = 0;
2658 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2659 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2660
2661 xprt->bind_timeout = XS_BIND_TO;
2662 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2663 xprt->idle_timeout = XS_IDLE_DISC_TO;
2664
2665 xprt->ops = &xs_local_ops;
2666 xprt->timeout = &xs_local_default_timeout;
2667
NeilBrown93dc41b2013-10-31 16:14:36 +11002668 INIT_DELAYED_WORK(&transport->connect_worker,
2669 xs_dummy_setup_socket);
2670
Chuck Lever176e21e2011-05-09 15:22:44 -04002671 switch (sun->sun_family) {
2672 case AF_LOCAL:
2673 if (sun->sun_path[0] != '/') {
2674 dprintk("RPC: bad AF_LOCAL address: %s\n",
2675 sun->sun_path);
2676 ret = ERR_PTR(-EINVAL);
2677 goto out_err;
2678 }
2679 xprt_set_bound(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002680 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
J. Bruce Fields7073ea82013-02-21 10:14:22 -05002681 ret = ERR_PTR(xs_local_setup_socket(transport));
2682 if (ret)
2683 goto out_err;
Chuck Lever176e21e2011-05-09 15:22:44 -04002684 break;
2685 default:
2686 ret = ERR_PTR(-EAFNOSUPPORT);
2687 goto out_err;
2688 }
2689
2690 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2691 xprt->address_strings[RPC_DISPLAY_ADDR]);
2692
2693 if (try_module_get(THIS_MODULE))
2694 return xprt;
2695 ret = ERR_PTR(-EINVAL);
2696out_err:
2697 xprt_free(xprt);
2698 return ret;
2699}
2700
Trond Myklebust2881ae72007-12-20 16:03:54 -05002701static const struct rpc_timeout xs_udp_default_timeout = {
2702 .to_initval = 5 * HZ,
2703 .to_maxval = 30 * HZ,
2704 .to_increment = 5 * HZ,
2705 .to_retries = 5,
2706};
2707
Chuck Lever9903cd12005-08-11 16:25:26 -04002708/**
2709 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002710 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002711 *
2712 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002713static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002714{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002715 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002716 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002717 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002718 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002719
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002720 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2721 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002722 if (IS_ERR(xprt))
2723 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002724 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002725
Chuck Leverec739ef2006-08-22 20:06:15 -04002726 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002727 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002728 /* XXX: header size can vary due to auth type, IPv6, etc. */
2729 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2730
Chuck Lever03bf4b72005-08-25 16:25:55 -07002731 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002732 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2733 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002734
Chuck Lever262965f2005-08-11 16:25:56 -04002735 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002736
Trond Myklebustba7392b2007-12-20 16:03:55 -05002737 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002738
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002739 switch (addr->sa_family) {
2740 case AF_INET:
2741 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2742 xprt_set_bound(xprt);
2743
2744 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002745 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002746 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002747 break;
2748 case AF_INET6:
2749 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2750 xprt_set_bound(xprt);
2751
2752 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002753 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002754 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002755 break;
2756 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002757 ret = ERR_PTR(-EAFNOSUPPORT);
2758 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002759 }
2760
Chuck Leverc740eff2009-08-09 15:09:46 -04002761 if (xprt_bound(xprt))
2762 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2763 xprt->address_strings[RPC_DISPLAY_ADDR],
2764 xprt->address_strings[RPC_DISPLAY_PORT],
2765 xprt->address_strings[RPC_DISPLAY_PROTO]);
2766 else
2767 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2768 xprt->address_strings[RPC_DISPLAY_ADDR],
2769 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002770
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002771 if (try_module_get(THIS_MODULE))
2772 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002773 ret = ERR_PTR(-EINVAL);
2774out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002775 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002776 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002777}
2778
Trond Myklebust2881ae72007-12-20 16:03:54 -05002779static const struct rpc_timeout xs_tcp_default_timeout = {
2780 .to_initval = 60 * HZ,
2781 .to_maxval = 60 * HZ,
2782 .to_retries = 2,
2783};
2784
Chuck Lever9903cd12005-08-11 16:25:26 -04002785/**
2786 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002787 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002788 *
2789 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002790static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002791{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002792 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002793 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002794 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002795 struct rpc_xprt *ret;
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002796 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2797
2798 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2799 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Levera246b012005-08-11 16:25:23 -04002800
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002801 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002802 max_slot_table_size);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002803 if (IS_ERR(xprt))
2804 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002805 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002806
Chuck Leverec739ef2006-08-22 20:06:15 -04002807 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002808 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002809 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002810
Chuck Lever03bf4b72005-08-25 16:25:55 -07002811 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002812 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2813 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002814
Chuck Lever262965f2005-08-11 16:25:56 -04002815 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002816 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002817
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002818 switch (addr->sa_family) {
2819 case AF_INET:
2820 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2821 xprt_set_bound(xprt);
2822
Chuck Lever9dc3b092009-08-09 15:09:46 -04002823 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002824 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002825 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002826 break;
2827 case AF_INET6:
2828 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2829 xprt_set_bound(xprt);
2830
Chuck Lever9dc3b092009-08-09 15:09:46 -04002831 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002832 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002833 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002834 break;
2835 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002836 ret = ERR_PTR(-EAFNOSUPPORT);
2837 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002838 }
2839
Chuck Leverc740eff2009-08-09 15:09:46 -04002840 if (xprt_bound(xprt))
2841 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2842 xprt->address_strings[RPC_DISPLAY_ADDR],
2843 xprt->address_strings[RPC_DISPLAY_PORT],
2844 xprt->address_strings[RPC_DISPLAY_PROTO]);
2845 else
2846 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2847 xprt->address_strings[RPC_DISPLAY_ADDR],
2848 xprt->address_strings[RPC_DISPLAY_PROTO]);
2849
Chuck Leveredb267a2006-08-22 20:06:18 -04002850
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002851 if (try_module_get(THIS_MODULE))
2852 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002853 ret = ERR_PTR(-EINVAL);
2854out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002855 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002856 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002857}
Chuck Lever282b32e2006-12-05 16:35:51 -05002858
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002859/**
2860 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2861 * @args: rpc transport creation arguments
2862 *
2863 */
2864static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2865{
2866 struct sockaddr *addr = args->dstaddr;
2867 struct rpc_xprt *xprt;
2868 struct sock_xprt *transport;
2869 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002870 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002871
J. Bruce Fieldsf0418aa2010-12-08 13:48:19 -05002872 if (args->bc_xprt->xpt_bc_xprt) {
2873 /*
2874 * This server connection already has a backchannel
J. Bruce Fieldse3bfab12013-10-02 09:48:15 -04002875 * transport; we can't create a new one, as we wouldn't
2876 * be able to match replies based on xid any more. So,
J. Bruce Fieldsf0418aa2010-12-08 13:48:19 -05002877 * reuse the already-existing one:
2878 */
2879 return args->bc_xprt->xpt_bc_xprt;
2880 }
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002881 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2882 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002883 if (IS_ERR(xprt))
2884 return xprt;
2885 transport = container_of(xprt, struct sock_xprt, xprt);
2886
2887 xprt->prot = IPPROTO_TCP;
2888 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2889 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2890 xprt->timeout = &xs_tcp_default_timeout;
2891
2892 /* backchannel */
2893 xprt_set_bound(xprt);
2894 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002895 xprt->reestablish_timeout = 0;
2896 xprt->idle_timeout = 0;
2897
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002898 xprt->ops = &bc_tcp_ops;
2899
2900 switch (addr->sa_family) {
2901 case AF_INET:
2902 xs_format_peer_addresses(xprt, "tcp",
2903 RPCBIND_NETID_TCP);
2904 break;
2905 case AF_INET6:
2906 xs_format_peer_addresses(xprt, "tcp",
2907 RPCBIND_NETID_TCP6);
2908 break;
2909 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002910 ret = ERR_PTR(-EAFNOSUPPORT);
2911 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002912 }
2913
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04002914 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2915 xprt->address_strings[RPC_DISPLAY_ADDR],
2916 xprt->address_strings[RPC_DISPLAY_PORT],
2917 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002918
2919 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002920 * Once we've associated a backchannel xprt with a connection,
2921 * we want to keep it around as long as long as the connection
2922 * lasts, in case we need to start using it for a backchannel
2923 * again; this reference won't be dropped until bc_xprt is
2924 * destroyed.
2925 */
2926 xprt_get(xprt);
2927 args->bc_xprt->xpt_bc_xprt = xprt;
2928 xprt->bc_xprt = args->bc_xprt;
2929 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2930 transport->sock = bc_sock->sk_sock;
2931 transport->inet = bc_sock->sk_sk;
2932
2933 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002934 * Since we don't want connections for the backchannel, we set
2935 * the xprt status to connected
2936 */
2937 xprt_set_connected(xprt);
2938
2939
2940 if (try_module_get(THIS_MODULE))
2941 return xprt;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002942 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002943 ret = ERR_PTR(-EINVAL);
2944out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002945 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002946 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002947}
2948
Chuck Lever176e21e2011-05-09 15:22:44 -04002949static struct xprt_class xs_local_transport = {
2950 .list = LIST_HEAD_INIT(xs_local_transport.list),
2951 .name = "named UNIX socket",
2952 .owner = THIS_MODULE,
2953 .ident = XPRT_TRANSPORT_LOCAL,
2954 .setup = xs_setup_local,
2955};
2956
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002957static struct xprt_class xs_udp_transport = {
2958 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2959 .name = "udp",
2960 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002961 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002962 .setup = xs_setup_udp,
2963};
2964
2965static struct xprt_class xs_tcp_transport = {
2966 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2967 .name = "tcp",
2968 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002969 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002970 .setup = xs_setup_tcp,
2971};
2972
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002973static struct xprt_class xs_bc_tcp_transport = {
2974 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2975 .name = "tcp NFSv4.1 backchannel",
2976 .owner = THIS_MODULE,
2977 .ident = XPRT_TRANSPORT_BC_TCP,
2978 .setup = xs_setup_bc_tcp,
2979};
2980
Chuck Lever282b32e2006-12-05 16:35:51 -05002981/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002982 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002983 *
2984 */
2985int init_socket_xprt(void)
2986{
Chuck Leverfbf76682006-12-05 16:35:54 -05002987#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002988 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002989 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002990#endif
2991
Chuck Lever176e21e2011-05-09 15:22:44 -04002992 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002993 xprt_register_transport(&xs_udp_transport);
2994 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002995 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002996
Chuck Lever282b32e2006-12-05 16:35:51 -05002997 return 0;
2998}
2999
3000/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003001 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05003002 *
3003 */
3004void cleanup_socket_xprt(void)
3005{
Chuck Leverfbf76682006-12-05 16:35:54 -05003006#ifdef RPC_DEBUG
3007 if (sunrpc_table_header) {
3008 unregister_sysctl_table(sunrpc_table_header);
3009 sunrpc_table_header = NULL;
3010 }
3011#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003012
Chuck Lever176e21e2011-05-09 15:22:44 -04003013 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003014 xprt_unregister_transport(&xs_udp_transport);
3015 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003016 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05003017}
Trond Myklebustcbf11072009-08-09 15:06:19 -04003018
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003019static int param_set_uint_minmax(const char *val,
3020 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003021 unsigned int min, unsigned int max)
3022{
3023 unsigned long num;
3024 int ret;
3025
3026 if (!val)
3027 return -EINVAL;
3028 ret = strict_strtoul(val, 0, &num);
3029 if (ret == -EINVAL || num < min || num > max)
3030 return -EINVAL;
3031 *((unsigned int *)kp->arg) = num;
3032 return 0;
3033}
3034
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003035static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003036{
3037 return param_set_uint_minmax(val, kp,
3038 RPC_MIN_RESVPORT,
3039 RPC_MAX_RESVPORT);
3040}
3041
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003042static struct kernel_param_ops param_ops_portnr = {
3043 .set = param_set_portnr,
3044 .get = param_get_uint,
3045};
3046
Trond Myklebustcbf11072009-08-09 15:06:19 -04003047#define param_check_portnr(name, p) \
3048 __param_check(name, p, unsigned int);
3049
3050module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3051module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3052
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003053static int param_set_slot_table_size(const char *val,
3054 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003055{
3056 return param_set_uint_minmax(val, kp,
3057 RPC_MIN_SLOT_TABLE,
3058 RPC_MAX_SLOT_TABLE);
3059}
3060
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003061static struct kernel_param_ops param_ops_slot_table_size = {
3062 .set = param_set_slot_table_size,
3063 .get = param_get_uint,
3064};
3065
Trond Myklebustcbf11072009-08-09 15:06:19 -04003066#define param_check_slot_table_size(name, p) \
3067 __param_check(name, p, unsigned int);
3068
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003069static int param_set_max_slot_table_size(const char *val,
3070 const struct kernel_param *kp)
3071{
3072 return param_set_uint_minmax(val, kp,
3073 RPC_MIN_SLOT_TABLE,
3074 RPC_MAX_SLOT_TABLE_LIMIT);
3075}
3076
3077static struct kernel_param_ops param_ops_max_slot_table_size = {
3078 .set = param_set_max_slot_table_size,
3079 .get = param_get_uint,
3080};
3081
3082#define param_check_max_slot_table_size(name, p) \
3083 __param_check(name, p, unsigned int);
3084
Trond Myklebustcbf11072009-08-09 15:06:19 -04003085module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3086 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003087module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3088 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003089module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3090 slot_table_size, 0644);
3091