blob: 75b045e1cd50b00096a3a2ec938c2f82423dffce [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 Myklebusta6b31d12013-11-08 16:03:50 -0500396static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more, bool zerocopy)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400397{
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500398 ssize_t (*do_sendpage)(struct socket *sock, struct page *page,
399 int offset, size_t size, int flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400400 struct page **ppage;
401 unsigned int remainder;
402 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400403
Trond Myklebust24c56842006-10-17 15:06:22 -0400404 remainder = xdr->page_len - base;
405 base += xdr->page_base;
406 ppage = xdr->pages + (base >> PAGE_SHIFT);
407 base &= ~PAGE_MASK;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500408 do_sendpage = sock->ops->sendpage;
409 if (!zerocopy)
410 do_sendpage = sock_no_sendpage;
Trond Myklebust24c56842006-10-17 15:06:22 -0400411 for(;;) {
412 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
413 int flags = XS_SENDMSG_FLAGS;
414
415 remainder -= len;
416 if (remainder != 0 || more)
417 flags |= MSG_MORE;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500418 err = do_sendpage(sock, *ppage, base, len, flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400419 if (remainder == 0 || err != len)
420 break;
421 sent += err;
422 ppage++;
423 base = 0;
424 }
425 if (sent == 0)
426 return err;
427 if (err > 0)
428 sent += err;
429 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400430}
431
Chuck Lever9903cd12005-08-11 16:25:26 -0400432/**
433 * xs_sendpages - write pages directly to a socket
434 * @sock: socket to send on
435 * @addr: UDP only -- address of destination
436 * @addrlen: UDP only -- length of destination address
437 * @xdr: buffer containing this request
438 * @base: starting position in the buffer
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500439 * @zerocopy: true if it is safe to use sendpage()
Chuck Lever9903cd12005-08-11 16:25:26 -0400440 *
Chuck Levera246b012005-08-11 16:25:23 -0400441 */
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500442static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, bool zerocopy)
Chuck Levera246b012005-08-11 16:25:23 -0400443{
Trond Myklebust24c56842006-10-17 15:06:22 -0400444 unsigned int remainder = xdr->len - base;
445 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400446
Chuck Lever262965f2005-08-11 16:25:56 -0400447 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400448 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400449
450 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400451 if (base != 0) {
452 addr = NULL;
453 addrlen = 0;
454 }
Chuck Lever262965f2005-08-11 16:25:56 -0400455
Trond Myklebust24c56842006-10-17 15:06:22 -0400456 if (base < xdr->head[0].iov_len || addr != NULL) {
457 unsigned int len = xdr->head[0].iov_len - base;
458 remainder -= len;
459 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
460 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400461 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400462 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400463 base = 0;
464 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400465 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400466
Trond Myklebust24c56842006-10-17 15:06:22 -0400467 if (base < xdr->page_len) {
468 unsigned int len = xdr->page_len - base;
469 remainder -= len;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500470 err = xs_send_pagedata(sock, xdr, base, remainder != 0, zerocopy);
Trond Myklebust24c56842006-10-17 15:06:22 -0400471 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400472 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400473 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400474 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400475 } else
476 base -= xdr->page_len;
477
478 if (base >= xdr->tail[0].iov_len)
479 return sent;
480 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400481out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400482 if (sent == 0)
483 return err;
484 if (err > 0)
485 sent += err;
486 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400487}
488
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400489static void xs_nospace_callback(struct rpc_task *task)
490{
491 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
492
493 transport->inet->sk_write_pending--;
494 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
495}
496
Chuck Lever9903cd12005-08-11 16:25:26 -0400497/**
Chuck Lever262965f2005-08-11 16:25:56 -0400498 * xs_nospace - place task on wait queue if transmit was incomplete
499 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400500 *
Chuck Levera246b012005-08-11 16:25:23 -0400501 */
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400502static int xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400503{
Chuck Lever262965f2005-08-11 16:25:56 -0400504 struct rpc_rqst *req = task->tk_rqstp;
505 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500506 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust24ca9a82011-11-22 14:44:28 +0200507 int ret = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400508
Chuck Lever46121cf2007-01-31 12:14:08 -0500509 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400510 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
511 req->rq_slen);
512
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400513 /* Protect against races with write_space */
514 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400515
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400516 /* Don't race with disconnect */
517 if (xprt_connected(xprt)) {
518 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
519 /*
520 * Notify TCP that we're limited by the application
521 * window size
522 */
523 set_bit(SOCK_NOSPACE, &transport->sock->flags);
524 transport->inet->sk_write_pending++;
525 /* ...and wait for more buffer space */
526 xprt_wait_for_buffer_space(task, xs_nospace_callback);
527 }
528 } else {
529 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400530 ret = -ENOTCONN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400531 }
Chuck Lever262965f2005-08-11 16:25:56 -0400532
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400533 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400534 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400535}
536
Chuck Lever61677ee2011-05-09 15:22:34 -0400537/*
538 * Construct a stream transport record marker in @buf.
539 */
540static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
541{
542 u32 reclen = buf->len - sizeof(rpc_fraghdr);
543 rpc_fraghdr *base = buf->head[0].iov_base;
544 *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
545}
546
Chuck Lever262965f2005-08-11 16:25:56 -0400547/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400548 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
549 * @task: RPC task that manages the state of an RPC request
550 *
551 * Return values:
552 * 0: The request has been sent
553 * EAGAIN: The socket was blocked, please call again later to
554 * complete the request
555 * ENOTCONN: Caller needs to invoke connect logic then call again
556 * other: Some other error occured, the request was not sent
557 */
558static int xs_local_send_request(struct rpc_task *task)
559{
560 struct rpc_rqst *req = task->tk_rqstp;
561 struct rpc_xprt *xprt = req->rq_xprt;
562 struct sock_xprt *transport =
563 container_of(xprt, struct sock_xprt, xprt);
564 struct xdr_buf *xdr = &req->rq_snd_buf;
565 int status;
566
567 xs_encode_stream_record_marker(&req->rq_snd_buf);
568
569 xs_pktdump("packet data:",
570 req->rq_svec->iov_base, req->rq_svec->iov_len);
571
572 status = xs_sendpages(transport->sock, NULL, 0,
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500573 xdr, req->rq_bytes_sent, true);
Chuck Lever176e21e2011-05-09 15:22:44 -0400574 dprintk("RPC: %s(%u) = %d\n",
575 __func__, xdr->len - req->rq_bytes_sent, status);
576 if (likely(status >= 0)) {
577 req->rq_bytes_sent += status;
578 req->rq_xmit_bytes_sent += status;
579 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
580 req->rq_bytes_sent = 0;
581 return 0;
582 }
583 status = -EAGAIN;
584 }
585
586 switch (status) {
587 case -EAGAIN:
588 status = xs_nospace(task);
589 break;
590 default:
591 dprintk("RPC: sendmsg returned unrecognized error %d\n",
592 -status);
593 case -EPIPE:
594 xs_close(xprt);
595 status = -ENOTCONN;
596 }
597
598 return status;
599}
600
601/**
Chuck Lever262965f2005-08-11 16:25:56 -0400602 * xs_udp_send_request - write an RPC request to a UDP socket
603 * @task: address of RPC task that manages the state of an RPC request
604 *
605 * Return values:
606 * 0: The request has been sent
607 * EAGAIN: The socket was blocked, please call again later to
608 * complete the request
609 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300610 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -0400611 */
612static int xs_udp_send_request(struct rpc_task *task)
613{
614 struct rpc_rqst *req = task->tk_rqstp;
615 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500616 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400617 struct xdr_buf *xdr = &req->rq_snd_buf;
618 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400619
Chuck Lever9903cd12005-08-11 16:25:26 -0400620 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400621 req->rq_svec->iov_base,
622 req->rq_svec->iov_len);
623
Trond Myklebust01d37c42009-03-11 14:09:39 -0400624 if (!xprt_bound(xprt))
625 return -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500626 status = xs_sendpages(transport->sock,
Chuck Lever95392c52007-08-06 11:57:58 -0400627 xs_addr(xprt),
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500628 xprt->addrlen, xdr,
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500629 req->rq_bytes_sent, true);
Chuck Levera246b012005-08-11 16:25:23 -0400630
Chuck Lever46121cf2007-01-31 12:14:08 -0500631 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400632 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400633
Trond Myklebust21997002007-10-01 11:43:37 -0400634 if (status >= 0) {
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400635 req->rq_xmit_bytes_sent += status;
Trond Myklebust21997002007-10-01 11:43:37 -0400636 if (status >= req->rq_slen)
637 return 0;
638 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400639 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400640 }
Chuck Levera246b012005-08-11 16:25:23 -0400641
Chuck Lever262965f2005-08-11 16:25:56 -0400642 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400643 case -ENOTSOCK:
644 status = -ENOTCONN;
645 /* Should we call xs_close() here? */
646 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400647 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400648 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400649 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400650 default:
651 dprintk("RPC: sendmsg returned unrecognized error %d\n",
652 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400653 case -ENETUNREACH:
654 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400655 case -ECONNREFUSED:
656 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400657 * prompts ECONNREFUSED. */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400658 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400659 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800660
Chuck Lever262965f2005-08-11 16:25:56 -0400661 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400662}
663
Trond Myklebuste06799f2007-11-05 15:44:12 -0500664/**
665 * xs_tcp_shutdown - gracefully shut down a TCP socket
666 * @xprt: transport
667 *
668 * Initiates a graceful shutdown of the TCP socket by calling the
669 * equivalent of shutdown(SHUT_WR);
670 */
671static void xs_tcp_shutdown(struct rpc_xprt *xprt)
672{
673 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
674 struct socket *sock = transport->sock;
675
Trond Myklebust40b5ea02013-09-04 12:16:23 -0400676 if (sock != NULL) {
Trond Myklebuste06799f2007-11-05 15:44:12 -0500677 kernel_sock_shutdown(sock, SHUT_WR);
Trond Myklebust40b5ea02013-09-04 12:16:23 -0400678 trace_rpc_socket_shutdown(xprt, sock);
679 }
Trond Myklebuste06799f2007-11-05 15:44:12 -0500680}
681
Chuck Lever9903cd12005-08-11 16:25:26 -0400682/**
Chuck Lever262965f2005-08-11 16:25:56 -0400683 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400684 * @task: address of RPC task that manages the state of an RPC request
685 *
686 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400687 * 0: The request has been sent
688 * EAGAIN: The socket was blocked, please call again later to
689 * complete the request
690 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300691 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400692 *
693 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400694 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400695 */
Chuck Lever262965f2005-08-11 16:25:56 -0400696static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400697{
698 struct rpc_rqst *req = task->tk_rqstp;
699 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500700 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400701 struct xdr_buf *xdr = &req->rq_snd_buf;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500702 bool zerocopy = true;
Chuck Leverb595bb12007-08-06 11:56:42 -0400703 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400704
Chuck Lever61677ee2011-05-09 15:22:34 -0400705 xs_encode_stream_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400706
Chuck Lever262965f2005-08-11 16:25:56 -0400707 xs_pktdump("packet data:",
708 req->rq_svec->iov_base,
709 req->rq_svec->iov_len);
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500710 /* Don't use zero copy if this is a resend. If the RPC call
711 * completes while the socket holds a reference to the pages,
712 * then we may end up resending corrupted data.
713 */
714 if (task->tk_flags & RPC_TASK_SENT)
715 zerocopy = false;
Chuck Levera246b012005-08-11 16:25:23 -0400716
717 /* Continue transmitting the packet/record. We must be careful
718 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400719 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400720 while (1) {
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500721 status = xs_sendpages(transport->sock,
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500722 NULL, 0, xdr, req->rq_bytes_sent,
723 zerocopy);
Chuck Levera246b012005-08-11 16:25:23 -0400724
Chuck Lever46121cf2007-01-31 12:14:08 -0500725 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400726 xdr->len - req->rq_bytes_sent, status);
727
728 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400729 break;
730
Chuck Lever262965f2005-08-11 16:25:56 -0400731 /* If we've sent the entire packet, immediately
732 * reset the count of bytes sent. */
733 req->rq_bytes_sent += status;
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400734 req->rq_xmit_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400735 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
736 req->rq_bytes_sent = 0;
737 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400738 }
739
Trond Myklebust06b4b682008-04-16 16:51:38 -0400740 if (status != 0)
741 continue;
Chuck Levera246b012005-08-11 16:25:23 -0400742 status = -EAGAIN;
Trond Myklebust06b4b682008-04-16 16:51:38 -0400743 break;
Chuck Levera246b012005-08-11 16:25:23 -0400744 }
745
Chuck Lever262965f2005-08-11 16:25:56 -0400746 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400747 case -ENOTSOCK:
748 status = -ENOTCONN;
749 /* Should we call xs_close() here? */
750 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400751 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400752 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400753 break;
754 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500755 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400756 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400757 case -ECONNRESET:
Trond Myklebuste06799f2007-11-05 15:44:12 -0500758 xs_tcp_shutdown(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400759 case -ECONNREFUSED:
760 case -ENOTCONN:
Trond Myklebustb9d2bb22012-10-23 11:40:02 -0400761 case -EPIPE:
Chuck Lever262965f2005-08-11 16:25:56 -0400762 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400763 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800764
Chuck Levera246b012005-08-11 16:25:23 -0400765 return status;
766}
767
Chuck Lever9903cd12005-08-11 16:25:26 -0400768/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400769 * xs_tcp_release_xprt - clean up after a tcp transmission
770 * @xprt: transport
771 * @task: rpc task
772 *
773 * This cleans up if an error causes us to abort the transmission of a request.
774 * In this case, the socket may need to be reset in order to avoid confusing
775 * the server.
776 */
777static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
778{
779 struct rpc_rqst *req;
780
781 if (task != xprt->snd_task)
782 return;
783 if (task == NULL)
784 goto out_release;
785 req = task->tk_rqstp;
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400786 if (req == NULL)
787 goto out_release;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400788 if (req->rq_bytes_sent == 0)
789 goto out_release;
790 if (req->rq_bytes_sent == req->rq_snd_buf.len)
791 goto out_release;
Trond Myklebusta4f08352013-01-08 09:10:21 -0500792 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400793out_release:
794 xprt_release_xprt(xprt, task);
795}
796
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400797static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
798{
799 transport->old_data_ready = sk->sk_data_ready;
800 transport->old_state_change = sk->sk_state_change;
801 transport->old_write_space = sk->sk_write_space;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400802}
803
804static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
805{
806 sk->sk_data_ready = transport->old_data_ready;
807 sk->sk_state_change = transport->old_state_change;
808 sk->sk_write_space = transport->old_write_space;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400809}
810
Chuck Leverfe315e72009-03-11 14:10:21 -0400811static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400812{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500813 struct socket *sock = transport->sock;
814 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400815
Chuck Leverfe315e72009-03-11 14:10:21 -0400816 if (sk == NULL)
817 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400818
Trond Myklebust246408d2011-03-22 18:40:10 -0400819 transport->srcport = 0;
820
Chuck Levera246b012005-08-11 16:25:23 -0400821 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500822 transport->inet = NULL;
823 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400824
Chuck Lever9903cd12005-08-11 16:25:26 -0400825 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400826
827 xs_restore_old_callbacks(transport, sk);
Chuck Levera246b012005-08-11 16:25:23 -0400828 write_unlock_bh(&sk->sk_callback_lock);
829
Chuck Lever9903cd12005-08-11 16:25:26 -0400830 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400831
Trond Myklebust40b5ea02013-09-04 12:16:23 -0400832 trace_rpc_socket_close(&transport->xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -0400833 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -0400834}
835
836/**
837 * xs_close - close a socket
838 * @xprt: transport
839 *
840 * This is used when all requests are complete; ie, no DRC state remains
841 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400842 *
843 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
844 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e72009-03-11 14:10:21 -0400845 */
846static void xs_close(struct rpc_xprt *xprt)
847{
848 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
849
850 dprintk("RPC: xs_close xprt %p\n", xprt);
851
NeilBrown93dc41b2013-10-31 16:14:36 +1100852 cancel_delayed_work_sync(&transport->connect_worker);
853
Chuck Leverfe315e72009-03-11 14:10:21 -0400854 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -0400855 xprt->reestablish_timeout = 0;
Chuck Leverfe315e72009-03-11 14:10:21 -0400856
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100857 smp_mb__before_clear_bit();
Trond Myklebust7d1e8252009-03-11 14:38:03 -0400858 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100859 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -0500860 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100861 smp_mb__after_clear_bit();
Trond Myklebust62da3b22007-11-06 18:44:20 -0500862 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400863}
864
Trond Myklebustf75e6742009-04-21 17:18:20 -0400865static void xs_tcp_close(struct rpc_xprt *xprt)
866{
867 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
868 xs_close(xprt);
869 else
870 xs_tcp_shutdown(xprt);
871}
872
Chuck Lever9903cd12005-08-11 16:25:26 -0400873/**
874 * xs_destroy - prepare to shutdown a transport
875 * @xprt: doomed transport
876 *
877 */
878static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400879{
Chuck Lever46121cf2007-01-31 12:14:08 -0500880 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400881
Trond Myklebusta1311d82013-10-31 09:18:49 -0400882 xs_close(xprt);
883 xs_free_peer_addresses(xprt);
884 xprt_free(xprt);
885 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400886}
887
Chuck Lever9903cd12005-08-11 16:25:26 -0400888static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400889{
Chuck Lever9903cd12005-08-11 16:25:26 -0400890 return (struct rpc_xprt *) sk->sk_user_data;
891}
892
Chuck Lever176e21e2011-05-09 15:22:44 -0400893static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
894{
895 struct xdr_skb_reader desc = {
896 .skb = skb,
897 .offset = sizeof(rpc_fraghdr),
898 .count = skb->len - sizeof(rpc_fraghdr),
899 };
900
901 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
902 return -1;
903 if (desc.count)
904 return -1;
905 return 0;
906}
907
908/**
909 * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets
910 * @sk: socket with data to read
911 * @len: how much data to read
912 *
913 * Currently this assumes we can read the whole reply in a single gulp.
914 */
915static void xs_local_data_ready(struct sock *sk, int len)
916{
917 struct rpc_task *task;
918 struct rpc_xprt *xprt;
919 struct rpc_rqst *rovr;
920 struct sk_buff *skb;
921 int err, repsize, copied;
922 u32 _xid;
923 __be32 *xp;
924
925 read_lock_bh(&sk->sk_callback_lock);
926 dprintk("RPC: %s...\n", __func__);
927 xprt = xprt_from_sock(sk);
928 if (xprt == NULL)
929 goto out;
930
931 skb = skb_recv_datagram(sk, 0, 1, &err);
932 if (skb == NULL)
933 goto out;
934
Chuck Lever176e21e2011-05-09 15:22:44 -0400935 repsize = skb->len - sizeof(rpc_fraghdr);
936 if (repsize < 4) {
937 dprintk("RPC: impossible RPC reply size %d\n", repsize);
938 goto dropit;
939 }
940
941 /* Copy the XID from the skb... */
942 xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
943 if (xp == NULL)
944 goto dropit;
945
946 /* Look up and lock the request corresponding to the given XID */
947 spin_lock(&xprt->transport_lock);
948 rovr = xprt_lookup_rqst(xprt, *xp);
949 if (!rovr)
950 goto out_unlock;
951 task = rovr->rq_task;
952
953 copied = rovr->rq_private_buf.buflen;
954 if (copied > repsize)
955 copied = repsize;
956
957 if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
958 dprintk("RPC: sk_buff copy failed\n");
959 goto out_unlock;
960 }
961
962 xprt_complete_rqst(task, copied);
963
964 out_unlock:
965 spin_unlock(&xprt->transport_lock);
966 dropit:
967 skb_free_datagram(sk, skb);
968 out:
969 read_unlock_bh(&sk->sk_callback_lock);
970}
971
Chuck Lever9903cd12005-08-11 16:25:26 -0400972/**
973 * xs_udp_data_ready - "data ready" callback for UDP sockets
974 * @sk: socket with data to read
975 * @len: how much data to read
976 *
977 */
978static void xs_udp_data_ready(struct sock *sk, int len)
979{
980 struct rpc_task *task;
981 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400982 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400983 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400984 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700985 u32 _xid;
986 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400987
Eric Dumazetf064af12010-09-22 12:43:39 +0000988 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500989 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400990 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400991 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400992
993 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
994 goto out;
995
Chuck Levera246b012005-08-11 16:25:23 -0400996 repsize = skb->len - sizeof(struct udphdr);
997 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500998 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400999 goto dropit;
1000 }
1001
1002 /* Copy the XID from the skb... */
1003 xp = skb_header_pointer(skb, sizeof(struct udphdr),
1004 sizeof(_xid), &_xid);
1005 if (xp == NULL)
1006 goto dropit;
1007
1008 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001009 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001010 rovr = xprt_lookup_rqst(xprt, *xp);
1011 if (!rovr)
1012 goto out_unlock;
1013 task = rovr->rq_task;
1014
Chuck Levera246b012005-08-11 16:25:23 -04001015 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1016 copied = repsize;
1017
1018 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001019 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
1020 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -04001021 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001022 }
1023
1024 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -04001025
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001026 xprt_adjust_cwnd(xprt, task, copied);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001027 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -04001028
1029 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001030 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001031 dropit:
1032 skb_free_datagram(sk, skb);
1033 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001034 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001035}
1036
Trond Myklebusta519fc72012-09-12 16:49:15 -04001037/*
1038 * Helper function to force a TCP close if the server is sending
1039 * junk and/or it has put us in CLOSE_WAIT
1040 */
1041static void xs_tcp_force_close(struct rpc_xprt *xprt)
1042{
1043 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1044 xprt_force_disconnect(xprt);
1045}
1046
Chuck Leverdd456472006-12-05 16:35:44 -05001047static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001048{
Chuck Lever51971132006-12-05 16:35:19 -05001049 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001050 size_t len, used;
1051 char *p;
1052
Chuck Lever51971132006-12-05 16:35:19 -05001053 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
1054 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001055 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001056 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001057 if (used != len)
1058 return;
Chuck Lever808012f2005-08-25 16:25:49 -07001059
Chuck Lever51971132006-12-05 16:35:19 -05001060 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
1061 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -05001062 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -04001063 else
Chuck Levere136d092006-12-05 16:35:23 -05001064 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -05001065 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -07001066
Chuck Levere136d092006-12-05 16:35:23 -05001067 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001068 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -07001069
Chuck Levera246b012005-08-11 16:25:23 -04001070 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001071 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001072 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebusta519fc72012-09-12 16:49:15 -04001073 xs_tcp_force_close(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001074 return;
Chuck Levera246b012005-08-11 16:25:23 -04001075 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001076 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -05001077 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001078}
1079
Chuck Lever51971132006-12-05 16:35:19 -05001080static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001081{
Chuck Lever51971132006-12-05 16:35:19 -05001082 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001083 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001084 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001085 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
1086 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1087 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001088 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001089 }
1090 }
1091}
1092
Chuck Leverdd456472006-12-05 16:35:44 -05001093static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001094{
1095 size_t len, used;
1096 char *p;
1097
Chuck Lever51971132006-12-05 16:35:19 -05001098 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -05001099 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001100 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001101 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001102 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001103 if (used != len)
1104 return;
Chuck Levere136d092006-12-05 16:35:23 -05001105 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001106 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -05001107 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001108 dprintk("RPC: reading %s XID %08x\n",
1109 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1110 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -05001111 ntohl(transport->tcp_xid));
1112 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001113}
1114
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001115static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1116 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001117{
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001118 size_t len, used;
1119 u32 offset;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001120 char *p;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001121
1122 /*
1123 * We want transport->tcp_offset to be 8 at the end of this routine
1124 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1125 * When this function is called for the first time,
1126 * transport->tcp_offset is 4 (after having already read the xid).
1127 */
1128 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001129 len = sizeof(transport->tcp_calldir) - offset;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001130 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001131 p = ((char *) &transport->tcp_calldir) + offset;
1132 used = xdr_skb_read_bits(desc, p, len);
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001133 transport->tcp_offset += used;
1134 if (used != len)
1135 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001136 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001137 /*
1138 * We don't yet have the XDR buffer, so we will write the calldir
1139 * out after we get the buffer from the 'struct rpc_rqst'
1140 */
Trond Myklebustb76ce562010-06-16 13:57:32 -04001141 switch (ntohl(transport->tcp_calldir)) {
1142 case RPC_REPLY:
1143 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1144 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001145 transport->tcp_flags |= TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001146 break;
1147 case RPC_CALL:
1148 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1149 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001150 transport->tcp_flags &= ~TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001151 break;
1152 default:
1153 dprintk("RPC: invalid request message type\n");
Trond Myklebusta519fc72012-09-12 16:49:15 -04001154 xs_tcp_force_close(&transport->xprt);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001155 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001156 xs_tcp_check_fraghdr(transport);
1157}
1158
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001159static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
1160 struct xdr_skb_reader *desc,
1161 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001162{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001163 struct sock_xprt *transport =
1164 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001165 struct xdr_buf *rcvbuf;
1166 size_t len;
1167 ssize_t r;
1168
Chuck Levera246b012005-08-11 16:25:23 -04001169 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001170
1171 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1172 /*
1173 * Save the RPC direction in the XDR buffer
1174 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001175 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
Trond Myklebustb76ce562010-06-16 13:57:32 -04001176 &transport->tcp_calldir,
1177 sizeof(transport->tcp_calldir));
1178 transport->tcp_copied += sizeof(transport->tcp_calldir);
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001179 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -04001180 }
1181
Chuck Levera246b012005-08-11 16:25:23 -04001182 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -05001183 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -05001184 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -04001185
Chuck Lever51971132006-12-05 16:35:19 -05001186 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001187 memcpy(&my_desc, desc, sizeof(my_desc));
1188 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -05001189 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001190 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001191 desc->count -= r;
1192 desc->offset += r;
1193 } else
Chuck Lever51971132006-12-05 16:35:19 -05001194 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001195 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001196
1197 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001198 transport->tcp_copied += r;
1199 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001200 }
1201 if (r != len) {
1202 /* Error when copying to the receive buffer,
1203 * usually because we weren't able to allocate
1204 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001205 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001206 * will not receive any additional updates,
1207 * and time out.
1208 * Any remaining data from this record will
1209 * be discarded.
1210 */
Chuck Levere136d092006-12-05 16:35:23 -05001211 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001212 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001213 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001214 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1215 "tcp_offset = %u, tcp_reclen = %u\n",
1216 xprt, transport->tcp_copied,
1217 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001218 return;
Chuck Levera246b012005-08-11 16:25:23 -04001219 }
1220
Chuck Lever46121cf2007-01-31 12:14:08 -05001221 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001222 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001223 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1224 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1225 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001226
Chuck Lever51971132006-12-05 16:35:19 -05001227 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001228 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001229 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001230 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1231 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001232 }
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001233}
1234
1235/*
1236 * Finds the request corresponding to the RPC xid and invokes the common
1237 * tcp read code to read the data.
1238 */
1239static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1240 struct xdr_skb_reader *desc)
1241{
1242 struct sock_xprt *transport =
1243 container_of(xprt, struct sock_xprt, xprt);
1244 struct rpc_rqst *req;
1245
1246 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1247
1248 /* Find and lock the request corresponding to this xid */
1249 spin_lock(&xprt->transport_lock);
1250 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1251 if (!req) {
1252 dprintk("RPC: XID %08x request not found!\n",
1253 ntohl(transport->tcp_xid));
1254 spin_unlock(&xprt->transport_lock);
1255 return -1;
1256 }
1257
1258 xs_tcp_read_common(xprt, desc, req);
1259
Chuck Levere136d092006-12-05 16:35:23 -05001260 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001261 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001262
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001263 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001264 return 0;
1265}
1266
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001267#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001268/*
1269 * Obtains an rpc_rqst previously allocated and invokes the common
1270 * tcp read code to read the data. The result is placed in the callback
1271 * queue.
1272 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1273 * connection and return -1.
1274 */
1275static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1276 struct xdr_skb_reader *desc)
1277{
1278 struct sock_xprt *transport =
1279 container_of(xprt, struct sock_xprt, xprt);
1280 struct rpc_rqst *req;
1281
1282 req = xprt_alloc_bc_request(xprt);
1283 if (req == NULL) {
1284 printk(KERN_WARNING "Callback slot table overflowed\n");
1285 xprt_force_disconnect(xprt);
1286 return -1;
1287 }
1288
1289 req->rq_xid = transport->tcp_xid;
1290 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1291 xs_tcp_read_common(xprt, desc, req);
1292
1293 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1294 struct svc_serv *bc_serv = xprt->bc_serv;
1295
1296 /*
1297 * Add callback request to callback list. The callback
1298 * service sleeps on the sv_cb_waitq waiting for new
1299 * requests. Wake it up after adding enqueing the
1300 * request.
1301 */
1302 dprintk("RPC: add callback request to list\n");
1303 spin_lock(&bc_serv->sv_cb_lock);
1304 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1305 spin_unlock(&bc_serv->sv_cb_lock);
1306 wake_up(&bc_serv->sv_cb_waitq);
1307 }
1308
1309 req->rq_private_buf.len = transport->tcp_copied;
1310
1311 return 0;
1312}
1313
1314static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1315 struct xdr_skb_reader *desc)
1316{
1317 struct sock_xprt *transport =
1318 container_of(xprt, struct sock_xprt, xprt);
1319
1320 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1321 xs_tcp_read_reply(xprt, desc) :
1322 xs_tcp_read_callback(xprt, desc);
1323}
1324#else
1325static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1326 struct xdr_skb_reader *desc)
1327{
1328 return xs_tcp_read_reply(xprt, desc);
1329}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001330#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001331
1332/*
1333 * Read data off the transport. This can be either an RPC_CALL or an
1334 * RPC_REPLY. Relay the processing to helper functions.
1335 */
1336static void xs_tcp_read_data(struct rpc_xprt *xprt,
1337 struct xdr_skb_reader *desc)
1338{
1339 struct sock_xprt *transport =
1340 container_of(xprt, struct sock_xprt, xprt);
1341
1342 if (_xs_tcp_read_data(xprt, desc) == 0)
1343 xs_tcp_check_fraghdr(transport);
1344 else {
1345 /*
1346 * The transport_lock protects the request handling.
1347 * There's no need to hold it to update the tcp_flags.
1348 */
1349 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1350 }
Chuck Levera246b012005-08-11 16:25:23 -04001351}
1352
Chuck Leverdd456472006-12-05 16:35:44 -05001353static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001354{
1355 size_t len;
1356
Chuck Lever51971132006-12-05 16:35:19 -05001357 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001358 if (len > desc->count)
1359 len = desc->count;
1360 desc->count -= len;
1361 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001362 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001363 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001364 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001365}
1366
Chuck Lever9903cd12005-08-11 16:25:26 -04001367static 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 -04001368{
1369 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001370 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001371 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001372 .skb = skb,
1373 .offset = offset,
1374 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001375 };
Chuck Levera246b012005-08-11 16:25:23 -04001376
Chuck Lever46121cf2007-01-31 12:14:08 -05001377 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001378 do {
1379 /* Read in a new fragment marker if necessary */
1380 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001381 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001382 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001383 continue;
1384 }
1385 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001386 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001387 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001388 continue;
1389 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001390 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001391 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001392 xs_tcp_read_calldir(transport, &desc);
1393 continue;
1394 }
Chuck Levera246b012005-08-11 16:25:23 -04001395 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001396 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001397 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001398 continue;
1399 }
1400 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001401 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001402 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001403 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001404 return len - desc.count;
1405}
1406
Chuck Lever9903cd12005-08-11 16:25:26 -04001407/**
1408 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1409 * @sk: socket with data to read
1410 * @bytes: how much data to read
1411 *
1412 */
1413static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001414{
1415 struct rpc_xprt *xprt;
1416 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001417 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001418
Chuck Lever46121cf2007-01-31 12:14:08 -05001419 dprintk("RPC: xs_tcp_data_ready...\n");
1420
Eric Dumazetf064af12010-09-22 12:43:39 +00001421 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001422 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001423 goto out;
Neil Brown61d0a8e2009-09-23 14:36:37 -04001424 /* Any data means we had a useful conversation, so
1425 * the we don't need to delay the next reconnect
1426 */
1427 if (xprt->reestablish_timeout)
1428 xprt->reestablish_timeout = 0;
1429
Chuck Lever9903cd12005-08-11 16:25:26 -04001430 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001431 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001432 do {
1433 rd_desc.count = 65536;
1434 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1435 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001436out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001437 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001438}
1439
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001440/*
1441 * Do the equivalent of linger/linger2 handling for dealing with
1442 * broken servers that don't close the socket in a timely
1443 * fashion
1444 */
1445static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1446 unsigned long timeout)
1447{
1448 struct sock_xprt *transport;
1449
1450 if (xprt_test_and_set_connecting(xprt))
1451 return;
1452 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1453 transport = container_of(xprt, struct sock_xprt, xprt);
1454 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1455 timeout);
1456}
1457
1458static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1459{
1460 struct sock_xprt *transport;
1461
1462 transport = container_of(xprt, struct sock_xprt, xprt);
1463
1464 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1465 !cancel_delayed_work(&transport->connect_worker))
1466 return;
1467 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1468 xprt_clear_connecting(xprt);
1469}
1470
Trond Myklebust4bc1e682012-10-23 17:50:07 -04001471static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001472{
1473 smp_mb__before_clear_bit();
Trond Myklebusta519fc72012-09-12 16:49:15 -04001474 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1475 clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001476 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1477 clear_bit(XPRT_CLOSING, &xprt->state);
1478 smp_mb__after_clear_bit();
Trond Myklebust4bc1e682012-10-23 17:50:07 -04001479}
1480
1481static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1482{
1483 xs_sock_reset_connection_flags(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001484 /* Mark transport as closed and wake up all pending tasks */
1485 xprt_disconnect_done(xprt);
1486}
1487
Chuck Lever9903cd12005-08-11 16:25:26 -04001488/**
1489 * xs_tcp_state_change - callback to handle TCP socket state changes
1490 * @sk: socket whose state has changed
1491 *
1492 */
1493static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001494{
Chuck Lever9903cd12005-08-11 16:25:26 -04001495 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001496
Eric Dumazetf064af12010-09-22 12:43:39 +00001497 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001498 if (!(xprt = xprt_from_sock(sk)))
1499 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001500 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001501 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001502 sk->sk_state, xprt_connected(xprt),
1503 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001504 sock_flag(sk, SOCK_ZAPPED),
1505 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001506
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001507 trace_rpc_socket_state_change(xprt, sk->sk_socket);
Chuck Levera246b012005-08-11 16:25:23 -04001508 switch (sk->sk_state) {
1509 case TCP_ESTABLISHED:
Eric Dumazetf064af12010-09-22 12:43:39 +00001510 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001511 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001512 struct sock_xprt *transport = container_of(xprt,
1513 struct sock_xprt, xprt);
1514
Chuck Levera246b012005-08-11 16:25:23 -04001515 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001516 transport->tcp_offset = 0;
1517 transport->tcp_reclen = 0;
1518 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001519 transport->tcp_flags =
1520 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Trond Myklebust8b717982013-09-26 10:18:04 -04001521 xprt->connect_cookie++;
Chuck Lever51971132006-12-05 16:35:19 -05001522
Trond Myklebust2a491992009-03-11 14:38:00 -04001523 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001524 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001525 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001526 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001527 case TCP_FIN_WAIT1:
1528 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001529 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001530 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001531 set_bit(XPRT_CLOSING, &xprt->state);
1532 smp_mb__before_clear_bit();
1533 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001534 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001535 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001536 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001537 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001538 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001539 /* The server initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001540 xprt->connect_cookie++;
Trond Myklebustd0bea452012-10-23 11:35:47 -04001541 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebusta519fc72012-09-12 16:49:15 -04001542 xs_tcp_force_close(xprt);
Trond Myklebust663b8852008-01-01 18:42:12 -05001543 case TCP_CLOSING:
1544 /*
1545 * If the server closed down the connection, make sure that
1546 * we back off before reconnecting
1547 */
1548 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1549 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001550 break;
1551 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001552 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001553 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001554 smp_mb__before_clear_bit();
1555 clear_bit(XPRT_CONNECTED, &xprt->state);
1556 smp_mb__after_clear_bit();
1557 break;
1558 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001559 xs_tcp_cancel_linger_timeout(xprt);
1560 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001561 }
1562 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001563 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001564}
1565
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001566static void xs_write_space(struct sock *sk)
1567{
1568 struct socket *sock;
1569 struct rpc_xprt *xprt;
1570
1571 if (unlikely(!(sock = sk->sk_socket)))
1572 return;
1573 clear_bit(SOCK_NOSPACE, &sock->flags);
1574
1575 if (unlikely(!(xprt = xprt_from_sock(sk))))
1576 return;
1577 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1578 return;
1579
1580 xprt_write_space(xprt);
1581}
1582
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001583/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001584 * xs_udp_write_space - callback invoked when socket buffer space
1585 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001586 * @sk: socket whose state has changed
1587 *
Chuck Levera246b012005-08-11 16:25:23 -04001588 * Called when more output buffer space is available for this socket.
1589 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001590 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001591 * with a bunch of small requests.
1592 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001593static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001594{
Eric Dumazetf064af12010-09-22 12:43:39 +00001595 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001596
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001597 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001598 if (sock_writeable(sk))
1599 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001600
Eric Dumazetf064af12010-09-22 12:43:39 +00001601 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001602}
Chuck Levera246b012005-08-11 16:25:23 -04001603
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001604/**
1605 * xs_tcp_write_space - callback invoked when socket buffer space
1606 * becomes available
1607 * @sk: socket whose state has changed
1608 *
1609 * Called when more output buffer space is available for this socket.
1610 * We try not to wake our writers until they can make "significant"
1611 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1612 * with a bunch of small requests.
1613 */
1614static void xs_tcp_write_space(struct sock *sk)
1615{
Eric Dumazetf064af12010-09-22 12:43:39 +00001616 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001617
1618 /* from net/core/stream.c:sk_stream_write_space */
Eric Dumazet64dc6132013-07-22 20:26:31 -07001619 if (sk_stream_is_writeable(sk))
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001620 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001621
Eric Dumazetf064af12010-09-22 12:43:39 +00001622 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001623}
1624
Chuck Lever470056c2005-08-25 16:25:56 -07001625static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001626{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001627 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1628 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001629
Chuck Lever7c6e0662006-12-05 16:35:30 -05001630 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001631 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001632 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001633 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001634 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001635 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001636 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001637 sk->sk_write_space(sk);
1638 }
1639}
1640
Chuck Lever43118c22005-08-25 16:25:49 -07001641/**
Chuck Lever470056c2005-08-25 16:25:56 -07001642 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001643 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001644 * @sndsize: requested size of send buffer, in bytes
1645 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001646 *
Chuck Lever470056c2005-08-25 16:25:56 -07001647 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001648 */
Chuck Lever470056c2005-08-25 16:25:56 -07001649static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001650{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001651 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1652
1653 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001654 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001655 transport->sndsize = sndsize + 1024;
1656 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001657 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001658 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001659
1660 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001661}
1662
Chuck Lever46c0ee82005-08-25 16:25:52 -07001663/**
1664 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1665 * @task: task that timed out
1666 *
1667 * Adjust the congestion window after a retransmit timeout has occurred.
1668 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001669static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever46c0ee82005-08-25 16:25:52 -07001670{
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001671 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
Chuck Lever46c0ee82005-08-25 16:25:52 -07001672}
1673
Chuck Leverb85d8802006-05-25 01:40:49 -04001674static unsigned short xs_get_random_port(void)
1675{
1676 unsigned short range = xprt_max_resvport - xprt_min_resvport;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001677 unsigned short rand = (unsigned short) prandom_u32() % range;
Chuck Leverb85d8802006-05-25 01:40:49 -04001678 return rand + xprt_min_resvport;
1679}
1680
Chuck Lever92200412006-01-03 09:55:51 +01001681/**
1682 * xs_set_port - reset the port number in the remote endpoint address
1683 * @xprt: generic transport
1684 * @port: new port number
1685 *
1686 */
1687static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1688{
Chuck Lever46121cf2007-01-31 12:14:08 -05001689 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001690
Chuck Lever9dc3b092009-08-09 15:09:46 -04001691 rpc_set_port(xs_addr(xprt), port);
1692 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001693}
1694
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001695static unsigned short xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001696{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001697 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001698
1699 if (port == 0 && transport->xprt.resvport)
1700 port = xs_get_random_port();
1701 return port;
1702}
1703
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001704static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001705{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001706 if (transport->srcport != 0)
1707 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001708 if (!transport->xprt.resvport)
1709 return 0;
1710 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1711 return xprt_max_resvport;
1712 return --port;
1713}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001714static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001715{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001716 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001717 int err, nloop = 0;
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001718 unsigned short port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001719 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001720
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001721 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001722 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001723 rpc_set_port((struct sockaddr *)&myaddr, port);
1724 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1725 transport->xprt.addrlen);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001726 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001727 break;
Chuck Levera246b012005-08-11 16:25:23 -04001728 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001729 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001730 break;
Chuck Levera246b012005-08-11 16:25:23 -04001731 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001732 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001733 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001734 if (port > last)
1735 nloop++;
1736 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001737
Chuck Lever4232e862010-10-20 11:52:51 -04001738 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001739 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1740 &((struct sockaddr_in *)&myaddr)->sin_addr,
1741 port, err ? "failed" : "ok", err);
1742 else
1743 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1744 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1745 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001746 return err;
1747}
1748
Chuck Lever176e21e2011-05-09 15:22:44 -04001749/*
1750 * We don't support autobind on AF_LOCAL sockets
1751 */
1752static void xs_local_rpcbind(struct rpc_task *task)
1753{
Trond Myklebust3dc0da22013-01-08 09:31:13 -05001754 rcu_read_lock();
1755 xprt_set_bound(rcu_dereference(task->tk_client->cl_xprt));
1756 rcu_read_unlock();
Chuck Lever176e21e2011-05-09 15:22:44 -04001757}
1758
1759static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1760{
1761}
Chuck Levera246b012005-08-11 16:25:23 -04001762
Peter Zijlstraed075362006-12-06 20:35:24 -08001763#ifdef CONFIG_DEBUG_LOCK_ALLOC
1764static struct lock_class_key xs_key[2];
1765static struct lock_class_key xs_slock_key[2];
1766
Chuck Lever176e21e2011-05-09 15:22:44 -04001767static inline void xs_reclassify_socketu(struct socket *sock)
1768{
1769 struct sock *sk = sock->sk;
1770
Chuck Lever176e21e2011-05-09 15:22:44 -04001771 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1772 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1773}
1774
Chuck Lever8945ee52007-08-06 11:58:04 -04001775static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001776{
1777 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001778
Chuck Lever8945ee52007-08-06 11:58:04 -04001779 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1780 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1781}
Peter Zijlstraed075362006-12-06 20:35:24 -08001782
Chuck Lever8945ee52007-08-06 11:58:04 -04001783static inline void xs_reclassify_socket6(struct socket *sock)
1784{
1785 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001786
Chuck Lever8945ee52007-08-06 11:58:04 -04001787 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1788 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001789}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001790
1791static inline void xs_reclassify_socket(int family, struct socket *sock)
1792{
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -04001793 WARN_ON_ONCE(sock_owned_by_user(sock->sk));
1794 if (sock_owned_by_user(sock->sk))
1795 return;
1796
Chuck Lever4232e862010-10-20 11:52:51 -04001797 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001798 case AF_LOCAL:
1799 xs_reclassify_socketu(sock);
1800 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001801 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001802 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001803 break;
1804 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001805 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001806 break;
1807 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001808}
Peter Zijlstraed075362006-12-06 20:35:24 -08001809#else
Chuck Lever176e21e2011-05-09 15:22:44 -04001810static inline void xs_reclassify_socketu(struct socket *sock)
1811{
1812}
1813
Chuck Lever8945ee52007-08-06 11:58:04 -04001814static inline void xs_reclassify_socket4(struct socket *sock)
1815{
1816}
1817
1818static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001819{
1820}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001821
1822static inline void xs_reclassify_socket(int family, struct socket *sock)
1823{
1824}
Peter Zijlstraed075362006-12-06 20:35:24 -08001825#endif
1826
NeilBrown93dc41b2013-10-31 16:14:36 +11001827static void xs_dummy_setup_socket(struct work_struct *work)
1828{
1829}
1830
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001831static struct socket *xs_create_sock(struct rpc_xprt *xprt,
1832 struct sock_xprt *transport, int family, int type, int protocol)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001833{
1834 struct socket *sock;
1835 int err;
1836
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001837 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001838 if (err < 0) {
1839 dprintk("RPC: can't create %d transport socket (%d).\n",
1840 protocol, -err);
1841 goto out;
1842 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001843 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001844
Ben Hutchings4cea2882011-02-22 21:54:34 +00001845 err = xs_bind(transport, sock);
1846 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001847 sock_release(sock);
1848 goto out;
1849 }
1850
1851 return sock;
1852out:
1853 return ERR_PTR(err);
1854}
1855
Chuck Lever176e21e2011-05-09 15:22:44 -04001856static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1857 struct socket *sock)
1858{
1859 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1860 xprt);
1861
1862 if (!transport->inet) {
1863 struct sock *sk = sock->sk;
1864
1865 write_lock_bh(&sk->sk_callback_lock);
1866
1867 xs_save_old_callbacks(transport, sk);
1868
1869 sk->sk_user_data = xprt;
1870 sk->sk_data_ready = xs_local_data_ready;
1871 sk->sk_write_space = xs_udp_write_space;
Chuck Lever176e21e2011-05-09 15:22:44 -04001872 sk->sk_allocation = GFP_ATOMIC;
1873
1874 xprt_clear_connected(xprt);
1875
1876 /* Reset to new socket */
1877 transport->sock = sock;
1878 transport->inet = sk;
1879
1880 write_unlock_bh(&sk->sk_callback_lock);
1881 }
1882
1883 /* Tell the socket layer to start connecting... */
1884 xprt->stat.connect_count++;
1885 xprt->stat.connect_start = jiffies;
1886 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1887}
1888
1889/**
1890 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1891 * @xprt: RPC transport to connect
1892 * @transport: socket transport to connect
1893 * @create_sock: function to create a socket of the correct type
Chuck Lever176e21e2011-05-09 15:22:44 -04001894 */
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001895static int xs_local_setup_socket(struct sock_xprt *transport)
Chuck Lever176e21e2011-05-09 15:22:44 -04001896{
Chuck Lever176e21e2011-05-09 15:22:44 -04001897 struct rpc_xprt *xprt = &transport->xprt;
1898 struct socket *sock;
1899 int status = -EIO;
1900
Jeff Layton5cf02d02012-07-23 13:58:51 -04001901 current->flags |= PF_FSTRANS;
1902
Chuck Lever176e21e2011-05-09 15:22:44 -04001903 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1904 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1905 SOCK_STREAM, 0, &sock, 1);
1906 if (status < 0) {
1907 dprintk("RPC: can't create AF_LOCAL "
1908 "transport socket (%d).\n", -status);
1909 goto out;
1910 }
1911 xs_reclassify_socketu(sock);
1912
1913 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1914 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1915
1916 status = xs_local_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001917 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever176e21e2011-05-09 15:22:44 -04001918 switch (status) {
1919 case 0:
1920 dprintk("RPC: xprt %p connected to %s\n",
1921 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1922 xprt_set_connected(xprt);
1923 break;
1924 case -ENOENT:
1925 dprintk("RPC: xprt %p: socket %s does not exist\n",
1926 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1927 break;
Trond Myklebust4a20a982012-12-15 17:02:29 -05001928 case -ECONNREFUSED:
1929 dprintk("RPC: xprt %p: connection refused for %s\n",
1930 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1931 break;
Chuck Lever176e21e2011-05-09 15:22:44 -04001932 default:
1933 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1934 __func__, -status,
1935 xprt->address_strings[RPC_DISPLAY_ADDR]);
1936 }
1937
1938out:
1939 xprt_clear_connecting(xprt);
1940 xprt_wake_pending_tasks(xprt, status);
Jeff Layton5cf02d02012-07-23 13:58:51 -04001941 current->flags &= ~PF_FSTRANS;
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001942 return status;
1943}
1944
Linus Torvaldsb6669732013-02-28 18:02:55 -08001945static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001946{
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001947 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1948 int ret;
1949
1950 if (RPC_IS_ASYNC(task)) {
1951 /*
1952 * We want the AF_LOCAL connect to be resolved in the
1953 * filesystem namespace of the process making the rpc
1954 * call. Thus we connect synchronously.
1955 *
1956 * If we want to support asynchronous AF_LOCAL calls,
1957 * we'll need to figure out how to pass a namespace to
1958 * connect.
1959 */
1960 rpc_exit(task, -ENOTCONN);
1961 return;
1962 }
1963 ret = xs_local_setup_socket(transport);
1964 if (ret && !RPC_IS_SOFTCONN(task))
1965 msleep_interruptible(15000);
Chuck Lever176e21e2011-05-09 15:22:44 -04001966}
1967
Mel Gormana564b8f2012-07-31 16:45:12 -07001968#ifdef CONFIG_SUNRPC_SWAP
1969static void xs_set_memalloc(struct rpc_xprt *xprt)
1970{
1971 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1972 xprt);
1973
1974 if (xprt->swapper)
1975 sk_set_memalloc(transport->inet);
1976}
1977
1978/**
1979 * xs_swapper - Tag this transport as being used for swap.
1980 * @xprt: transport to tag
1981 * @enable: enable/disable
1982 *
1983 */
1984int xs_swapper(struct rpc_xprt *xprt, int enable)
1985{
1986 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1987 xprt);
1988 int err = 0;
1989
1990 if (enable) {
1991 xprt->swapper++;
1992 xs_set_memalloc(xprt);
1993 } else if (xprt->swapper) {
1994 xprt->swapper--;
1995 sk_clear_memalloc(transport->inet);
1996 }
1997
1998 return err;
1999}
2000EXPORT_SYMBOL_GPL(xs_swapper);
2001#else
2002static void xs_set_memalloc(struct rpc_xprt *xprt)
2003{
2004}
2005#endif
2006
Chuck Lever16be2d22007-08-06 11:57:38 -04002007static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04002008{
Chuck Lever16be2d22007-08-06 11:57:38 -04002009 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04002010
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002011 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002012 struct sock *sk = sock->sk;
2013
2014 write_lock_bh(&sk->sk_callback_lock);
2015
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002016 xs_save_old_callbacks(transport, sk);
2017
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002018 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002019 sk->sk_data_ready = xs_udp_data_ready;
2020 sk->sk_write_space = xs_udp_write_space;
2021 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05002022 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002023
2024 xprt_set_connected(xprt);
2025
2026 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002027 transport->sock = sock;
2028 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002029
Mel Gormana564b8f2012-07-31 16:45:12 -07002030 xs_set_memalloc(xprt);
2031
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002032 write_unlock_bh(&sk->sk_callback_lock);
2033 }
Chuck Lever470056c2005-08-25 16:25:56 -07002034 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04002035}
2036
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002037static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04002038{
2039 struct sock_xprt *transport =
2040 container_of(work, struct sock_xprt, connect_worker.work);
2041 struct rpc_xprt *xprt = &transport->xprt;
2042 struct socket *sock = transport->sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002043 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04002044
Jeff Layton5cf02d02012-07-23 13:58:51 -04002045 current->flags |= PF_FSTRANS;
2046
Chuck Levera246b012005-08-11 16:25:23 -04002047 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04002048 xs_reset_transport(transport);
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002049 sock = xs_create_sock(xprt, transport,
2050 xs_addr(xprt)->sa_family, SOCK_DGRAM, IPPROTO_UDP);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002051 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04002052 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04002053
Chuck Leverc740eff2009-08-09 15:09:46 -04002054 dprintk("RPC: worker connecting xprt %p via %s to "
2055 "%s (port %s)\n", xprt,
2056 xprt->address_strings[RPC_DISPLAY_PROTO],
2057 xprt->address_strings[RPC_DISPLAY_ADDR],
2058 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04002059
2060 xs_udp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002061 trace_rpc_socket_connect(xprt, sock, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002062 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002063out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002064 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002065 xprt_wake_pending_tasks(xprt, status);
Jeff Layton5cf02d02012-07-23 13:58:51 -04002066 current->flags &= ~PF_FSTRANS;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002067}
2068
Chuck Lever3167e122005-08-25 16:25:55 -07002069/*
2070 * We need to preserve the port number so the reply cache on the server can
2071 * find our cached RPC replies when we get around to reconnecting.
2072 */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002073static void xs_abort_connection(struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07002074{
2075 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07002076 struct sockaddr any;
2077
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002078 dprintk("RPC: disconnecting xprt %p to reuse port\n", transport);
Chuck Lever3167e122005-08-25 16:25:55 -07002079
2080 /*
2081 * Disconnect the transport socket by doing a connect operation
2082 * with AF_UNSPEC. This should return immediately...
2083 */
2084 memset(&any, 0, sizeof(any));
2085 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002086 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002087 trace_rpc_socket_reset_connection(&transport->xprt,
2088 transport->sock, result);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002089 if (!result)
Trond Myklebust4bc1e682012-10-23 17:50:07 -04002090 xs_sock_reset_connection_flags(&transport->xprt);
2091 dprintk("RPC: AF_UNSPEC connect return code %d\n", result);
Chuck Lever3167e122005-08-25 16:25:55 -07002092}
2093
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002094static void xs_tcp_reuse_connection(struct sock_xprt *transport)
Trond Myklebust40d25492009-03-11 14:37:58 -04002095{
2096 unsigned int state = transport->inet->sk_state;
2097
Andy Chittenden669502f2010-08-10 10:19:53 -04002098 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) {
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: TCP_CLOSEd and sk_shutdown set to %d\n",
2105 __func__, transport->inet->sk_shutdown);
2106 }
2107 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) {
2108 /* we don't need to abort the connection if the socket
2109 * hasn't undergone a shutdown
2110 */
2111 if (transport->inet->sk_shutdown == 0)
2112 return;
2113 dprintk("RPC: %s: ESTABLISHED/SYN_SENT "
2114 "sk_shutdown set to %d\n",
2115 __func__, transport->inet->sk_shutdown);
2116 }
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002117 xs_abort_connection(transport);
Trond Myklebust40d25492009-03-11 14:37:58 -04002118}
2119
Chuck Lever16be2d22007-08-06 11:57:38 -04002120static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002121{
Chuck Lever16be2d22007-08-06 11:57:38 -04002122 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002123 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002124
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002125 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002126 struct sock *sk = sock->sk;
Trond Myklebust7f260e82013-09-24 11:25:22 -04002127 unsigned int keepidle = xprt->timeout->to_initval / HZ;
2128 unsigned int keepcnt = xprt->timeout->to_retries + 1;
2129 unsigned int opt_on = 1;
2130
2131 /* TCP Keepalive options */
2132 kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
2133 (char *)&opt_on, sizeof(opt_on));
2134 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
2135 (char *)&keepidle, sizeof(keepidle));
2136 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
2137 (char *)&keepidle, sizeof(keepidle));
2138 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
2139 (char *)&keepcnt, sizeof(keepcnt));
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002140
2141 write_lock_bh(&sk->sk_callback_lock);
2142
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002143 xs_save_old_callbacks(transport, sk);
2144
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002145 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002146 sk->sk_data_ready = xs_tcp_data_ready;
2147 sk->sk_state_change = xs_tcp_state_change;
2148 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05002149 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07002150
2151 /* socket options */
2152 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
2153 sock_reset_flag(sk, SOCK_LINGER);
2154 tcp_sk(sk)->linger2 = 0;
2155 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002156
2157 xprt_clear_connected(xprt);
2158
2159 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002160 transport->sock = sock;
2161 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002162
2163 write_unlock_bh(&sk->sk_callback_lock);
2164 }
2165
Trond Myklebust01d37c42009-03-11 14:09:39 -04002166 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002167 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002168
Mel Gormana564b8f2012-07-31 16:45:12 -07002169 xs_set_memalloc(xprt);
2170
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002171 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05002172 xprt->stat.connect_count++;
2173 xprt->stat.connect_start = jiffies;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002174 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2175 switch (ret) {
2176 case 0:
2177 case -EINPROGRESS:
2178 /* SYN_SENT! */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002179 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2180 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2181 }
2182out:
2183 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002184}
2185
2186/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002187 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2188 * @xprt: RPC transport to connect
2189 * @transport: socket transport to connect
2190 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04002191 *
2192 * Invoked by a work queue tasklet.
2193 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002194static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002195{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002196 struct sock_xprt *transport =
2197 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002198 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002199 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002200 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002201
Jeff Layton5cf02d02012-07-23 13:58:51 -04002202 current->flags |= PF_FSTRANS;
2203
Chuck Lever16be2d22007-08-06 11:57:38 -04002204 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002205 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002206 sock = xs_create_sock(xprt, transport,
2207 xs_addr(xprt)->sa_family, SOCK_STREAM, IPPROTO_TCP);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002208 if (IS_ERR(sock)) {
2209 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002210 goto out;
2211 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002212 } else {
2213 int abort_and_exit;
2214
2215 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
2216 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04002217 /* "close" the socket, preserving the local port */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002218 xs_tcp_reuse_connection(transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04002219
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002220 if (abort_and_exit)
2221 goto out_eagain;
2222 }
2223
Chuck Leverc740eff2009-08-09 15:09:46 -04002224 dprintk("RPC: worker connecting xprt %p via %s to "
2225 "%s (port %s)\n", xprt,
2226 xprt->address_strings[RPC_DISPLAY_PROTO],
2227 xprt->address_strings[RPC_DISPLAY_ADDR],
2228 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002229
2230 status = xs_tcp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002231 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever46121cf2007-01-31 12:14:08 -05002232 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2233 xprt, -status, xprt_connected(xprt),
2234 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002235 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002236 default:
2237 printk("%s: connect returned unhandled error %d\n",
2238 __func__, status);
2239 case -EADDRNOTAVAIL:
2240 /* We're probably in TIME_WAIT. Get rid of existing socket,
2241 * and retry
2242 */
Trond Myklebusta519fc72012-09-12 16:49:15 -04002243 xs_tcp_force_close(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002244 break;
Trond Myklebust2a491992009-03-11 14:38:00 -04002245 case 0:
2246 case -EINPROGRESS:
2247 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002248 xprt_clear_connecting(xprt);
Jeff Layton5cf02d02012-07-23 13:58:51 -04002249 current->flags &= ~PF_FSTRANS;
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002250 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002251 case -EINVAL:
2252 /* Happens, for instance, if the user specified a link
2253 * local IPv6 address without a scope-id.
2254 */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002255 case -ECONNREFUSED:
2256 case -ECONNRESET:
2257 case -ENETUNREACH:
2258 /* retry with existing socket, after a delay */
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002259 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002260 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002261out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04002262 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002263out:
Chuck Lever2226feb2005-08-11 16:25:38 -04002264 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002265 xprt_wake_pending_tasks(xprt, status);
Jeff Layton5cf02d02012-07-23 13:58:51 -04002266 current->flags &= ~PF_FSTRANS;
Chuck Levera246b012005-08-11 16:25:23 -04002267}
2268
Chuck Lever68e220b2007-08-06 11:57:48 -04002269/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002270 * xs_connect - connect a socket to a remote endpoint
Trond Myklebust1b092092013-01-08 09:26:49 -05002271 * @xprt: pointer to transport structure
Chuck Lever9903cd12005-08-11 16:25:26 -04002272 * @task: address of RPC task that manages state of connect request
2273 *
2274 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002275 *
2276 * UDP socket connects are synchronous, but we use a work queue anyway
2277 * to guarantee that even unprivileged user processes can set up a
2278 * socket on a privileged port.
2279 *
2280 * If a UDP socket connect fails, the delay behavior here prevents
2281 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002282 */
Trond Myklebust1b092092013-01-08 09:26:49 -05002283static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002284{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002285 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002286
Chuck Lever09a21c42009-12-03 15:58:56 -05002287 if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002288 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2289 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002290 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002291 queue_delayed_work(rpciod_workqueue,
2292 &transport->connect_worker,
2293 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07002294 xprt->reestablish_timeout <<= 1;
Neil Brown61d0a8e2009-09-23 14:36:37 -04002295 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2296 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002297 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2298 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002299 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05002300 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002301 queue_delayed_work(rpciod_workqueue,
2302 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002303 }
2304}
2305
Chuck Lever262ca072006-03-20 13:44:16 -05002306/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002307 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2308 * @xprt: rpc_xprt struct containing statistics
2309 * @seq: output file
2310 *
2311 */
2312static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2313{
2314 long idle_time = 0;
2315
2316 if (xprt_connected(xprt))
2317 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2318
2319 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
Andy Adamson15a45202012-02-14 16:19:18 -05002320 "%llu %llu %lu %llu %llu\n",
Chuck Lever176e21e2011-05-09 15:22:44 -04002321 xprt->stat.bind_count,
2322 xprt->stat.connect_count,
2323 xprt->stat.connect_time,
2324 idle_time,
2325 xprt->stat.sends,
2326 xprt->stat.recvs,
2327 xprt->stat.bad_xids,
2328 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002329 xprt->stat.bklog_u,
2330 xprt->stat.max_slots,
2331 xprt->stat.sending_u,
2332 xprt->stat.pending_u);
Chuck Lever176e21e2011-05-09 15:22:44 -04002333}
2334
2335/**
Chuck Lever262ca072006-03-20 13:44:16 -05002336 * xs_udp_print_stats - display UDP socket-specifc stats
2337 * @xprt: rpc_xprt struct containing statistics
2338 * @seq: output file
2339 *
2340 */
2341static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2342{
Chuck Leverc8475462006-12-05 16:35:26 -05002343 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2344
Andy Adamson15a45202012-02-14 16:19:18 -05002345 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2346 "%lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002347 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002348 xprt->stat.bind_count,
2349 xprt->stat.sends,
2350 xprt->stat.recvs,
2351 xprt->stat.bad_xids,
2352 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002353 xprt->stat.bklog_u,
2354 xprt->stat.max_slots,
2355 xprt->stat.sending_u,
2356 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002357}
2358
2359/**
2360 * xs_tcp_print_stats - display TCP socket-specifc stats
2361 * @xprt: rpc_xprt struct containing statistics
2362 * @seq: output file
2363 *
2364 */
2365static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2366{
Chuck Leverc8475462006-12-05 16:35:26 -05002367 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002368 long idle_time = 0;
2369
2370 if (xprt_connected(xprt))
2371 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2372
Andy Adamson15a45202012-02-14 16:19:18 -05002373 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2374 "%llu %llu %lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002375 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002376 xprt->stat.bind_count,
2377 xprt->stat.connect_count,
2378 xprt->stat.connect_time,
2379 idle_time,
2380 xprt->stat.sends,
2381 xprt->stat.recvs,
2382 xprt->stat.bad_xids,
2383 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002384 xprt->stat.bklog_u,
2385 xprt->stat.max_slots,
2386 xprt->stat.sending_u,
2387 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002388}
2389
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002390/*
2391 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2392 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2393 * to use the server side send routines.
2394 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002395static void *bc_malloc(struct rpc_task *task, size_t size)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002396{
2397 struct page *page;
2398 struct rpc_buffer *buf;
2399
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002400 WARN_ON_ONCE(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2401 if (size > PAGE_SIZE - sizeof(struct rpc_buffer))
2402 return NULL;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002403
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002404 page = alloc_page(GFP_KERNEL);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002405 if (!page)
2406 return NULL;
2407
2408 buf = page_address(page);
2409 buf->len = PAGE_SIZE;
2410
2411 return buf->data;
2412}
2413
2414/*
2415 * Free the space allocated in the bc_alloc routine
2416 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002417static void bc_free(void *buffer)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002418{
2419 struct rpc_buffer *buf;
2420
2421 if (!buffer)
2422 return;
2423
2424 buf = container_of(buffer, struct rpc_buffer, data);
2425 free_page((unsigned long)buf);
2426}
2427
2428/*
2429 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2430 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2431 */
2432static int bc_sendto(struct rpc_rqst *req)
2433{
2434 int len;
2435 struct xdr_buf *xbufp = &req->rq_snd_buf;
2436 struct rpc_xprt *xprt = req->rq_xprt;
2437 struct sock_xprt *transport =
2438 container_of(xprt, struct sock_xprt, xprt);
2439 struct socket *sock = transport->sock;
2440 unsigned long headoff;
2441 unsigned long tailoff;
2442
Chuck Lever61677ee2011-05-09 15:22:34 -04002443 xs_encode_stream_record_marker(xbufp);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002444
2445 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2446 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2447 len = svc_send_common(sock, xbufp,
2448 virt_to_page(xbufp->head[0].iov_base), headoff,
2449 xbufp->tail[0].iov_base, tailoff);
2450
2451 if (len != xbufp->len) {
2452 printk(KERN_NOTICE "Error sending entire callback!\n");
2453 len = -EAGAIN;
2454 }
2455
2456 return len;
2457}
2458
2459/*
2460 * The send routine. Borrows from svc_send
2461 */
2462static int bc_send_request(struct rpc_task *task)
2463{
2464 struct rpc_rqst *req = task->tk_rqstp;
2465 struct svc_xprt *xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002466 u32 len;
2467
2468 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2469 /*
2470 * Get the server socket associated with this callback xprt
2471 */
2472 xprt = req->rq_xprt->bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002473
2474 /*
2475 * Grab the mutex to serialize data as the connection is shared
2476 * with the fore channel
2477 */
2478 if (!mutex_trylock(&xprt->xpt_mutex)) {
2479 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2480 if (!mutex_trylock(&xprt->xpt_mutex))
2481 return -EAGAIN;
2482 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2483 }
2484 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2485 len = -ENOTCONN;
2486 else
2487 len = bc_sendto(req);
2488 mutex_unlock(&xprt->xpt_mutex);
2489
2490 if (len > 0)
2491 len = 0;
2492
2493 return len;
2494}
2495
2496/*
2497 * The close routine. Since this is client initiated, we do nothing
2498 */
2499
2500static void bc_close(struct rpc_xprt *xprt)
2501{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002502}
2503
2504/*
2505 * The xprt destroy routine. Again, because this connection is client
2506 * initiated, we do nothing
2507 */
2508
2509static void bc_destroy(struct rpc_xprt *xprt)
2510{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002511}
2512
Chuck Lever176e21e2011-05-09 15:22:44 -04002513static struct rpc_xprt_ops xs_local_ops = {
2514 .reserve_xprt = xprt_reserve_xprt,
2515 .release_xprt = xs_tcp_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002516 .alloc_slot = xprt_alloc_slot,
Chuck Lever176e21e2011-05-09 15:22:44 -04002517 .rpcbind = xs_local_rpcbind,
2518 .set_port = xs_local_set_port,
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002519 .connect = xs_local_connect,
Chuck Lever176e21e2011-05-09 15:22:44 -04002520 .buf_alloc = rpc_malloc,
2521 .buf_free = rpc_free,
2522 .send_request = xs_local_send_request,
2523 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2524 .close = xs_close,
Trond Myklebusta1311d82013-10-31 09:18:49 -04002525 .destroy = xs_destroy,
Chuck Lever176e21e2011-05-09 15:22:44 -04002526 .print_stats = xs_local_print_stats,
2527};
2528
Chuck Lever262965f2005-08-11 16:25:56 -04002529static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002530 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002531 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002532 .release_xprt = xprt_release_xprt_cong,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002533 .alloc_slot = xprt_alloc_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002534 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002535 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002536 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002537 .buf_alloc = rpc_malloc,
2538 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002539 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002540 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002541 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002542 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002543 .close = xs_close,
2544 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002545 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04002546};
2547
2548static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002549 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002550 .release_xprt = xs_tcp_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002551 .alloc_slot = xprt_lock_and_alloc_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002552 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002553 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002554 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002555 .buf_alloc = rpc_malloc,
2556 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002557 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002558 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustf75e6742009-04-21 17:18:20 -04002559 .close = xs_tcp_close,
Chuck Lever9903cd12005-08-11 16:25:26 -04002560 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002561 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002562};
2563
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002564/*
2565 * The rpc_xprt_ops for the server backchannel
2566 */
2567
2568static struct rpc_xprt_ops bc_tcp_ops = {
2569 .reserve_xprt = xprt_reserve_xprt,
2570 .release_xprt = xprt_release_xprt,
Bryan Schumaker84e28a32012-09-24 13:39:01 -04002571 .alloc_slot = xprt_alloc_slot,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002572 .buf_alloc = bc_malloc,
2573 .buf_free = bc_free,
2574 .send_request = bc_send_request,
2575 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2576 .close = bc_close,
2577 .destroy = bc_destroy,
2578 .print_stats = xs_tcp_print_stats,
2579};
2580
Chuck Lever92476852010-10-20 11:53:01 -04002581static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2582{
2583 static const struct sockaddr_in sin = {
2584 .sin_family = AF_INET,
2585 .sin_addr.s_addr = htonl(INADDR_ANY),
2586 };
2587 static const struct sockaddr_in6 sin6 = {
2588 .sin6_family = AF_INET6,
2589 .sin6_addr = IN6ADDR_ANY_INIT,
2590 };
2591
2592 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002593 case AF_LOCAL:
2594 break;
Chuck Lever92476852010-10-20 11:53:01 -04002595 case AF_INET:
2596 memcpy(sap, &sin, sizeof(sin));
2597 break;
2598 case AF_INET6:
2599 memcpy(sap, &sin6, sizeof(sin6));
2600 break;
2601 default:
2602 dprintk("RPC: %s: Bad address family\n", __func__);
2603 return -EAFNOSUPPORT;
2604 }
2605 return 0;
2606}
2607
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002608static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002609 unsigned int slot_table_size,
2610 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002611{
2612 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002613 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002614
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002615 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002616 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002617 return ERR_PTR(-EBADF);
2618 }
2619
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002620 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2621 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002622 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002623 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2624 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002625 return ERR_PTR(-ENOMEM);
2626 }
2627
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002628 new = container_of(xprt, struct sock_xprt, xprt);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002629 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2630 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002631 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002632 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002633 else {
2634 int err;
2635 err = xs_init_anyaddr(args->dstaddr->sa_family,
2636 (struct sockaddr *)&new->srcaddr);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002637 if (err != 0) {
2638 xprt_free(xprt);
Chuck Lever92476852010-10-20 11:53:01 -04002639 return ERR_PTR(err);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002640 }
Chuck Lever92476852010-10-20 11:53:01 -04002641 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002642
2643 return xprt;
2644}
2645
Chuck Lever176e21e2011-05-09 15:22:44 -04002646static const struct rpc_timeout xs_local_default_timeout = {
2647 .to_initval = 10 * HZ,
2648 .to_maxval = 10 * HZ,
2649 .to_retries = 2,
2650};
2651
2652/**
2653 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2654 * @args: rpc transport creation arguments
2655 *
2656 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2657 */
2658static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2659{
2660 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2661 struct sock_xprt *transport;
2662 struct rpc_xprt *xprt;
2663 struct rpc_xprt *ret;
2664
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002665 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2666 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002667 if (IS_ERR(xprt))
2668 return xprt;
2669 transport = container_of(xprt, struct sock_xprt, xprt);
2670
2671 xprt->prot = 0;
2672 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2673 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2674
2675 xprt->bind_timeout = XS_BIND_TO;
2676 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2677 xprt->idle_timeout = XS_IDLE_DISC_TO;
2678
2679 xprt->ops = &xs_local_ops;
2680 xprt->timeout = &xs_local_default_timeout;
2681
NeilBrown93dc41b2013-10-31 16:14:36 +11002682 INIT_DELAYED_WORK(&transport->connect_worker,
2683 xs_dummy_setup_socket);
2684
Chuck Lever176e21e2011-05-09 15:22:44 -04002685 switch (sun->sun_family) {
2686 case AF_LOCAL:
2687 if (sun->sun_path[0] != '/') {
2688 dprintk("RPC: bad AF_LOCAL address: %s\n",
2689 sun->sun_path);
2690 ret = ERR_PTR(-EINVAL);
2691 goto out_err;
2692 }
2693 xprt_set_bound(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002694 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
J. Bruce Fields7073ea82013-02-21 10:14:22 -05002695 ret = ERR_PTR(xs_local_setup_socket(transport));
2696 if (ret)
2697 goto out_err;
Chuck Lever176e21e2011-05-09 15:22:44 -04002698 break;
2699 default:
2700 ret = ERR_PTR(-EAFNOSUPPORT);
2701 goto out_err;
2702 }
2703
2704 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2705 xprt->address_strings[RPC_DISPLAY_ADDR]);
2706
2707 if (try_module_get(THIS_MODULE))
2708 return xprt;
2709 ret = ERR_PTR(-EINVAL);
2710out_err:
2711 xprt_free(xprt);
2712 return ret;
2713}
2714
Trond Myklebust2881ae72007-12-20 16:03:54 -05002715static const struct rpc_timeout xs_udp_default_timeout = {
2716 .to_initval = 5 * HZ,
2717 .to_maxval = 30 * HZ,
2718 .to_increment = 5 * HZ,
2719 .to_retries = 5,
2720};
2721
Chuck Lever9903cd12005-08-11 16:25:26 -04002722/**
2723 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002724 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002725 *
2726 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002727static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002728{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002729 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002730 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002731 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002732 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002733
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002734 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2735 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002736 if (IS_ERR(xprt))
2737 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002738 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002739
Chuck Leverec739ef2006-08-22 20:06:15 -04002740 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002741 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002742 /* XXX: header size can vary due to auth type, IPv6, etc. */
2743 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2744
Chuck Lever03bf4b72005-08-25 16:25:55 -07002745 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002746 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2747 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002748
Chuck Lever262965f2005-08-11 16:25:56 -04002749 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002750
Trond Myklebustba7392b2007-12-20 16:03:55 -05002751 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002752
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002753 switch (addr->sa_family) {
2754 case AF_INET:
2755 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2756 xprt_set_bound(xprt);
2757
2758 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002759 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002760 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002761 break;
2762 case AF_INET6:
2763 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2764 xprt_set_bound(xprt);
2765
2766 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002767 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002768 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002769 break;
2770 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002771 ret = ERR_PTR(-EAFNOSUPPORT);
2772 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002773 }
2774
Chuck Leverc740eff2009-08-09 15:09:46 -04002775 if (xprt_bound(xprt))
2776 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2777 xprt->address_strings[RPC_DISPLAY_ADDR],
2778 xprt->address_strings[RPC_DISPLAY_PORT],
2779 xprt->address_strings[RPC_DISPLAY_PROTO]);
2780 else
2781 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2782 xprt->address_strings[RPC_DISPLAY_ADDR],
2783 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002784
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002785 if (try_module_get(THIS_MODULE))
2786 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002787 ret = ERR_PTR(-EINVAL);
2788out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002789 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002790 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002791}
2792
Trond Myklebust2881ae72007-12-20 16:03:54 -05002793static const struct rpc_timeout xs_tcp_default_timeout = {
2794 .to_initval = 60 * HZ,
2795 .to_maxval = 60 * HZ,
2796 .to_retries = 2,
2797};
2798
Chuck Lever9903cd12005-08-11 16:25:26 -04002799/**
2800 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002801 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002802 *
2803 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002804static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002805{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002806 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002807 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002808 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002809 struct rpc_xprt *ret;
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002810 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2811
2812 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2813 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Levera246b012005-08-11 16:25:23 -04002814
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002815 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002816 max_slot_table_size);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002817 if (IS_ERR(xprt))
2818 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002819 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002820
Chuck Leverec739ef2006-08-22 20:06:15 -04002821 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002822 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002823 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002824
Chuck Lever03bf4b72005-08-25 16:25:55 -07002825 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002826 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2827 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002828
Chuck Lever262965f2005-08-11 16:25:56 -04002829 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002830 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002831
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002832 switch (addr->sa_family) {
2833 case AF_INET:
2834 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2835 xprt_set_bound(xprt);
2836
Chuck Lever9dc3b092009-08-09 15:09:46 -04002837 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002838 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002839 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002840 break;
2841 case AF_INET6:
2842 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2843 xprt_set_bound(xprt);
2844
Chuck Lever9dc3b092009-08-09 15:09:46 -04002845 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002846 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002847 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002848 break;
2849 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002850 ret = ERR_PTR(-EAFNOSUPPORT);
2851 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002852 }
2853
Chuck Leverc740eff2009-08-09 15:09:46 -04002854 if (xprt_bound(xprt))
2855 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2856 xprt->address_strings[RPC_DISPLAY_ADDR],
2857 xprt->address_strings[RPC_DISPLAY_PORT],
2858 xprt->address_strings[RPC_DISPLAY_PROTO]);
2859 else
2860 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2861 xprt->address_strings[RPC_DISPLAY_ADDR],
2862 xprt->address_strings[RPC_DISPLAY_PROTO]);
2863
Chuck Leveredb267a2006-08-22 20:06:18 -04002864
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002865 if (try_module_get(THIS_MODULE))
2866 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002867 ret = ERR_PTR(-EINVAL);
2868out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002869 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002870 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002871}
Chuck Lever282b32e2006-12-05 16:35:51 -05002872
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002873/**
2874 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2875 * @args: rpc transport creation arguments
2876 *
2877 */
2878static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2879{
2880 struct sockaddr *addr = args->dstaddr;
2881 struct rpc_xprt *xprt;
2882 struct sock_xprt *transport;
2883 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002884 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002885
J. Bruce Fieldsf0418aa2010-12-08 13:48:19 -05002886 if (args->bc_xprt->xpt_bc_xprt) {
2887 /*
2888 * This server connection already has a backchannel
J. Bruce Fieldse3bfab12013-10-02 09:48:15 -04002889 * transport; we can't create a new one, as we wouldn't
2890 * be able to match replies based on xid any more. So,
J. Bruce Fieldsf0418aa2010-12-08 13:48:19 -05002891 * reuse the already-existing one:
2892 */
2893 return args->bc_xprt->xpt_bc_xprt;
2894 }
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002895 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2896 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002897 if (IS_ERR(xprt))
2898 return xprt;
2899 transport = container_of(xprt, struct sock_xprt, xprt);
2900
2901 xprt->prot = IPPROTO_TCP;
2902 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2903 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2904 xprt->timeout = &xs_tcp_default_timeout;
2905
2906 /* backchannel */
2907 xprt_set_bound(xprt);
2908 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002909 xprt->reestablish_timeout = 0;
2910 xprt->idle_timeout = 0;
2911
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002912 xprt->ops = &bc_tcp_ops;
2913
2914 switch (addr->sa_family) {
2915 case AF_INET:
2916 xs_format_peer_addresses(xprt, "tcp",
2917 RPCBIND_NETID_TCP);
2918 break;
2919 case AF_INET6:
2920 xs_format_peer_addresses(xprt, "tcp",
2921 RPCBIND_NETID_TCP6);
2922 break;
2923 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002924 ret = ERR_PTR(-EAFNOSUPPORT);
2925 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002926 }
2927
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04002928 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2929 xprt->address_strings[RPC_DISPLAY_ADDR],
2930 xprt->address_strings[RPC_DISPLAY_PORT],
2931 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002932
2933 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002934 * Once we've associated a backchannel xprt with a connection,
2935 * we want to keep it around as long as long as the connection
2936 * lasts, in case we need to start using it for a backchannel
2937 * again; this reference won't be dropped until bc_xprt is
2938 * destroyed.
2939 */
2940 xprt_get(xprt);
2941 args->bc_xprt->xpt_bc_xprt = xprt;
2942 xprt->bc_xprt = args->bc_xprt;
2943 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2944 transport->sock = bc_sock->sk_sock;
2945 transport->inet = bc_sock->sk_sk;
2946
2947 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002948 * Since we don't want connections for the backchannel, we set
2949 * the xprt status to connected
2950 */
2951 xprt_set_connected(xprt);
2952
2953
2954 if (try_module_get(THIS_MODULE))
2955 return xprt;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002956 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002957 ret = ERR_PTR(-EINVAL);
2958out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002959 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002960 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002961}
2962
Chuck Lever176e21e2011-05-09 15:22:44 -04002963static struct xprt_class xs_local_transport = {
2964 .list = LIST_HEAD_INIT(xs_local_transport.list),
2965 .name = "named UNIX socket",
2966 .owner = THIS_MODULE,
2967 .ident = XPRT_TRANSPORT_LOCAL,
2968 .setup = xs_setup_local,
2969};
2970
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002971static struct xprt_class xs_udp_transport = {
2972 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2973 .name = "udp",
2974 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002975 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002976 .setup = xs_setup_udp,
2977};
2978
2979static struct xprt_class xs_tcp_transport = {
2980 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2981 .name = "tcp",
2982 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002983 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002984 .setup = xs_setup_tcp,
2985};
2986
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002987static struct xprt_class xs_bc_tcp_transport = {
2988 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2989 .name = "tcp NFSv4.1 backchannel",
2990 .owner = THIS_MODULE,
2991 .ident = XPRT_TRANSPORT_BC_TCP,
2992 .setup = xs_setup_bc_tcp,
2993};
2994
Chuck Lever282b32e2006-12-05 16:35:51 -05002995/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002996 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002997 *
2998 */
2999int init_socket_xprt(void)
3000{
Chuck Leverfbf76682006-12-05 16:35:54 -05003001#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08003002 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08003003 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05003004#endif
3005
Chuck Lever176e21e2011-05-09 15:22:44 -04003006 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003007 xprt_register_transport(&xs_udp_transport);
3008 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003009 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003010
Chuck Lever282b32e2006-12-05 16:35:51 -05003011 return 0;
3012}
3013
3014/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003015 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05003016 *
3017 */
3018void cleanup_socket_xprt(void)
3019{
Chuck Leverfbf76682006-12-05 16:35:54 -05003020#ifdef RPC_DEBUG
3021 if (sunrpc_table_header) {
3022 unregister_sysctl_table(sunrpc_table_header);
3023 sunrpc_table_header = NULL;
3024 }
3025#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003026
Chuck Lever176e21e2011-05-09 15:22:44 -04003027 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003028 xprt_unregister_transport(&xs_udp_transport);
3029 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003030 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05003031}
Trond Myklebustcbf11072009-08-09 15:06:19 -04003032
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003033static int param_set_uint_minmax(const char *val,
3034 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003035 unsigned int min, unsigned int max)
3036{
3037 unsigned long num;
3038 int ret;
3039
3040 if (!val)
3041 return -EINVAL;
3042 ret = strict_strtoul(val, 0, &num);
3043 if (ret == -EINVAL || num < min || num > max)
3044 return -EINVAL;
3045 *((unsigned int *)kp->arg) = num;
3046 return 0;
3047}
3048
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003049static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003050{
3051 return param_set_uint_minmax(val, kp,
3052 RPC_MIN_RESVPORT,
3053 RPC_MAX_RESVPORT);
3054}
3055
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003056static struct kernel_param_ops param_ops_portnr = {
3057 .set = param_set_portnr,
3058 .get = param_get_uint,
3059};
3060
Trond Myklebustcbf11072009-08-09 15:06:19 -04003061#define param_check_portnr(name, p) \
3062 __param_check(name, p, unsigned int);
3063
3064module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3065module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3066
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003067static int param_set_slot_table_size(const char *val,
3068 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003069{
3070 return param_set_uint_minmax(val, kp,
3071 RPC_MIN_SLOT_TABLE,
3072 RPC_MAX_SLOT_TABLE);
3073}
3074
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003075static struct kernel_param_ops param_ops_slot_table_size = {
3076 .set = param_set_slot_table_size,
3077 .get = param_get_uint,
3078};
3079
Trond Myklebustcbf11072009-08-09 15:06:19 -04003080#define param_check_slot_table_size(name, p) \
3081 __param_check(name, p, unsigned int);
3082
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003083static int param_set_max_slot_table_size(const char *val,
3084 const struct kernel_param *kp)
3085{
3086 return param_set_uint_minmax(val, kp,
3087 RPC_MIN_SLOT_TABLE,
3088 RPC_MAX_SLOT_TABLE_LIMIT);
3089}
3090
3091static struct kernel_param_ops param_ops_max_slot_table_size = {
3092 .set = param_set_max_slot_table_size,
3093 .get = param_get_uint,
3094};
3095
3096#define param_check_max_slot_table_size(name, p) \
3097 __param_check(name, p, unsigned int);
3098
Trond Myklebustcbf11072009-08-09 15:06:19 -04003099module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3100 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003101module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3102 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003103module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3104 slot_table_size, 0644);
3105