blob: a4709bbf8e5e658c949b503494bb492f6d96a319 [file] [log] [blame]
Chuck Levera246b012005-08-11 16:25:23 -04001/*
2 * linux/net/sunrpc/xprtsock.c
3 *
4 * Client-side transport implementation for sockets.
5 *
Alan Cox113aa832008-10-13 19:01:08 -07006 * TCP callback races fixes (C) 1998 Red Hat
7 * TCP send fixes (C) 1998 Red Hat
Chuck Levera246b012005-08-11 16:25:23 -04008 * TCP NFS related read + write fixes
9 * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
10 *
11 * Rewrite of larges part of the code in order to stabilize TCP stuff.
12 * Fix behaviour when socket buffer is full.
13 * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
Chuck Lever55aa4f52005-08-11 16:25:47 -040014 *
15 * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
Chuck Lever8f9d5b12007-08-06 11:57:53 -040016 *
17 * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
18 * <gilles.quillard@bull.net>
Chuck Levera246b012005-08-11 16:25:23 -040019 */
20
21#include <linux/types.h>
Chuck Lever176e21e2011-05-09 15:22:44 -040022#include <linux/string.h>
Chuck Levera246b012005-08-11 16:25:23 -040023#include <linux/slab.h>
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -040024#include <linux/module.h>
Chuck Levera246b012005-08-11 16:25:23 -040025#include <linux/capability.h>
Chuck Levera246b012005-08-11 16:25:23 -040026#include <linux/pagemap.h>
27#include <linux/errno.h>
28#include <linux/socket.h>
29#include <linux/in.h>
30#include <linux/net.h>
31#include <linux/mm.h>
Chuck Lever176e21e2011-05-09 15:22:44 -040032#include <linux/un.h>
Chuck Levera246b012005-08-11 16:25:23 -040033#include <linux/udp.h>
34#include <linux/tcp.h>
35#include <linux/sunrpc/clnt.h>
Jeff Layton59766872013-02-04 12:50:00 -050036#include <linux/sunrpc/addr.h>
Chuck Lever02107142006-01-03 09:55:49 +010037#include <linux/sunrpc/sched.h>
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030038#include <linux/sunrpc/svcsock.h>
\"Talpey, Thomas\49c36fc2007-09-10 13:47:31 -040039#include <linux/sunrpc/xprtsock.h>
Chuck Levera246b012005-08-11 16:25:23 -040040#include <linux/file.h>
Trond Myklebust9e00abc2011-07-13 19:20:49 -040041#ifdef CONFIG_SUNRPC_BACKCHANNEL
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -040042#include <linux/sunrpc/bc_xprt.h>
43#endif
Chuck Levera246b012005-08-11 16:25:23 -040044
45#include <net/sock.h>
46#include <net/checksum.h>
47#include <net/udp.h>
48#include <net/tcp.h>
49
Trond Myklebust40b5ea02013-09-04 12:16:23 -040050#include <trace/events/sunrpc.h>
51
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030052#include "sunrpc.h"
Chuck Lever176e21e2011-05-09 15:22:44 -040053
54static void xs_close(struct rpc_xprt *xprt);
55
Chuck Lever9903cd12005-08-11 16:25:26 -040056/*
Chuck Leverc556b752005-11-01 12:24:48 -050057 * xprtsock tunables
58 */
Trond Myklebust09acfea2012-03-11 15:22:54 -040059static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
60static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
61static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
Chuck Leverc556b752005-11-01 12:24:48 -050062
Trond Myklebust09acfea2012-03-11 15:22:54 -040063static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
64static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
Chuck Leverc556b752005-11-01 12:24:48 -050065
Trond Myklebust7d1e8252009-03-11 14:38:03 -040066#define XS_TCP_LINGER_TO (15U * HZ)
Trond Myklebust25fe6142009-03-11 14:38:03 -040067static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
Trond Myklebust7d1e8252009-03-11 14:38:03 -040068
Chuck Leverc556b752005-11-01 12:24:48 -050069/*
Chuck Leverfbf76682006-12-05 16:35:54 -050070 * We can register our own files under /proc/sys/sunrpc by
71 * calling register_sysctl_table() again. The files in that
72 * directory become the union of all files registered there.
73 *
74 * We simply need to make sure that we don't collide with
75 * someone else's file names!
76 */
77
78#ifdef RPC_DEBUG
79
80static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
81static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
Trond Myklebustd9ba1312011-07-17 18:11:30 -040082static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Leverfbf76682006-12-05 16:35:54 -050083static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
84static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
85
86static struct ctl_table_header *sunrpc_table_header;
87
88/*
89 * FIXME: changing the UDP slot table size should also resize the UDP
90 * socket buffers for existing UDP transports
91 */
Joe Perchesfe2c6332013-06-11 23:04:25 -070092static struct ctl_table xs_tunables_table[] = {
Chuck Leverfbf76682006-12-05 16:35:54 -050093 {
Chuck Leverfbf76682006-12-05 16:35:54 -050094 .procname = "udp_slot_table_entries",
95 .data = &xprt_udp_slot_table_entries,
96 .maxlen = sizeof(unsigned int),
97 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080098 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -050099 .extra1 = &min_slot_table_size,
100 .extra2 = &max_slot_table_size
101 },
102 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500103 .procname = "tcp_slot_table_entries",
104 .data = &xprt_tcp_slot_table_entries,
105 .maxlen = sizeof(unsigned int),
106 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800107 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500108 .extra1 = &min_slot_table_size,
109 .extra2 = &max_slot_table_size
110 },
111 {
Trond Myklebustd9ba1312011-07-17 18:11:30 -0400112 .procname = "tcp_max_slot_table_entries",
113 .data = &xprt_max_tcp_slot_table_entries,
114 .maxlen = sizeof(unsigned int),
115 .mode = 0644,
116 .proc_handler = proc_dointvec_minmax,
117 .extra1 = &min_slot_table_size,
118 .extra2 = &max_tcp_slot_table_limit
119 },
120 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500121 .procname = "min_resvport",
122 .data = &xprt_min_resvport,
123 .maxlen = sizeof(unsigned int),
124 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800125 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500126 .extra1 = &xprt_min_resvport_limit,
127 .extra2 = &xprt_max_resvport_limit
128 },
129 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500130 .procname = "max_resvport",
131 .data = &xprt_max_resvport,
132 .maxlen = sizeof(unsigned int),
133 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800134 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500135 .extra1 = &xprt_min_resvport_limit,
136 .extra2 = &xprt_max_resvport_limit
137 },
138 {
Trond Myklebust25fe6142009-03-11 14:38:03 -0400139 .procname = "tcp_fin_timeout",
140 .data = &xs_tcp_fin_timeout,
141 .maxlen = sizeof(xs_tcp_fin_timeout),
142 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800143 .proc_handler = proc_dointvec_jiffies,
Trond Myklebust25fe6142009-03-11 14:38:03 -0400144 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800145 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500146};
147
Joe Perchesfe2c6332013-06-11 23:04:25 -0700148static struct ctl_table sunrpc_table[] = {
Chuck Leverfbf76682006-12-05 16:35:54 -0500149 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500150 .procname = "sunrpc",
151 .mode = 0555,
152 .child = xs_tunables_table
153 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800154 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500155};
156
157#endif
158
159/*
Chuck Lever03bf4b72005-08-25 16:25:55 -0700160 * Wait duration for a reply from the RPC portmapper.
161 */
162#define XS_BIND_TO (60U * HZ)
163
164/*
165 * Delay if a UDP socket connect error occurs. This is most likely some
166 * kind of resource problem on the local host.
167 */
168#define XS_UDP_REEST_TO (2U * HZ)
169
170/*
171 * The reestablish timeout allows clients to delay for a bit before attempting
172 * to reconnect to a server that just dropped our connection.
173 *
174 * We implement an exponential backoff when trying to reestablish a TCP
175 * transport connection with the server. Some servers like to drop a TCP
176 * connection when they are overworked, so we start with a short timeout and
177 * increase over time if the server is down or not responding.
178 */
179#define XS_TCP_INIT_REEST_TO (3U * HZ)
180#define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
181
182/*
183 * TCP idle timeout; client drops the transport socket if it is idle
184 * for this long. Note that we also timeout UDP sockets to prevent
185 * holding port numbers when there is no RPC traffic.
186 */
187#define XS_IDLE_DISC_TO (5U * 60 * HZ)
188
Chuck Levera246b012005-08-11 16:25:23 -0400189#ifdef RPC_DEBUG
190# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400191# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400192#endif
193
Chuck Levera246b012005-08-11 16:25:23 -0400194#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400195static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400196{
Chuck Lever9903cd12005-08-11 16:25:26 -0400197 u8 *buf = (u8 *) packet;
198 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400199
Chuck Lever46121cf2007-01-31 12:14:08 -0500200 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400201 for (j = 0; j < count && j < 128; j += 4) {
202 if (!(j & 31)) {
203 if (j)
204 dprintk("\n");
205 dprintk("0x%04x ", j);
206 }
207 dprintk("%02x%02x%02x%02x ",
208 buf[j], buf[j+1], buf[j+2], buf[j+3]);
209 }
210 dprintk("\n");
211}
212#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400213static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400214{
215 /* NOP */
216}
217#endif
218
Chuck Leverffc2e512006-12-05 16:35:11 -0500219struct sock_xprt {
220 struct rpc_xprt xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500221
222 /*
223 * Network layer
224 */
225 struct socket * sock;
226 struct sock * inet;
Chuck Lever51971132006-12-05 16:35:19 -0500227
228 /*
229 * State of TCP reply receive
230 */
231 __be32 tcp_fraghdr,
Trond Myklebustb76ce562010-06-16 13:57:32 -0400232 tcp_xid,
233 tcp_calldir;
Chuck Lever51971132006-12-05 16:35:19 -0500234
235 u32 tcp_offset,
236 tcp_reclen;
237
238 unsigned long tcp_copied,
239 tcp_flags;
Chuck Leverc8475462006-12-05 16:35:26 -0500240
241 /*
242 * Connection of transports
243 */
Trond Myklebust34161db2006-12-07 15:48:15 -0500244 struct delayed_work connect_worker;
Chuck Leverfbfffbd2009-08-09 15:09:46 -0400245 struct sockaddr_storage srcaddr;
246 unsigned short srcport;
Chuck Lever7c6e0662006-12-05 16:35:30 -0500247
248 /*
249 * UDP socket buffer size parameters
250 */
251 size_t rcvsize,
252 sndsize;
Chuck Lever314dfd72006-12-05 16:35:34 -0500253
254 /*
255 * Saved socket callback addresses
256 */
257 void (*old_data_ready)(struct sock *, int);
258 void (*old_state_change)(struct sock *);
259 void (*old_write_space)(struct sock *);
Chuck Leverffc2e512006-12-05 16:35:11 -0500260};
261
Chuck Levere136d092006-12-05 16:35:23 -0500262/*
263 * TCP receive state flags
264 */
265#define TCP_RCV_LAST_FRAG (1UL << 0)
266#define TCP_RCV_COPY_FRAGHDR (1UL << 1)
267#define TCP_RCV_COPY_XID (1UL << 2)
268#define TCP_RCV_COPY_DATA (1UL << 3)
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400269#define TCP_RCV_READ_CALLDIR (1UL << 4)
270#define TCP_RCV_COPY_CALLDIR (1UL << 5)
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400271
272/*
273 * TCP RPC flags
274 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400275#define TCP_RPC_REPLY (1UL << 6)
Chuck Levere136d092006-12-05 16:35:23 -0500276
Chuck Lever95392c52007-08-06 11:57:58 -0400277static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400278{
Chuck Lever95392c52007-08-06 11:57:58 -0400279 return (struct sockaddr *) &xprt->addr;
280}
281
Chuck Lever176e21e2011-05-09 15:22:44 -0400282static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
283{
284 return (struct sockaddr_un *) &xprt->addr;
285}
286
Chuck Lever95392c52007-08-06 11:57:58 -0400287static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
288{
289 return (struct sockaddr_in *) &xprt->addr;
290}
291
292static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
293{
294 return (struct sockaddr_in6 *) &xprt->addr;
295}
296
Chuck Leverc877b842009-08-09 15:09:36 -0400297static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400298{
Chuck Leverc877b842009-08-09 15:09:36 -0400299 struct sockaddr *sap = xs_addr(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400300 struct sockaddr_in6 *sin6;
301 struct sockaddr_in *sin;
Chuck Lever176e21e2011-05-09 15:22:44 -0400302 struct sockaddr_un *sun;
Chuck Leverc877b842009-08-09 15:09:36 -0400303 char buf[128];
Chuck Leveredb267a2006-08-22 20:06:18 -0400304
Chuck Lever9dc3b092009-08-09 15:09:46 -0400305 switch (sap->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400306 case AF_LOCAL:
307 sun = xs_addr_un(xprt);
308 strlcpy(buf, sun->sun_path, sizeof(buf));
309 xprt->address_strings[RPC_DISPLAY_ADDR] =
310 kstrdup(buf, GFP_KERNEL);
311 break;
Chuck Lever9dc3b092009-08-09 15:09:46 -0400312 case AF_INET:
Chuck Lever176e21e2011-05-09 15:22:44 -0400313 (void)rpc_ntop(sap, buf, sizeof(buf));
314 xprt->address_strings[RPC_DISPLAY_ADDR] =
315 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400316 sin = xs_addr_in(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800317 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
Chuck Lever9dc3b092009-08-09 15:09:46 -0400318 break;
319 case AF_INET6:
Chuck Lever176e21e2011-05-09 15:22:44 -0400320 (void)rpc_ntop(sap, buf, sizeof(buf));
321 xprt->address_strings[RPC_DISPLAY_ADDR] =
322 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400323 sin6 = xs_addr_in6(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800324 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400325 break;
326 default:
327 BUG();
Chuck Leveredb267a2006-08-22 20:06:18 -0400328 }
Chuck Lever176e21e2011-05-09 15:22:44 -0400329
Chuck Lever9dc3b092009-08-09 15:09:46 -0400330 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
Chuck Leveredb267a2006-08-22 20:06:18 -0400331}
332
Chuck Lever9dc3b092009-08-09 15:09:46 -0400333static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400334{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400335 struct sockaddr *sap = xs_addr(xprt);
336 char buf[128];
Chuck Lever4b6473f2007-08-06 11:57:12 -0400337
Joe Perches81160e662010-03-08 12:15:59 -0800338 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400339 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400340
Joe Perches81160e662010-03-08 12:15:59 -0800341 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400342 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
343}
Chuck Lever4b6473f2007-08-06 11:57:12 -0400344
Chuck Lever9dc3b092009-08-09 15:09:46 -0400345static void xs_format_peer_addresses(struct rpc_xprt *xprt,
346 const char *protocol,
347 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400348{
Chuck Leverb454ae92008-01-07 18:34:48 -0500349 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leverb454ae92008-01-07 18:34:48 -0500350 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leverc877b842009-08-09 15:09:36 -0400351 xs_format_common_peer_addresses(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400352 xs_format_common_peer_ports(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400353}
354
Chuck Lever9dc3b092009-08-09 15:09:46 -0400355static void xs_update_peer_port(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400356{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400357 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
358 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400359
Chuck Lever9dc3b092009-08-09 15:09:46 -0400360 xs_format_common_peer_ports(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400361}
362
363static void xs_free_peer_addresses(struct rpc_xprt *xprt)
364{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500365 unsigned int i;
366
367 for (i = 0; i < RPC_DISPLAY_MAX; i++)
368 switch (i) {
369 case RPC_DISPLAY_PROTO:
370 case RPC_DISPLAY_NETID:
371 continue;
372 default:
373 kfree(xprt->address_strings[i]);
374 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400375}
376
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400377#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
378
Trond Myklebust24c56842006-10-17 15:06:22 -0400379static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400380{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400381 struct msghdr msg = {
382 .msg_name = addr,
383 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400384 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
385 };
386 struct kvec iov = {
387 .iov_base = vec->iov_base + base,
388 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400389 };
390
Trond Myklebust24c56842006-10-17 15:06:22 -0400391 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400392 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
393 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
394}
395
Trond Myklebust24c56842006-10-17 15:06:22 -0400396static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400397{
Trond Myklebust24c56842006-10-17 15:06:22 -0400398 struct page **ppage;
399 unsigned int remainder;
400 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400401
Trond Myklebust24c56842006-10-17 15:06:22 -0400402 remainder = xdr->page_len - base;
403 base += xdr->page_base;
404 ppage = xdr->pages + (base >> PAGE_SHIFT);
405 base &= ~PAGE_MASK;
406 for(;;) {
407 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
408 int flags = XS_SENDMSG_FLAGS;
409
410 remainder -= len;
411 if (remainder != 0 || more)
412 flags |= MSG_MORE;
413 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
414 if (remainder == 0 || err != len)
415 break;
416 sent += err;
417 ppage++;
418 base = 0;
419 }
420 if (sent == 0)
421 return err;
422 if (err > 0)
423 sent += err;
424 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400425}
426
Chuck Lever9903cd12005-08-11 16:25:26 -0400427/**
428 * xs_sendpages - write pages directly to a socket
429 * @sock: socket to send on
430 * @addr: UDP only -- address of destination
431 * @addrlen: UDP only -- length of destination address
432 * @xdr: buffer containing this request
433 * @base: starting position in the buffer
434 *
Chuck Levera246b012005-08-11 16:25:23 -0400435 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400436static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
Chuck Levera246b012005-08-11 16:25:23 -0400437{
Trond Myklebust24c56842006-10-17 15:06:22 -0400438 unsigned int remainder = xdr->len - base;
439 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400440
Chuck Lever262965f2005-08-11 16:25:56 -0400441 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400442 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400443
444 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400445 if (base != 0) {
446 addr = NULL;
447 addrlen = 0;
448 }
Chuck Lever262965f2005-08-11 16:25:56 -0400449
Trond Myklebust24c56842006-10-17 15:06:22 -0400450 if (base < xdr->head[0].iov_len || addr != NULL) {
451 unsigned int len = xdr->head[0].iov_len - base;
452 remainder -= len;
453 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
454 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400455 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400456 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400457 base = 0;
458 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400459 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400460
Trond Myklebust24c56842006-10-17 15:06:22 -0400461 if (base < xdr->page_len) {
462 unsigned int len = xdr->page_len - base;
463 remainder -= len;
464 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
465 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400466 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400467 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400468 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400469 } else
470 base -= xdr->page_len;
471
472 if (base >= xdr->tail[0].iov_len)
473 return sent;
474 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400475out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400476 if (sent == 0)
477 return err;
478 if (err > 0)
479 sent += err;
480 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400481}
482
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400483static void xs_nospace_callback(struct rpc_task *task)
484{
485 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
486
487 transport->inet->sk_write_pending--;
488 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
489}
490
Chuck Lever9903cd12005-08-11 16:25:26 -0400491/**
Chuck Lever262965f2005-08-11 16:25:56 -0400492 * xs_nospace - place task on wait queue if transmit was incomplete
493 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400494 *
Chuck Levera246b012005-08-11 16:25:23 -0400495 */
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400496static int xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400497{
Chuck Lever262965f2005-08-11 16:25:56 -0400498 struct rpc_rqst *req = task->tk_rqstp;
499 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500500 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust24ca9a82011-11-22 14:44:28 +0200501 int ret = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400502
Chuck Lever46121cf2007-01-31 12:14:08 -0500503 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400504 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
505 req->rq_slen);
506
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400507 /* Protect against races with write_space */
508 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400509
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400510 /* Don't race with disconnect */
511 if (xprt_connected(xprt)) {
512 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
513 /*
514 * Notify TCP that we're limited by the application
515 * window size
516 */
517 set_bit(SOCK_NOSPACE, &transport->sock->flags);
518 transport->inet->sk_write_pending++;
519 /* ...and wait for more buffer space */
520 xprt_wait_for_buffer_space(task, xs_nospace_callback);
521 }
522 } else {
523 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400524 ret = -ENOTCONN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400525 }
Chuck Lever262965f2005-08-11 16:25:56 -0400526
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400527 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400528 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400529}
530
Chuck Lever61677ee2011-05-09 15:22:34 -0400531/*
532 * Construct a stream transport record marker in @buf.
533 */
534static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
535{
536 u32 reclen = buf->len - sizeof(rpc_fraghdr);
537 rpc_fraghdr *base = buf->head[0].iov_base;
538 *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
539}
540
Chuck Lever262965f2005-08-11 16:25:56 -0400541/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400542 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
543 * @task: RPC task that manages the state of an RPC request
544 *
545 * Return values:
546 * 0: The request has been sent
547 * EAGAIN: The socket was blocked, please call again later to
548 * complete the request
549 * ENOTCONN: Caller needs to invoke connect logic then call again
550 * other: Some other error occured, the request was not sent
551 */
552static int xs_local_send_request(struct rpc_task *task)
553{
554 struct rpc_rqst *req = task->tk_rqstp;
555 struct rpc_xprt *xprt = req->rq_xprt;
556 struct sock_xprt *transport =
557 container_of(xprt, struct sock_xprt, xprt);
558 struct xdr_buf *xdr = &req->rq_snd_buf;
559 int status;
560
561 xs_encode_stream_record_marker(&req->rq_snd_buf);
562
563 xs_pktdump("packet data:",
564 req->rq_svec->iov_base, req->rq_svec->iov_len);
565
566 status = xs_sendpages(transport->sock, NULL, 0,
567 xdr, req->rq_bytes_sent);
568 dprintk("RPC: %s(%u) = %d\n",
569 __func__, xdr->len - req->rq_bytes_sent, status);
570 if (likely(status >= 0)) {
571 req->rq_bytes_sent += status;
572 req->rq_xmit_bytes_sent += status;
573 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
574 req->rq_bytes_sent = 0;
575 return 0;
576 }
577 status = -EAGAIN;
578 }
579
580 switch (status) {
581 case -EAGAIN:
582 status = xs_nospace(task);
583 break;
584 default:
585 dprintk("RPC: sendmsg returned unrecognized error %d\n",
586 -status);
587 case -EPIPE:
588 xs_close(xprt);
589 status = -ENOTCONN;
590 }
591
592 return status;
593}
594
595/**
Chuck Lever262965f2005-08-11 16:25:56 -0400596 * xs_udp_send_request - write an RPC request to a UDP socket
597 * @task: address of RPC task that manages the state of an RPC request
598 *
599 * Return values:
600 * 0: The request has been sent
601 * EAGAIN: The socket was blocked, please call again later to
602 * complete the request
603 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300604 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -0400605 */
606static int xs_udp_send_request(struct rpc_task *task)
607{
608 struct rpc_rqst *req = task->tk_rqstp;
609 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500610 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400611 struct xdr_buf *xdr = &req->rq_snd_buf;
612 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400613
Chuck Lever9903cd12005-08-11 16:25:26 -0400614 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400615 req->rq_svec->iov_base,
616 req->rq_svec->iov_len);
617
Trond Myklebust01d37c42009-03-11 14:09:39 -0400618 if (!xprt_bound(xprt))
619 return -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500620 status = xs_sendpages(transport->sock,
Chuck Lever95392c52007-08-06 11:57:58 -0400621 xs_addr(xprt),
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500622 xprt->addrlen, xdr,
623 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400624
Chuck Lever46121cf2007-01-31 12:14:08 -0500625 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400626 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400627
Trond Myklebust21997002007-10-01 11:43:37 -0400628 if (status >= 0) {
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400629 req->rq_xmit_bytes_sent += status;
Trond Myklebust21997002007-10-01 11:43:37 -0400630 if (status >= req->rq_slen)
631 return 0;
632 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400633 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400634 }
Chuck Levera246b012005-08-11 16:25:23 -0400635
Chuck Lever262965f2005-08-11 16:25:56 -0400636 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400637 case -ENOTSOCK:
638 status = -ENOTCONN;
639 /* Should we call xs_close() here? */
640 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400641 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400642 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400643 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400644 default:
645 dprintk("RPC: sendmsg returned unrecognized error %d\n",
646 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400647 case -ENETUNREACH:
648 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400649 case -ECONNREFUSED:
650 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400651 * prompts ECONNREFUSED. */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400652 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400653 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800654
Chuck Lever262965f2005-08-11 16:25:56 -0400655 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400656}
657
Trond Myklebuste06799f2007-11-05 15:44:12 -0500658/**
659 * xs_tcp_shutdown - gracefully shut down a TCP socket
660 * @xprt: transport
661 *
662 * Initiates a graceful shutdown of the TCP socket by calling the
663 * equivalent of shutdown(SHUT_WR);
664 */
665static void xs_tcp_shutdown(struct rpc_xprt *xprt)
666{
667 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
668 struct socket *sock = transport->sock;
669
Trond Myklebust40b5ea02013-09-04 12:16:23 -0400670 if (sock != NULL) {
Trond Myklebuste06799f2007-11-05 15:44:12 -0500671 kernel_sock_shutdown(sock, SHUT_WR);
Trond Myklebust40b5ea02013-09-04 12:16:23 -0400672 trace_rpc_socket_shutdown(xprt, sock);
673 }
Trond Myklebuste06799f2007-11-05 15:44:12 -0500674}
675
Chuck Lever9903cd12005-08-11 16:25:26 -0400676/**
Chuck Lever262965f2005-08-11 16:25:56 -0400677 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400678 * @task: address of RPC task that manages the state of an RPC request
679 *
680 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400681 * 0: The request has been sent
682 * EAGAIN: The socket was blocked, please call again later to
683 * complete the request
684 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300685 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400686 *
687 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400688 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400689 */
Chuck Lever262965f2005-08-11 16:25:56 -0400690static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400691{
692 struct rpc_rqst *req = task->tk_rqstp;
693 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500694 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400695 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Leverb595bb12007-08-06 11:56:42 -0400696 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400697
Chuck Lever61677ee2011-05-09 15:22:34 -0400698 xs_encode_stream_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400699
Chuck Lever262965f2005-08-11 16:25:56 -0400700 xs_pktdump("packet data:",
701 req->rq_svec->iov_base,
702 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400703
704 /* Continue transmitting the packet/record. We must be careful
705 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400706 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400707 while (1) {
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500708 status = xs_sendpages(transport->sock,
709 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400710
Chuck Lever46121cf2007-01-31 12:14:08 -0500711 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400712 xdr->len - req->rq_bytes_sent, status);
713
714 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400715 break;
716
Chuck Lever262965f2005-08-11 16:25:56 -0400717 /* If we've sent the entire packet, immediately
718 * reset the count of bytes sent. */
719 req->rq_bytes_sent += status;
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400720 req->rq_xmit_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400721 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
722 req->rq_bytes_sent = 0;
723 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400724 }
725
Trond Myklebust06b4b682008-04-16 16:51:38 -0400726 if (status != 0)
727 continue;
Chuck Levera246b012005-08-11 16:25:23 -0400728 status = -EAGAIN;
Trond Myklebust06b4b682008-04-16 16:51:38 -0400729 break;
Chuck Levera246b012005-08-11 16:25:23 -0400730 }
731
Chuck Lever262965f2005-08-11 16:25:56 -0400732 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400733 case -ENOTSOCK:
734 status = -ENOTCONN;
735 /* Should we call xs_close() here? */
736 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400737 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400738 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400739 break;
740 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500741 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400742 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400743 case -ECONNRESET:
Trond Myklebuste06799f2007-11-05 15:44:12 -0500744 xs_tcp_shutdown(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400745 case -ECONNREFUSED:
746 case -ENOTCONN:
Trond Myklebustb9d2bb22012-10-23 11:40:02 -0400747 case -EPIPE:
Chuck Lever262965f2005-08-11 16:25:56 -0400748 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400749 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800750
Chuck Levera246b012005-08-11 16:25:23 -0400751 return status;
752}
753
Chuck Lever9903cd12005-08-11 16:25:26 -0400754/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400755 * xs_tcp_release_xprt - clean up after a tcp transmission
756 * @xprt: transport
757 * @task: rpc task
758 *
759 * This cleans up if an error causes us to abort the transmission of a request.
760 * In this case, the socket may need to be reset in order to avoid confusing
761 * the server.
762 */
763static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
764{
765 struct rpc_rqst *req;
766
767 if (task != xprt->snd_task)
768 return;
769 if (task == NULL)
770 goto out_release;
771 req = task->tk_rqstp;
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400772 if (req == NULL)
773 goto out_release;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400774 if (req->rq_bytes_sent == 0)
775 goto out_release;
776 if (req->rq_bytes_sent == req->rq_snd_buf.len)
777 goto out_release;
Trond Myklebusta4f08352013-01-08 09:10:21 -0500778 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400779out_release:
780 xprt_release_xprt(xprt, task);
781}
782
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400783static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
784{
785 transport->old_data_ready = sk->sk_data_ready;
786 transport->old_state_change = sk->sk_state_change;
787 transport->old_write_space = sk->sk_write_space;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400788}
789
790static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
791{
792 sk->sk_data_ready = transport->old_data_ready;
793 sk->sk_state_change = transport->old_state_change;
794 sk->sk_write_space = transport->old_write_space;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400795}
796
Chuck 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
J. Bruce Fields190b1ecf2013-03-08 13:13:08 -0500859static void xs_local_destroy(struct rpc_xprt *xprt)
860{
861 xs_close(xprt);
862 xs_free_peer_addresses(xprt);
863 xprt_free(xprt);
864 module_put(THIS_MODULE);
865}
866
Chuck Lever9903cd12005-08-11 16:25:26 -0400867/**
868 * xs_destroy - prepare to shutdown a transport
869 * @xprt: doomed transport
870 *
871 */
872static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400873{
Chuck Lever46121cf2007-01-31 12:14:08 -0500874 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400875
J. Bruce Fields190b1ecf2013-03-08 13:13:08 -0500876 xs_local_destroy(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400877}
878
Chuck Lever9903cd12005-08-11 16:25:26 -0400879static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400880{
Chuck Lever9903cd12005-08-11 16:25:26 -0400881 return (struct rpc_xprt *) sk->sk_user_data;
882}
883
Chuck Lever176e21e2011-05-09 15:22:44 -0400884static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
885{
886 struct xdr_skb_reader desc = {
887 .skb = skb,
888 .offset = sizeof(rpc_fraghdr),
889 .count = skb->len - sizeof(rpc_fraghdr),
890 };
891
892 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
893 return -1;
894 if (desc.count)
895 return -1;
896 return 0;
897}
898
899/**
900 * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets
901 * @sk: socket with data to read
902 * @len: how much data to read
903 *
904 * Currently this assumes we can read the whole reply in a single gulp.
905 */
906static void xs_local_data_ready(struct sock *sk, int len)
907{
908 struct rpc_task *task;
909 struct rpc_xprt *xprt;
910 struct rpc_rqst *rovr;
911 struct sk_buff *skb;
912 int err, repsize, copied;
913 u32 _xid;
914 __be32 *xp;
915
916 read_lock_bh(&sk->sk_callback_lock);
917 dprintk("RPC: %s...\n", __func__);
918 xprt = xprt_from_sock(sk);
919 if (xprt == NULL)
920 goto out;
921
922 skb = skb_recv_datagram(sk, 0, 1, &err);
923 if (skb == NULL)
924 goto out;
925
Chuck Lever176e21e2011-05-09 15:22:44 -0400926 repsize = skb->len - sizeof(rpc_fraghdr);
927 if (repsize < 4) {
928 dprintk("RPC: impossible RPC reply size %d\n", repsize);
929 goto dropit;
930 }
931
932 /* Copy the XID from the skb... */
933 xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
934 if (xp == NULL)
935 goto dropit;
936
937 /* Look up and lock the request corresponding to the given XID */
938 spin_lock(&xprt->transport_lock);
939 rovr = xprt_lookup_rqst(xprt, *xp);
940 if (!rovr)
941 goto out_unlock;
942 task = rovr->rq_task;
943
944 copied = rovr->rq_private_buf.buflen;
945 if (copied > repsize)
946 copied = repsize;
947
948 if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
949 dprintk("RPC: sk_buff copy failed\n");
950 goto out_unlock;
951 }
952
953 xprt_complete_rqst(task, copied);
954
955 out_unlock:
956 spin_unlock(&xprt->transport_lock);
957 dropit:
958 skb_free_datagram(sk, skb);
959 out:
960 read_unlock_bh(&sk->sk_callback_lock);
961}
962
Chuck Lever9903cd12005-08-11 16:25:26 -0400963/**
964 * xs_udp_data_ready - "data ready" callback for UDP sockets
965 * @sk: socket with data to read
966 * @len: how much data to read
967 *
968 */
969static void xs_udp_data_ready(struct sock *sk, int len)
970{
971 struct rpc_task *task;
972 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400973 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400974 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400975 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700976 u32 _xid;
977 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400978
Eric Dumazetf064af12010-09-22 12:43:39 +0000979 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500980 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400981 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400982 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400983
984 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
985 goto out;
986
Chuck Levera246b012005-08-11 16:25:23 -0400987 repsize = skb->len - sizeof(struct udphdr);
988 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500989 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400990 goto dropit;
991 }
992
993 /* Copy the XID from the skb... */
994 xp = skb_header_pointer(skb, sizeof(struct udphdr),
995 sizeof(_xid), &_xid);
996 if (xp == NULL)
997 goto dropit;
998
999 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001000 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001001 rovr = xprt_lookup_rqst(xprt, *xp);
1002 if (!rovr)
1003 goto out_unlock;
1004 task = rovr->rq_task;
1005
Chuck Levera246b012005-08-11 16:25:23 -04001006 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1007 copied = repsize;
1008
1009 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001010 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
1011 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -04001012 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001013 }
1014
1015 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -04001016
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001017 xprt_adjust_cwnd(xprt, task, copied);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001018 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -04001019
1020 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001021 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001022 dropit:
1023 skb_free_datagram(sk, skb);
1024 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001025 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001026}
1027
Trond Myklebusta519fc72012-09-12 16:49:15 -04001028/*
1029 * Helper function to force a TCP close if the server is sending
1030 * junk and/or it has put us in CLOSE_WAIT
1031 */
1032static void xs_tcp_force_close(struct rpc_xprt *xprt)
1033{
1034 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1035 xprt_force_disconnect(xprt);
1036}
1037
Chuck Leverdd456472006-12-05 16:35:44 -05001038static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001039{
Chuck Lever51971132006-12-05 16:35:19 -05001040 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001041 size_t len, used;
1042 char *p;
1043
Chuck Lever51971132006-12-05 16:35:19 -05001044 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
1045 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001046 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001047 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001048 if (used != len)
1049 return;
Chuck Lever808012f2005-08-25 16:25:49 -07001050
Chuck Lever51971132006-12-05 16:35:19 -05001051 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
1052 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -05001053 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -04001054 else
Chuck Levere136d092006-12-05 16:35:23 -05001055 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -05001056 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -07001057
Chuck Levere136d092006-12-05 16:35:23 -05001058 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001059 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -07001060
Chuck Levera246b012005-08-11 16:25:23 -04001061 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001062 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001063 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebusta519fc72012-09-12 16:49:15 -04001064 xs_tcp_force_close(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001065 return;
Chuck Levera246b012005-08-11 16:25:23 -04001066 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001067 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -05001068 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001069}
1070
Chuck Lever51971132006-12-05 16:35:19 -05001071static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001072{
Chuck Lever51971132006-12-05 16:35:19 -05001073 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001074 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001075 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001076 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
1077 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1078 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001079 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001080 }
1081 }
1082}
1083
Chuck Leverdd456472006-12-05 16:35:44 -05001084static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001085{
1086 size_t len, used;
1087 char *p;
1088
Chuck Lever51971132006-12-05 16:35:19 -05001089 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -05001090 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001091 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001092 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001093 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001094 if (used != len)
1095 return;
Chuck Levere136d092006-12-05 16:35:23 -05001096 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001097 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -05001098 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001099 dprintk("RPC: reading %s XID %08x\n",
1100 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1101 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -05001102 ntohl(transport->tcp_xid));
1103 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001104}
1105
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001106static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1107 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001108{
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001109 size_t len, used;
1110 u32 offset;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001111 char *p;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001112
1113 /*
1114 * We want transport->tcp_offset to be 8 at the end of this routine
1115 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1116 * When this function is called for the first time,
1117 * transport->tcp_offset is 4 (after having already read the xid).
1118 */
1119 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001120 len = sizeof(transport->tcp_calldir) - offset;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001121 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001122 p = ((char *) &transport->tcp_calldir) + offset;
1123 used = xdr_skb_read_bits(desc, p, len);
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001124 transport->tcp_offset += used;
1125 if (used != len)
1126 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001127 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001128 /*
1129 * We don't yet have the XDR buffer, so we will write the calldir
1130 * out after we get the buffer from the 'struct rpc_rqst'
1131 */
Trond Myklebustb76ce562010-06-16 13:57:32 -04001132 switch (ntohl(transport->tcp_calldir)) {
1133 case RPC_REPLY:
1134 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1135 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001136 transport->tcp_flags |= TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001137 break;
1138 case RPC_CALL:
1139 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1140 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001141 transport->tcp_flags &= ~TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001142 break;
1143 default:
1144 dprintk("RPC: invalid request message type\n");
Trond Myklebusta519fc72012-09-12 16:49:15 -04001145 xs_tcp_force_close(&transport->xprt);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001146 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001147 xs_tcp_check_fraghdr(transport);
1148}
1149
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001150static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
1151 struct xdr_skb_reader *desc,
1152 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001153{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001154 struct sock_xprt *transport =
1155 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001156 struct xdr_buf *rcvbuf;
1157 size_t len;
1158 ssize_t r;
1159
Chuck Levera246b012005-08-11 16:25:23 -04001160 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001161
1162 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1163 /*
1164 * Save the RPC direction in the XDR buffer
1165 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001166 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
Trond Myklebustb76ce562010-06-16 13:57:32 -04001167 &transport->tcp_calldir,
1168 sizeof(transport->tcp_calldir));
1169 transport->tcp_copied += sizeof(transport->tcp_calldir);
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001170 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -04001171 }
1172
Chuck Levera246b012005-08-11 16:25:23 -04001173 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -05001174 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -05001175 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -04001176
Chuck Lever51971132006-12-05 16:35:19 -05001177 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001178 memcpy(&my_desc, desc, sizeof(my_desc));
1179 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -05001180 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001181 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001182 desc->count -= r;
1183 desc->offset += r;
1184 } else
Chuck Lever51971132006-12-05 16:35:19 -05001185 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001186 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001187
1188 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001189 transport->tcp_copied += r;
1190 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001191 }
1192 if (r != len) {
1193 /* Error when copying to the receive buffer,
1194 * usually because we weren't able to allocate
1195 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001196 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001197 * will not receive any additional updates,
1198 * and time out.
1199 * Any remaining data from this record will
1200 * be discarded.
1201 */
Chuck Levere136d092006-12-05 16:35:23 -05001202 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001203 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001204 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001205 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1206 "tcp_offset = %u, tcp_reclen = %u\n",
1207 xprt, transport->tcp_copied,
1208 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001209 return;
Chuck Levera246b012005-08-11 16:25:23 -04001210 }
1211
Chuck Lever46121cf2007-01-31 12:14:08 -05001212 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001213 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001214 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1215 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1216 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001217
Chuck Lever51971132006-12-05 16:35:19 -05001218 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001219 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001220 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001221 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1222 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001223 }
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001224}
1225
1226/*
1227 * Finds the request corresponding to the RPC xid and invokes the common
1228 * tcp read code to read the data.
1229 */
1230static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1231 struct xdr_skb_reader *desc)
1232{
1233 struct sock_xprt *transport =
1234 container_of(xprt, struct sock_xprt, xprt);
1235 struct rpc_rqst *req;
1236
1237 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1238
1239 /* Find and lock the request corresponding to this xid */
1240 spin_lock(&xprt->transport_lock);
1241 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1242 if (!req) {
1243 dprintk("RPC: XID %08x request not found!\n",
1244 ntohl(transport->tcp_xid));
1245 spin_unlock(&xprt->transport_lock);
1246 return -1;
1247 }
1248
1249 xs_tcp_read_common(xprt, desc, req);
1250
Chuck Levere136d092006-12-05 16:35:23 -05001251 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001252 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001253
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001254 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001255 return 0;
1256}
1257
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001258#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001259/*
1260 * Obtains an rpc_rqst previously allocated and invokes the common
1261 * tcp read code to read the data. The result is placed in the callback
1262 * queue.
1263 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1264 * connection and return -1.
1265 */
1266static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1267 struct xdr_skb_reader *desc)
1268{
1269 struct sock_xprt *transport =
1270 container_of(xprt, struct sock_xprt, xprt);
1271 struct rpc_rqst *req;
1272
1273 req = xprt_alloc_bc_request(xprt);
1274 if (req == NULL) {
1275 printk(KERN_WARNING "Callback slot table overflowed\n");
1276 xprt_force_disconnect(xprt);
1277 return -1;
1278 }
1279
1280 req->rq_xid = transport->tcp_xid;
1281 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1282 xs_tcp_read_common(xprt, desc, req);
1283
1284 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1285 struct svc_serv *bc_serv = xprt->bc_serv;
1286
1287 /*
1288 * Add callback request to callback list. The callback
1289 * service sleeps on the sv_cb_waitq waiting for new
1290 * requests. Wake it up after adding enqueing the
1291 * request.
1292 */
1293 dprintk("RPC: add callback request to list\n");
1294 spin_lock(&bc_serv->sv_cb_lock);
1295 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1296 spin_unlock(&bc_serv->sv_cb_lock);
1297 wake_up(&bc_serv->sv_cb_waitq);
1298 }
1299
1300 req->rq_private_buf.len = transport->tcp_copied;
1301
1302 return 0;
1303}
1304
1305static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1306 struct xdr_skb_reader *desc)
1307{
1308 struct sock_xprt *transport =
1309 container_of(xprt, struct sock_xprt, xprt);
1310
1311 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1312 xs_tcp_read_reply(xprt, desc) :
1313 xs_tcp_read_callback(xprt, desc);
1314}
1315#else
1316static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1317 struct xdr_skb_reader *desc)
1318{
1319 return xs_tcp_read_reply(xprt, desc);
1320}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001321#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001322
1323/*
1324 * Read data off the transport. This can be either an RPC_CALL or an
1325 * RPC_REPLY. Relay the processing to helper functions.
1326 */
1327static void xs_tcp_read_data(struct rpc_xprt *xprt,
1328 struct xdr_skb_reader *desc)
1329{
1330 struct sock_xprt *transport =
1331 container_of(xprt, struct sock_xprt, xprt);
1332
1333 if (_xs_tcp_read_data(xprt, desc) == 0)
1334 xs_tcp_check_fraghdr(transport);
1335 else {
1336 /*
1337 * The transport_lock protects the request handling.
1338 * There's no need to hold it to update the tcp_flags.
1339 */
1340 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1341 }
Chuck Levera246b012005-08-11 16:25:23 -04001342}
1343
Chuck Leverdd456472006-12-05 16:35:44 -05001344static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001345{
1346 size_t len;
1347
Chuck Lever51971132006-12-05 16:35:19 -05001348 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001349 if (len > desc->count)
1350 len = desc->count;
1351 desc->count -= len;
1352 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001353 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001354 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001355 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001356}
1357
Chuck Lever9903cd12005-08-11 16:25:26 -04001358static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
Chuck Levera246b012005-08-11 16:25:23 -04001359{
1360 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001361 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001362 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001363 .skb = skb,
1364 .offset = offset,
1365 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001366 };
Chuck Levera246b012005-08-11 16:25:23 -04001367
Chuck Lever46121cf2007-01-31 12:14:08 -05001368 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001369 do {
1370 /* Read in a new fragment marker if necessary */
1371 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001372 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001373 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001374 continue;
1375 }
1376 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001377 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001378 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001379 continue;
1380 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001381 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001382 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001383 xs_tcp_read_calldir(transport, &desc);
1384 continue;
1385 }
Chuck Levera246b012005-08-11 16:25:23 -04001386 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001387 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001388 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001389 continue;
1390 }
1391 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001392 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001393 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001394 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001395 return len - desc.count;
1396}
1397
Chuck Lever9903cd12005-08-11 16:25:26 -04001398/**
1399 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1400 * @sk: socket with data to read
1401 * @bytes: how much data to read
1402 *
1403 */
1404static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001405{
1406 struct rpc_xprt *xprt;
1407 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001408 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001409
Chuck Lever46121cf2007-01-31 12:14:08 -05001410 dprintk("RPC: xs_tcp_data_ready...\n");
1411
Eric Dumazetf064af12010-09-22 12:43:39 +00001412 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001413 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001414 goto out;
Neil Brown61d0a8e2009-09-23 14:36:37 -04001415 /* Any data means we had a useful conversation, so
1416 * the we don't need to delay the next reconnect
1417 */
1418 if (xprt->reestablish_timeout)
1419 xprt->reestablish_timeout = 0;
1420
Chuck Lever9903cd12005-08-11 16:25:26 -04001421 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001422 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001423 do {
1424 rd_desc.count = 65536;
1425 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1426 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001427out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001428 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001429}
1430
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001431/*
1432 * Do the equivalent of linger/linger2 handling for dealing with
1433 * broken servers that don't close the socket in a timely
1434 * fashion
1435 */
1436static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1437 unsigned long timeout)
1438{
1439 struct sock_xprt *transport;
1440
1441 if (xprt_test_and_set_connecting(xprt))
1442 return;
1443 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1444 transport = container_of(xprt, struct sock_xprt, xprt);
1445 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1446 timeout);
1447}
1448
1449static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1450{
1451 struct sock_xprt *transport;
1452
1453 transport = container_of(xprt, struct sock_xprt, xprt);
1454
1455 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1456 !cancel_delayed_work(&transport->connect_worker))
1457 return;
1458 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1459 xprt_clear_connecting(xprt);
1460}
1461
Trond Myklebust4bc1e682012-10-23 17:50:07 -04001462static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001463{
1464 smp_mb__before_clear_bit();
Trond Myklebusta519fc72012-09-12 16:49:15 -04001465 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1466 clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001467 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1468 clear_bit(XPRT_CLOSING, &xprt->state);
1469 smp_mb__after_clear_bit();
Trond Myklebust4bc1e682012-10-23 17:50:07 -04001470}
1471
1472static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1473{
1474 xs_sock_reset_connection_flags(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001475 /* Mark transport as closed and wake up all pending tasks */
1476 xprt_disconnect_done(xprt);
1477}
1478
Chuck Lever9903cd12005-08-11 16:25:26 -04001479/**
1480 * xs_tcp_state_change - callback to handle TCP socket state changes
1481 * @sk: socket whose state has changed
1482 *
1483 */
1484static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001485{
Chuck Lever9903cd12005-08-11 16:25:26 -04001486 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001487
Eric Dumazetf064af12010-09-22 12:43:39 +00001488 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001489 if (!(xprt = xprt_from_sock(sk)))
1490 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001491 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001492 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001493 sk->sk_state, xprt_connected(xprt),
1494 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001495 sock_flag(sk, SOCK_ZAPPED),
1496 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001497
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001498 trace_rpc_socket_state_change(xprt, sk->sk_socket);
Chuck Levera246b012005-08-11 16:25:23 -04001499 switch (sk->sk_state) {
1500 case TCP_ESTABLISHED:
Eric Dumazetf064af12010-09-22 12:43:39 +00001501 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001502 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001503 struct sock_xprt *transport = container_of(xprt,
1504 struct sock_xprt, xprt);
1505
Chuck Levera246b012005-08-11 16:25:23 -04001506 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001507 transport->tcp_offset = 0;
1508 transport->tcp_reclen = 0;
1509 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001510 transport->tcp_flags =
1511 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Trond Myklebust8b717982013-09-26 10:18:04 -04001512 xprt->connect_cookie++;
Chuck Lever51971132006-12-05 16:35:19 -05001513
Trond Myklebust2a491992009-03-11 14:38:00 -04001514 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001515 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001516 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001517 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001518 case TCP_FIN_WAIT1:
1519 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001520 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001521 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001522 set_bit(XPRT_CLOSING, &xprt->state);
1523 smp_mb__before_clear_bit();
1524 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001525 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001526 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001527 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001528 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001529 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001530 /* The server initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001531 xprt->connect_cookie++;
Trond Myklebustd0bea452012-10-23 11:35:47 -04001532 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebusta519fc72012-09-12 16:49:15 -04001533 xs_tcp_force_close(xprt);
Trond Myklebust663b8852008-01-01 18:42:12 -05001534 case TCP_CLOSING:
1535 /*
1536 * If the server closed down the connection, make sure that
1537 * we back off before reconnecting
1538 */
1539 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1540 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001541 break;
1542 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001543 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001544 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001545 smp_mb__before_clear_bit();
1546 clear_bit(XPRT_CONNECTED, &xprt->state);
1547 smp_mb__after_clear_bit();
1548 break;
1549 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001550 xs_tcp_cancel_linger_timeout(xprt);
1551 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001552 }
1553 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001554 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001555}
1556
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001557static void xs_write_space(struct sock *sk)
1558{
1559 struct socket *sock;
1560 struct rpc_xprt *xprt;
1561
1562 if (unlikely(!(sock = sk->sk_socket)))
1563 return;
1564 clear_bit(SOCK_NOSPACE, &sock->flags);
1565
1566 if (unlikely(!(xprt = xprt_from_sock(sk))))
1567 return;
1568 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1569 return;
1570
1571 xprt_write_space(xprt);
1572}
1573
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001574/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001575 * xs_udp_write_space - callback invoked when socket buffer space
1576 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001577 * @sk: socket whose state has changed
1578 *
Chuck Levera246b012005-08-11 16:25:23 -04001579 * Called when more output buffer space is available for this socket.
1580 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001581 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001582 * with a bunch of small requests.
1583 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001584static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001585{
Eric Dumazetf064af12010-09-22 12:43:39 +00001586 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001587
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001588 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001589 if (sock_writeable(sk))
1590 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001591
Eric Dumazetf064af12010-09-22 12:43:39 +00001592 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001593}
Chuck Levera246b012005-08-11 16:25:23 -04001594
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001595/**
1596 * xs_tcp_write_space - callback invoked when socket buffer space
1597 * becomes available
1598 * @sk: socket whose state has changed
1599 *
1600 * Called when more output buffer space is available for this socket.
1601 * We try not to wake our writers until they can make "significant"
1602 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1603 * with a bunch of small requests.
1604 */
1605static void xs_tcp_write_space(struct sock *sk)
1606{
Eric Dumazetf064af12010-09-22 12:43:39 +00001607 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001608
1609 /* from net/core/stream.c:sk_stream_write_space */
Eric Dumazet64dc6132013-07-22 20:26:31 -07001610 if (sk_stream_is_writeable(sk))
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001611 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001612
Eric Dumazetf064af12010-09-22 12:43:39 +00001613 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001614}
1615
Chuck Lever470056c2005-08-25 16:25:56 -07001616static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001617{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001618 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1619 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001620
Chuck Lever7c6e0662006-12-05 16:35:30 -05001621 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001622 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001623 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001624 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001625 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001626 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001627 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001628 sk->sk_write_space(sk);
1629 }
1630}
1631
Chuck Lever43118c22005-08-25 16:25:49 -07001632/**
Chuck Lever470056c2005-08-25 16:25:56 -07001633 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001634 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001635 * @sndsize: requested size of send buffer, in bytes
1636 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001637 *
Chuck Lever470056c2005-08-25 16:25:56 -07001638 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001639 */
Chuck Lever470056c2005-08-25 16:25:56 -07001640static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001641{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001642 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1643
1644 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001645 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001646 transport->sndsize = sndsize + 1024;
1647 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001648 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001649 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001650
1651 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001652}
1653
Chuck Lever46c0ee82005-08-25 16:25:52 -07001654/**
1655 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1656 * @task: task that timed out
1657 *
1658 * Adjust the congestion window after a retransmit timeout has occurred.
1659 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001660static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever46c0ee82005-08-25 16:25:52 -07001661{
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001662 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
Chuck Lever46c0ee82005-08-25 16:25:52 -07001663}
1664
Chuck Leverb85d8802006-05-25 01:40:49 -04001665static unsigned short xs_get_random_port(void)
1666{
1667 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1668 unsigned short rand = (unsigned short) net_random() % range;
1669 return rand + xprt_min_resvport;
1670}
1671
Chuck Lever92200412006-01-03 09:55:51 +01001672/**
1673 * xs_set_port - reset the port number in the remote endpoint address
1674 * @xprt: generic transport
1675 * @port: new port number
1676 *
1677 */
1678static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1679{
Chuck Lever46121cf2007-01-31 12:14:08 -05001680 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001681
Chuck Lever9dc3b092009-08-09 15:09:46 -04001682 rpc_set_port(xs_addr(xprt), port);
1683 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001684}
1685
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001686static unsigned short xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001687{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001688 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001689
1690 if (port == 0 && transport->xprt.resvport)
1691 port = xs_get_random_port();
1692 return port;
1693}
1694
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001695static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001696{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001697 if (transport->srcport != 0)
1698 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001699 if (!transport->xprt.resvport)
1700 return 0;
1701 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1702 return xprt_max_resvport;
1703 return --port;
1704}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001705static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001706{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001707 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001708 int err, nloop = 0;
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001709 unsigned short port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001710 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001711
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001712 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001713 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001714 rpc_set_port((struct sockaddr *)&myaddr, port);
1715 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1716 transport->xprt.addrlen);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001717 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001718 break;
Chuck Levera246b012005-08-11 16:25:23 -04001719 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001720 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001721 break;
Chuck Levera246b012005-08-11 16:25:23 -04001722 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001723 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001724 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001725 if (port > last)
1726 nloop++;
1727 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001728
Chuck Lever4232e862010-10-20 11:52:51 -04001729 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001730 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1731 &((struct sockaddr_in *)&myaddr)->sin_addr,
1732 port, err ? "failed" : "ok", err);
1733 else
1734 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1735 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1736 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001737 return err;
1738}
1739
Chuck Lever176e21e2011-05-09 15:22:44 -04001740/*
1741 * We don't support autobind on AF_LOCAL sockets
1742 */
1743static void xs_local_rpcbind(struct rpc_task *task)
1744{
Trond Myklebust3dc0da22013-01-08 09:31:13 -05001745 rcu_read_lock();
1746 xprt_set_bound(rcu_dereference(task->tk_client->cl_xprt));
1747 rcu_read_unlock();
Chuck Lever176e21e2011-05-09 15:22:44 -04001748}
1749
1750static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1751{
1752}
Chuck Levera246b012005-08-11 16:25:23 -04001753
Peter Zijlstraed075362006-12-06 20:35:24 -08001754#ifdef CONFIG_DEBUG_LOCK_ALLOC
1755static struct lock_class_key xs_key[2];
1756static struct lock_class_key xs_slock_key[2];
1757
Chuck Lever176e21e2011-05-09 15:22:44 -04001758static inline void xs_reclassify_socketu(struct socket *sock)
1759{
1760 struct sock *sk = sock->sk;
1761
Chuck Lever176e21e2011-05-09 15:22:44 -04001762 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1763 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1764}
1765
Chuck Lever8945ee52007-08-06 11:58:04 -04001766static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001767{
1768 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001769
Chuck Lever8945ee52007-08-06 11:58:04 -04001770 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1771 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1772}
Peter Zijlstraed075362006-12-06 20:35:24 -08001773
Chuck Lever8945ee52007-08-06 11:58:04 -04001774static inline void xs_reclassify_socket6(struct socket *sock)
1775{
1776 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001777
Chuck Lever8945ee52007-08-06 11:58:04 -04001778 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1779 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001780}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001781
1782static inline void xs_reclassify_socket(int family, struct socket *sock)
1783{
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -04001784 WARN_ON_ONCE(sock_owned_by_user(sock->sk));
1785 if (sock_owned_by_user(sock->sk))
1786 return;
1787
Chuck Lever4232e862010-10-20 11:52:51 -04001788 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001789 case AF_LOCAL:
1790 xs_reclassify_socketu(sock);
1791 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001792 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001793 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001794 break;
1795 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001796 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001797 break;
1798 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001799}
Peter Zijlstraed075362006-12-06 20:35:24 -08001800#else
Chuck Lever176e21e2011-05-09 15:22:44 -04001801static inline void xs_reclassify_socketu(struct socket *sock)
1802{
1803}
1804
Chuck Lever8945ee52007-08-06 11:58:04 -04001805static inline void xs_reclassify_socket4(struct socket *sock)
1806{
1807}
1808
1809static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001810{
1811}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001812
1813static inline void xs_reclassify_socket(int family, struct socket *sock)
1814{
1815}
Peter Zijlstraed075362006-12-06 20:35:24 -08001816#endif
1817
NeilBrown93dc41b2013-10-31 16:14:36 +11001818static void xs_dummy_setup_socket(struct work_struct *work)
1819{
1820}
1821
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001822static struct socket *xs_create_sock(struct rpc_xprt *xprt,
1823 struct sock_xprt *transport, int family, int type, int protocol)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001824{
1825 struct socket *sock;
1826 int err;
1827
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001828 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001829 if (err < 0) {
1830 dprintk("RPC: can't create %d transport socket (%d).\n",
1831 protocol, -err);
1832 goto out;
1833 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001834 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001835
Ben Hutchings4cea2882011-02-22 21:54:34 +00001836 err = xs_bind(transport, sock);
1837 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001838 sock_release(sock);
1839 goto out;
1840 }
1841
1842 return sock;
1843out:
1844 return ERR_PTR(err);
1845}
1846
Chuck Lever176e21e2011-05-09 15:22:44 -04001847static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1848 struct socket *sock)
1849{
1850 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1851 xprt);
1852
1853 if (!transport->inet) {
1854 struct sock *sk = sock->sk;
1855
1856 write_lock_bh(&sk->sk_callback_lock);
1857
1858 xs_save_old_callbacks(transport, sk);
1859
1860 sk->sk_user_data = xprt;
1861 sk->sk_data_ready = xs_local_data_ready;
1862 sk->sk_write_space = xs_udp_write_space;
Chuck Lever176e21e2011-05-09 15:22:44 -04001863 sk->sk_allocation = GFP_ATOMIC;
1864
1865 xprt_clear_connected(xprt);
1866
1867 /* Reset to new socket */
1868 transport->sock = sock;
1869 transport->inet = sk;
1870
1871 write_unlock_bh(&sk->sk_callback_lock);
1872 }
1873
1874 /* Tell the socket layer to start connecting... */
1875 xprt->stat.connect_count++;
1876 xprt->stat.connect_start = jiffies;
1877 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1878}
1879
1880/**
1881 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1882 * @xprt: RPC transport to connect
1883 * @transport: socket transport to connect
1884 * @create_sock: function to create a socket of the correct type
Chuck Lever176e21e2011-05-09 15:22:44 -04001885 */
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001886static int xs_local_setup_socket(struct sock_xprt *transport)
Chuck Lever176e21e2011-05-09 15:22:44 -04001887{
Chuck Lever176e21e2011-05-09 15:22:44 -04001888 struct rpc_xprt *xprt = &transport->xprt;
1889 struct socket *sock;
1890 int status = -EIO;
1891
Jeff Layton5cf02d02012-07-23 13:58:51 -04001892 current->flags |= PF_FSTRANS;
1893
Chuck Lever176e21e2011-05-09 15:22:44 -04001894 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1895 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1896 SOCK_STREAM, 0, &sock, 1);
1897 if (status < 0) {
1898 dprintk("RPC: can't create AF_LOCAL "
1899 "transport socket (%d).\n", -status);
1900 goto out;
1901 }
1902 xs_reclassify_socketu(sock);
1903
1904 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1905 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1906
1907 status = xs_local_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001908 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever176e21e2011-05-09 15:22:44 -04001909 switch (status) {
1910 case 0:
1911 dprintk("RPC: xprt %p connected to %s\n",
1912 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1913 xprt_set_connected(xprt);
1914 break;
1915 case -ENOENT:
1916 dprintk("RPC: xprt %p: socket %s does not exist\n",
1917 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1918 break;
Trond Myklebust4a20a982012-12-15 17:02:29 -05001919 case -ECONNREFUSED:
1920 dprintk("RPC: xprt %p: connection refused for %s\n",
1921 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1922 break;
Chuck Lever176e21e2011-05-09 15:22:44 -04001923 default:
1924 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1925 __func__, -status,
1926 xprt->address_strings[RPC_DISPLAY_ADDR]);
1927 }
1928
1929out:
1930 xprt_clear_connecting(xprt);
1931 xprt_wake_pending_tasks(xprt, status);
Jeff Layton5cf02d02012-07-23 13:58:51 -04001932 current->flags &= ~PF_FSTRANS;
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001933 return status;
1934}
1935
Linus Torvaldsb6669732013-02-28 18:02:55 -08001936static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001937{
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001938 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1939 int ret;
1940
1941 if (RPC_IS_ASYNC(task)) {
1942 /*
1943 * We want the AF_LOCAL connect to be resolved in the
1944 * filesystem namespace of the process making the rpc
1945 * call. Thus we connect synchronously.
1946 *
1947 * If we want to support asynchronous AF_LOCAL calls,
1948 * we'll need to figure out how to pass a namespace to
1949 * connect.
1950 */
1951 rpc_exit(task, -ENOTCONN);
1952 return;
1953 }
1954 ret = xs_local_setup_socket(transport);
1955 if (ret && !RPC_IS_SOFTCONN(task))
1956 msleep_interruptible(15000);
Chuck Lever176e21e2011-05-09 15:22:44 -04001957}
1958
Mel Gormana564b8f2012-07-31 16:45:12 -07001959#ifdef CONFIG_SUNRPC_SWAP
1960static void xs_set_memalloc(struct rpc_xprt *xprt)
1961{
1962 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1963 xprt);
1964
1965 if (xprt->swapper)
1966 sk_set_memalloc(transport->inet);
1967}
1968
1969/**
1970 * xs_swapper - Tag this transport as being used for swap.
1971 * @xprt: transport to tag
1972 * @enable: enable/disable
1973 *
1974 */
1975int xs_swapper(struct rpc_xprt *xprt, int enable)
1976{
1977 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1978 xprt);
1979 int err = 0;
1980
1981 if (enable) {
1982 xprt->swapper++;
1983 xs_set_memalloc(xprt);
1984 } else if (xprt->swapper) {
1985 xprt->swapper--;
1986 sk_clear_memalloc(transport->inet);
1987 }
1988
1989 return err;
1990}
1991EXPORT_SYMBOL_GPL(xs_swapper);
1992#else
1993static void xs_set_memalloc(struct rpc_xprt *xprt)
1994{
1995}
1996#endif
1997
Chuck Lever16be2d22007-08-06 11:57:38 -04001998static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001999{
Chuck Lever16be2d22007-08-06 11:57:38 -04002000 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04002001
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002002 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002003 struct sock *sk = sock->sk;
2004
2005 write_lock_bh(&sk->sk_callback_lock);
2006
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002007 xs_save_old_callbacks(transport, sk);
2008
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002009 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002010 sk->sk_data_ready = xs_udp_data_ready;
2011 sk->sk_write_space = xs_udp_write_space;
2012 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05002013 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002014
2015 xprt_set_connected(xprt);
2016
2017 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002018 transport->sock = sock;
2019 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002020
Mel Gormana564b8f2012-07-31 16:45:12 -07002021 xs_set_memalloc(xprt);
2022
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002023 write_unlock_bh(&sk->sk_callback_lock);
2024 }
Chuck Lever470056c2005-08-25 16:25:56 -07002025 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04002026}
2027
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002028static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04002029{
2030 struct sock_xprt *transport =
2031 container_of(work, struct sock_xprt, connect_worker.work);
2032 struct rpc_xprt *xprt = &transport->xprt;
2033 struct socket *sock = transport->sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002034 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04002035
Jeff Layton5cf02d02012-07-23 13:58:51 -04002036 current->flags |= PF_FSTRANS;
2037
Chuck Levera246b012005-08-11 16:25:23 -04002038 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04002039 xs_reset_transport(transport);
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002040 sock = xs_create_sock(xprt, transport,
2041 xs_addr(xprt)->sa_family, SOCK_DGRAM, IPPROTO_UDP);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002042 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04002043 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04002044
Chuck Leverc740eff2009-08-09 15:09:46 -04002045 dprintk("RPC: worker connecting xprt %p via %s to "
2046 "%s (port %s)\n", xprt,
2047 xprt->address_strings[RPC_DISPLAY_PROTO],
2048 xprt->address_strings[RPC_DISPLAY_ADDR],
2049 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04002050
2051 xs_udp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002052 trace_rpc_socket_connect(xprt, sock, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002053 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002054out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002055 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002056 xprt_wake_pending_tasks(xprt, status);
Jeff Layton5cf02d02012-07-23 13:58:51 -04002057 current->flags &= ~PF_FSTRANS;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002058}
2059
Chuck Lever3167e122005-08-25 16:25:55 -07002060/*
2061 * We need to preserve the port number so the reply cache on the server can
2062 * find our cached RPC replies when we get around to reconnecting.
2063 */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002064static void xs_abort_connection(struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07002065{
2066 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07002067 struct sockaddr any;
2068
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002069 dprintk("RPC: disconnecting xprt %p to reuse port\n", transport);
Chuck Lever3167e122005-08-25 16:25:55 -07002070
2071 /*
2072 * Disconnect the transport socket by doing a connect operation
2073 * with AF_UNSPEC. This should return immediately...
2074 */
2075 memset(&any, 0, sizeof(any));
2076 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002077 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002078 trace_rpc_socket_reset_connection(&transport->xprt,
2079 transport->sock, result);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002080 if (!result)
Trond Myklebust4bc1e682012-10-23 17:50:07 -04002081 xs_sock_reset_connection_flags(&transport->xprt);
2082 dprintk("RPC: AF_UNSPEC connect return code %d\n", result);
Chuck Lever3167e122005-08-25 16:25:55 -07002083}
2084
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002085static void xs_tcp_reuse_connection(struct sock_xprt *transport)
Trond Myklebust40d25492009-03-11 14:37:58 -04002086{
2087 unsigned int state = transport->inet->sk_state;
2088
Andy Chittenden669502f2010-08-10 10:19:53 -04002089 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) {
2090 /* we don't need to abort the connection if the socket
2091 * hasn't undergone a shutdown
2092 */
2093 if (transport->inet->sk_shutdown == 0)
2094 return;
2095 dprintk("RPC: %s: TCP_CLOSEd and sk_shutdown set to %d\n",
2096 __func__, transport->inet->sk_shutdown);
2097 }
2098 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) {
2099 /* we don't need to abort the connection if the socket
2100 * hasn't undergone a shutdown
2101 */
2102 if (transport->inet->sk_shutdown == 0)
2103 return;
2104 dprintk("RPC: %s: ESTABLISHED/SYN_SENT "
2105 "sk_shutdown set to %d\n",
2106 __func__, transport->inet->sk_shutdown);
2107 }
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002108 xs_abort_connection(transport);
Trond Myklebust40d25492009-03-11 14:37:58 -04002109}
2110
Chuck Lever16be2d22007-08-06 11:57:38 -04002111static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002112{
Chuck Lever16be2d22007-08-06 11:57:38 -04002113 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002114 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002115
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002116 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002117 struct sock *sk = sock->sk;
Trond Myklebust7f260e82013-09-24 11:25:22 -04002118 unsigned int keepidle = xprt->timeout->to_initval / HZ;
2119 unsigned int keepcnt = xprt->timeout->to_retries + 1;
2120 unsigned int opt_on = 1;
2121
2122 /* TCP Keepalive options */
2123 kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
2124 (char *)&opt_on, sizeof(opt_on));
2125 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
2126 (char *)&keepidle, sizeof(keepidle));
2127 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
2128 (char *)&keepidle, sizeof(keepidle));
2129 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
2130 (char *)&keepcnt, sizeof(keepcnt));
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002131
2132 write_lock_bh(&sk->sk_callback_lock);
2133
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002134 xs_save_old_callbacks(transport, sk);
2135
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002136 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002137 sk->sk_data_ready = xs_tcp_data_ready;
2138 sk->sk_state_change = xs_tcp_state_change;
2139 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05002140 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07002141
2142 /* socket options */
2143 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
2144 sock_reset_flag(sk, SOCK_LINGER);
2145 tcp_sk(sk)->linger2 = 0;
2146 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002147
2148 xprt_clear_connected(xprt);
2149
2150 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002151 transport->sock = sock;
2152 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002153
2154 write_unlock_bh(&sk->sk_callback_lock);
2155 }
2156
Trond Myklebust01d37c42009-03-11 14:09:39 -04002157 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002158 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002159
Mel Gormana564b8f2012-07-31 16:45:12 -07002160 xs_set_memalloc(xprt);
2161
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002162 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05002163 xprt->stat.connect_count++;
2164 xprt->stat.connect_start = jiffies;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002165 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2166 switch (ret) {
2167 case 0:
2168 case -EINPROGRESS:
2169 /* SYN_SENT! */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002170 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2171 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2172 }
2173out:
2174 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002175}
2176
2177/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002178 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2179 * @xprt: RPC transport to connect
2180 * @transport: socket transport to connect
2181 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04002182 *
2183 * Invoked by a work queue tasklet.
2184 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002185static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002186{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002187 struct sock_xprt *transport =
2188 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002189 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002190 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002191 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002192
Jeff Layton5cf02d02012-07-23 13:58:51 -04002193 current->flags |= PF_FSTRANS;
2194
Chuck Lever16be2d22007-08-06 11:57:38 -04002195 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002196 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002197 sock = xs_create_sock(xprt, transport,
2198 xs_addr(xprt)->sa_family, SOCK_STREAM, IPPROTO_TCP);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002199 if (IS_ERR(sock)) {
2200 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002201 goto out;
2202 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002203 } else {
2204 int abort_and_exit;
2205
2206 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
2207 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04002208 /* "close" the socket, preserving the local port */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002209 xs_tcp_reuse_connection(transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04002210
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002211 if (abort_and_exit)
2212 goto out_eagain;
2213 }
2214
Chuck Leverc740eff2009-08-09 15:09:46 -04002215 dprintk("RPC: worker connecting xprt %p via %s to "
2216 "%s (port %s)\n", xprt,
2217 xprt->address_strings[RPC_DISPLAY_PROTO],
2218 xprt->address_strings[RPC_DISPLAY_ADDR],
2219 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002220
2221 status = xs_tcp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002222 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever46121cf2007-01-31 12:14:08 -05002223 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2224 xprt, -status, xprt_connected(xprt),
2225 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002226 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002227 default:
2228 printk("%s: connect returned unhandled error %d\n",
2229 __func__, status);
2230 case -EADDRNOTAVAIL:
2231 /* We're probably in TIME_WAIT. Get rid of existing socket,
2232 * and retry
2233 */
Trond Myklebusta519fc72012-09-12 16:49:15 -04002234 xs_tcp_force_close(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002235 break;
Trond Myklebust2a491992009-03-11 14:38:00 -04002236 case 0:
2237 case -EINPROGRESS:
2238 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002239 xprt_clear_connecting(xprt);
Jeff Layton5cf02d02012-07-23 13:58:51 -04002240 current->flags &= ~PF_FSTRANS;
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002241 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002242 case -EINVAL:
2243 /* Happens, for instance, if the user specified a link
2244 * local IPv6 address without a scope-id.
2245 */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002246 case -ECONNREFUSED:
2247 case -ECONNRESET:
2248 case -ENETUNREACH:
2249 /* retry with existing socket, after a delay */
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002250 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002251 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002252out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04002253 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002254out:
Chuck Lever2226feb2005-08-11 16:25:38 -04002255 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002256 xprt_wake_pending_tasks(xprt, status);
Jeff Layton5cf02d02012-07-23 13:58:51 -04002257 current->flags &= ~PF_FSTRANS;
Chuck Levera246b012005-08-11 16:25:23 -04002258}
2259
Chuck Lever68e220b2007-08-06 11:57:48 -04002260/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002261 * xs_connect - connect a socket to a remote endpoint
Trond Myklebust1b092092013-01-08 09:26:49 -05002262 * @xprt: pointer to transport structure
Chuck Lever9903cd12005-08-11 16:25:26 -04002263 * @task: address of RPC task that manages state of connect request
2264 *
2265 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002266 *
2267 * UDP socket connects are synchronous, but we use a work queue anyway
2268 * to guarantee that even unprivileged user processes can set up a
2269 * socket on a privileged port.
2270 *
2271 * If a UDP socket connect fails, the delay behavior here prevents
2272 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002273 */
Trond Myklebust1b092092013-01-08 09:26:49 -05002274static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002275{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002276 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002277
Chuck Lever09a21c42009-12-03 15:58:56 -05002278 if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002279 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2280 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002281 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002282 queue_delayed_work(rpciod_workqueue,
2283 &transport->connect_worker,
2284 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07002285 xprt->reestablish_timeout <<= 1;
Neil Brown61d0a8e2009-09-23 14:36:37 -04002286 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2287 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002288 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2289 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002290 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05002291 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002292 queue_delayed_work(rpciod_workqueue,
2293 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002294 }
2295}
2296
Chuck Lever262ca072006-03-20 13:44:16 -05002297/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002298 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2299 * @xprt: rpc_xprt struct containing statistics
2300 * @seq: output file
2301 *
2302 */
2303static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2304{
2305 long idle_time = 0;
2306
2307 if (xprt_connected(xprt))
2308 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2309
2310 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
Andy Adamson15a45202012-02-14 16:19:18 -05002311 "%llu %llu %lu %llu %llu\n",
Chuck Lever176e21e2011-05-09 15:22:44 -04002312 xprt->stat.bind_count,
2313 xprt->stat.connect_count,
2314 xprt->stat.connect_time,
2315 idle_time,
2316 xprt->stat.sends,
2317 xprt->stat.recvs,
2318 xprt->stat.bad_xids,
2319 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002320 xprt->stat.bklog_u,
2321 xprt->stat.max_slots,
2322 xprt->stat.sending_u,
2323 xprt->stat.pending_u);
Chuck Lever176e21e2011-05-09 15:22:44 -04002324}
2325
2326/**
Chuck Lever262ca072006-03-20 13:44:16 -05002327 * xs_udp_print_stats - display UDP socket-specifc stats
2328 * @xprt: rpc_xprt struct containing statistics
2329 * @seq: output file
2330 *
2331 */
2332static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2333{
Chuck Leverc8475462006-12-05 16:35:26 -05002334 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2335
Andy Adamson15a45202012-02-14 16:19:18 -05002336 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2337 "%lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002338 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002339 xprt->stat.bind_count,
2340 xprt->stat.sends,
2341 xprt->stat.recvs,
2342 xprt->stat.bad_xids,
2343 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002344 xprt->stat.bklog_u,
2345 xprt->stat.max_slots,
2346 xprt->stat.sending_u,
2347 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002348}
2349
2350/**
2351 * xs_tcp_print_stats - display TCP socket-specifc stats
2352 * @xprt: rpc_xprt struct containing statistics
2353 * @seq: output file
2354 *
2355 */
2356static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2357{
Chuck Leverc8475462006-12-05 16:35:26 -05002358 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002359 long idle_time = 0;
2360
2361 if (xprt_connected(xprt))
2362 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2363
Andy Adamson15a45202012-02-14 16:19:18 -05002364 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2365 "%llu %llu %lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002366 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002367 xprt->stat.bind_count,
2368 xprt->stat.connect_count,
2369 xprt->stat.connect_time,
2370 idle_time,
2371 xprt->stat.sends,
2372 xprt->stat.recvs,
2373 xprt->stat.bad_xids,
2374 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002375 xprt->stat.bklog_u,
2376 xprt->stat.max_slots,
2377 xprt->stat.sending_u,
2378 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002379}
2380
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002381/*
2382 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2383 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2384 * to use the server side send routines.
2385 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002386static void *bc_malloc(struct rpc_task *task, size_t size)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002387{
2388 struct page *page;
2389 struct rpc_buffer *buf;
2390
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002391 WARN_ON_ONCE(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2392 if (size > PAGE_SIZE - sizeof(struct rpc_buffer))
2393 return NULL;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002394
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002395 page = alloc_page(GFP_KERNEL);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002396 if (!page)
2397 return NULL;
2398
2399 buf = page_address(page);
2400 buf->len = PAGE_SIZE;
2401
2402 return buf->data;
2403}
2404
2405/*
2406 * Free the space allocated in the bc_alloc routine
2407 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002408static void bc_free(void *buffer)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002409{
2410 struct rpc_buffer *buf;
2411
2412 if (!buffer)
2413 return;
2414
2415 buf = container_of(buffer, struct rpc_buffer, data);
2416 free_page((unsigned long)buf);
2417}
2418
2419/*
2420 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2421 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2422 */
2423static int bc_sendto(struct rpc_rqst *req)
2424{
2425 int len;
2426 struct xdr_buf *xbufp = &req->rq_snd_buf;
2427 struct rpc_xprt *xprt = req->rq_xprt;
2428 struct sock_xprt *transport =
2429 container_of(xprt, struct sock_xprt, xprt);
2430 struct socket *sock = transport->sock;
2431 unsigned long headoff;
2432 unsigned long tailoff;
2433
Chuck Lever61677ee2011-05-09 15:22:34 -04002434 xs_encode_stream_record_marker(xbufp);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002435
2436 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2437 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2438 len = svc_send_common(sock, xbufp,
2439 virt_to_page(xbufp->head[0].iov_base), headoff,
2440 xbufp->tail[0].iov_base, tailoff);
2441
2442 if (len != xbufp->len) {
2443 printk(KERN_NOTICE "Error sending entire callback!\n");
2444 len = -EAGAIN;
2445 }
2446
2447 return len;
2448}
2449
2450/*
2451 * The send routine. Borrows from svc_send
2452 */
2453static int bc_send_request(struct rpc_task *task)
2454{
2455 struct rpc_rqst *req = task->tk_rqstp;
2456 struct svc_xprt *xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002457 u32 len;
2458
2459 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2460 /*
2461 * Get the server socket associated with this callback xprt
2462 */
2463 xprt = req->rq_xprt->bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002464
2465 /*
2466 * Grab the mutex to serialize data as the connection is shared
2467 * with the fore channel
2468 */
2469 if (!mutex_trylock(&xprt->xpt_mutex)) {
2470 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2471 if (!mutex_trylock(&xprt->xpt_mutex))
2472 return -EAGAIN;
2473 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2474 }
2475 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2476 len = -ENOTCONN;
2477 else
2478 len = bc_sendto(req);
2479 mutex_unlock(&xprt->xpt_mutex);
2480
2481 if (len > 0)
2482 len = 0;
2483
2484 return len;
2485}
2486
2487/*
2488 * The close routine. Since this is client initiated, we do nothing
2489 */
2490
2491static void bc_close(struct rpc_xprt *xprt)
2492{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002493}
2494
2495/*
2496 * The xprt destroy routine. Again, because this connection is client
2497 * initiated, we do nothing
2498 */
2499
2500static void bc_destroy(struct rpc_xprt *xprt)
2501{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002502}
2503
Chuck Lever176e21e2011-05-09 15:22:44 -04002504static struct rpc_xprt_ops xs_local_ops = {
2505 .reserve_xprt = xprt_reserve_xprt,
2506 .release_xprt = xs_tcp_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002507 .alloc_slot = xprt_alloc_slot,
Chuck Lever176e21e2011-05-09 15:22:44 -04002508 .rpcbind = xs_local_rpcbind,
2509 .set_port = xs_local_set_port,
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002510 .connect = xs_local_connect,
Chuck Lever176e21e2011-05-09 15:22:44 -04002511 .buf_alloc = rpc_malloc,
2512 .buf_free = rpc_free,
2513 .send_request = xs_local_send_request,
2514 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2515 .close = xs_close,
J. Bruce Fields190b1ecf2013-03-08 13:13:08 -05002516 .destroy = xs_local_destroy,
Chuck Lever176e21e2011-05-09 15:22:44 -04002517 .print_stats = xs_local_print_stats,
2518};
2519
Chuck Lever262965f2005-08-11 16:25:56 -04002520static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002521 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002522 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002523 .release_xprt = xprt_release_xprt_cong,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002524 .alloc_slot = xprt_alloc_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002525 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002526 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002527 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002528 .buf_alloc = rpc_malloc,
2529 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002530 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002531 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002532 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002533 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002534 .close = xs_close,
2535 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002536 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04002537};
2538
2539static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002540 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002541 .release_xprt = xs_tcp_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002542 .alloc_slot = xprt_lock_and_alloc_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002543 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002544 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002545 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002546 .buf_alloc = rpc_malloc,
2547 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002548 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002549 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustf75e6742009-04-21 17:18:20 -04002550 .close = xs_tcp_close,
Chuck Lever9903cd12005-08-11 16:25:26 -04002551 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002552 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002553};
2554
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002555/*
2556 * The rpc_xprt_ops for the server backchannel
2557 */
2558
2559static struct rpc_xprt_ops bc_tcp_ops = {
2560 .reserve_xprt = xprt_reserve_xprt,
2561 .release_xprt = xprt_release_xprt,
Bryan Schumaker84e28a32012-09-24 13:39:01 -04002562 .alloc_slot = xprt_alloc_slot,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002563 .buf_alloc = bc_malloc,
2564 .buf_free = bc_free,
2565 .send_request = bc_send_request,
2566 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2567 .close = bc_close,
2568 .destroy = bc_destroy,
2569 .print_stats = xs_tcp_print_stats,
2570};
2571
Chuck Lever92476852010-10-20 11:53:01 -04002572static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2573{
2574 static const struct sockaddr_in sin = {
2575 .sin_family = AF_INET,
2576 .sin_addr.s_addr = htonl(INADDR_ANY),
2577 };
2578 static const struct sockaddr_in6 sin6 = {
2579 .sin6_family = AF_INET6,
2580 .sin6_addr = IN6ADDR_ANY_INIT,
2581 };
2582
2583 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002584 case AF_LOCAL:
2585 break;
Chuck Lever92476852010-10-20 11:53:01 -04002586 case AF_INET:
2587 memcpy(sap, &sin, sizeof(sin));
2588 break;
2589 case AF_INET6:
2590 memcpy(sap, &sin6, sizeof(sin6));
2591 break;
2592 default:
2593 dprintk("RPC: %s: Bad address family\n", __func__);
2594 return -EAFNOSUPPORT;
2595 }
2596 return 0;
2597}
2598
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002599static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002600 unsigned int slot_table_size,
2601 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002602{
2603 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002604 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002605
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002606 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002607 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002608 return ERR_PTR(-EBADF);
2609 }
2610
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002611 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2612 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002613 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002614 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2615 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002616 return ERR_PTR(-ENOMEM);
2617 }
2618
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002619 new = container_of(xprt, struct sock_xprt, xprt);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002620 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2621 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002622 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002623 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002624 else {
2625 int err;
2626 err = xs_init_anyaddr(args->dstaddr->sa_family,
2627 (struct sockaddr *)&new->srcaddr);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002628 if (err != 0) {
2629 xprt_free(xprt);
Chuck Lever92476852010-10-20 11:53:01 -04002630 return ERR_PTR(err);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002631 }
Chuck Lever92476852010-10-20 11:53:01 -04002632 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002633
2634 return xprt;
2635}
2636
Chuck Lever176e21e2011-05-09 15:22:44 -04002637static const struct rpc_timeout xs_local_default_timeout = {
2638 .to_initval = 10 * HZ,
2639 .to_maxval = 10 * HZ,
2640 .to_retries = 2,
2641};
2642
2643/**
2644 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2645 * @args: rpc transport creation arguments
2646 *
2647 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2648 */
2649static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2650{
2651 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2652 struct sock_xprt *transport;
2653 struct rpc_xprt *xprt;
2654 struct rpc_xprt *ret;
2655
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002656 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2657 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002658 if (IS_ERR(xprt))
2659 return xprt;
2660 transport = container_of(xprt, struct sock_xprt, xprt);
2661
2662 xprt->prot = 0;
2663 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2664 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2665
2666 xprt->bind_timeout = XS_BIND_TO;
2667 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2668 xprt->idle_timeout = XS_IDLE_DISC_TO;
2669
2670 xprt->ops = &xs_local_ops;
2671 xprt->timeout = &xs_local_default_timeout;
2672
NeilBrown93dc41b2013-10-31 16:14:36 +11002673 INIT_DELAYED_WORK(&transport->connect_worker,
2674 xs_dummy_setup_socket);
2675
Chuck Lever176e21e2011-05-09 15:22:44 -04002676 switch (sun->sun_family) {
2677 case AF_LOCAL:
2678 if (sun->sun_path[0] != '/') {
2679 dprintk("RPC: bad AF_LOCAL address: %s\n",
2680 sun->sun_path);
2681 ret = ERR_PTR(-EINVAL);
2682 goto out_err;
2683 }
2684 xprt_set_bound(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002685 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
J. Bruce Fields7073ea82013-02-21 10:14:22 -05002686 ret = ERR_PTR(xs_local_setup_socket(transport));
2687 if (ret)
2688 goto out_err;
Chuck Lever176e21e2011-05-09 15:22:44 -04002689 break;
2690 default:
2691 ret = ERR_PTR(-EAFNOSUPPORT);
2692 goto out_err;
2693 }
2694
2695 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2696 xprt->address_strings[RPC_DISPLAY_ADDR]);
2697
2698 if (try_module_get(THIS_MODULE))
2699 return xprt;
2700 ret = ERR_PTR(-EINVAL);
2701out_err:
2702 xprt_free(xprt);
2703 return ret;
2704}
2705
Trond Myklebust2881ae72007-12-20 16:03:54 -05002706static const struct rpc_timeout xs_udp_default_timeout = {
2707 .to_initval = 5 * HZ,
2708 .to_maxval = 30 * HZ,
2709 .to_increment = 5 * HZ,
2710 .to_retries = 5,
2711};
2712
Chuck Lever9903cd12005-08-11 16:25:26 -04002713/**
2714 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002715 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002716 *
2717 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002718static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002719{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002720 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002721 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002722 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002723 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002724
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002725 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2726 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002727 if (IS_ERR(xprt))
2728 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002729 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002730
Chuck Leverec739ef2006-08-22 20:06:15 -04002731 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002732 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002733 /* XXX: header size can vary due to auth type, IPv6, etc. */
2734 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2735
Chuck Lever03bf4b72005-08-25 16:25:55 -07002736 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002737 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2738 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002739
Chuck Lever262965f2005-08-11 16:25:56 -04002740 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002741
Trond Myklebustba7392b2007-12-20 16:03:55 -05002742 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002743
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002744 switch (addr->sa_family) {
2745 case AF_INET:
2746 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2747 xprt_set_bound(xprt);
2748
2749 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002750 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002751 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002752 break;
2753 case AF_INET6:
2754 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2755 xprt_set_bound(xprt);
2756
2757 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002758 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002759 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002760 break;
2761 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002762 ret = ERR_PTR(-EAFNOSUPPORT);
2763 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002764 }
2765
Chuck Leverc740eff2009-08-09 15:09:46 -04002766 if (xprt_bound(xprt))
2767 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2768 xprt->address_strings[RPC_DISPLAY_ADDR],
2769 xprt->address_strings[RPC_DISPLAY_PORT],
2770 xprt->address_strings[RPC_DISPLAY_PROTO]);
2771 else
2772 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2773 xprt->address_strings[RPC_DISPLAY_ADDR],
2774 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002775
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002776 if (try_module_get(THIS_MODULE))
2777 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002778 ret = ERR_PTR(-EINVAL);
2779out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002780 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002781 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002782}
2783
Trond Myklebust2881ae72007-12-20 16:03:54 -05002784static const struct rpc_timeout xs_tcp_default_timeout = {
2785 .to_initval = 60 * HZ,
2786 .to_maxval = 60 * HZ,
2787 .to_retries = 2,
2788};
2789
Chuck Lever9903cd12005-08-11 16:25:26 -04002790/**
2791 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002792 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002793 *
2794 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002795static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002796{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002797 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002798 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002799 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002800 struct rpc_xprt *ret;
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002801 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2802
2803 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2804 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Levera246b012005-08-11 16:25:23 -04002805
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002806 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002807 max_slot_table_size);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002808 if (IS_ERR(xprt))
2809 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002810 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002811
Chuck Leverec739ef2006-08-22 20:06:15 -04002812 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002813 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002814 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002815
Chuck Lever03bf4b72005-08-25 16:25:55 -07002816 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002817 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2818 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002819
Chuck Lever262965f2005-08-11 16:25:56 -04002820 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002821 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002822
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002823 switch (addr->sa_family) {
2824 case AF_INET:
2825 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2826 xprt_set_bound(xprt);
2827
Chuck Lever9dc3b092009-08-09 15:09:46 -04002828 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002829 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002830 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002831 break;
2832 case AF_INET6:
2833 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2834 xprt_set_bound(xprt);
2835
Chuck Lever9dc3b092009-08-09 15:09:46 -04002836 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002837 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002838 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002839 break;
2840 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002841 ret = ERR_PTR(-EAFNOSUPPORT);
2842 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002843 }
2844
Chuck Leverc740eff2009-08-09 15:09:46 -04002845 if (xprt_bound(xprt))
2846 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2847 xprt->address_strings[RPC_DISPLAY_ADDR],
2848 xprt->address_strings[RPC_DISPLAY_PORT],
2849 xprt->address_strings[RPC_DISPLAY_PROTO]);
2850 else
2851 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2852 xprt->address_strings[RPC_DISPLAY_ADDR],
2853 xprt->address_strings[RPC_DISPLAY_PROTO]);
2854
Chuck Leveredb267a2006-08-22 20:06:18 -04002855
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002856 if (try_module_get(THIS_MODULE))
2857 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002858 ret = ERR_PTR(-EINVAL);
2859out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002860 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002861 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002862}
Chuck Lever282b32e2006-12-05 16:35:51 -05002863
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002864/**
2865 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2866 * @args: rpc transport creation arguments
2867 *
2868 */
2869static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2870{
2871 struct sockaddr *addr = args->dstaddr;
2872 struct rpc_xprt *xprt;
2873 struct sock_xprt *transport;
2874 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002875 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002876
J. Bruce Fieldsf0418aa2010-12-08 13:48:19 -05002877 if (args->bc_xprt->xpt_bc_xprt) {
2878 /*
2879 * This server connection already has a backchannel
J. Bruce Fieldse3bfab12013-10-02 09:48:15 -04002880 * transport; we can't create a new one, as we wouldn't
2881 * be able to match replies based on xid any more. So,
J. Bruce Fieldsf0418aa2010-12-08 13:48:19 -05002882 * reuse the already-existing one:
2883 */
2884 return args->bc_xprt->xpt_bc_xprt;
2885 }
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002886 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2887 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002888 if (IS_ERR(xprt))
2889 return xprt;
2890 transport = container_of(xprt, struct sock_xprt, xprt);
2891
2892 xprt->prot = IPPROTO_TCP;
2893 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2894 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2895 xprt->timeout = &xs_tcp_default_timeout;
2896
2897 /* backchannel */
2898 xprt_set_bound(xprt);
2899 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002900 xprt->reestablish_timeout = 0;
2901 xprt->idle_timeout = 0;
2902
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002903 xprt->ops = &bc_tcp_ops;
2904
2905 switch (addr->sa_family) {
2906 case AF_INET:
2907 xs_format_peer_addresses(xprt, "tcp",
2908 RPCBIND_NETID_TCP);
2909 break;
2910 case AF_INET6:
2911 xs_format_peer_addresses(xprt, "tcp",
2912 RPCBIND_NETID_TCP6);
2913 break;
2914 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002915 ret = ERR_PTR(-EAFNOSUPPORT);
2916 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002917 }
2918
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04002919 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2920 xprt->address_strings[RPC_DISPLAY_ADDR],
2921 xprt->address_strings[RPC_DISPLAY_PORT],
2922 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002923
2924 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002925 * Once we've associated a backchannel xprt with a connection,
2926 * we want to keep it around as long as long as the connection
2927 * lasts, in case we need to start using it for a backchannel
2928 * again; this reference won't be dropped until bc_xprt is
2929 * destroyed.
2930 */
2931 xprt_get(xprt);
2932 args->bc_xprt->xpt_bc_xprt = xprt;
2933 xprt->bc_xprt = args->bc_xprt;
2934 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2935 transport->sock = bc_sock->sk_sock;
2936 transport->inet = bc_sock->sk_sk;
2937
2938 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002939 * Since we don't want connections for the backchannel, we set
2940 * the xprt status to connected
2941 */
2942 xprt_set_connected(xprt);
2943
2944
2945 if (try_module_get(THIS_MODULE))
2946 return xprt;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002947 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002948 ret = ERR_PTR(-EINVAL);
2949out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002950 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002951 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002952}
2953
Chuck Lever176e21e2011-05-09 15:22:44 -04002954static struct xprt_class xs_local_transport = {
2955 .list = LIST_HEAD_INIT(xs_local_transport.list),
2956 .name = "named UNIX socket",
2957 .owner = THIS_MODULE,
2958 .ident = XPRT_TRANSPORT_LOCAL,
2959 .setup = xs_setup_local,
2960};
2961
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002962static struct xprt_class xs_udp_transport = {
2963 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2964 .name = "udp",
2965 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002966 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002967 .setup = xs_setup_udp,
2968};
2969
2970static struct xprt_class xs_tcp_transport = {
2971 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2972 .name = "tcp",
2973 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002974 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002975 .setup = xs_setup_tcp,
2976};
2977
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002978static struct xprt_class xs_bc_tcp_transport = {
2979 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2980 .name = "tcp NFSv4.1 backchannel",
2981 .owner = THIS_MODULE,
2982 .ident = XPRT_TRANSPORT_BC_TCP,
2983 .setup = xs_setup_bc_tcp,
2984};
2985
Chuck Lever282b32e2006-12-05 16:35:51 -05002986/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002987 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002988 *
2989 */
2990int init_socket_xprt(void)
2991{
Chuck Leverfbf76682006-12-05 16:35:54 -05002992#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002993 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002994 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002995#endif
2996
Chuck Lever176e21e2011-05-09 15:22:44 -04002997 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002998 xprt_register_transport(&xs_udp_transport);
2999 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003000 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003001
Chuck Lever282b32e2006-12-05 16:35:51 -05003002 return 0;
3003}
3004
3005/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003006 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05003007 *
3008 */
3009void cleanup_socket_xprt(void)
3010{
Chuck Leverfbf76682006-12-05 16:35:54 -05003011#ifdef RPC_DEBUG
3012 if (sunrpc_table_header) {
3013 unregister_sysctl_table(sunrpc_table_header);
3014 sunrpc_table_header = NULL;
3015 }
3016#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003017
Chuck Lever176e21e2011-05-09 15:22:44 -04003018 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003019 xprt_unregister_transport(&xs_udp_transport);
3020 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003021 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05003022}
Trond Myklebustcbf11072009-08-09 15:06:19 -04003023
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003024static int param_set_uint_minmax(const char *val,
3025 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003026 unsigned int min, unsigned int max)
3027{
3028 unsigned long num;
3029 int ret;
3030
3031 if (!val)
3032 return -EINVAL;
3033 ret = strict_strtoul(val, 0, &num);
3034 if (ret == -EINVAL || num < min || num > max)
3035 return -EINVAL;
3036 *((unsigned int *)kp->arg) = num;
3037 return 0;
3038}
3039
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003040static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003041{
3042 return param_set_uint_minmax(val, kp,
3043 RPC_MIN_RESVPORT,
3044 RPC_MAX_RESVPORT);
3045}
3046
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003047static struct kernel_param_ops param_ops_portnr = {
3048 .set = param_set_portnr,
3049 .get = param_get_uint,
3050};
3051
Trond Myklebustcbf11072009-08-09 15:06:19 -04003052#define param_check_portnr(name, p) \
3053 __param_check(name, p, unsigned int);
3054
3055module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3056module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3057
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003058static int param_set_slot_table_size(const char *val,
3059 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003060{
3061 return param_set_uint_minmax(val, kp,
3062 RPC_MIN_SLOT_TABLE,
3063 RPC_MAX_SLOT_TABLE);
3064}
3065
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003066static struct kernel_param_ops param_ops_slot_table_size = {
3067 .set = param_set_slot_table_size,
3068 .get = param_get_uint,
3069};
3070
Trond Myklebustcbf11072009-08-09 15:06:19 -04003071#define param_check_slot_table_size(name, p) \
3072 __param_check(name, p, unsigned int);
3073
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003074static int param_set_max_slot_table_size(const char *val,
3075 const struct kernel_param *kp)
3076{
3077 return param_set_uint_minmax(val, kp,
3078 RPC_MIN_SLOT_TABLE,
3079 RPC_MAX_SLOT_TABLE_LIMIT);
3080}
3081
3082static struct kernel_param_ops param_ops_max_slot_table_size = {
3083 .set = param_set_max_slot_table_size,
3084 .get = param_get_uint,
3085};
3086
3087#define param_check_max_slot_table_size(name, p) \
3088 __param_check(name, p, unsigned int);
3089
Trond Myklebustcbf11072009-08-09 15:06:19 -04003090module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3091 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003092module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3093 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003094module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3095 slot_table_size, 0644);
3096