blob: 726e702b7a2935265b5f0c70378229f26c6f5c38 [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
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030050#include "sunrpc.h"
Chuck Lever176e21e2011-05-09 15:22:44 -040051
52static void xs_close(struct rpc_xprt *xprt);
53
Chuck Lever9903cd12005-08-11 16:25:26 -040054/*
Chuck Leverc556b752005-11-01 12:24:48 -050055 * xprtsock tunables
56 */
Trond Myklebust09acfea2012-03-11 15:22:54 -040057static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
58static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
59static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
Chuck Leverc556b752005-11-01 12:24:48 -050060
Trond Myklebust09acfea2012-03-11 15:22:54 -040061static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
62static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
Chuck Leverc556b752005-11-01 12:24:48 -050063
Trond Myklebust7d1e8252009-03-11 14:38:03 -040064#define XS_TCP_LINGER_TO (15U * HZ)
Trond Myklebust25fe6142009-03-11 14:38:03 -040065static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
Trond Myklebust7d1e8252009-03-11 14:38:03 -040066
Chuck Leverc556b752005-11-01 12:24:48 -050067/*
Chuck Leverfbf76682006-12-05 16:35:54 -050068 * We can register our own files under /proc/sys/sunrpc by
69 * calling register_sysctl_table() again. The files in that
70 * directory become the union of all files registered there.
71 *
72 * We simply need to make sure that we don't collide with
73 * someone else's file names!
74 */
75
76#ifdef RPC_DEBUG
77
78static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
79static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
Trond Myklebustd9ba1312011-07-17 18:11:30 -040080static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Leverfbf76682006-12-05 16:35:54 -050081static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
82static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
83
84static struct ctl_table_header *sunrpc_table_header;
85
86/*
87 * FIXME: changing the UDP slot table size should also resize the UDP
88 * socket buffers for existing UDP transports
89 */
90static ctl_table xs_tunables_table[] = {
91 {
Chuck Leverfbf76682006-12-05 16:35:54 -050092 .procname = "udp_slot_table_entries",
93 .data = &xprt_udp_slot_table_entries,
94 .maxlen = sizeof(unsigned int),
95 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080096 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -050097 .extra1 = &min_slot_table_size,
98 .extra2 = &max_slot_table_size
99 },
100 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500101 .procname = "tcp_slot_table_entries",
102 .data = &xprt_tcp_slot_table_entries,
103 .maxlen = sizeof(unsigned int),
104 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800105 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500106 .extra1 = &min_slot_table_size,
107 .extra2 = &max_slot_table_size
108 },
109 {
Trond Myklebustd9ba1312011-07-17 18:11:30 -0400110 .procname = "tcp_max_slot_table_entries",
111 .data = &xprt_max_tcp_slot_table_entries,
112 .maxlen = sizeof(unsigned int),
113 .mode = 0644,
114 .proc_handler = proc_dointvec_minmax,
115 .extra1 = &min_slot_table_size,
116 .extra2 = &max_tcp_slot_table_limit
117 },
118 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500119 .procname = "min_resvport",
120 .data = &xprt_min_resvport,
121 .maxlen = sizeof(unsigned int),
122 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800123 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500124 .extra1 = &xprt_min_resvport_limit,
125 .extra2 = &xprt_max_resvport_limit
126 },
127 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500128 .procname = "max_resvport",
129 .data = &xprt_max_resvport,
130 .maxlen = sizeof(unsigned int),
131 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800132 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500133 .extra1 = &xprt_min_resvport_limit,
134 .extra2 = &xprt_max_resvport_limit
135 },
136 {
Trond Myklebust25fe6142009-03-11 14:38:03 -0400137 .procname = "tcp_fin_timeout",
138 .data = &xs_tcp_fin_timeout,
139 .maxlen = sizeof(xs_tcp_fin_timeout),
140 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800141 .proc_handler = proc_dointvec_jiffies,
Trond Myklebust25fe6142009-03-11 14:38:03 -0400142 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800143 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500144};
145
146static ctl_table sunrpc_table[] = {
147 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500148 .procname = "sunrpc",
149 .mode = 0555,
150 .child = xs_tunables_table
151 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800152 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500153};
154
155#endif
156
157/*
Chuck Lever03bf4b72005-08-25 16:25:55 -0700158 * Wait duration for a reply from the RPC portmapper.
159 */
160#define XS_BIND_TO (60U * HZ)
161
162/*
163 * Delay if a UDP socket connect error occurs. This is most likely some
164 * kind of resource problem on the local host.
165 */
166#define XS_UDP_REEST_TO (2U * HZ)
167
168/*
169 * The reestablish timeout allows clients to delay for a bit before attempting
170 * to reconnect to a server that just dropped our connection.
171 *
172 * We implement an exponential backoff when trying to reestablish a TCP
173 * transport connection with the server. Some servers like to drop a TCP
174 * connection when they are overworked, so we start with a short timeout and
175 * increase over time if the server is down or not responding.
176 */
177#define XS_TCP_INIT_REEST_TO (3U * HZ)
178#define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
179
180/*
181 * TCP idle timeout; client drops the transport socket if it is idle
182 * for this long. Note that we also timeout UDP sockets to prevent
183 * holding port numbers when there is no RPC traffic.
184 */
185#define XS_IDLE_DISC_TO (5U * 60 * HZ)
186
Chuck Levera246b012005-08-11 16:25:23 -0400187#ifdef RPC_DEBUG
188# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400189# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400190#endif
191
Chuck Levera246b012005-08-11 16:25:23 -0400192#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400193static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400194{
Chuck Lever9903cd12005-08-11 16:25:26 -0400195 u8 *buf = (u8 *) packet;
196 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400197
Chuck Lever46121cf2007-01-31 12:14:08 -0500198 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400199 for (j = 0; j < count && j < 128; j += 4) {
200 if (!(j & 31)) {
201 if (j)
202 dprintk("\n");
203 dprintk("0x%04x ", j);
204 }
205 dprintk("%02x%02x%02x%02x ",
206 buf[j], buf[j+1], buf[j+2], buf[j+3]);
207 }
208 dprintk("\n");
209}
210#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400211static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400212{
213 /* NOP */
214}
215#endif
216
Chuck Leverffc2e512006-12-05 16:35:11 -0500217struct sock_xprt {
218 struct rpc_xprt xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500219
220 /*
221 * Network layer
222 */
223 struct socket * sock;
224 struct sock * inet;
Chuck Lever51971132006-12-05 16:35:19 -0500225
226 /*
227 * State of TCP reply receive
228 */
229 __be32 tcp_fraghdr,
Trond Myklebustb76ce562010-06-16 13:57:32 -0400230 tcp_xid,
231 tcp_calldir;
Chuck Lever51971132006-12-05 16:35:19 -0500232
233 u32 tcp_offset,
234 tcp_reclen;
235
236 unsigned long tcp_copied,
237 tcp_flags;
Chuck Leverc8475462006-12-05 16:35:26 -0500238
239 /*
240 * Connection of transports
241 */
Trond Myklebust34161db2006-12-07 15:48:15 -0500242 struct delayed_work connect_worker;
Chuck Leverfbfffbd2009-08-09 15:09:46 -0400243 struct sockaddr_storage srcaddr;
244 unsigned short srcport;
Chuck Lever7c6e0662006-12-05 16:35:30 -0500245
246 /*
247 * UDP socket buffer size parameters
248 */
249 size_t rcvsize,
250 sndsize;
Chuck Lever314dfd72006-12-05 16:35:34 -0500251
252 /*
253 * Saved socket callback addresses
254 */
255 void (*old_data_ready)(struct sock *, int);
256 void (*old_state_change)(struct sock *);
257 void (*old_write_space)(struct sock *);
Chuck Leverffc2e512006-12-05 16:35:11 -0500258};
259
Chuck Levere136d092006-12-05 16:35:23 -0500260/*
261 * TCP receive state flags
262 */
263#define TCP_RCV_LAST_FRAG (1UL << 0)
264#define TCP_RCV_COPY_FRAGHDR (1UL << 1)
265#define TCP_RCV_COPY_XID (1UL << 2)
266#define TCP_RCV_COPY_DATA (1UL << 3)
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400267#define TCP_RCV_READ_CALLDIR (1UL << 4)
268#define TCP_RCV_COPY_CALLDIR (1UL << 5)
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400269
270/*
271 * TCP RPC flags
272 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400273#define TCP_RPC_REPLY (1UL << 6)
Chuck Levere136d092006-12-05 16:35:23 -0500274
Chuck Lever95392c52007-08-06 11:57:58 -0400275static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400276{
Chuck Lever95392c52007-08-06 11:57:58 -0400277 return (struct sockaddr *) &xprt->addr;
278}
279
Chuck Lever176e21e2011-05-09 15:22:44 -0400280static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
281{
282 return (struct sockaddr_un *) &xprt->addr;
283}
284
Chuck Lever95392c52007-08-06 11:57:58 -0400285static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
286{
287 return (struct sockaddr_in *) &xprt->addr;
288}
289
290static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
291{
292 return (struct sockaddr_in6 *) &xprt->addr;
293}
294
Chuck Leverc877b842009-08-09 15:09:36 -0400295static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400296{
Chuck Leverc877b842009-08-09 15:09:36 -0400297 struct sockaddr *sap = xs_addr(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400298 struct sockaddr_in6 *sin6;
299 struct sockaddr_in *sin;
Chuck Lever176e21e2011-05-09 15:22:44 -0400300 struct sockaddr_un *sun;
Chuck Leverc877b842009-08-09 15:09:36 -0400301 char buf[128];
Chuck Leveredb267a2006-08-22 20:06:18 -0400302
Chuck Lever9dc3b092009-08-09 15:09:46 -0400303 switch (sap->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400304 case AF_LOCAL:
305 sun = xs_addr_un(xprt);
306 strlcpy(buf, sun->sun_path, sizeof(buf));
307 xprt->address_strings[RPC_DISPLAY_ADDR] =
308 kstrdup(buf, GFP_KERNEL);
309 break;
Chuck Lever9dc3b092009-08-09 15:09:46 -0400310 case AF_INET:
Chuck Lever176e21e2011-05-09 15:22:44 -0400311 (void)rpc_ntop(sap, buf, sizeof(buf));
312 xprt->address_strings[RPC_DISPLAY_ADDR] =
313 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400314 sin = xs_addr_in(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800315 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
Chuck Lever9dc3b092009-08-09 15:09:46 -0400316 break;
317 case AF_INET6:
Chuck Lever176e21e2011-05-09 15:22:44 -0400318 (void)rpc_ntop(sap, buf, sizeof(buf));
319 xprt->address_strings[RPC_DISPLAY_ADDR] =
320 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400321 sin6 = xs_addr_in6(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800322 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400323 break;
324 default:
325 BUG();
Chuck Leveredb267a2006-08-22 20:06:18 -0400326 }
Chuck Lever176e21e2011-05-09 15:22:44 -0400327
Chuck Lever9dc3b092009-08-09 15:09:46 -0400328 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
Chuck Leveredb267a2006-08-22 20:06:18 -0400329}
330
Chuck Lever9dc3b092009-08-09 15:09:46 -0400331static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400332{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400333 struct sockaddr *sap = xs_addr(xprt);
334 char buf[128];
Chuck Lever4b6473f2007-08-06 11:57:12 -0400335
Joe Perches81160e662010-03-08 12:15:59 -0800336 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400337 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400338
Joe Perches81160e662010-03-08 12:15:59 -0800339 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400340 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
341}
Chuck Lever4b6473f2007-08-06 11:57:12 -0400342
Chuck Lever9dc3b092009-08-09 15:09:46 -0400343static void xs_format_peer_addresses(struct rpc_xprt *xprt,
344 const char *protocol,
345 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400346{
Chuck Leverb454ae92008-01-07 18:34:48 -0500347 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leverb454ae92008-01-07 18:34:48 -0500348 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leverc877b842009-08-09 15:09:36 -0400349 xs_format_common_peer_addresses(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400350 xs_format_common_peer_ports(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400351}
352
Chuck Lever9dc3b092009-08-09 15:09:46 -0400353static void xs_update_peer_port(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400354{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400355 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
356 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400357
Chuck Lever9dc3b092009-08-09 15:09:46 -0400358 xs_format_common_peer_ports(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400359}
360
361static void xs_free_peer_addresses(struct rpc_xprt *xprt)
362{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500363 unsigned int i;
364
365 for (i = 0; i < RPC_DISPLAY_MAX; i++)
366 switch (i) {
367 case RPC_DISPLAY_PROTO:
368 case RPC_DISPLAY_NETID:
369 continue;
370 default:
371 kfree(xprt->address_strings[i]);
372 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400373}
374
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400375#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
376
Trond Myklebust24c56842006-10-17 15:06:22 -0400377static 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 -0400378{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400379 struct msghdr msg = {
380 .msg_name = addr,
381 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400382 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
383 };
384 struct kvec iov = {
385 .iov_base = vec->iov_base + base,
386 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400387 };
388
Trond Myklebust24c56842006-10-17 15:06:22 -0400389 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400390 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
391 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
392}
393
Trond Myklebust24c56842006-10-17 15:06:22 -0400394static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400395{
Trond Myklebust24c56842006-10-17 15:06:22 -0400396 struct page **ppage;
397 unsigned int remainder;
398 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400399
Trond Myklebust24c56842006-10-17 15:06:22 -0400400 remainder = xdr->page_len - base;
401 base += xdr->page_base;
402 ppage = xdr->pages + (base >> PAGE_SHIFT);
403 base &= ~PAGE_MASK;
404 for(;;) {
405 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
406 int flags = XS_SENDMSG_FLAGS;
407
408 remainder -= len;
409 if (remainder != 0 || more)
410 flags |= MSG_MORE;
411 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
412 if (remainder == 0 || err != len)
413 break;
414 sent += err;
415 ppage++;
416 base = 0;
417 }
418 if (sent == 0)
419 return err;
420 if (err > 0)
421 sent += err;
422 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400423}
424
Chuck Lever9903cd12005-08-11 16:25:26 -0400425/**
426 * xs_sendpages - write pages directly to a socket
427 * @sock: socket to send on
428 * @addr: UDP only -- address of destination
429 * @addrlen: UDP only -- length of destination address
430 * @xdr: buffer containing this request
431 * @base: starting position in the buffer
432 *
Chuck Levera246b012005-08-11 16:25:23 -0400433 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400434static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
Chuck Levera246b012005-08-11 16:25:23 -0400435{
Trond Myklebust24c56842006-10-17 15:06:22 -0400436 unsigned int remainder = xdr->len - base;
437 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400438
Chuck Lever262965f2005-08-11 16:25:56 -0400439 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400440 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400441
442 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400443 if (base != 0) {
444 addr = NULL;
445 addrlen = 0;
446 }
Chuck Lever262965f2005-08-11 16:25:56 -0400447
Trond Myklebust24c56842006-10-17 15:06:22 -0400448 if (base < xdr->head[0].iov_len || addr != NULL) {
449 unsigned int len = xdr->head[0].iov_len - base;
450 remainder -= len;
451 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
452 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400453 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400454 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400455 base = 0;
456 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400457 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400458
Trond Myklebust24c56842006-10-17 15:06:22 -0400459 if (base < xdr->page_len) {
460 unsigned int len = xdr->page_len - base;
461 remainder -= len;
462 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
463 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400464 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400465 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400466 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400467 } else
468 base -= xdr->page_len;
469
470 if (base >= xdr->tail[0].iov_len)
471 return sent;
472 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400473out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400474 if (sent == 0)
475 return err;
476 if (err > 0)
477 sent += err;
478 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400479}
480
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400481static void xs_nospace_callback(struct rpc_task *task)
482{
483 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
484
485 transport->inet->sk_write_pending--;
486 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
487}
488
Chuck Lever9903cd12005-08-11 16:25:26 -0400489/**
Chuck Lever262965f2005-08-11 16:25:56 -0400490 * xs_nospace - place task on wait queue if transmit was incomplete
491 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400492 *
Chuck Levera246b012005-08-11 16:25:23 -0400493 */
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400494static int xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400495{
Chuck Lever262965f2005-08-11 16:25:56 -0400496 struct rpc_rqst *req = task->tk_rqstp;
497 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500498 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust24ca9a82011-11-22 14:44:28 +0200499 int ret = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400500
Chuck Lever46121cf2007-01-31 12:14:08 -0500501 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400502 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
503 req->rq_slen);
504
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400505 /* Protect against races with write_space */
506 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400507
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400508 /* Don't race with disconnect */
509 if (xprt_connected(xprt)) {
510 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
511 /*
512 * Notify TCP that we're limited by the application
513 * window size
514 */
515 set_bit(SOCK_NOSPACE, &transport->sock->flags);
516 transport->inet->sk_write_pending++;
517 /* ...and wait for more buffer space */
518 xprt_wait_for_buffer_space(task, xs_nospace_callback);
519 }
520 } else {
521 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400522 ret = -ENOTCONN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400523 }
Chuck Lever262965f2005-08-11 16:25:56 -0400524
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400525 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400526 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400527}
528
Chuck Lever61677ee2011-05-09 15:22:34 -0400529/*
530 * Construct a stream transport record marker in @buf.
531 */
532static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
533{
534 u32 reclen = buf->len - sizeof(rpc_fraghdr);
535 rpc_fraghdr *base = buf->head[0].iov_base;
536 *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
537}
538
Chuck Lever262965f2005-08-11 16:25:56 -0400539/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400540 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
541 * @task: RPC task that manages the state of an RPC request
542 *
543 * Return values:
544 * 0: The request has been sent
545 * EAGAIN: The socket was blocked, please call again later to
546 * complete the request
547 * ENOTCONN: Caller needs to invoke connect logic then call again
548 * other: Some other error occured, the request was not sent
549 */
550static int xs_local_send_request(struct rpc_task *task)
551{
552 struct rpc_rqst *req = task->tk_rqstp;
553 struct rpc_xprt *xprt = req->rq_xprt;
554 struct sock_xprt *transport =
555 container_of(xprt, struct sock_xprt, xprt);
556 struct xdr_buf *xdr = &req->rq_snd_buf;
557 int status;
558
559 xs_encode_stream_record_marker(&req->rq_snd_buf);
560
561 xs_pktdump("packet data:",
562 req->rq_svec->iov_base, req->rq_svec->iov_len);
563
564 status = xs_sendpages(transport->sock, NULL, 0,
565 xdr, req->rq_bytes_sent);
566 dprintk("RPC: %s(%u) = %d\n",
567 __func__, xdr->len - req->rq_bytes_sent, status);
568 if (likely(status >= 0)) {
569 req->rq_bytes_sent += status;
570 req->rq_xmit_bytes_sent += status;
571 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
572 req->rq_bytes_sent = 0;
573 return 0;
574 }
575 status = -EAGAIN;
576 }
577
578 switch (status) {
579 case -EAGAIN:
580 status = xs_nospace(task);
581 break;
582 default:
583 dprintk("RPC: sendmsg returned unrecognized error %d\n",
584 -status);
585 case -EPIPE:
586 xs_close(xprt);
587 status = -ENOTCONN;
588 }
589
590 return status;
591}
592
593/**
Chuck Lever262965f2005-08-11 16:25:56 -0400594 * xs_udp_send_request - write an RPC request to a UDP socket
595 * @task: address of RPC task that manages the state of an RPC request
596 *
597 * Return values:
598 * 0: The request has been sent
599 * EAGAIN: The socket was blocked, please call again later to
600 * complete the request
601 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300602 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -0400603 */
604static int xs_udp_send_request(struct rpc_task *task)
605{
606 struct rpc_rqst *req = task->tk_rqstp;
607 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500608 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400609 struct xdr_buf *xdr = &req->rq_snd_buf;
610 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400611
Chuck Lever9903cd12005-08-11 16:25:26 -0400612 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400613 req->rq_svec->iov_base,
614 req->rq_svec->iov_len);
615
Trond Myklebust01d37c42009-03-11 14:09:39 -0400616 if (!xprt_bound(xprt))
617 return -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500618 status = xs_sendpages(transport->sock,
Chuck Lever95392c52007-08-06 11:57:58 -0400619 xs_addr(xprt),
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500620 xprt->addrlen, xdr,
621 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400622
Chuck Lever46121cf2007-01-31 12:14:08 -0500623 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400624 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400625
Trond Myklebust21997002007-10-01 11:43:37 -0400626 if (status >= 0) {
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400627 req->rq_xmit_bytes_sent += status;
Trond Myklebust21997002007-10-01 11:43:37 -0400628 if (status >= req->rq_slen)
629 return 0;
630 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400631 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400632 }
Chuck Levera246b012005-08-11 16:25:23 -0400633
Chuck Lever262965f2005-08-11 16:25:56 -0400634 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400635 case -ENOTSOCK:
636 status = -ENOTCONN;
637 /* Should we call xs_close() here? */
638 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400639 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400640 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400641 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400642 default:
643 dprintk("RPC: sendmsg returned unrecognized error %d\n",
644 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400645 case -ENETUNREACH:
646 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400647 case -ECONNREFUSED:
648 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400649 * prompts ECONNREFUSED. */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400650 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400651 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800652
Chuck Lever262965f2005-08-11 16:25:56 -0400653 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400654}
655
Trond Myklebuste06799f2007-11-05 15:44:12 -0500656/**
657 * xs_tcp_shutdown - gracefully shut down a TCP socket
658 * @xprt: transport
659 *
660 * Initiates a graceful shutdown of the TCP socket by calling the
661 * equivalent of shutdown(SHUT_WR);
662 */
663static void xs_tcp_shutdown(struct rpc_xprt *xprt)
664{
665 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
666 struct socket *sock = transport->sock;
667
668 if (sock != NULL)
669 kernel_sock_shutdown(sock, SHUT_WR);
670}
671
Chuck Lever9903cd12005-08-11 16:25:26 -0400672/**
Chuck Lever262965f2005-08-11 16:25:56 -0400673 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400674 * @task: address of RPC task that manages the state of an RPC request
675 *
676 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400677 * 0: The request has been sent
678 * EAGAIN: The socket was blocked, please call again later to
679 * complete the request
680 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300681 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400682 *
683 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400684 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400685 */
Chuck Lever262965f2005-08-11 16:25:56 -0400686static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400687{
688 struct rpc_rqst *req = task->tk_rqstp;
689 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500690 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400691 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Leverb595bb12007-08-06 11:56:42 -0400692 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400693
Chuck Lever61677ee2011-05-09 15:22:34 -0400694 xs_encode_stream_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400695
Chuck Lever262965f2005-08-11 16:25:56 -0400696 xs_pktdump("packet data:",
697 req->rq_svec->iov_base,
698 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400699
700 /* Continue transmitting the packet/record. We must be careful
701 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400702 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400703 while (1) {
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500704 status = xs_sendpages(transport->sock,
705 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400706
Chuck Lever46121cf2007-01-31 12:14:08 -0500707 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400708 xdr->len - req->rq_bytes_sent, status);
709
710 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400711 break;
712
Chuck Lever262965f2005-08-11 16:25:56 -0400713 /* If we've sent the entire packet, immediately
714 * reset the count of bytes sent. */
715 req->rq_bytes_sent += status;
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400716 req->rq_xmit_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400717 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
718 req->rq_bytes_sent = 0;
719 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400720 }
721
Trond Myklebust06b4b682008-04-16 16:51:38 -0400722 if (status != 0)
723 continue;
Chuck Levera246b012005-08-11 16:25:23 -0400724 status = -EAGAIN;
Trond Myklebust06b4b682008-04-16 16:51:38 -0400725 break;
Chuck Levera246b012005-08-11 16:25:23 -0400726 }
727
Chuck Lever262965f2005-08-11 16:25:56 -0400728 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400729 case -ENOTSOCK:
730 status = -ENOTCONN;
731 /* Should we call xs_close() here? */
732 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400733 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400734 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400735 break;
736 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500737 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400738 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400739 case -ECONNRESET:
Trond Myklebuste06799f2007-11-05 15:44:12 -0500740 xs_tcp_shutdown(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400741 case -ECONNREFUSED:
742 case -ENOTCONN:
Trond Myklebustb9d2bb22012-10-23 11:40:02 -0400743 case -EPIPE:
Chuck Lever262965f2005-08-11 16:25:56 -0400744 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400745 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800746
Chuck Levera246b012005-08-11 16:25:23 -0400747 return status;
748}
749
Chuck Lever9903cd12005-08-11 16:25:26 -0400750/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400751 * xs_tcp_release_xprt - clean up after a tcp transmission
752 * @xprt: transport
753 * @task: rpc task
754 *
755 * This cleans up if an error causes us to abort the transmission of a request.
756 * In this case, the socket may need to be reset in order to avoid confusing
757 * the server.
758 */
759static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
760{
761 struct rpc_rqst *req;
762
763 if (task != xprt->snd_task)
764 return;
765 if (task == NULL)
766 goto out_release;
767 req = task->tk_rqstp;
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400768 if (req == NULL)
769 goto out_release;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400770 if (req->rq_bytes_sent == 0)
771 goto out_release;
772 if (req->rq_bytes_sent == req->rq_snd_buf.len)
773 goto out_release;
Trond Myklebusta4f08352013-01-08 09:10:21 -0500774 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400775out_release:
776 xprt_release_xprt(xprt, task);
777}
778
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400779static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
780{
781 transport->old_data_ready = sk->sk_data_ready;
782 transport->old_state_change = sk->sk_state_change;
783 transport->old_write_space = sk->sk_write_space;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400784}
785
786static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
787{
788 sk->sk_data_ready = transport->old_data_ready;
789 sk->sk_state_change = transport->old_state_change;
790 sk->sk_write_space = transport->old_write_space;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400791}
792
Chuck Leverfe315e72009-03-11 14:10:21 -0400793static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400794{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500795 struct socket *sock = transport->sock;
796 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400797
Chuck Leverfe315e72009-03-11 14:10:21 -0400798 if (sk == NULL)
799 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400800
Trond Myklebust246408d2011-03-22 18:40:10 -0400801 transport->srcport = 0;
802
Chuck Levera246b012005-08-11 16:25:23 -0400803 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500804 transport->inet = NULL;
805 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400806
Chuck Lever9903cd12005-08-11 16:25:26 -0400807 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400808
809 xs_restore_old_callbacks(transport, sk);
Chuck Levera246b012005-08-11 16:25:23 -0400810 write_unlock_bh(&sk->sk_callback_lock);
811
Chuck Lever9903cd12005-08-11 16:25:26 -0400812 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400813
814 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -0400815}
816
817/**
818 * xs_close - close a socket
819 * @xprt: transport
820 *
821 * This is used when all requests are complete; ie, no DRC state remains
822 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400823 *
824 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
825 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e72009-03-11 14:10:21 -0400826 */
827static void xs_close(struct rpc_xprt *xprt)
828{
829 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
830
831 dprintk("RPC: xs_close xprt %p\n", xprt);
832
833 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -0400834 xprt->reestablish_timeout = 0;
Chuck Leverfe315e72009-03-11 14:10:21 -0400835
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100836 smp_mb__before_clear_bit();
Trond Myklebust7d1e8252009-03-11 14:38:03 -0400837 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100838 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -0500839 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100840 smp_mb__after_clear_bit();
Trond Myklebust62da3b22007-11-06 18:44:20 -0500841 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400842}
843
Trond Myklebustf75e6742009-04-21 17:18:20 -0400844static void xs_tcp_close(struct rpc_xprt *xprt)
845{
846 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
847 xs_close(xprt);
848 else
849 xs_tcp_shutdown(xprt);
850}
851
Chuck Lever9903cd12005-08-11 16:25:26 -0400852/**
853 * xs_destroy - prepare to shutdown a transport
854 * @xprt: doomed transport
855 *
856 */
857static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400858{
Chuck Leverc8475462006-12-05 16:35:26 -0500859 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
860
Chuck Lever46121cf2007-01-31 12:14:08 -0500861 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400862
Tejun Heoafe2c512010-12-14 16:21:17 +0100863 cancel_delayed_work_sync(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400864
Chuck Lever9903cd12005-08-11 16:25:26 -0400865 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400866 xs_free_peer_addresses(xprt);
Pavel Emelyanove204e622010-09-29 16:03:13 +0400867 xprt_free(xprt);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -0400868 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400869}
870
Chuck Lever9903cd12005-08-11 16:25:26 -0400871static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400872{
Chuck Lever9903cd12005-08-11 16:25:26 -0400873 return (struct rpc_xprt *) sk->sk_user_data;
874}
875
Chuck Lever176e21e2011-05-09 15:22:44 -0400876static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
877{
878 struct xdr_skb_reader desc = {
879 .skb = skb,
880 .offset = sizeof(rpc_fraghdr),
881 .count = skb->len - sizeof(rpc_fraghdr),
882 };
883
884 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
885 return -1;
886 if (desc.count)
887 return -1;
888 return 0;
889}
890
891/**
892 * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets
893 * @sk: socket with data to read
894 * @len: how much data to read
895 *
896 * Currently this assumes we can read the whole reply in a single gulp.
897 */
898static void xs_local_data_ready(struct sock *sk, int len)
899{
900 struct rpc_task *task;
901 struct rpc_xprt *xprt;
902 struct rpc_rqst *rovr;
903 struct sk_buff *skb;
904 int err, repsize, copied;
905 u32 _xid;
906 __be32 *xp;
907
908 read_lock_bh(&sk->sk_callback_lock);
909 dprintk("RPC: %s...\n", __func__);
910 xprt = xprt_from_sock(sk);
911 if (xprt == NULL)
912 goto out;
913
914 skb = skb_recv_datagram(sk, 0, 1, &err);
915 if (skb == NULL)
916 goto out;
917
Chuck Lever176e21e2011-05-09 15:22:44 -0400918 repsize = skb->len - sizeof(rpc_fraghdr);
919 if (repsize < 4) {
920 dprintk("RPC: impossible RPC reply size %d\n", repsize);
921 goto dropit;
922 }
923
924 /* Copy the XID from the skb... */
925 xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
926 if (xp == NULL)
927 goto dropit;
928
929 /* Look up and lock the request corresponding to the given XID */
930 spin_lock(&xprt->transport_lock);
931 rovr = xprt_lookup_rqst(xprt, *xp);
932 if (!rovr)
933 goto out_unlock;
934 task = rovr->rq_task;
935
936 copied = rovr->rq_private_buf.buflen;
937 if (copied > repsize)
938 copied = repsize;
939
940 if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
941 dprintk("RPC: sk_buff copy failed\n");
942 goto out_unlock;
943 }
944
945 xprt_complete_rqst(task, copied);
946
947 out_unlock:
948 spin_unlock(&xprt->transport_lock);
949 dropit:
950 skb_free_datagram(sk, skb);
951 out:
952 read_unlock_bh(&sk->sk_callback_lock);
953}
954
Chuck Lever9903cd12005-08-11 16:25:26 -0400955/**
956 * xs_udp_data_ready - "data ready" callback for UDP sockets
957 * @sk: socket with data to read
958 * @len: how much data to read
959 *
960 */
961static void xs_udp_data_ready(struct sock *sk, int len)
962{
963 struct rpc_task *task;
964 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400965 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400966 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400967 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700968 u32 _xid;
969 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400970
Eric Dumazetf064af12010-09-22 12:43:39 +0000971 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500972 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400973 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400974 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400975
976 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
977 goto out;
978
Chuck Levera246b012005-08-11 16:25:23 -0400979 repsize = skb->len - sizeof(struct udphdr);
980 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500981 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400982 goto dropit;
983 }
984
985 /* Copy the XID from the skb... */
986 xp = skb_header_pointer(skb, sizeof(struct udphdr),
987 sizeof(_xid), &_xid);
988 if (xp == NULL)
989 goto dropit;
990
991 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400992 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400993 rovr = xprt_lookup_rqst(xprt, *xp);
994 if (!rovr)
995 goto out_unlock;
996 task = rovr->rq_task;
997
Chuck Levera246b012005-08-11 16:25:23 -0400998 if ((copied = rovr->rq_private_buf.buflen) > repsize)
999 copied = repsize;
1000
1001 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001002 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
1003 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -04001004 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001005 }
1006
1007 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -04001008
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001009 xprt_adjust_cwnd(xprt, task, copied);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001010 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -04001011
1012 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001013 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001014 dropit:
1015 skb_free_datagram(sk, skb);
1016 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001017 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001018}
1019
Trond Myklebusta519fc72012-09-12 16:49:15 -04001020/*
1021 * Helper function to force a TCP close if the server is sending
1022 * junk and/or it has put us in CLOSE_WAIT
1023 */
1024static void xs_tcp_force_close(struct rpc_xprt *xprt)
1025{
1026 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1027 xprt_force_disconnect(xprt);
1028}
1029
Chuck Leverdd456472006-12-05 16:35:44 -05001030static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001031{
Chuck Lever51971132006-12-05 16:35:19 -05001032 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001033 size_t len, used;
1034 char *p;
1035
Chuck Lever51971132006-12-05 16:35:19 -05001036 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
1037 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001038 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001039 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001040 if (used != len)
1041 return;
Chuck Lever808012f2005-08-25 16:25:49 -07001042
Chuck Lever51971132006-12-05 16:35:19 -05001043 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
1044 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -05001045 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -04001046 else
Chuck Levere136d092006-12-05 16:35:23 -05001047 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -05001048 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -07001049
Chuck Levere136d092006-12-05 16:35:23 -05001050 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001051 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -07001052
Chuck Levera246b012005-08-11 16:25:23 -04001053 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001054 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001055 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebusta519fc72012-09-12 16:49:15 -04001056 xs_tcp_force_close(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001057 return;
Chuck Levera246b012005-08-11 16:25:23 -04001058 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001059 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -05001060 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001061}
1062
Chuck Lever51971132006-12-05 16:35:19 -05001063static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001064{
Chuck Lever51971132006-12-05 16:35:19 -05001065 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001066 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001067 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001068 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
1069 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1070 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001071 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001072 }
1073 }
1074}
1075
Chuck Leverdd456472006-12-05 16:35:44 -05001076static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001077{
1078 size_t len, used;
1079 char *p;
1080
Chuck Lever51971132006-12-05 16:35:19 -05001081 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -05001082 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001083 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001084 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001085 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001086 if (used != len)
1087 return;
Chuck Levere136d092006-12-05 16:35:23 -05001088 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001089 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -05001090 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001091 dprintk("RPC: reading %s XID %08x\n",
1092 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1093 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -05001094 ntohl(transport->tcp_xid));
1095 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001096}
1097
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001098static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1099 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001100{
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001101 size_t len, used;
1102 u32 offset;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001103 char *p;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001104
1105 /*
1106 * We want transport->tcp_offset to be 8 at the end of this routine
1107 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1108 * When this function is called for the first time,
1109 * transport->tcp_offset is 4 (after having already read the xid).
1110 */
1111 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001112 len = sizeof(transport->tcp_calldir) - offset;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001113 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001114 p = ((char *) &transport->tcp_calldir) + offset;
1115 used = xdr_skb_read_bits(desc, p, len);
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001116 transport->tcp_offset += used;
1117 if (used != len)
1118 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001119 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001120 /*
1121 * We don't yet have the XDR buffer, so we will write the calldir
1122 * out after we get the buffer from the 'struct rpc_rqst'
1123 */
Trond Myklebustb76ce562010-06-16 13:57:32 -04001124 switch (ntohl(transport->tcp_calldir)) {
1125 case RPC_REPLY:
1126 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1127 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001128 transport->tcp_flags |= TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001129 break;
1130 case RPC_CALL:
1131 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1132 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001133 transport->tcp_flags &= ~TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001134 break;
1135 default:
1136 dprintk("RPC: invalid request message type\n");
Trond Myklebusta519fc72012-09-12 16:49:15 -04001137 xs_tcp_force_close(&transport->xprt);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001138 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001139 xs_tcp_check_fraghdr(transport);
1140}
1141
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001142static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
1143 struct xdr_skb_reader *desc,
1144 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001145{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001146 struct sock_xprt *transport =
1147 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001148 struct xdr_buf *rcvbuf;
1149 size_t len;
1150 ssize_t r;
1151
Chuck Levera246b012005-08-11 16:25:23 -04001152 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001153
1154 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1155 /*
1156 * Save the RPC direction in the XDR buffer
1157 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001158 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
Trond Myklebustb76ce562010-06-16 13:57:32 -04001159 &transport->tcp_calldir,
1160 sizeof(transport->tcp_calldir));
1161 transport->tcp_copied += sizeof(transport->tcp_calldir);
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001162 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -04001163 }
1164
Chuck Levera246b012005-08-11 16:25:23 -04001165 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -05001166 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -05001167 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -04001168
Chuck Lever51971132006-12-05 16:35:19 -05001169 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001170 memcpy(&my_desc, desc, sizeof(my_desc));
1171 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -05001172 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001173 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001174 desc->count -= r;
1175 desc->offset += r;
1176 } else
Chuck Lever51971132006-12-05 16:35:19 -05001177 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001178 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001179
1180 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001181 transport->tcp_copied += r;
1182 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001183 }
1184 if (r != len) {
1185 /* Error when copying to the receive buffer,
1186 * usually because we weren't able to allocate
1187 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001188 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001189 * will not receive any additional updates,
1190 * and time out.
1191 * Any remaining data from this record will
1192 * be discarded.
1193 */
Chuck Levere136d092006-12-05 16:35:23 -05001194 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001195 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001196 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001197 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1198 "tcp_offset = %u, tcp_reclen = %u\n",
1199 xprt, transport->tcp_copied,
1200 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001201 return;
Chuck Levera246b012005-08-11 16:25:23 -04001202 }
1203
Chuck Lever46121cf2007-01-31 12:14:08 -05001204 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001205 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001206 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1207 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1208 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001209
Chuck Lever51971132006-12-05 16:35:19 -05001210 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001211 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001212 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001213 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1214 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001215 }
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001216}
1217
1218/*
1219 * Finds the request corresponding to the RPC xid and invokes the common
1220 * tcp read code to read the data.
1221 */
1222static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1223 struct xdr_skb_reader *desc)
1224{
1225 struct sock_xprt *transport =
1226 container_of(xprt, struct sock_xprt, xprt);
1227 struct rpc_rqst *req;
1228
1229 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1230
1231 /* Find and lock the request corresponding to this xid */
1232 spin_lock(&xprt->transport_lock);
1233 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1234 if (!req) {
1235 dprintk("RPC: XID %08x request not found!\n",
1236 ntohl(transport->tcp_xid));
1237 spin_unlock(&xprt->transport_lock);
1238 return -1;
1239 }
1240
1241 xs_tcp_read_common(xprt, desc, req);
1242
Chuck Levere136d092006-12-05 16:35:23 -05001243 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001244 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001245
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001246 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001247 return 0;
1248}
1249
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001250#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001251/*
1252 * Obtains an rpc_rqst previously allocated and invokes the common
1253 * tcp read code to read the data. The result is placed in the callback
1254 * queue.
1255 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1256 * connection and return -1.
1257 */
1258static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1259 struct xdr_skb_reader *desc)
1260{
1261 struct sock_xprt *transport =
1262 container_of(xprt, struct sock_xprt, xprt);
1263 struct rpc_rqst *req;
1264
1265 req = xprt_alloc_bc_request(xprt);
1266 if (req == NULL) {
1267 printk(KERN_WARNING "Callback slot table overflowed\n");
1268 xprt_force_disconnect(xprt);
1269 return -1;
1270 }
1271
1272 req->rq_xid = transport->tcp_xid;
1273 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1274 xs_tcp_read_common(xprt, desc, req);
1275
1276 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1277 struct svc_serv *bc_serv = xprt->bc_serv;
1278
1279 /*
1280 * Add callback request to callback list. The callback
1281 * service sleeps on the sv_cb_waitq waiting for new
1282 * requests. Wake it up after adding enqueing the
1283 * request.
1284 */
1285 dprintk("RPC: add callback request to list\n");
1286 spin_lock(&bc_serv->sv_cb_lock);
1287 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1288 spin_unlock(&bc_serv->sv_cb_lock);
1289 wake_up(&bc_serv->sv_cb_waitq);
1290 }
1291
1292 req->rq_private_buf.len = transport->tcp_copied;
1293
1294 return 0;
1295}
1296
1297static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1298 struct xdr_skb_reader *desc)
1299{
1300 struct sock_xprt *transport =
1301 container_of(xprt, struct sock_xprt, xprt);
1302
1303 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1304 xs_tcp_read_reply(xprt, desc) :
1305 xs_tcp_read_callback(xprt, desc);
1306}
1307#else
1308static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1309 struct xdr_skb_reader *desc)
1310{
1311 return xs_tcp_read_reply(xprt, desc);
1312}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001313#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001314
1315/*
1316 * Read data off the transport. This can be either an RPC_CALL or an
1317 * RPC_REPLY. Relay the processing to helper functions.
1318 */
1319static void xs_tcp_read_data(struct rpc_xprt *xprt,
1320 struct xdr_skb_reader *desc)
1321{
1322 struct sock_xprt *transport =
1323 container_of(xprt, struct sock_xprt, xprt);
1324
1325 if (_xs_tcp_read_data(xprt, desc) == 0)
1326 xs_tcp_check_fraghdr(transport);
1327 else {
1328 /*
1329 * The transport_lock protects the request handling.
1330 * There's no need to hold it to update the tcp_flags.
1331 */
1332 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1333 }
Chuck Levera246b012005-08-11 16:25:23 -04001334}
1335
Chuck Leverdd456472006-12-05 16:35:44 -05001336static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001337{
1338 size_t len;
1339
Chuck Lever51971132006-12-05 16:35:19 -05001340 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001341 if (len > desc->count)
1342 len = desc->count;
1343 desc->count -= len;
1344 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001345 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001346 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001347 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001348}
1349
Chuck Lever9903cd12005-08-11 16:25:26 -04001350static 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 -04001351{
1352 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001353 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001354 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001355 .skb = skb,
1356 .offset = offset,
1357 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001358 };
Chuck Levera246b012005-08-11 16:25:23 -04001359
Chuck Lever46121cf2007-01-31 12:14:08 -05001360 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001361 do {
1362 /* Read in a new fragment marker if necessary */
1363 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001364 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001365 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001366 continue;
1367 }
1368 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001369 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001370 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001371 continue;
1372 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001373 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001374 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001375 xs_tcp_read_calldir(transport, &desc);
1376 continue;
1377 }
Chuck Levera246b012005-08-11 16:25:23 -04001378 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001379 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001380 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001381 continue;
1382 }
1383 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001384 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001385 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001386 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001387 return len - desc.count;
1388}
1389
Chuck Lever9903cd12005-08-11 16:25:26 -04001390/**
1391 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1392 * @sk: socket with data to read
1393 * @bytes: how much data to read
1394 *
1395 */
1396static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001397{
1398 struct rpc_xprt *xprt;
1399 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001400 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001401
Chuck Lever46121cf2007-01-31 12:14:08 -05001402 dprintk("RPC: xs_tcp_data_ready...\n");
1403
Eric Dumazetf064af12010-09-22 12:43:39 +00001404 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001405 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001406 goto out;
Neil Brown61d0a8e2009-09-23 14:36:37 -04001407 /* Any data means we had a useful conversation, so
1408 * the we don't need to delay the next reconnect
1409 */
1410 if (xprt->reestablish_timeout)
1411 xprt->reestablish_timeout = 0;
1412
Chuck Lever9903cd12005-08-11 16:25:26 -04001413 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001414 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001415 do {
1416 rd_desc.count = 65536;
1417 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1418 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001419out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001420 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001421}
1422
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001423/*
1424 * Do the equivalent of linger/linger2 handling for dealing with
1425 * broken servers that don't close the socket in a timely
1426 * fashion
1427 */
1428static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1429 unsigned long timeout)
1430{
1431 struct sock_xprt *transport;
1432
1433 if (xprt_test_and_set_connecting(xprt))
1434 return;
1435 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1436 transport = container_of(xprt, struct sock_xprt, xprt);
1437 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1438 timeout);
1439}
1440
1441static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1442{
1443 struct sock_xprt *transport;
1444
1445 transport = container_of(xprt, struct sock_xprt, xprt);
1446
1447 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1448 !cancel_delayed_work(&transport->connect_worker))
1449 return;
1450 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1451 xprt_clear_connecting(xprt);
1452}
1453
Trond Myklebust4bc1e682012-10-23 17:50:07 -04001454static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001455{
1456 smp_mb__before_clear_bit();
Trond Myklebusta519fc72012-09-12 16:49:15 -04001457 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1458 clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001459 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1460 clear_bit(XPRT_CLOSING, &xprt->state);
1461 smp_mb__after_clear_bit();
Trond Myklebust4bc1e682012-10-23 17:50:07 -04001462}
1463
1464static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1465{
1466 xs_sock_reset_connection_flags(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001467 /* Mark transport as closed and wake up all pending tasks */
1468 xprt_disconnect_done(xprt);
1469}
1470
Chuck Lever9903cd12005-08-11 16:25:26 -04001471/**
1472 * xs_tcp_state_change - callback to handle TCP socket state changes
1473 * @sk: socket whose state has changed
1474 *
1475 */
1476static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001477{
Chuck Lever9903cd12005-08-11 16:25:26 -04001478 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001479
Eric Dumazetf064af12010-09-22 12:43:39 +00001480 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001481 if (!(xprt = xprt_from_sock(sk)))
1482 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001483 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001484 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001485 sk->sk_state, xprt_connected(xprt),
1486 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001487 sock_flag(sk, SOCK_ZAPPED),
1488 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001489
1490 switch (sk->sk_state) {
1491 case TCP_ESTABLISHED:
Eric Dumazetf064af12010-09-22 12:43:39 +00001492 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001493 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001494 struct sock_xprt *transport = container_of(xprt,
1495 struct sock_xprt, xprt);
1496
Chuck Levera246b012005-08-11 16:25:23 -04001497 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001498 transport->tcp_offset = 0;
1499 transport->tcp_reclen = 0;
1500 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001501 transport->tcp_flags =
1502 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001503
Trond Myklebust2a491992009-03-11 14:38:00 -04001504 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001505 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001506 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001507 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001508 case TCP_FIN_WAIT1:
1509 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001510 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001511 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001512 set_bit(XPRT_CLOSING, &xprt->state);
1513 smp_mb__before_clear_bit();
1514 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001515 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001516 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001517 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001518 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001519 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001520 /* The server initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001521 xprt->connect_cookie++;
Trond Myklebustd0bea452012-10-23 11:35:47 -04001522 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebusta519fc72012-09-12 16:49:15 -04001523 xs_tcp_force_close(xprt);
Trond Myklebust663b8852008-01-01 18:42:12 -05001524 case TCP_CLOSING:
1525 /*
1526 * If the server closed down the connection, make sure that
1527 * we back off before reconnecting
1528 */
1529 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1530 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001531 break;
1532 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001533 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001534 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001535 smp_mb__before_clear_bit();
1536 clear_bit(XPRT_CONNECTED, &xprt->state);
1537 smp_mb__after_clear_bit();
1538 break;
1539 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001540 xs_tcp_cancel_linger_timeout(xprt);
1541 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001542 }
1543 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001544 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001545}
1546
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001547static void xs_write_space(struct sock *sk)
1548{
1549 struct socket *sock;
1550 struct rpc_xprt *xprt;
1551
1552 if (unlikely(!(sock = sk->sk_socket)))
1553 return;
1554 clear_bit(SOCK_NOSPACE, &sock->flags);
1555
1556 if (unlikely(!(xprt = xprt_from_sock(sk))))
1557 return;
1558 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1559 return;
1560
1561 xprt_write_space(xprt);
1562}
1563
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001564/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001565 * xs_udp_write_space - callback invoked when socket buffer space
1566 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001567 * @sk: socket whose state has changed
1568 *
Chuck Levera246b012005-08-11 16:25:23 -04001569 * Called when more output buffer space is available for this socket.
1570 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001571 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001572 * with a bunch of small requests.
1573 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001574static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001575{
Eric Dumazetf064af12010-09-22 12:43:39 +00001576 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001577
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001578 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001579 if (sock_writeable(sk))
1580 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001581
Eric Dumazetf064af12010-09-22 12:43:39 +00001582 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001583}
Chuck Levera246b012005-08-11 16:25:23 -04001584
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001585/**
1586 * xs_tcp_write_space - callback invoked when socket buffer space
1587 * becomes available
1588 * @sk: socket whose state has changed
1589 *
1590 * Called when more output buffer space is available for this socket.
1591 * We try not to wake our writers until they can make "significant"
1592 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1593 * with a bunch of small requests.
1594 */
1595static void xs_tcp_write_space(struct sock *sk)
1596{
Eric Dumazetf064af12010-09-22 12:43:39 +00001597 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001598
1599 /* from net/core/stream.c:sk_stream_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001600 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1601 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001602
Eric Dumazetf064af12010-09-22 12:43:39 +00001603 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001604}
1605
Chuck Lever470056c2005-08-25 16:25:56 -07001606static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001607{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001608 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1609 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001610
Chuck Lever7c6e0662006-12-05 16:35:30 -05001611 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001612 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001613 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001614 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001615 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001616 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001617 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001618 sk->sk_write_space(sk);
1619 }
1620}
1621
Chuck Lever43118c22005-08-25 16:25:49 -07001622/**
Chuck Lever470056c2005-08-25 16:25:56 -07001623 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001624 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001625 * @sndsize: requested size of send buffer, in bytes
1626 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001627 *
Chuck Lever470056c2005-08-25 16:25:56 -07001628 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001629 */
Chuck Lever470056c2005-08-25 16:25:56 -07001630static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001631{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001632 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1633
1634 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001635 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001636 transport->sndsize = sndsize + 1024;
1637 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001638 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001639 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001640
1641 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001642}
1643
Chuck Lever46c0ee82005-08-25 16:25:52 -07001644/**
1645 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1646 * @task: task that timed out
1647 *
1648 * Adjust the congestion window after a retransmit timeout has occurred.
1649 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001650static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever46c0ee82005-08-25 16:25:52 -07001651{
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001652 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
Chuck Lever46c0ee82005-08-25 16:25:52 -07001653}
1654
Chuck Leverb85d8802006-05-25 01:40:49 -04001655static unsigned short xs_get_random_port(void)
1656{
1657 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1658 unsigned short rand = (unsigned short) net_random() % range;
1659 return rand + xprt_min_resvport;
1660}
1661
Chuck Lever92200412006-01-03 09:55:51 +01001662/**
1663 * xs_set_port - reset the port number in the remote endpoint address
1664 * @xprt: generic transport
1665 * @port: new port number
1666 *
1667 */
1668static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1669{
Chuck Lever46121cf2007-01-31 12:14:08 -05001670 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001671
Chuck Lever9dc3b092009-08-09 15:09:46 -04001672 rpc_set_port(xs_addr(xprt), port);
1673 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001674}
1675
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001676static unsigned short xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001677{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001678 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001679
1680 if (port == 0 && transport->xprt.resvport)
1681 port = xs_get_random_port();
1682 return port;
1683}
1684
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001685static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001686{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001687 if (transport->srcport != 0)
1688 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001689 if (!transport->xprt.resvport)
1690 return 0;
1691 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1692 return xprt_max_resvport;
1693 return --port;
1694}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001695static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001696{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001697 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001698 int err, nloop = 0;
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001699 unsigned short port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001700 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001701
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001702 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001703 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001704 rpc_set_port((struct sockaddr *)&myaddr, port);
1705 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1706 transport->xprt.addrlen);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001707 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001708 break;
Chuck Levera246b012005-08-11 16:25:23 -04001709 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001710 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001711 break;
Chuck Levera246b012005-08-11 16:25:23 -04001712 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001713 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001714 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001715 if (port > last)
1716 nloop++;
1717 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001718
Chuck Lever4232e862010-10-20 11:52:51 -04001719 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001720 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1721 &((struct sockaddr_in *)&myaddr)->sin_addr,
1722 port, err ? "failed" : "ok", err);
1723 else
1724 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1725 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1726 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001727 return err;
1728}
1729
Chuck Lever176e21e2011-05-09 15:22:44 -04001730/*
1731 * We don't support autobind on AF_LOCAL sockets
1732 */
1733static void xs_local_rpcbind(struct rpc_task *task)
1734{
Trond Myklebust3dc0da22013-01-08 09:31:13 -05001735 rcu_read_lock();
1736 xprt_set_bound(rcu_dereference(task->tk_client->cl_xprt));
1737 rcu_read_unlock();
Chuck Lever176e21e2011-05-09 15:22:44 -04001738}
1739
1740static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1741{
1742}
Chuck Levera246b012005-08-11 16:25:23 -04001743
Peter Zijlstraed075362006-12-06 20:35:24 -08001744#ifdef CONFIG_DEBUG_LOCK_ALLOC
1745static struct lock_class_key xs_key[2];
1746static struct lock_class_key xs_slock_key[2];
1747
Chuck Lever176e21e2011-05-09 15:22:44 -04001748static inline void xs_reclassify_socketu(struct socket *sock)
1749{
1750 struct sock *sk = sock->sk;
1751
Chuck Lever176e21e2011-05-09 15:22:44 -04001752 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1753 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1754}
1755
Chuck Lever8945ee52007-08-06 11:58:04 -04001756static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001757{
1758 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001759
Chuck Lever8945ee52007-08-06 11:58:04 -04001760 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1761 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1762}
Peter Zijlstraed075362006-12-06 20:35:24 -08001763
Chuck Lever8945ee52007-08-06 11:58:04 -04001764static inline void xs_reclassify_socket6(struct socket *sock)
1765{
1766 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001767
Chuck Lever8945ee52007-08-06 11:58:04 -04001768 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1769 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001770}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001771
1772static inline void xs_reclassify_socket(int family, struct socket *sock)
1773{
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -04001774 WARN_ON_ONCE(sock_owned_by_user(sock->sk));
1775 if (sock_owned_by_user(sock->sk))
1776 return;
1777
Chuck Lever4232e862010-10-20 11:52:51 -04001778 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001779 case AF_LOCAL:
1780 xs_reclassify_socketu(sock);
1781 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001782 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001783 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001784 break;
1785 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001786 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001787 break;
1788 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001789}
Peter Zijlstraed075362006-12-06 20:35:24 -08001790#else
Chuck Lever176e21e2011-05-09 15:22:44 -04001791static inline void xs_reclassify_socketu(struct socket *sock)
1792{
1793}
1794
Chuck Lever8945ee52007-08-06 11:58:04 -04001795static inline void xs_reclassify_socket4(struct socket *sock)
1796{
1797}
1798
1799static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001800{
1801}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001802
1803static inline void xs_reclassify_socket(int family, struct socket *sock)
1804{
1805}
Peter Zijlstraed075362006-12-06 20:35:24 -08001806#endif
1807
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001808static struct socket *xs_create_sock(struct rpc_xprt *xprt,
1809 struct sock_xprt *transport, int family, int type, int protocol)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001810{
1811 struct socket *sock;
1812 int err;
1813
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001814 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001815 if (err < 0) {
1816 dprintk("RPC: can't create %d transport socket (%d).\n",
1817 protocol, -err);
1818 goto out;
1819 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001820 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001821
Ben Hutchings4cea2882011-02-22 21:54:34 +00001822 err = xs_bind(transport, sock);
1823 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001824 sock_release(sock);
1825 goto out;
1826 }
1827
1828 return sock;
1829out:
1830 return ERR_PTR(err);
1831}
1832
Chuck Lever176e21e2011-05-09 15:22:44 -04001833static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1834 struct socket *sock)
1835{
1836 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1837 xprt);
1838
1839 if (!transport->inet) {
1840 struct sock *sk = sock->sk;
1841
1842 write_lock_bh(&sk->sk_callback_lock);
1843
1844 xs_save_old_callbacks(transport, sk);
1845
1846 sk->sk_user_data = xprt;
1847 sk->sk_data_ready = xs_local_data_ready;
1848 sk->sk_write_space = xs_udp_write_space;
Chuck Lever176e21e2011-05-09 15:22:44 -04001849 sk->sk_allocation = GFP_ATOMIC;
1850
1851 xprt_clear_connected(xprt);
1852
1853 /* Reset to new socket */
1854 transport->sock = sock;
1855 transport->inet = sk;
1856
1857 write_unlock_bh(&sk->sk_callback_lock);
1858 }
1859
1860 /* Tell the socket layer to start connecting... */
1861 xprt->stat.connect_count++;
1862 xprt->stat.connect_start = jiffies;
1863 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1864}
1865
1866/**
1867 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1868 * @xprt: RPC transport to connect
1869 * @transport: socket transport to connect
1870 * @create_sock: function to create a socket of the correct type
Chuck Lever176e21e2011-05-09 15:22:44 -04001871 */
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001872static int xs_local_setup_socket(struct sock_xprt *transport)
Chuck Lever176e21e2011-05-09 15:22:44 -04001873{
Chuck Lever176e21e2011-05-09 15:22:44 -04001874 struct rpc_xprt *xprt = &transport->xprt;
1875 struct socket *sock;
1876 int status = -EIO;
1877
Jeff Layton5cf02d02012-07-23 13:58:51 -04001878 current->flags |= PF_FSTRANS;
1879
Chuck Lever176e21e2011-05-09 15:22:44 -04001880 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1881 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1882 SOCK_STREAM, 0, &sock, 1);
1883 if (status < 0) {
1884 dprintk("RPC: can't create AF_LOCAL "
1885 "transport socket (%d).\n", -status);
1886 goto out;
1887 }
1888 xs_reclassify_socketu(sock);
1889
1890 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1891 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1892
1893 status = xs_local_finish_connecting(xprt, sock);
1894 switch (status) {
1895 case 0:
1896 dprintk("RPC: xprt %p connected to %s\n",
1897 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1898 xprt_set_connected(xprt);
1899 break;
1900 case -ENOENT:
1901 dprintk("RPC: xprt %p: socket %s does not exist\n",
1902 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1903 break;
Trond Myklebust4a20a982012-12-15 17:02:29 -05001904 case -ECONNREFUSED:
1905 dprintk("RPC: xprt %p: connection refused for %s\n",
1906 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1907 break;
Chuck Lever176e21e2011-05-09 15:22:44 -04001908 default:
1909 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1910 __func__, -status,
1911 xprt->address_strings[RPC_DISPLAY_ADDR]);
1912 }
1913
1914out:
1915 xprt_clear_connecting(xprt);
1916 xprt_wake_pending_tasks(xprt, status);
Jeff Layton5cf02d02012-07-23 13:58:51 -04001917 current->flags &= ~PF_FSTRANS;
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001918 return status;
1919}
1920
Linus Torvaldsb6669732013-02-28 18:02:55 -08001921static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001922{
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001923 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1924 int ret;
1925
1926 if (RPC_IS_ASYNC(task)) {
1927 /*
1928 * We want the AF_LOCAL connect to be resolved in the
1929 * filesystem namespace of the process making the rpc
1930 * call. Thus we connect synchronously.
1931 *
1932 * If we want to support asynchronous AF_LOCAL calls,
1933 * we'll need to figure out how to pass a namespace to
1934 * connect.
1935 */
1936 rpc_exit(task, -ENOTCONN);
1937 return;
1938 }
1939 ret = xs_local_setup_socket(transport);
1940 if (ret && !RPC_IS_SOFTCONN(task))
1941 msleep_interruptible(15000);
Chuck Lever176e21e2011-05-09 15:22:44 -04001942}
1943
Mel Gormana564b8f2012-07-31 16:45:12 -07001944#ifdef CONFIG_SUNRPC_SWAP
1945static void xs_set_memalloc(struct rpc_xprt *xprt)
1946{
1947 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1948 xprt);
1949
1950 if (xprt->swapper)
1951 sk_set_memalloc(transport->inet);
1952}
1953
1954/**
1955 * xs_swapper - Tag this transport as being used for swap.
1956 * @xprt: transport to tag
1957 * @enable: enable/disable
1958 *
1959 */
1960int xs_swapper(struct rpc_xprt *xprt, int enable)
1961{
1962 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1963 xprt);
1964 int err = 0;
1965
1966 if (enable) {
1967 xprt->swapper++;
1968 xs_set_memalloc(xprt);
1969 } else if (xprt->swapper) {
1970 xprt->swapper--;
1971 sk_clear_memalloc(transport->inet);
1972 }
1973
1974 return err;
1975}
1976EXPORT_SYMBOL_GPL(xs_swapper);
1977#else
1978static void xs_set_memalloc(struct rpc_xprt *xprt)
1979{
1980}
1981#endif
1982
Chuck Lever16be2d22007-08-06 11:57:38 -04001983static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001984{
Chuck Lever16be2d22007-08-06 11:57:38 -04001985 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001986
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001987 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001988 struct sock *sk = sock->sk;
1989
1990 write_lock_bh(&sk->sk_callback_lock);
1991
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001992 xs_save_old_callbacks(transport, sk);
1993
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001994 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001995 sk->sk_data_ready = xs_udp_data_ready;
1996 sk->sk_write_space = xs_udp_write_space;
1997 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001998 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001999
2000 xprt_set_connected(xprt);
2001
2002 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002003 transport->sock = sock;
2004 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002005
Mel Gormana564b8f2012-07-31 16:45:12 -07002006 xs_set_memalloc(xprt);
2007
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002008 write_unlock_bh(&sk->sk_callback_lock);
2009 }
Chuck Lever470056c2005-08-25 16:25:56 -07002010 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04002011}
2012
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002013static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04002014{
2015 struct sock_xprt *transport =
2016 container_of(work, struct sock_xprt, connect_worker.work);
2017 struct rpc_xprt *xprt = &transport->xprt;
2018 struct socket *sock = transport->sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002019 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04002020
Jeff Layton5cf02d02012-07-23 13:58:51 -04002021 current->flags |= PF_FSTRANS;
2022
Chuck Levera246b012005-08-11 16:25:23 -04002023 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04002024 xs_reset_transport(transport);
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002025 sock = xs_create_sock(xprt, transport,
2026 xs_addr(xprt)->sa_family, SOCK_DGRAM, IPPROTO_UDP);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002027 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04002028 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04002029
Chuck Leverc740eff2009-08-09 15:09:46 -04002030 dprintk("RPC: worker connecting xprt %p via %s to "
2031 "%s (port %s)\n", xprt,
2032 xprt->address_strings[RPC_DISPLAY_PROTO],
2033 xprt->address_strings[RPC_DISPLAY_ADDR],
2034 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04002035
2036 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04002037 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002038out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002039 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002040 xprt_wake_pending_tasks(xprt, status);
Jeff Layton5cf02d02012-07-23 13:58:51 -04002041 current->flags &= ~PF_FSTRANS;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002042}
2043
Chuck Lever3167e122005-08-25 16:25:55 -07002044/*
2045 * We need to preserve the port number so the reply cache on the server can
2046 * find our cached RPC replies when we get around to reconnecting.
2047 */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002048static void xs_abort_connection(struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07002049{
2050 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07002051 struct sockaddr any;
2052
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002053 dprintk("RPC: disconnecting xprt %p to reuse port\n", transport);
Chuck Lever3167e122005-08-25 16:25:55 -07002054
2055 /*
2056 * Disconnect the transport socket by doing a connect operation
2057 * with AF_UNSPEC. This should return immediately...
2058 */
2059 memset(&any, 0, sizeof(any));
2060 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002061 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002062 if (!result)
Trond Myklebust4bc1e682012-10-23 17:50:07 -04002063 xs_sock_reset_connection_flags(&transport->xprt);
2064 dprintk("RPC: AF_UNSPEC connect return code %d\n", result);
Chuck Lever3167e122005-08-25 16:25:55 -07002065}
2066
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002067static void xs_tcp_reuse_connection(struct sock_xprt *transport)
Trond Myklebust40d25492009-03-11 14:37:58 -04002068{
2069 unsigned int state = transport->inet->sk_state;
2070
Andy Chittenden669502f2010-08-10 10:19:53 -04002071 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) {
2072 /* we don't need to abort the connection if the socket
2073 * hasn't undergone a shutdown
2074 */
2075 if (transport->inet->sk_shutdown == 0)
2076 return;
2077 dprintk("RPC: %s: TCP_CLOSEd and sk_shutdown set to %d\n",
2078 __func__, transport->inet->sk_shutdown);
2079 }
2080 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) {
2081 /* we don't need to abort the connection if the socket
2082 * hasn't undergone a shutdown
2083 */
2084 if (transport->inet->sk_shutdown == 0)
2085 return;
2086 dprintk("RPC: %s: ESTABLISHED/SYN_SENT "
2087 "sk_shutdown set to %d\n",
2088 __func__, transport->inet->sk_shutdown);
2089 }
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002090 xs_abort_connection(transport);
Trond Myklebust40d25492009-03-11 14:37:58 -04002091}
2092
Chuck Lever16be2d22007-08-06 11:57:38 -04002093static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002094{
Chuck Lever16be2d22007-08-06 11:57:38 -04002095 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002096 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002097
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002098 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002099 struct sock *sk = sock->sk;
2100
2101 write_lock_bh(&sk->sk_callback_lock);
2102
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002103 xs_save_old_callbacks(transport, sk);
2104
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002105 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002106 sk->sk_data_ready = xs_tcp_data_ready;
2107 sk->sk_state_change = xs_tcp_state_change;
2108 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05002109 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07002110
2111 /* socket options */
2112 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
2113 sock_reset_flag(sk, SOCK_LINGER);
2114 tcp_sk(sk)->linger2 = 0;
2115 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002116
2117 xprt_clear_connected(xprt);
2118
2119 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002120 transport->sock = sock;
2121 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002122
2123 write_unlock_bh(&sk->sk_callback_lock);
2124 }
2125
Trond Myklebust01d37c42009-03-11 14:09:39 -04002126 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002127 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002128
Mel Gormana564b8f2012-07-31 16:45:12 -07002129 xs_set_memalloc(xprt);
2130
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002131 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05002132 xprt->stat.connect_count++;
2133 xprt->stat.connect_start = jiffies;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002134 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2135 switch (ret) {
2136 case 0:
2137 case -EINPROGRESS:
2138 /* SYN_SENT! */
2139 xprt->connect_cookie++;
2140 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2141 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2142 }
2143out:
2144 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002145}
2146
2147/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002148 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2149 * @xprt: RPC transport to connect
2150 * @transport: socket transport to connect
2151 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04002152 *
2153 * Invoked by a work queue tasklet.
2154 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002155static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002156{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002157 struct sock_xprt *transport =
2158 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002159 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002160 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002161 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002162
Jeff Layton5cf02d02012-07-23 13:58:51 -04002163 current->flags |= PF_FSTRANS;
2164
Chuck Lever16be2d22007-08-06 11:57:38 -04002165 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002166 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002167 sock = xs_create_sock(xprt, transport,
2168 xs_addr(xprt)->sa_family, SOCK_STREAM, IPPROTO_TCP);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002169 if (IS_ERR(sock)) {
2170 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002171 goto out;
2172 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002173 } else {
2174 int abort_and_exit;
2175
2176 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
2177 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04002178 /* "close" the socket, preserving the local port */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002179 xs_tcp_reuse_connection(transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04002180
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002181 if (abort_and_exit)
2182 goto out_eagain;
2183 }
2184
Chuck Leverc740eff2009-08-09 15:09:46 -04002185 dprintk("RPC: worker connecting xprt %p via %s to "
2186 "%s (port %s)\n", xprt,
2187 xprt->address_strings[RPC_DISPLAY_PROTO],
2188 xprt->address_strings[RPC_DISPLAY_ADDR],
2189 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002190
2191 status = xs_tcp_finish_connecting(xprt, sock);
Chuck Lever46121cf2007-01-31 12:14:08 -05002192 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2193 xprt, -status, xprt_connected(xprt),
2194 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002195 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002196 default:
2197 printk("%s: connect returned unhandled error %d\n",
2198 __func__, status);
2199 case -EADDRNOTAVAIL:
2200 /* We're probably in TIME_WAIT. Get rid of existing socket,
2201 * and retry
2202 */
Trond Myklebusta519fc72012-09-12 16:49:15 -04002203 xs_tcp_force_close(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002204 break;
Trond Myklebust2a491992009-03-11 14:38:00 -04002205 case 0:
2206 case -EINPROGRESS:
2207 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002208 xprt_clear_connecting(xprt);
Jeff Layton5cf02d02012-07-23 13:58:51 -04002209 current->flags &= ~PF_FSTRANS;
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002210 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002211 case -EINVAL:
2212 /* Happens, for instance, if the user specified a link
2213 * local IPv6 address without a scope-id.
2214 */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002215 case -ECONNREFUSED:
2216 case -ECONNRESET:
2217 case -ENETUNREACH:
2218 /* retry with existing socket, after a delay */
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002219 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002220 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002221out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04002222 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002223out:
Chuck Lever2226feb2005-08-11 16:25:38 -04002224 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002225 xprt_wake_pending_tasks(xprt, status);
Jeff Layton5cf02d02012-07-23 13:58:51 -04002226 current->flags &= ~PF_FSTRANS;
Chuck Levera246b012005-08-11 16:25:23 -04002227}
2228
Chuck Lever68e220b2007-08-06 11:57:48 -04002229/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002230 * xs_connect - connect a socket to a remote endpoint
Trond Myklebust1b092092013-01-08 09:26:49 -05002231 * @xprt: pointer to transport structure
Chuck Lever9903cd12005-08-11 16:25:26 -04002232 * @task: address of RPC task that manages state of connect request
2233 *
2234 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002235 *
2236 * UDP socket connects are synchronous, but we use a work queue anyway
2237 * to guarantee that even unprivileged user processes can set up a
2238 * socket on a privileged port.
2239 *
2240 * If a UDP socket connect fails, the delay behavior here prevents
2241 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002242 */
Trond Myklebust1b092092013-01-08 09:26:49 -05002243static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002244{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002245 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002246
Chuck Lever09a21c42009-12-03 15:58:56 -05002247 if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002248 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2249 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002250 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002251 queue_delayed_work(rpciod_workqueue,
2252 &transport->connect_worker,
2253 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07002254 xprt->reestablish_timeout <<= 1;
Neil Brown61d0a8e2009-09-23 14:36:37 -04002255 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2256 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002257 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2258 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002259 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05002260 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002261 queue_delayed_work(rpciod_workqueue,
2262 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002263 }
2264}
2265
Chuck Lever262ca072006-03-20 13:44:16 -05002266/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002267 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2268 * @xprt: rpc_xprt struct containing statistics
2269 * @seq: output file
2270 *
2271 */
2272static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2273{
2274 long idle_time = 0;
2275
2276 if (xprt_connected(xprt))
2277 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2278
2279 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
Andy Adamson15a45202012-02-14 16:19:18 -05002280 "%llu %llu %lu %llu %llu\n",
Chuck Lever176e21e2011-05-09 15:22:44 -04002281 xprt->stat.bind_count,
2282 xprt->stat.connect_count,
2283 xprt->stat.connect_time,
2284 idle_time,
2285 xprt->stat.sends,
2286 xprt->stat.recvs,
2287 xprt->stat.bad_xids,
2288 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002289 xprt->stat.bklog_u,
2290 xprt->stat.max_slots,
2291 xprt->stat.sending_u,
2292 xprt->stat.pending_u);
Chuck Lever176e21e2011-05-09 15:22:44 -04002293}
2294
2295/**
Chuck Lever262ca072006-03-20 13:44:16 -05002296 * xs_udp_print_stats - display UDP socket-specifc stats
2297 * @xprt: rpc_xprt struct containing statistics
2298 * @seq: output file
2299 *
2300 */
2301static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2302{
Chuck Leverc8475462006-12-05 16:35:26 -05002303 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2304
Andy Adamson15a45202012-02-14 16:19:18 -05002305 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2306 "%lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002307 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002308 xprt->stat.bind_count,
2309 xprt->stat.sends,
2310 xprt->stat.recvs,
2311 xprt->stat.bad_xids,
2312 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002313 xprt->stat.bklog_u,
2314 xprt->stat.max_slots,
2315 xprt->stat.sending_u,
2316 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002317}
2318
2319/**
2320 * xs_tcp_print_stats - display TCP socket-specifc stats
2321 * @xprt: rpc_xprt struct containing statistics
2322 * @seq: output file
2323 *
2324 */
2325static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2326{
Chuck Leverc8475462006-12-05 16:35:26 -05002327 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002328 long idle_time = 0;
2329
2330 if (xprt_connected(xprt))
2331 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2332
Andy Adamson15a45202012-02-14 16:19:18 -05002333 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2334 "%llu %llu %lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002335 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002336 xprt->stat.bind_count,
2337 xprt->stat.connect_count,
2338 xprt->stat.connect_time,
2339 idle_time,
2340 xprt->stat.sends,
2341 xprt->stat.recvs,
2342 xprt->stat.bad_xids,
2343 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002344 xprt->stat.bklog_u,
2345 xprt->stat.max_slots,
2346 xprt->stat.sending_u,
2347 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002348}
2349
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002350/*
2351 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2352 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2353 * to use the server side send routines.
2354 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002355static void *bc_malloc(struct rpc_task *task, size_t size)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002356{
2357 struct page *page;
2358 struct rpc_buffer *buf;
2359
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002360 WARN_ON_ONCE(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2361 if (size > PAGE_SIZE - sizeof(struct rpc_buffer))
2362 return NULL;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002363
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002364 page = alloc_page(GFP_KERNEL);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002365 if (!page)
2366 return NULL;
2367
2368 buf = page_address(page);
2369 buf->len = PAGE_SIZE;
2370
2371 return buf->data;
2372}
2373
2374/*
2375 * Free the space allocated in the bc_alloc routine
2376 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002377static void bc_free(void *buffer)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002378{
2379 struct rpc_buffer *buf;
2380
2381 if (!buffer)
2382 return;
2383
2384 buf = container_of(buffer, struct rpc_buffer, data);
2385 free_page((unsigned long)buf);
2386}
2387
2388/*
2389 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2390 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2391 */
2392static int bc_sendto(struct rpc_rqst *req)
2393{
2394 int len;
2395 struct xdr_buf *xbufp = &req->rq_snd_buf;
2396 struct rpc_xprt *xprt = req->rq_xprt;
2397 struct sock_xprt *transport =
2398 container_of(xprt, struct sock_xprt, xprt);
2399 struct socket *sock = transport->sock;
2400 unsigned long headoff;
2401 unsigned long tailoff;
2402
Chuck Lever61677ee2011-05-09 15:22:34 -04002403 xs_encode_stream_record_marker(xbufp);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002404
2405 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2406 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2407 len = svc_send_common(sock, xbufp,
2408 virt_to_page(xbufp->head[0].iov_base), headoff,
2409 xbufp->tail[0].iov_base, tailoff);
2410
2411 if (len != xbufp->len) {
2412 printk(KERN_NOTICE "Error sending entire callback!\n");
2413 len = -EAGAIN;
2414 }
2415
2416 return len;
2417}
2418
2419/*
2420 * The send routine. Borrows from svc_send
2421 */
2422static int bc_send_request(struct rpc_task *task)
2423{
2424 struct rpc_rqst *req = task->tk_rqstp;
2425 struct svc_xprt *xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002426 u32 len;
2427
2428 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2429 /*
2430 * Get the server socket associated with this callback xprt
2431 */
2432 xprt = req->rq_xprt->bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002433
2434 /*
2435 * Grab the mutex to serialize data as the connection is shared
2436 * with the fore channel
2437 */
2438 if (!mutex_trylock(&xprt->xpt_mutex)) {
2439 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2440 if (!mutex_trylock(&xprt->xpt_mutex))
2441 return -EAGAIN;
2442 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2443 }
2444 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2445 len = -ENOTCONN;
2446 else
2447 len = bc_sendto(req);
2448 mutex_unlock(&xprt->xpt_mutex);
2449
2450 if (len > 0)
2451 len = 0;
2452
2453 return len;
2454}
2455
2456/*
2457 * The close routine. Since this is client initiated, we do nothing
2458 */
2459
2460static void bc_close(struct rpc_xprt *xprt)
2461{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002462}
2463
2464/*
2465 * The xprt destroy routine. Again, because this connection is client
2466 * initiated, we do nothing
2467 */
2468
2469static void bc_destroy(struct rpc_xprt *xprt)
2470{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002471}
2472
Chuck Lever176e21e2011-05-09 15:22:44 -04002473static struct rpc_xprt_ops xs_local_ops = {
2474 .reserve_xprt = xprt_reserve_xprt,
2475 .release_xprt = xs_tcp_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002476 .alloc_slot = xprt_alloc_slot,
Chuck Lever176e21e2011-05-09 15:22:44 -04002477 .rpcbind = xs_local_rpcbind,
2478 .set_port = xs_local_set_port,
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002479 .connect = xs_local_connect,
Chuck Lever176e21e2011-05-09 15:22:44 -04002480 .buf_alloc = rpc_malloc,
2481 .buf_free = rpc_free,
2482 .send_request = xs_local_send_request,
2483 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2484 .close = xs_close,
2485 .destroy = xs_destroy,
2486 .print_stats = xs_local_print_stats,
2487};
2488
Chuck Lever262965f2005-08-11 16:25:56 -04002489static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002490 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002491 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002492 .release_xprt = xprt_release_xprt_cong,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002493 .alloc_slot = xprt_alloc_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002494 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002495 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002496 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002497 .buf_alloc = rpc_malloc,
2498 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002499 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002500 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002501 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002502 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002503 .close = xs_close,
2504 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002505 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04002506};
2507
2508static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002509 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002510 .release_xprt = xs_tcp_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002511 .alloc_slot = xprt_lock_and_alloc_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002512 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002513 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002514 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002515 .buf_alloc = rpc_malloc,
2516 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002517 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002518 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustf75e6742009-04-21 17:18:20 -04002519 .close = xs_tcp_close,
Chuck Lever9903cd12005-08-11 16:25:26 -04002520 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002521 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002522};
2523
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002524/*
2525 * The rpc_xprt_ops for the server backchannel
2526 */
2527
2528static struct rpc_xprt_ops bc_tcp_ops = {
2529 .reserve_xprt = xprt_reserve_xprt,
2530 .release_xprt = xprt_release_xprt,
Bryan Schumaker84e28a32012-09-24 13:39:01 -04002531 .alloc_slot = xprt_alloc_slot,
J. Bruce Fields92769102012-03-23 15:25:03 -04002532 .rpcbind = xs_local_rpcbind,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002533 .buf_alloc = bc_malloc,
2534 .buf_free = bc_free,
2535 .send_request = bc_send_request,
2536 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2537 .close = bc_close,
2538 .destroy = bc_destroy,
2539 .print_stats = xs_tcp_print_stats,
2540};
2541
Chuck Lever92476852010-10-20 11:53:01 -04002542static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2543{
2544 static const struct sockaddr_in sin = {
2545 .sin_family = AF_INET,
2546 .sin_addr.s_addr = htonl(INADDR_ANY),
2547 };
2548 static const struct sockaddr_in6 sin6 = {
2549 .sin6_family = AF_INET6,
2550 .sin6_addr = IN6ADDR_ANY_INIT,
2551 };
2552
2553 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002554 case AF_LOCAL:
2555 break;
Chuck Lever92476852010-10-20 11:53:01 -04002556 case AF_INET:
2557 memcpy(sap, &sin, sizeof(sin));
2558 break;
2559 case AF_INET6:
2560 memcpy(sap, &sin6, sizeof(sin6));
2561 break;
2562 default:
2563 dprintk("RPC: %s: Bad address family\n", __func__);
2564 return -EAFNOSUPPORT;
2565 }
2566 return 0;
2567}
2568
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002569static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002570 unsigned int slot_table_size,
2571 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002572{
2573 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002574 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002575
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002576 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002577 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002578 return ERR_PTR(-EBADF);
2579 }
2580
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002581 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2582 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002583 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002584 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2585 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002586 return ERR_PTR(-ENOMEM);
2587 }
2588
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002589 new = container_of(xprt, struct sock_xprt, xprt);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002590 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2591 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002592 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002593 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002594 else {
2595 int err;
2596 err = xs_init_anyaddr(args->dstaddr->sa_family,
2597 (struct sockaddr *)&new->srcaddr);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002598 if (err != 0) {
2599 xprt_free(xprt);
Chuck Lever92476852010-10-20 11:53:01 -04002600 return ERR_PTR(err);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002601 }
Chuck Lever92476852010-10-20 11:53:01 -04002602 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002603
2604 return xprt;
2605}
2606
Chuck Lever176e21e2011-05-09 15:22:44 -04002607static const struct rpc_timeout xs_local_default_timeout = {
2608 .to_initval = 10 * HZ,
2609 .to_maxval = 10 * HZ,
2610 .to_retries = 2,
2611};
2612
2613/**
2614 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2615 * @args: rpc transport creation arguments
2616 *
2617 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2618 */
2619static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2620{
2621 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2622 struct sock_xprt *transport;
2623 struct rpc_xprt *xprt;
2624 struct rpc_xprt *ret;
2625
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002626 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2627 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002628 if (IS_ERR(xprt))
2629 return xprt;
2630 transport = container_of(xprt, struct sock_xprt, xprt);
2631
2632 xprt->prot = 0;
2633 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2634 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2635
2636 xprt->bind_timeout = XS_BIND_TO;
2637 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2638 xprt->idle_timeout = XS_IDLE_DISC_TO;
2639
2640 xprt->ops = &xs_local_ops;
2641 xprt->timeout = &xs_local_default_timeout;
2642
2643 switch (sun->sun_family) {
2644 case AF_LOCAL:
2645 if (sun->sun_path[0] != '/') {
2646 dprintk("RPC: bad AF_LOCAL address: %s\n",
2647 sun->sun_path);
2648 ret = ERR_PTR(-EINVAL);
2649 goto out_err;
2650 }
2651 xprt_set_bound(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002652 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
2653 break;
2654 default:
2655 ret = ERR_PTR(-EAFNOSUPPORT);
2656 goto out_err;
2657 }
2658
2659 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2660 xprt->address_strings[RPC_DISPLAY_ADDR]);
2661
2662 if (try_module_get(THIS_MODULE))
2663 return xprt;
2664 ret = ERR_PTR(-EINVAL);
2665out_err:
2666 xprt_free(xprt);
2667 return ret;
2668}
2669
Trond Myklebust2881ae72007-12-20 16:03:54 -05002670static const struct rpc_timeout xs_udp_default_timeout = {
2671 .to_initval = 5 * HZ,
2672 .to_maxval = 30 * HZ,
2673 .to_increment = 5 * HZ,
2674 .to_retries = 5,
2675};
2676
Chuck Lever9903cd12005-08-11 16:25:26 -04002677/**
2678 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002679 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002680 *
2681 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002682static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002683{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002684 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002685 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002686 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002687 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002688
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002689 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2690 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002691 if (IS_ERR(xprt))
2692 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002693 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002694
Chuck Leverec739ef2006-08-22 20:06:15 -04002695 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002696 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002697 /* XXX: header size can vary due to auth type, IPv6, etc. */
2698 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2699
Chuck Lever03bf4b72005-08-25 16:25:55 -07002700 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002701 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2702 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002703
Chuck Lever262965f2005-08-11 16:25:56 -04002704 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002705
Trond Myklebustba7392b2007-12-20 16:03:55 -05002706 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002707
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002708 switch (addr->sa_family) {
2709 case AF_INET:
2710 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2711 xprt_set_bound(xprt);
2712
2713 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002714 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002715 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002716 break;
2717 case AF_INET6:
2718 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2719 xprt_set_bound(xprt);
2720
2721 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002722 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002723 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002724 break;
2725 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002726 ret = ERR_PTR(-EAFNOSUPPORT);
2727 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002728 }
2729
Chuck Leverc740eff2009-08-09 15:09:46 -04002730 if (xprt_bound(xprt))
2731 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2732 xprt->address_strings[RPC_DISPLAY_ADDR],
2733 xprt->address_strings[RPC_DISPLAY_PORT],
2734 xprt->address_strings[RPC_DISPLAY_PROTO]);
2735 else
2736 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2737 xprt->address_strings[RPC_DISPLAY_ADDR],
2738 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002739
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002740 if (try_module_get(THIS_MODULE))
2741 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002742 ret = ERR_PTR(-EINVAL);
2743out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002744 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002745 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002746}
2747
Trond Myklebust2881ae72007-12-20 16:03:54 -05002748static const struct rpc_timeout xs_tcp_default_timeout = {
2749 .to_initval = 60 * HZ,
2750 .to_maxval = 60 * HZ,
2751 .to_retries = 2,
2752};
2753
Chuck Lever9903cd12005-08-11 16:25:26 -04002754/**
2755 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002756 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002757 *
2758 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002759static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002760{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002761 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002762 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002763 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002764 struct rpc_xprt *ret;
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002765 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2766
2767 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2768 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Levera246b012005-08-11 16:25:23 -04002769
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002770 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002771 max_slot_table_size);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002772 if (IS_ERR(xprt))
2773 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002774 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002775
Chuck Leverec739ef2006-08-22 20:06:15 -04002776 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002777 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002778 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002779
Chuck Lever03bf4b72005-08-25 16:25:55 -07002780 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002781 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2782 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002783
Chuck Lever262965f2005-08-11 16:25:56 -04002784 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002785 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002786
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002787 switch (addr->sa_family) {
2788 case AF_INET:
2789 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2790 xprt_set_bound(xprt);
2791
Chuck Lever9dc3b092009-08-09 15:09:46 -04002792 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002793 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002794 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002795 break;
2796 case AF_INET6:
2797 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2798 xprt_set_bound(xprt);
2799
Chuck Lever9dc3b092009-08-09 15:09:46 -04002800 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002801 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002802 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002803 break;
2804 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002805 ret = ERR_PTR(-EAFNOSUPPORT);
2806 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002807 }
2808
Chuck Leverc740eff2009-08-09 15:09:46 -04002809 if (xprt_bound(xprt))
2810 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2811 xprt->address_strings[RPC_DISPLAY_ADDR],
2812 xprt->address_strings[RPC_DISPLAY_PORT],
2813 xprt->address_strings[RPC_DISPLAY_PROTO]);
2814 else
2815 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2816 xprt->address_strings[RPC_DISPLAY_ADDR],
2817 xprt->address_strings[RPC_DISPLAY_PROTO]);
2818
Chuck Leveredb267a2006-08-22 20:06:18 -04002819
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002820 if (try_module_get(THIS_MODULE))
2821 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002822 ret = ERR_PTR(-EINVAL);
2823out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002824 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002825 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002826}
Chuck Lever282b32e2006-12-05 16:35:51 -05002827
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002828/**
2829 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2830 * @args: rpc transport creation arguments
2831 *
2832 */
2833static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2834{
2835 struct sockaddr *addr = args->dstaddr;
2836 struct rpc_xprt *xprt;
2837 struct sock_xprt *transport;
2838 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002839 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002840
J. Bruce Fieldsf0418aa2010-12-08 13:48:19 -05002841 if (args->bc_xprt->xpt_bc_xprt) {
2842 /*
2843 * This server connection already has a backchannel
2844 * export; we can't create a new one, as we wouldn't be
2845 * able to match replies based on xid any more. So,
2846 * reuse the already-existing one:
2847 */
2848 return args->bc_xprt->xpt_bc_xprt;
2849 }
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002850 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2851 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002852 if (IS_ERR(xprt))
2853 return xprt;
2854 transport = container_of(xprt, struct sock_xprt, xprt);
2855
2856 xprt->prot = IPPROTO_TCP;
2857 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2858 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2859 xprt->timeout = &xs_tcp_default_timeout;
2860
2861 /* backchannel */
2862 xprt_set_bound(xprt);
2863 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002864 xprt->reestablish_timeout = 0;
2865 xprt->idle_timeout = 0;
2866
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002867 xprt->ops = &bc_tcp_ops;
2868
2869 switch (addr->sa_family) {
2870 case AF_INET:
2871 xs_format_peer_addresses(xprt, "tcp",
2872 RPCBIND_NETID_TCP);
2873 break;
2874 case AF_INET6:
2875 xs_format_peer_addresses(xprt, "tcp",
2876 RPCBIND_NETID_TCP6);
2877 break;
2878 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002879 ret = ERR_PTR(-EAFNOSUPPORT);
2880 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002881 }
2882
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04002883 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2884 xprt->address_strings[RPC_DISPLAY_ADDR],
2885 xprt->address_strings[RPC_DISPLAY_PORT],
2886 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002887
2888 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002889 * Once we've associated a backchannel xprt with a connection,
2890 * we want to keep it around as long as long as the connection
2891 * lasts, in case we need to start using it for a backchannel
2892 * again; this reference won't be dropped until bc_xprt is
2893 * destroyed.
2894 */
2895 xprt_get(xprt);
2896 args->bc_xprt->xpt_bc_xprt = xprt;
2897 xprt->bc_xprt = args->bc_xprt;
2898 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2899 transport->sock = bc_sock->sk_sock;
2900 transport->inet = bc_sock->sk_sk;
2901
2902 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002903 * Since we don't want connections for the backchannel, we set
2904 * the xprt status to connected
2905 */
2906 xprt_set_connected(xprt);
2907
2908
2909 if (try_module_get(THIS_MODULE))
2910 return xprt;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002911 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002912 ret = ERR_PTR(-EINVAL);
2913out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002914 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002915 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002916}
2917
Chuck Lever176e21e2011-05-09 15:22:44 -04002918static struct xprt_class xs_local_transport = {
2919 .list = LIST_HEAD_INIT(xs_local_transport.list),
2920 .name = "named UNIX socket",
2921 .owner = THIS_MODULE,
2922 .ident = XPRT_TRANSPORT_LOCAL,
2923 .setup = xs_setup_local,
2924};
2925
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002926static struct xprt_class xs_udp_transport = {
2927 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2928 .name = "udp",
2929 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002930 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002931 .setup = xs_setup_udp,
2932};
2933
2934static struct xprt_class xs_tcp_transport = {
2935 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2936 .name = "tcp",
2937 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002938 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002939 .setup = xs_setup_tcp,
2940};
2941
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002942static struct xprt_class xs_bc_tcp_transport = {
2943 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2944 .name = "tcp NFSv4.1 backchannel",
2945 .owner = THIS_MODULE,
2946 .ident = XPRT_TRANSPORT_BC_TCP,
2947 .setup = xs_setup_bc_tcp,
2948};
2949
Chuck Lever282b32e2006-12-05 16:35:51 -05002950/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002951 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002952 *
2953 */
2954int init_socket_xprt(void)
2955{
Chuck Leverfbf76682006-12-05 16:35:54 -05002956#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002957 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002958 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002959#endif
2960
Chuck Lever176e21e2011-05-09 15:22:44 -04002961 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002962 xprt_register_transport(&xs_udp_transport);
2963 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002964 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002965
Chuck Lever282b32e2006-12-05 16:35:51 -05002966 return 0;
2967}
2968
2969/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002970 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05002971 *
2972 */
2973void cleanup_socket_xprt(void)
2974{
Chuck Leverfbf76682006-12-05 16:35:54 -05002975#ifdef RPC_DEBUG
2976 if (sunrpc_table_header) {
2977 unregister_sysctl_table(sunrpc_table_header);
2978 sunrpc_table_header = NULL;
2979 }
2980#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002981
Chuck Lever176e21e2011-05-09 15:22:44 -04002982 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002983 xprt_unregister_transport(&xs_udp_transport);
2984 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002985 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05002986}
Trond Myklebustcbf11072009-08-09 15:06:19 -04002987
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002988static int param_set_uint_minmax(const char *val,
2989 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04002990 unsigned int min, unsigned int max)
2991{
2992 unsigned long num;
2993 int ret;
2994
2995 if (!val)
2996 return -EINVAL;
2997 ret = strict_strtoul(val, 0, &num);
2998 if (ret == -EINVAL || num < min || num > max)
2999 return -EINVAL;
3000 *((unsigned int *)kp->arg) = num;
3001 return 0;
3002}
3003
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003004static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003005{
3006 return param_set_uint_minmax(val, kp,
3007 RPC_MIN_RESVPORT,
3008 RPC_MAX_RESVPORT);
3009}
3010
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003011static struct kernel_param_ops param_ops_portnr = {
3012 .set = param_set_portnr,
3013 .get = param_get_uint,
3014};
3015
Trond Myklebustcbf11072009-08-09 15:06:19 -04003016#define param_check_portnr(name, p) \
3017 __param_check(name, p, unsigned int);
3018
3019module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3020module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3021
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003022static int param_set_slot_table_size(const char *val,
3023 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003024{
3025 return param_set_uint_minmax(val, kp,
3026 RPC_MIN_SLOT_TABLE,
3027 RPC_MAX_SLOT_TABLE);
3028}
3029
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003030static struct kernel_param_ops param_ops_slot_table_size = {
3031 .set = param_set_slot_table_size,
3032 .get = param_get_uint,
3033};
3034
Trond Myklebustcbf11072009-08-09 15:06:19 -04003035#define param_check_slot_table_size(name, p) \
3036 __param_check(name, p, unsigned int);
3037
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003038static int param_set_max_slot_table_size(const char *val,
3039 const struct kernel_param *kp)
3040{
3041 return param_set_uint_minmax(val, kp,
3042 RPC_MIN_SLOT_TABLE,
3043 RPC_MAX_SLOT_TABLE_LIMIT);
3044}
3045
3046static struct kernel_param_ops param_ops_max_slot_table_size = {
3047 .set = param_set_max_slot_table_size,
3048 .get = param_get_uint,
3049};
3050
3051#define param_check_max_slot_table_size(name, p) \
3052 __param_check(name, p, unsigned int);
3053
Trond Myklebustcbf11072009-08-09 15:06:19 -04003054module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3055 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003056module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3057 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003058module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3059 slot_table_size, 0644);
3060