blob: 72abb735893321d4616d5d9b0801e5cb6f777cff [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>
Chuck Lever02107142006-01-03 09:55:49 +010036#include <linux/sunrpc/sched.h>
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030037#include <linux/sunrpc/svcsock.h>
\"Talpey, Thomas\49c36fc2007-09-10 13:47:31 -040038#include <linux/sunrpc/xprtsock.h>
Chuck Levera246b012005-08-11 16:25:23 -040039#include <linux/file.h>
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -040040#ifdef CONFIG_NFS_V4_1
41#include <linux/sunrpc/bc_xprt.h>
42#endif
Chuck Levera246b012005-08-11 16:25:23 -040043
44#include <net/sock.h>
45#include <net/checksum.h>
46#include <net/udp.h>
47#include <net/tcp.h>
48
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030049#include "sunrpc.h"
Chuck Lever176e21e2011-05-09 15:22:44 -040050
51static void xs_close(struct rpc_xprt *xprt);
52
Chuck Lever9903cd12005-08-11 16:25:26 -040053/*
Chuck Leverc556b752005-11-01 12:24:48 -050054 * xprtsock tunables
55 */
56unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
57unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
58
59unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
60unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
61
Trond Myklebust7d1e8252009-03-11 14:38:03 -040062#define XS_TCP_LINGER_TO (15U * HZ)
Trond Myklebust25fe6142009-03-11 14:38:03 -040063static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
Trond Myklebust7d1e8252009-03-11 14:38:03 -040064
Chuck Leverc556b752005-11-01 12:24:48 -050065/*
Chuck Leverfbf76682006-12-05 16:35:54 -050066 * We can register our own files under /proc/sys/sunrpc by
67 * calling register_sysctl_table() again. The files in that
68 * directory become the union of all files registered there.
69 *
70 * We simply need to make sure that we don't collide with
71 * someone else's file names!
72 */
73
74#ifdef RPC_DEBUG
75
76static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
77static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
78static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
79static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
80
81static struct ctl_table_header *sunrpc_table_header;
82
83/*
84 * FIXME: changing the UDP slot table size should also resize the UDP
85 * socket buffers for existing UDP transports
86 */
87static ctl_table xs_tunables_table[] = {
88 {
Chuck Leverfbf76682006-12-05 16:35:54 -050089 .procname = "udp_slot_table_entries",
90 .data = &xprt_udp_slot_table_entries,
91 .maxlen = sizeof(unsigned int),
92 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080093 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -050094 .extra1 = &min_slot_table_size,
95 .extra2 = &max_slot_table_size
96 },
97 {
Chuck Leverfbf76682006-12-05 16:35:54 -050098 .procname = "tcp_slot_table_entries",
99 .data = &xprt_tcp_slot_table_entries,
100 .maxlen = sizeof(unsigned int),
101 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800102 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500103 .extra1 = &min_slot_table_size,
104 .extra2 = &max_slot_table_size
105 },
106 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500107 .procname = "min_resvport",
108 .data = &xprt_min_resvport,
109 .maxlen = sizeof(unsigned int),
110 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800111 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500112 .extra1 = &xprt_min_resvport_limit,
113 .extra2 = &xprt_max_resvport_limit
114 },
115 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500116 .procname = "max_resvport",
117 .data = &xprt_max_resvport,
118 .maxlen = sizeof(unsigned int),
119 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800120 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500121 .extra1 = &xprt_min_resvport_limit,
122 .extra2 = &xprt_max_resvport_limit
123 },
124 {
Trond Myklebust25fe6142009-03-11 14:38:03 -0400125 .procname = "tcp_fin_timeout",
126 .data = &xs_tcp_fin_timeout,
127 .maxlen = sizeof(xs_tcp_fin_timeout),
128 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800129 .proc_handler = proc_dointvec_jiffies,
Trond Myklebust25fe6142009-03-11 14:38:03 -0400130 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800131 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500132};
133
134static ctl_table sunrpc_table[] = {
135 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500136 .procname = "sunrpc",
137 .mode = 0555,
138 .child = xs_tunables_table
139 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800140 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500141};
142
143#endif
144
145/*
Chuck Lever03bf4b72005-08-25 16:25:55 -0700146 * Wait duration for a reply from the RPC portmapper.
147 */
148#define XS_BIND_TO (60U * HZ)
149
150/*
151 * Delay if a UDP socket connect error occurs. This is most likely some
152 * kind of resource problem on the local host.
153 */
154#define XS_UDP_REEST_TO (2U * HZ)
155
156/*
157 * The reestablish timeout allows clients to delay for a bit before attempting
158 * to reconnect to a server that just dropped our connection.
159 *
160 * We implement an exponential backoff when trying to reestablish a TCP
161 * transport connection with the server. Some servers like to drop a TCP
162 * connection when they are overworked, so we start with a short timeout and
163 * increase over time if the server is down or not responding.
164 */
165#define XS_TCP_INIT_REEST_TO (3U * HZ)
166#define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
167
168/*
169 * TCP idle timeout; client drops the transport socket if it is idle
170 * for this long. Note that we also timeout UDP sockets to prevent
171 * holding port numbers when there is no RPC traffic.
172 */
173#define XS_IDLE_DISC_TO (5U * 60 * HZ)
174
Chuck Levera246b012005-08-11 16:25:23 -0400175#ifdef RPC_DEBUG
176# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400177# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400178#endif
179
Chuck Levera246b012005-08-11 16:25:23 -0400180#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400181static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400182{
Chuck Lever9903cd12005-08-11 16:25:26 -0400183 u8 *buf = (u8 *) packet;
184 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400185
Chuck Lever46121cf2007-01-31 12:14:08 -0500186 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400187 for (j = 0; j < count && j < 128; j += 4) {
188 if (!(j & 31)) {
189 if (j)
190 dprintk("\n");
191 dprintk("0x%04x ", j);
192 }
193 dprintk("%02x%02x%02x%02x ",
194 buf[j], buf[j+1], buf[j+2], buf[j+3]);
195 }
196 dprintk("\n");
197}
198#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400199static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400200{
201 /* NOP */
202}
203#endif
204
Chuck Leverffc2e512006-12-05 16:35:11 -0500205struct sock_xprt {
206 struct rpc_xprt xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500207
208 /*
209 * Network layer
210 */
211 struct socket * sock;
212 struct sock * inet;
Chuck Lever51971132006-12-05 16:35:19 -0500213
214 /*
215 * State of TCP reply receive
216 */
217 __be32 tcp_fraghdr,
Trond Myklebustb76ce562010-06-16 13:57:32 -0400218 tcp_xid,
219 tcp_calldir;
Chuck Lever51971132006-12-05 16:35:19 -0500220
221 u32 tcp_offset,
222 tcp_reclen;
223
224 unsigned long tcp_copied,
225 tcp_flags;
Chuck Leverc8475462006-12-05 16:35:26 -0500226
227 /*
228 * Connection of transports
229 */
Trond Myklebust34161db2006-12-07 15:48:15 -0500230 struct delayed_work connect_worker;
Chuck Leverfbfffbd2009-08-09 15:09:46 -0400231 struct sockaddr_storage srcaddr;
232 unsigned short srcport;
Chuck Lever7c6e0662006-12-05 16:35:30 -0500233
234 /*
235 * UDP socket buffer size parameters
236 */
237 size_t rcvsize,
238 sndsize;
Chuck Lever314dfd72006-12-05 16:35:34 -0500239
240 /*
241 * Saved socket callback addresses
242 */
243 void (*old_data_ready)(struct sock *, int);
244 void (*old_state_change)(struct sock *);
245 void (*old_write_space)(struct sock *);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400246 void (*old_error_report)(struct sock *);
Chuck Leverffc2e512006-12-05 16:35:11 -0500247};
248
Chuck Levere136d092006-12-05 16:35:23 -0500249/*
250 * TCP receive state flags
251 */
252#define TCP_RCV_LAST_FRAG (1UL << 0)
253#define TCP_RCV_COPY_FRAGHDR (1UL << 1)
254#define TCP_RCV_COPY_XID (1UL << 2)
255#define TCP_RCV_COPY_DATA (1UL << 3)
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400256#define TCP_RCV_READ_CALLDIR (1UL << 4)
257#define TCP_RCV_COPY_CALLDIR (1UL << 5)
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400258
259/*
260 * TCP RPC flags
261 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400262#define TCP_RPC_REPLY (1UL << 6)
Chuck Levere136d092006-12-05 16:35:23 -0500263
Chuck Lever95392c52007-08-06 11:57:58 -0400264static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400265{
Chuck Lever95392c52007-08-06 11:57:58 -0400266 return (struct sockaddr *) &xprt->addr;
267}
268
Chuck Lever176e21e2011-05-09 15:22:44 -0400269static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
270{
271 return (struct sockaddr_un *) &xprt->addr;
272}
273
Chuck Lever95392c52007-08-06 11:57:58 -0400274static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
275{
276 return (struct sockaddr_in *) &xprt->addr;
277}
278
279static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
280{
281 return (struct sockaddr_in6 *) &xprt->addr;
282}
283
Chuck Leverc877b842009-08-09 15:09:36 -0400284static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400285{
Chuck Leverc877b842009-08-09 15:09:36 -0400286 struct sockaddr *sap = xs_addr(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400287 struct sockaddr_in6 *sin6;
288 struct sockaddr_in *sin;
Chuck Lever176e21e2011-05-09 15:22:44 -0400289 struct sockaddr_un *sun;
Chuck Leverc877b842009-08-09 15:09:36 -0400290 char buf[128];
Chuck Leveredb267a2006-08-22 20:06:18 -0400291
Chuck Lever9dc3b092009-08-09 15:09:46 -0400292 switch (sap->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400293 case AF_LOCAL:
294 sun = xs_addr_un(xprt);
295 strlcpy(buf, sun->sun_path, sizeof(buf));
296 xprt->address_strings[RPC_DISPLAY_ADDR] =
297 kstrdup(buf, GFP_KERNEL);
298 break;
Chuck Lever9dc3b092009-08-09 15:09:46 -0400299 case AF_INET:
Chuck Lever176e21e2011-05-09 15:22:44 -0400300 (void)rpc_ntop(sap, buf, sizeof(buf));
301 xprt->address_strings[RPC_DISPLAY_ADDR] =
302 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400303 sin = xs_addr_in(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800304 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
Chuck Lever9dc3b092009-08-09 15:09:46 -0400305 break;
306 case AF_INET6:
Chuck Lever176e21e2011-05-09 15:22:44 -0400307 (void)rpc_ntop(sap, buf, sizeof(buf));
308 xprt->address_strings[RPC_DISPLAY_ADDR] =
309 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400310 sin6 = xs_addr_in6(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800311 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400312 break;
313 default:
314 BUG();
Chuck Leveredb267a2006-08-22 20:06:18 -0400315 }
Chuck Lever176e21e2011-05-09 15:22:44 -0400316
Chuck Lever9dc3b092009-08-09 15:09:46 -0400317 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
Chuck Leveredb267a2006-08-22 20:06:18 -0400318}
319
Chuck Lever9dc3b092009-08-09 15:09:46 -0400320static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400321{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400322 struct sockaddr *sap = xs_addr(xprt);
323 char buf[128];
Chuck Lever4b6473f2007-08-06 11:57:12 -0400324
Joe Perches81160e662010-03-08 12:15:59 -0800325 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400326 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400327
Joe Perches81160e662010-03-08 12:15:59 -0800328 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400329 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
330}
Chuck Lever4b6473f2007-08-06 11:57:12 -0400331
Chuck Lever9dc3b092009-08-09 15:09:46 -0400332static void xs_format_peer_addresses(struct rpc_xprt *xprt,
333 const char *protocol,
334 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400335{
Chuck Leverb454ae92008-01-07 18:34:48 -0500336 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leverb454ae92008-01-07 18:34:48 -0500337 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leverc877b842009-08-09 15:09:36 -0400338 xs_format_common_peer_addresses(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400339 xs_format_common_peer_ports(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400340}
341
Chuck Lever9dc3b092009-08-09 15:09:46 -0400342static void xs_update_peer_port(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400343{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400344 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
345 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400346
Chuck Lever9dc3b092009-08-09 15:09:46 -0400347 xs_format_common_peer_ports(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400348}
349
350static void xs_free_peer_addresses(struct rpc_xprt *xprt)
351{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500352 unsigned int i;
353
354 for (i = 0; i < RPC_DISPLAY_MAX; i++)
355 switch (i) {
356 case RPC_DISPLAY_PROTO:
357 case RPC_DISPLAY_NETID:
358 continue;
359 default:
360 kfree(xprt->address_strings[i]);
361 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400362}
363
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400364#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
365
Trond Myklebust24c56842006-10-17 15:06:22 -0400366static 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 -0400367{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400368 struct msghdr msg = {
369 .msg_name = addr,
370 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400371 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
372 };
373 struct kvec iov = {
374 .iov_base = vec->iov_base + base,
375 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400376 };
377
Trond Myklebust24c56842006-10-17 15:06:22 -0400378 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400379 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
380 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
381}
382
Trond Myklebust24c56842006-10-17 15:06:22 -0400383static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400384{
Trond Myklebust24c56842006-10-17 15:06:22 -0400385 struct page **ppage;
386 unsigned int remainder;
387 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400388
Trond Myklebust24c56842006-10-17 15:06:22 -0400389 remainder = xdr->page_len - base;
390 base += xdr->page_base;
391 ppage = xdr->pages + (base >> PAGE_SHIFT);
392 base &= ~PAGE_MASK;
393 for(;;) {
394 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
395 int flags = XS_SENDMSG_FLAGS;
396
397 remainder -= len;
398 if (remainder != 0 || more)
399 flags |= MSG_MORE;
400 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
401 if (remainder == 0 || err != len)
402 break;
403 sent += err;
404 ppage++;
405 base = 0;
406 }
407 if (sent == 0)
408 return err;
409 if (err > 0)
410 sent += err;
411 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400412}
413
Chuck Lever9903cd12005-08-11 16:25:26 -0400414/**
415 * xs_sendpages - write pages directly to a socket
416 * @sock: socket to send on
417 * @addr: UDP only -- address of destination
418 * @addrlen: UDP only -- length of destination address
419 * @xdr: buffer containing this request
420 * @base: starting position in the buffer
421 *
Chuck Levera246b012005-08-11 16:25:23 -0400422 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400423static 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 -0400424{
Trond Myklebust24c56842006-10-17 15:06:22 -0400425 unsigned int remainder = xdr->len - base;
426 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400427
Chuck Lever262965f2005-08-11 16:25:56 -0400428 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400429 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400430
431 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400432 if (base != 0) {
433 addr = NULL;
434 addrlen = 0;
435 }
Chuck Lever262965f2005-08-11 16:25:56 -0400436
Trond Myklebust24c56842006-10-17 15:06:22 -0400437 if (base < xdr->head[0].iov_len || addr != NULL) {
438 unsigned int len = xdr->head[0].iov_len - base;
439 remainder -= len;
440 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
441 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400442 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400443 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400444 base = 0;
445 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400446 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400447
Trond Myklebust24c56842006-10-17 15:06:22 -0400448 if (base < xdr->page_len) {
449 unsigned int len = xdr->page_len - base;
450 remainder -= len;
451 err = xs_send_pagedata(sock, xdr, 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;
Trond Myklebust24c56842006-10-17 15:06:22 -0400456 } else
457 base -= xdr->page_len;
458
459 if (base >= xdr->tail[0].iov_len)
460 return sent;
461 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400462out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400463 if (sent == 0)
464 return err;
465 if (err > 0)
466 sent += err;
467 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400468}
469
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400470static void xs_nospace_callback(struct rpc_task *task)
471{
472 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
473
474 transport->inet->sk_write_pending--;
475 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
476}
477
Chuck Lever9903cd12005-08-11 16:25:26 -0400478/**
Chuck Lever262965f2005-08-11 16:25:56 -0400479 * xs_nospace - place task on wait queue if transmit was incomplete
480 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400481 *
Chuck Levera246b012005-08-11 16:25:23 -0400482 */
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400483static int xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400484{
Chuck Lever262965f2005-08-11 16:25:56 -0400485 struct rpc_rqst *req = task->tk_rqstp;
486 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500487 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400488 int ret = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400489
Chuck Lever46121cf2007-01-31 12:14:08 -0500490 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400491 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
492 req->rq_slen);
493
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400494 /* Protect against races with write_space */
495 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400496
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400497 /* Don't race with disconnect */
498 if (xprt_connected(xprt)) {
499 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400500 ret = -EAGAIN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400501 /*
502 * Notify TCP that we're limited by the application
503 * window size
504 */
505 set_bit(SOCK_NOSPACE, &transport->sock->flags);
506 transport->inet->sk_write_pending++;
507 /* ...and wait for more buffer space */
508 xprt_wait_for_buffer_space(task, xs_nospace_callback);
509 }
510 } else {
511 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400512 ret = -ENOTCONN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400513 }
Chuck Lever262965f2005-08-11 16:25:56 -0400514
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400515 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400516 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400517}
518
Chuck Lever61677ee2011-05-09 15:22:34 -0400519/*
520 * Construct a stream transport record marker in @buf.
521 */
522static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
523{
524 u32 reclen = buf->len - sizeof(rpc_fraghdr);
525 rpc_fraghdr *base = buf->head[0].iov_base;
526 *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
527}
528
Chuck Lever262965f2005-08-11 16:25:56 -0400529/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400530 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
531 * @task: RPC task that manages the state of an RPC request
532 *
533 * Return values:
534 * 0: The request has been sent
535 * EAGAIN: The socket was blocked, please call again later to
536 * complete the request
537 * ENOTCONN: Caller needs to invoke connect logic then call again
538 * other: Some other error occured, the request was not sent
539 */
540static int xs_local_send_request(struct rpc_task *task)
541{
542 struct rpc_rqst *req = task->tk_rqstp;
543 struct rpc_xprt *xprt = req->rq_xprt;
544 struct sock_xprt *transport =
545 container_of(xprt, struct sock_xprt, xprt);
546 struct xdr_buf *xdr = &req->rq_snd_buf;
547 int status;
548
549 xs_encode_stream_record_marker(&req->rq_snd_buf);
550
551 xs_pktdump("packet data:",
552 req->rq_svec->iov_base, req->rq_svec->iov_len);
553
554 status = xs_sendpages(transport->sock, NULL, 0,
555 xdr, req->rq_bytes_sent);
556 dprintk("RPC: %s(%u) = %d\n",
557 __func__, xdr->len - req->rq_bytes_sent, status);
558 if (likely(status >= 0)) {
559 req->rq_bytes_sent += status;
560 req->rq_xmit_bytes_sent += status;
561 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
562 req->rq_bytes_sent = 0;
563 return 0;
564 }
565 status = -EAGAIN;
566 }
567
568 switch (status) {
569 case -EAGAIN:
570 status = xs_nospace(task);
571 break;
572 default:
573 dprintk("RPC: sendmsg returned unrecognized error %d\n",
574 -status);
575 case -EPIPE:
576 xs_close(xprt);
577 status = -ENOTCONN;
578 }
579
580 return status;
581}
582
583/**
Chuck Lever262965f2005-08-11 16:25:56 -0400584 * xs_udp_send_request - write an RPC request to a UDP socket
585 * @task: address of RPC task that manages the state of an RPC request
586 *
587 * Return values:
588 * 0: The request has been sent
589 * EAGAIN: The socket was blocked, please call again later to
590 * complete the request
591 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300592 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -0400593 */
594static int xs_udp_send_request(struct rpc_task *task)
595{
596 struct rpc_rqst *req = task->tk_rqstp;
597 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500598 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400599 struct xdr_buf *xdr = &req->rq_snd_buf;
600 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400601
Chuck Lever9903cd12005-08-11 16:25:26 -0400602 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400603 req->rq_svec->iov_base,
604 req->rq_svec->iov_len);
605
Trond Myklebust01d37c42009-03-11 14:09:39 -0400606 if (!xprt_bound(xprt))
607 return -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500608 status = xs_sendpages(transport->sock,
Chuck Lever95392c52007-08-06 11:57:58 -0400609 xs_addr(xprt),
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500610 xprt->addrlen, xdr,
611 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400612
Chuck Lever46121cf2007-01-31 12:14:08 -0500613 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400614 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400615
Trond Myklebust21997002007-10-01 11:43:37 -0400616 if (status >= 0) {
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400617 req->rq_xmit_bytes_sent += status;
Trond Myklebust21997002007-10-01 11:43:37 -0400618 if (status >= req->rq_slen)
619 return 0;
620 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400621 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400622 }
Chuck Levera246b012005-08-11 16:25:23 -0400623
Chuck Lever262965f2005-08-11 16:25:56 -0400624 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400625 case -ENOTSOCK:
626 status = -ENOTCONN;
627 /* Should we call xs_close() here? */
628 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400629 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400630 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400631 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400632 default:
633 dprintk("RPC: sendmsg returned unrecognized error %d\n",
634 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400635 case -ENETUNREACH:
636 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400637 case -ECONNREFUSED:
638 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400639 * prompts ECONNREFUSED. */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400640 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400641 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800642
Chuck Lever262965f2005-08-11 16:25:56 -0400643 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400644}
645
Trond Myklebuste06799f2007-11-05 15:44:12 -0500646/**
647 * xs_tcp_shutdown - gracefully shut down a TCP socket
648 * @xprt: transport
649 *
650 * Initiates a graceful shutdown of the TCP socket by calling the
651 * equivalent of shutdown(SHUT_WR);
652 */
653static void xs_tcp_shutdown(struct rpc_xprt *xprt)
654{
655 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
656 struct socket *sock = transport->sock;
657
658 if (sock != NULL)
659 kernel_sock_shutdown(sock, SHUT_WR);
660}
661
Chuck Lever9903cd12005-08-11 16:25:26 -0400662/**
Chuck Lever262965f2005-08-11 16:25:56 -0400663 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400664 * @task: address of RPC task that manages the state of an RPC request
665 *
666 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400667 * 0: The request has been sent
668 * EAGAIN: The socket was blocked, please call again later to
669 * complete the request
670 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300671 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400672 *
673 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400674 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400675 */
Chuck Lever262965f2005-08-11 16:25:56 -0400676static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400677{
678 struct rpc_rqst *req = task->tk_rqstp;
679 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500680 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400681 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Leverb595bb12007-08-06 11:56:42 -0400682 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400683
Chuck Lever61677ee2011-05-09 15:22:34 -0400684 xs_encode_stream_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400685
Chuck Lever262965f2005-08-11 16:25:56 -0400686 xs_pktdump("packet data:",
687 req->rq_svec->iov_base,
688 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400689
690 /* Continue transmitting the packet/record. We must be careful
691 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400692 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400693 while (1) {
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500694 status = xs_sendpages(transport->sock,
695 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400696
Chuck Lever46121cf2007-01-31 12:14:08 -0500697 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400698 xdr->len - req->rq_bytes_sent, status);
699
700 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400701 break;
702
Chuck Lever262965f2005-08-11 16:25:56 -0400703 /* If we've sent the entire packet, immediately
704 * reset the count of bytes sent. */
705 req->rq_bytes_sent += status;
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400706 req->rq_xmit_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400707 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
708 req->rq_bytes_sent = 0;
709 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400710 }
711
Trond Myklebust06b4b682008-04-16 16:51:38 -0400712 if (status != 0)
713 continue;
Chuck Levera246b012005-08-11 16:25:23 -0400714 status = -EAGAIN;
Trond Myklebust06b4b682008-04-16 16:51:38 -0400715 break;
Chuck Levera246b012005-08-11 16:25:23 -0400716 }
717
Chuck Lever262965f2005-08-11 16:25:56 -0400718 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400719 case -ENOTSOCK:
720 status = -ENOTCONN;
721 /* Should we call xs_close() here? */
722 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400723 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400724 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400725 break;
726 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500727 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400728 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400729 case -ECONNRESET:
Trond Myklebust55420c22009-03-11 15:29:24 -0400730 case -EPIPE:
Trond Myklebuste06799f2007-11-05 15:44:12 -0500731 xs_tcp_shutdown(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400732 case -ECONNREFUSED:
733 case -ENOTCONN:
Chuck Lever262965f2005-08-11 16:25:56 -0400734 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400735 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800736
Chuck Levera246b012005-08-11 16:25:23 -0400737 return status;
738}
739
Chuck Lever9903cd12005-08-11 16:25:26 -0400740/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400741 * xs_tcp_release_xprt - clean up after a tcp transmission
742 * @xprt: transport
743 * @task: rpc task
744 *
745 * This cleans up if an error causes us to abort the transmission of a request.
746 * In this case, the socket may need to be reset in order to avoid confusing
747 * the server.
748 */
749static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
750{
751 struct rpc_rqst *req;
752
753 if (task != xprt->snd_task)
754 return;
755 if (task == NULL)
756 goto out_release;
757 req = task->tk_rqstp;
758 if (req->rq_bytes_sent == 0)
759 goto out_release;
760 if (req->rq_bytes_sent == req->rq_snd_buf.len)
761 goto out_release;
762 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
763out_release:
764 xprt_release_xprt(xprt, task);
765}
766
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400767static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
768{
769 transport->old_data_ready = sk->sk_data_ready;
770 transport->old_state_change = sk->sk_state_change;
771 transport->old_write_space = sk->sk_write_space;
772 transport->old_error_report = sk->sk_error_report;
773}
774
775static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
776{
777 sk->sk_data_ready = transport->old_data_ready;
778 sk->sk_state_change = transport->old_state_change;
779 sk->sk_write_space = transport->old_write_space;
780 sk->sk_error_report = transport->old_error_report;
781}
782
Chuck Leverfe315e72009-03-11 14:10:21 -0400783static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400784{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500785 struct socket *sock = transport->sock;
786 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400787
Chuck Leverfe315e72009-03-11 14:10:21 -0400788 if (sk == NULL)
789 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400790
Trond Myklebust246408d2011-03-22 18:40:10 -0400791 transport->srcport = 0;
792
Chuck Levera246b012005-08-11 16:25:23 -0400793 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500794 transport->inet = NULL;
795 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400796
Chuck Lever9903cd12005-08-11 16:25:26 -0400797 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400798
799 xs_restore_old_callbacks(transport, sk);
Chuck Levera246b012005-08-11 16:25:23 -0400800 write_unlock_bh(&sk->sk_callback_lock);
801
Chuck Lever9903cd12005-08-11 16:25:26 -0400802 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400803
804 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -0400805}
806
807/**
808 * xs_close - close a socket
809 * @xprt: transport
810 *
811 * This is used when all requests are complete; ie, no DRC state remains
812 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400813 *
814 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
815 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e72009-03-11 14:10:21 -0400816 */
817static void xs_close(struct rpc_xprt *xprt)
818{
819 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
820
821 dprintk("RPC: xs_close xprt %p\n", xprt);
822
823 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -0400824 xprt->reestablish_timeout = 0;
Chuck Leverfe315e72009-03-11 14:10:21 -0400825
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100826 smp_mb__before_clear_bit();
Trond Myklebust7d1e8252009-03-11 14:38:03 -0400827 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100828 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -0500829 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100830 smp_mb__after_clear_bit();
Trond Myklebust62da3b22007-11-06 18:44:20 -0500831 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400832}
833
Trond Myklebustf75e6742009-04-21 17:18:20 -0400834static void xs_tcp_close(struct rpc_xprt *xprt)
835{
836 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
837 xs_close(xprt);
838 else
839 xs_tcp_shutdown(xprt);
840}
841
Chuck Lever9903cd12005-08-11 16:25:26 -0400842/**
843 * xs_destroy - prepare to shutdown a transport
844 * @xprt: doomed transport
845 *
846 */
847static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400848{
Chuck Leverc8475462006-12-05 16:35:26 -0500849 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
850
Chuck Lever46121cf2007-01-31 12:14:08 -0500851 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400852
Tejun Heoafe2c512010-12-14 16:21:17 +0100853 cancel_delayed_work_sync(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400854
Chuck Lever9903cd12005-08-11 16:25:26 -0400855 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400856 xs_free_peer_addresses(xprt);
Pavel Emelyanove204e622010-09-29 16:03:13 +0400857 xprt_free(xprt);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -0400858 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400859}
860
Chuck Lever9903cd12005-08-11 16:25:26 -0400861static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400862{
Chuck Lever9903cd12005-08-11 16:25:26 -0400863 return (struct rpc_xprt *) sk->sk_user_data;
864}
865
Chuck Lever176e21e2011-05-09 15:22:44 -0400866static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
867{
868 struct xdr_skb_reader desc = {
869 .skb = skb,
870 .offset = sizeof(rpc_fraghdr),
871 .count = skb->len - sizeof(rpc_fraghdr),
872 };
873
874 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
875 return -1;
876 if (desc.count)
877 return -1;
878 return 0;
879}
880
881/**
882 * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets
883 * @sk: socket with data to read
884 * @len: how much data to read
885 *
886 * Currently this assumes we can read the whole reply in a single gulp.
887 */
888static void xs_local_data_ready(struct sock *sk, int len)
889{
890 struct rpc_task *task;
891 struct rpc_xprt *xprt;
892 struct rpc_rqst *rovr;
893 struct sk_buff *skb;
894 int err, repsize, copied;
895 u32 _xid;
896 __be32 *xp;
897
898 read_lock_bh(&sk->sk_callback_lock);
899 dprintk("RPC: %s...\n", __func__);
900 xprt = xprt_from_sock(sk);
901 if (xprt == NULL)
902 goto out;
903
904 skb = skb_recv_datagram(sk, 0, 1, &err);
905 if (skb == NULL)
906 goto out;
907
908 if (xprt->shutdown)
909 goto dropit;
910
911 repsize = skb->len - sizeof(rpc_fraghdr);
912 if (repsize < 4) {
913 dprintk("RPC: impossible RPC reply size %d\n", repsize);
914 goto dropit;
915 }
916
917 /* Copy the XID from the skb... */
918 xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
919 if (xp == NULL)
920 goto dropit;
921
922 /* Look up and lock the request corresponding to the given XID */
923 spin_lock(&xprt->transport_lock);
924 rovr = xprt_lookup_rqst(xprt, *xp);
925 if (!rovr)
926 goto out_unlock;
927 task = rovr->rq_task;
928
929 copied = rovr->rq_private_buf.buflen;
930 if (copied > repsize)
931 copied = repsize;
932
933 if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
934 dprintk("RPC: sk_buff copy failed\n");
935 goto out_unlock;
936 }
937
938 xprt_complete_rqst(task, copied);
939
940 out_unlock:
941 spin_unlock(&xprt->transport_lock);
942 dropit:
943 skb_free_datagram(sk, skb);
944 out:
945 read_unlock_bh(&sk->sk_callback_lock);
946}
947
Chuck Lever9903cd12005-08-11 16:25:26 -0400948/**
949 * xs_udp_data_ready - "data ready" callback for UDP sockets
950 * @sk: socket with data to read
951 * @len: how much data to read
952 *
953 */
954static void xs_udp_data_ready(struct sock *sk, int len)
955{
956 struct rpc_task *task;
957 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400958 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400959 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400960 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700961 u32 _xid;
962 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400963
Eric Dumazetf064af12010-09-22 12:43:39 +0000964 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500965 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400966 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400967 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400968
969 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
970 goto out;
971
972 if (xprt->shutdown)
973 goto dropit;
974
975 repsize = skb->len - sizeof(struct udphdr);
976 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500977 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400978 goto dropit;
979 }
980
981 /* Copy the XID from the skb... */
982 xp = skb_header_pointer(skb, sizeof(struct udphdr),
983 sizeof(_xid), &_xid);
984 if (xp == NULL)
985 goto dropit;
986
987 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400988 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400989 rovr = xprt_lookup_rqst(xprt, *xp);
990 if (!rovr)
991 goto out_unlock;
992 task = rovr->rq_task;
993
Chuck Levera246b012005-08-11 16:25:23 -0400994 if ((copied = rovr->rq_private_buf.buflen) > repsize)
995 copied = repsize;
996
997 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -0800998 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
999 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -04001000 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001001 }
1002
1003 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -04001004
1005 /* Something worked... */
Eric Dumazetadf30902009-06-02 05:19:30 +00001006 dst_confirm(skb_dst(skb));
Chuck Levera246b012005-08-11 16:25:23 -04001007
Chuck Lever1570c1e2005-08-25 16:25:52 -07001008 xprt_adjust_cwnd(task, copied);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001009 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -04001010
1011 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001012 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001013 dropit:
1014 skb_free_datagram(sk, skb);
1015 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001016 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001017}
1018
Chuck Leverdd456472006-12-05 16:35:44 -05001019static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001020{
Chuck Lever51971132006-12-05 16:35:19 -05001021 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001022 size_t len, used;
1023 char *p;
1024
Chuck Lever51971132006-12-05 16:35:19 -05001025 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
1026 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001027 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001028 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001029 if (used != len)
1030 return;
Chuck Lever808012f2005-08-25 16:25:49 -07001031
Chuck Lever51971132006-12-05 16:35:19 -05001032 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
1033 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -05001034 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -04001035 else
Chuck Levere136d092006-12-05 16:35:23 -05001036 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -05001037 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -07001038
Chuck Levere136d092006-12-05 16:35:23 -05001039 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001040 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -07001041
Chuck Levera246b012005-08-11 16:25:23 -04001042 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001043 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001044 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebust3ebb0672007-11-06 18:40:12 -05001045 xprt_force_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001046 return;
Chuck Levera246b012005-08-11 16:25:23 -04001047 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001048 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -05001049 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001050}
1051
Chuck Lever51971132006-12-05 16:35:19 -05001052static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001053{
Chuck Lever51971132006-12-05 16:35:19 -05001054 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001055 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001056 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001057 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
1058 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1059 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001060 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001061 }
1062 }
1063}
1064
Chuck Leverdd456472006-12-05 16:35:44 -05001065static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001066{
1067 size_t len, used;
1068 char *p;
1069
Chuck Lever51971132006-12-05 16:35:19 -05001070 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -05001071 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001072 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001073 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001074 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001075 if (used != len)
1076 return;
Chuck Levere136d092006-12-05 16:35:23 -05001077 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001078 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -05001079 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001080 dprintk("RPC: reading %s XID %08x\n",
1081 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1082 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -05001083 ntohl(transport->tcp_xid));
1084 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001085}
1086
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001087static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1088 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001089{
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001090 size_t len, used;
1091 u32 offset;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001092 char *p;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001093
1094 /*
1095 * We want transport->tcp_offset to be 8 at the end of this routine
1096 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1097 * When this function is called for the first time,
1098 * transport->tcp_offset is 4 (after having already read the xid).
1099 */
1100 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001101 len = sizeof(transport->tcp_calldir) - offset;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001102 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001103 p = ((char *) &transport->tcp_calldir) + offset;
1104 used = xdr_skb_read_bits(desc, p, len);
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001105 transport->tcp_offset += used;
1106 if (used != len)
1107 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001108 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001109 /*
1110 * We don't yet have the XDR buffer, so we will write the calldir
1111 * out after we get the buffer from the 'struct rpc_rqst'
1112 */
Trond Myklebustb76ce562010-06-16 13:57:32 -04001113 switch (ntohl(transport->tcp_calldir)) {
1114 case RPC_REPLY:
1115 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1116 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001117 transport->tcp_flags |= TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001118 break;
1119 case RPC_CALL:
1120 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1121 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001122 transport->tcp_flags &= ~TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001123 break;
1124 default:
1125 dprintk("RPC: invalid request message type\n");
1126 xprt_force_disconnect(&transport->xprt);
1127 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001128 xs_tcp_check_fraghdr(transport);
1129}
1130
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001131static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
1132 struct xdr_skb_reader *desc,
1133 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001134{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001135 struct sock_xprt *transport =
1136 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001137 struct xdr_buf *rcvbuf;
1138 size_t len;
1139 ssize_t r;
1140
Chuck Levera246b012005-08-11 16:25:23 -04001141 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001142
1143 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1144 /*
1145 * Save the RPC direction in the XDR buffer
1146 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001147 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
Trond Myklebustb76ce562010-06-16 13:57:32 -04001148 &transport->tcp_calldir,
1149 sizeof(transport->tcp_calldir));
1150 transport->tcp_copied += sizeof(transport->tcp_calldir);
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001151 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -04001152 }
1153
Chuck Levera246b012005-08-11 16:25:23 -04001154 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -05001155 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -05001156 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -04001157
Chuck Lever51971132006-12-05 16:35:19 -05001158 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001159 memcpy(&my_desc, desc, sizeof(my_desc));
1160 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -05001161 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001162 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001163 desc->count -= r;
1164 desc->offset += r;
1165 } else
Chuck Lever51971132006-12-05 16:35:19 -05001166 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001167 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001168
1169 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001170 transport->tcp_copied += r;
1171 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001172 }
1173 if (r != len) {
1174 /* Error when copying to the receive buffer,
1175 * usually because we weren't able to allocate
1176 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001177 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001178 * will not receive any additional updates,
1179 * and time out.
1180 * Any remaining data from this record will
1181 * be discarded.
1182 */
Chuck Levere136d092006-12-05 16:35:23 -05001183 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001184 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001185 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001186 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1187 "tcp_offset = %u, tcp_reclen = %u\n",
1188 xprt, transport->tcp_copied,
1189 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001190 return;
Chuck Levera246b012005-08-11 16:25:23 -04001191 }
1192
Chuck Lever46121cf2007-01-31 12:14:08 -05001193 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001194 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001195 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1196 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1197 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001198
Chuck Lever51971132006-12-05 16:35:19 -05001199 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001200 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001201 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001202 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1203 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001204 }
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001205}
1206
1207/*
1208 * Finds the request corresponding to the RPC xid and invokes the common
1209 * tcp read code to read the data.
1210 */
1211static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1212 struct xdr_skb_reader *desc)
1213{
1214 struct sock_xprt *transport =
1215 container_of(xprt, struct sock_xprt, xprt);
1216 struct rpc_rqst *req;
1217
1218 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1219
1220 /* Find and lock the request corresponding to this xid */
1221 spin_lock(&xprt->transport_lock);
1222 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1223 if (!req) {
1224 dprintk("RPC: XID %08x request not found!\n",
1225 ntohl(transport->tcp_xid));
1226 spin_unlock(&xprt->transport_lock);
1227 return -1;
1228 }
1229
1230 xs_tcp_read_common(xprt, desc, req);
1231
Chuck Levere136d092006-12-05 16:35:23 -05001232 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001233 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001234
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001235 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001236 return 0;
1237}
1238
1239#if defined(CONFIG_NFS_V4_1)
1240/*
1241 * Obtains an rpc_rqst previously allocated and invokes the common
1242 * tcp read code to read the data. The result is placed in the callback
1243 * queue.
1244 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1245 * connection and return -1.
1246 */
1247static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1248 struct xdr_skb_reader *desc)
1249{
1250 struct sock_xprt *transport =
1251 container_of(xprt, struct sock_xprt, xprt);
1252 struct rpc_rqst *req;
1253
1254 req = xprt_alloc_bc_request(xprt);
1255 if (req == NULL) {
1256 printk(KERN_WARNING "Callback slot table overflowed\n");
1257 xprt_force_disconnect(xprt);
1258 return -1;
1259 }
1260
1261 req->rq_xid = transport->tcp_xid;
1262 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1263 xs_tcp_read_common(xprt, desc, req);
1264
1265 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1266 struct svc_serv *bc_serv = xprt->bc_serv;
1267
1268 /*
1269 * Add callback request to callback list. The callback
1270 * service sleeps on the sv_cb_waitq waiting for new
1271 * requests. Wake it up after adding enqueing the
1272 * request.
1273 */
1274 dprintk("RPC: add callback request to list\n");
1275 spin_lock(&bc_serv->sv_cb_lock);
1276 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1277 spin_unlock(&bc_serv->sv_cb_lock);
1278 wake_up(&bc_serv->sv_cb_waitq);
1279 }
1280
1281 req->rq_private_buf.len = transport->tcp_copied;
1282
1283 return 0;
1284}
1285
1286static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1287 struct xdr_skb_reader *desc)
1288{
1289 struct sock_xprt *transport =
1290 container_of(xprt, struct sock_xprt, xprt);
1291
1292 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1293 xs_tcp_read_reply(xprt, desc) :
1294 xs_tcp_read_callback(xprt, desc);
1295}
1296#else
1297static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1298 struct xdr_skb_reader *desc)
1299{
1300 return xs_tcp_read_reply(xprt, desc);
1301}
1302#endif /* CONFIG_NFS_V4_1 */
1303
1304/*
1305 * Read data off the transport. This can be either an RPC_CALL or an
1306 * RPC_REPLY. Relay the processing to helper functions.
1307 */
1308static void xs_tcp_read_data(struct rpc_xprt *xprt,
1309 struct xdr_skb_reader *desc)
1310{
1311 struct sock_xprt *transport =
1312 container_of(xprt, struct sock_xprt, xprt);
1313
1314 if (_xs_tcp_read_data(xprt, desc) == 0)
1315 xs_tcp_check_fraghdr(transport);
1316 else {
1317 /*
1318 * The transport_lock protects the request handling.
1319 * There's no need to hold it to update the tcp_flags.
1320 */
1321 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1322 }
Chuck Levera246b012005-08-11 16:25:23 -04001323}
1324
Chuck Leverdd456472006-12-05 16:35:44 -05001325static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001326{
1327 size_t len;
1328
Chuck Lever51971132006-12-05 16:35:19 -05001329 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001330 if (len > desc->count)
1331 len = desc->count;
1332 desc->count -= len;
1333 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001334 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001335 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001336 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001337}
1338
Chuck Lever9903cd12005-08-11 16:25:26 -04001339static 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 -04001340{
1341 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001342 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001343 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001344 .skb = skb,
1345 .offset = offset,
1346 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001347 };
Chuck Levera246b012005-08-11 16:25:23 -04001348
Chuck Lever46121cf2007-01-31 12:14:08 -05001349 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001350 do {
1351 /* Read in a new fragment marker if necessary */
1352 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001353 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001354 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001355 continue;
1356 }
1357 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001358 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001359 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001360 continue;
1361 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001362 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001363 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001364 xs_tcp_read_calldir(transport, &desc);
1365 continue;
1366 }
Chuck Levera246b012005-08-11 16:25:23 -04001367 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001368 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001369 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001370 continue;
1371 }
1372 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001373 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001374 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001375 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001376 return len - desc.count;
1377}
1378
Chuck Lever9903cd12005-08-11 16:25:26 -04001379/**
1380 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1381 * @sk: socket with data to read
1382 * @bytes: how much data to read
1383 *
1384 */
1385static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001386{
1387 struct rpc_xprt *xprt;
1388 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001389 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001390
Chuck Lever46121cf2007-01-31 12:14:08 -05001391 dprintk("RPC: xs_tcp_data_ready...\n");
1392
Eric Dumazetf064af12010-09-22 12:43:39 +00001393 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001394 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001395 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001396 if (xprt->shutdown)
1397 goto out;
1398
Neil Brown61d0a8e2009-09-23 14:36:37 -04001399 /* Any data means we had a useful conversation, so
1400 * the we don't need to delay the next reconnect
1401 */
1402 if (xprt->reestablish_timeout)
1403 xprt->reestablish_timeout = 0;
1404
Chuck Lever9903cd12005-08-11 16:25:26 -04001405 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001406 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001407 do {
1408 rd_desc.count = 65536;
1409 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1410 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001411out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001412 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001413}
1414
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001415/*
1416 * Do the equivalent of linger/linger2 handling for dealing with
1417 * broken servers that don't close the socket in a timely
1418 * fashion
1419 */
1420static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1421 unsigned long timeout)
1422{
1423 struct sock_xprt *transport;
1424
1425 if (xprt_test_and_set_connecting(xprt))
1426 return;
1427 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1428 transport = container_of(xprt, struct sock_xprt, xprt);
1429 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1430 timeout);
1431}
1432
1433static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1434{
1435 struct sock_xprt *transport;
1436
1437 transport = container_of(xprt, struct sock_xprt, xprt);
1438
1439 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1440 !cancel_delayed_work(&transport->connect_worker))
1441 return;
1442 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1443 xprt_clear_connecting(xprt);
1444}
1445
1446static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1447{
1448 smp_mb__before_clear_bit();
1449 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1450 clear_bit(XPRT_CLOSING, &xprt->state);
1451 smp_mb__after_clear_bit();
1452 /* Mark transport as closed and wake up all pending tasks */
1453 xprt_disconnect_done(xprt);
1454}
1455
Chuck Lever9903cd12005-08-11 16:25:26 -04001456/**
1457 * xs_tcp_state_change - callback to handle TCP socket state changes
1458 * @sk: socket whose state has changed
1459 *
1460 */
1461static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001462{
Chuck Lever9903cd12005-08-11 16:25:26 -04001463 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001464
Eric Dumazetf064af12010-09-22 12:43:39 +00001465 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001466 if (!(xprt = xprt_from_sock(sk)))
1467 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001468 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001469 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001470 sk->sk_state, xprt_connected(xprt),
1471 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001472 sock_flag(sk, SOCK_ZAPPED),
1473 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001474
1475 switch (sk->sk_state) {
1476 case TCP_ESTABLISHED:
Eric Dumazetf064af12010-09-22 12:43:39 +00001477 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001478 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001479 struct sock_xprt *transport = container_of(xprt,
1480 struct sock_xprt, xprt);
1481
Chuck Levera246b012005-08-11 16:25:23 -04001482 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001483 transport->tcp_offset = 0;
1484 transport->tcp_reclen = 0;
1485 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001486 transport->tcp_flags =
1487 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001488
Trond Myklebust2a491992009-03-11 14:38:00 -04001489 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001490 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001491 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001492 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001493 case TCP_FIN_WAIT1:
1494 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001495 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001496 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001497 set_bit(XPRT_CLOSING, &xprt->state);
1498 smp_mb__before_clear_bit();
1499 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001500 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001501 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001502 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001503 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001504 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001505 /* The server initiated a shutdown of the socket */
Trond Myklebust66af1e52007-11-06 10:18:36 -05001506 xprt_force_disconnect(xprt);
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001507 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001508 case TCP_CLOSING:
1509 /*
1510 * If the server closed down the connection, make sure that
1511 * we back off before reconnecting
1512 */
1513 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1514 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001515 break;
1516 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001517 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001518 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001519 smp_mb__before_clear_bit();
1520 clear_bit(XPRT_CONNECTED, &xprt->state);
1521 smp_mb__after_clear_bit();
1522 break;
1523 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001524 xs_tcp_cancel_linger_timeout(xprt);
1525 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001526 }
1527 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001528 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001529}
1530
Chuck Lever9903cd12005-08-11 16:25:26 -04001531/**
Trond Myklebust482f32e2009-03-11 14:38:00 -04001532 * xs_error_report - callback mainly for catching socket errors
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001533 * @sk: socket
1534 */
Trond Myklebust482f32e2009-03-11 14:38:00 -04001535static void xs_error_report(struct sock *sk)
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001536{
1537 struct rpc_xprt *xprt;
1538
Eric Dumazetf064af12010-09-22 12:43:39 +00001539 read_lock_bh(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001540 if (!(xprt = xprt_from_sock(sk)))
1541 goto out;
1542 dprintk("RPC: %s client %p...\n"
1543 "RPC: error %d\n",
1544 __func__, xprt, sk->sk_err);
Trond Myklebust482f32e2009-03-11 14:38:00 -04001545 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001546out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001547 read_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001548}
1549
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001550static void xs_write_space(struct sock *sk)
1551{
1552 struct socket *sock;
1553 struct rpc_xprt *xprt;
1554
1555 if (unlikely(!(sock = sk->sk_socket)))
1556 return;
1557 clear_bit(SOCK_NOSPACE, &sock->flags);
1558
1559 if (unlikely(!(xprt = xprt_from_sock(sk))))
1560 return;
1561 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1562 return;
1563
1564 xprt_write_space(xprt);
1565}
1566
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001567/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001568 * xs_udp_write_space - callback invoked when socket buffer space
1569 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001570 * @sk: socket whose state has changed
1571 *
Chuck Levera246b012005-08-11 16:25:23 -04001572 * Called when more output buffer space is available for this socket.
1573 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001574 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001575 * with a bunch of small requests.
1576 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001577static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001578{
Eric Dumazetf064af12010-09-22 12:43:39 +00001579 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001580
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001581 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001582 if (sock_writeable(sk))
1583 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001584
Eric Dumazetf064af12010-09-22 12:43:39 +00001585 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001586}
Chuck Levera246b012005-08-11 16:25:23 -04001587
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001588/**
1589 * xs_tcp_write_space - callback invoked when socket buffer space
1590 * becomes available
1591 * @sk: socket whose state has changed
1592 *
1593 * Called when more output buffer space is available for this socket.
1594 * We try not to wake our writers until they can make "significant"
1595 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1596 * with a bunch of small requests.
1597 */
1598static void xs_tcp_write_space(struct sock *sk)
1599{
Eric Dumazetf064af12010-09-22 12:43:39 +00001600 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001601
1602 /* from net/core/stream.c:sk_stream_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001603 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1604 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001605
Eric Dumazetf064af12010-09-22 12:43:39 +00001606 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001607}
1608
Chuck Lever470056c2005-08-25 16:25:56 -07001609static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001610{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001611 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1612 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001613
Chuck Lever7c6e0662006-12-05 16:35:30 -05001614 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001615 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001616 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001617 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001618 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001619 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001620 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001621 sk->sk_write_space(sk);
1622 }
1623}
1624
Chuck Lever43118c22005-08-25 16:25:49 -07001625/**
Chuck Lever470056c2005-08-25 16:25:56 -07001626 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001627 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001628 * @sndsize: requested size of send buffer, in bytes
1629 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001630 *
Chuck Lever470056c2005-08-25 16:25:56 -07001631 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001632 */
Chuck Lever470056c2005-08-25 16:25:56 -07001633static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001634{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001635 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1636
1637 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001638 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001639 transport->sndsize = sndsize + 1024;
1640 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001641 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001642 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001643
1644 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001645}
1646
Chuck Lever46c0ee82005-08-25 16:25:52 -07001647/**
1648 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1649 * @task: task that timed out
1650 *
1651 * Adjust the congestion window after a retransmit timeout has occurred.
1652 */
1653static void xs_udp_timer(struct rpc_task *task)
1654{
1655 xprt_adjust_cwnd(task, -ETIMEDOUT);
1656}
1657
Chuck Leverb85d8802006-05-25 01:40:49 -04001658static unsigned short xs_get_random_port(void)
1659{
1660 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1661 unsigned short rand = (unsigned short) net_random() % range;
1662 return rand + xprt_min_resvport;
1663}
1664
Chuck Lever92200412006-01-03 09:55:51 +01001665/**
1666 * xs_set_port - reset the port number in the remote endpoint address
1667 * @xprt: generic transport
1668 * @port: new port number
1669 *
1670 */
1671static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1672{
Chuck Lever46121cf2007-01-31 12:14:08 -05001673 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001674
Chuck Lever9dc3b092009-08-09 15:09:46 -04001675 rpc_set_port(xs_addr(xprt), port);
1676 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001677}
1678
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001679static unsigned short xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001680{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001681 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001682
1683 if (port == 0 && transport->xprt.resvport)
1684 port = xs_get_random_port();
1685 return port;
1686}
1687
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001688static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001689{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001690 if (transport->srcport != 0)
1691 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001692 if (!transport->xprt.resvport)
1693 return 0;
1694 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1695 return xprt_max_resvport;
1696 return --port;
1697}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001698static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001699{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001700 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001701 int err, nloop = 0;
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001702 unsigned short port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001703 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001704
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001705 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001706 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001707 rpc_set_port((struct sockaddr *)&myaddr, port);
1708 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1709 transport->xprt.addrlen);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001710 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001711 break;
Chuck Levera246b012005-08-11 16:25:23 -04001712 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001713 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001714 break;
Chuck Levera246b012005-08-11 16:25:23 -04001715 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001716 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001717 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001718 if (port > last)
1719 nloop++;
1720 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001721
Chuck Lever4232e862010-10-20 11:52:51 -04001722 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001723 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1724 &((struct sockaddr_in *)&myaddr)->sin_addr,
1725 port, err ? "failed" : "ok", err);
1726 else
1727 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1728 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1729 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001730 return err;
1731}
1732
Chuck Lever176e21e2011-05-09 15:22:44 -04001733/*
1734 * We don't support autobind on AF_LOCAL sockets
1735 */
1736static void xs_local_rpcbind(struct rpc_task *task)
1737{
1738 xprt_set_bound(task->tk_xprt);
1739}
1740
1741static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1742{
1743}
Chuck Levera246b012005-08-11 16:25:23 -04001744
Peter Zijlstraed075362006-12-06 20:35:24 -08001745#ifdef CONFIG_DEBUG_LOCK_ALLOC
1746static struct lock_class_key xs_key[2];
1747static struct lock_class_key xs_slock_key[2];
1748
Chuck Lever176e21e2011-05-09 15:22:44 -04001749static inline void xs_reclassify_socketu(struct socket *sock)
1750{
1751 struct sock *sk = sock->sk;
1752
1753 BUG_ON(sock_owned_by_user(sk));
1754 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1755 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1756}
1757
Chuck Lever8945ee52007-08-06 11:58:04 -04001758static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001759{
1760 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001761
John Heffner02b3d342007-09-12 10:42:12 +02001762 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001763 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1764 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1765}
Peter Zijlstraed075362006-12-06 20:35:24 -08001766
Chuck Lever8945ee52007-08-06 11:58:04 -04001767static inline void xs_reclassify_socket6(struct socket *sock)
1768{
1769 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001770
Linus Torvaldsf4921af2007-10-15 10:46:05 -07001771 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001772 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1773 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001774}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001775
1776static inline void xs_reclassify_socket(int family, struct socket *sock)
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;
1849 sk->sk_error_report = xs_error_report;
1850 sk->sk_allocation = GFP_ATOMIC;
1851
1852 xprt_clear_connected(xprt);
1853
1854 /* Reset to new socket */
1855 transport->sock = sock;
1856 transport->inet = sk;
1857
1858 write_unlock_bh(&sk->sk_callback_lock);
1859 }
1860
1861 /* Tell the socket layer to start connecting... */
1862 xprt->stat.connect_count++;
1863 xprt->stat.connect_start = jiffies;
1864 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1865}
1866
1867/**
1868 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1869 * @xprt: RPC transport to connect
1870 * @transport: socket transport to connect
1871 * @create_sock: function to create a socket of the correct type
1872 *
1873 * Invoked by a work queue tasklet.
1874 */
1875static void xs_local_setup_socket(struct work_struct *work)
1876{
1877 struct sock_xprt *transport =
1878 container_of(work, struct sock_xprt, connect_worker.work);
1879 struct rpc_xprt *xprt = &transport->xprt;
1880 struct socket *sock;
1881 int status = -EIO;
1882
1883 if (xprt->shutdown)
1884 goto out;
1885
1886 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1887 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1888 SOCK_STREAM, 0, &sock, 1);
1889 if (status < 0) {
1890 dprintk("RPC: can't create AF_LOCAL "
1891 "transport socket (%d).\n", -status);
1892 goto out;
1893 }
1894 xs_reclassify_socketu(sock);
1895
1896 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1897 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1898
1899 status = xs_local_finish_connecting(xprt, sock);
1900 switch (status) {
1901 case 0:
1902 dprintk("RPC: xprt %p connected to %s\n",
1903 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1904 xprt_set_connected(xprt);
1905 break;
1906 case -ENOENT:
1907 dprintk("RPC: xprt %p: socket %s does not exist\n",
1908 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1909 break;
1910 default:
1911 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1912 __func__, -status,
1913 xprt->address_strings[RPC_DISPLAY_ADDR]);
1914 }
1915
1916out:
1917 xprt_clear_connecting(xprt);
1918 xprt_wake_pending_tasks(xprt, status);
1919}
1920
Chuck Lever16be2d22007-08-06 11:57:38 -04001921static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001922{
Chuck Lever16be2d22007-08-06 11:57:38 -04001923 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001924
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001925 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001926 struct sock *sk = sock->sk;
1927
1928 write_lock_bh(&sk->sk_callback_lock);
1929
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001930 xs_save_old_callbacks(transport, sk);
1931
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001932 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001933 sk->sk_data_ready = xs_udp_data_ready;
1934 sk->sk_write_space = xs_udp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001935 sk->sk_error_report = xs_error_report;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001936 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001937 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001938
1939 xprt_set_connected(xprt);
1940
1941 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001942 transport->sock = sock;
1943 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001944
1945 write_unlock_bh(&sk->sk_callback_lock);
1946 }
Chuck Lever470056c2005-08-25 16:25:56 -07001947 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04001948}
1949
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04001950static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04001951{
1952 struct sock_xprt *transport =
1953 container_of(work, struct sock_xprt, connect_worker.work);
1954 struct rpc_xprt *xprt = &transport->xprt;
1955 struct socket *sock = transport->sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04001956 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04001957
Trond Myklebust01d37c42009-03-11 14:09:39 -04001958 if (xprt->shutdown)
Chuck Lever9903cd12005-08-11 16:25:26 -04001959 goto out;
1960
Chuck Levera246b012005-08-11 16:25:23 -04001961 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001962 xs_reset_transport(transport);
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04001963 sock = xs_create_sock(xprt, transport,
1964 xs_addr(xprt)->sa_family, SOCK_DGRAM, IPPROTO_UDP);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04001965 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04001966 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04001967
Chuck Leverc740eff2009-08-09 15:09:46 -04001968 dprintk("RPC: worker connecting xprt %p via %s to "
1969 "%s (port %s)\n", xprt,
1970 xprt->address_strings[RPC_DISPLAY_PROTO],
1971 xprt->address_strings[RPC_DISPLAY_ADDR],
1972 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04001973
1974 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001975 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001976out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001977 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001978 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001979}
1980
Chuck Lever3167e122005-08-25 16:25:55 -07001981/*
1982 * We need to preserve the port number so the reply cache on the server can
1983 * find our cached RPC replies when we get around to reconnecting.
1984 */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04001985static void xs_abort_connection(struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07001986{
1987 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07001988 struct sockaddr any;
1989
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04001990 dprintk("RPC: disconnecting xprt %p to reuse port\n", transport);
Chuck Lever3167e122005-08-25 16:25:55 -07001991
1992 /*
1993 * Disconnect the transport socket by doing a connect operation
1994 * with AF_UNSPEC. This should return immediately...
1995 */
1996 memset(&any, 0, sizeof(any));
1997 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001998 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001999 if (!result)
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002000 xs_sock_mark_closed(&transport->xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002001 else
Chuck Lever46121cf2007-01-31 12:14:08 -05002002 dprintk("RPC: AF_UNSPEC connect return code %d\n",
Chuck Lever3167e122005-08-25 16:25:55 -07002003 result);
2004}
2005
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002006static void xs_tcp_reuse_connection(struct sock_xprt *transport)
Trond Myklebust40d25492009-03-11 14:37:58 -04002007{
2008 unsigned int state = transport->inet->sk_state;
2009
Andy Chittenden669502f2010-08-10 10:19:53 -04002010 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) {
2011 /* we don't need to abort the connection if the socket
2012 * hasn't undergone a shutdown
2013 */
2014 if (transport->inet->sk_shutdown == 0)
2015 return;
2016 dprintk("RPC: %s: TCP_CLOSEd and sk_shutdown set to %d\n",
2017 __func__, transport->inet->sk_shutdown);
2018 }
2019 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) {
2020 /* we don't need to abort the connection if the socket
2021 * hasn't undergone a shutdown
2022 */
2023 if (transport->inet->sk_shutdown == 0)
2024 return;
2025 dprintk("RPC: %s: ESTABLISHED/SYN_SENT "
2026 "sk_shutdown set to %d\n",
2027 __func__, transport->inet->sk_shutdown);
2028 }
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002029 xs_abort_connection(transport);
Trond Myklebust40d25492009-03-11 14:37:58 -04002030}
2031
Chuck Lever16be2d22007-08-06 11:57:38 -04002032static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002033{
Chuck Lever16be2d22007-08-06 11:57:38 -04002034 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002035 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002036
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002037 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002038 struct sock *sk = sock->sk;
2039
2040 write_lock_bh(&sk->sk_callback_lock);
2041
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002042 xs_save_old_callbacks(transport, sk);
2043
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002044 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002045 sk->sk_data_ready = xs_tcp_data_ready;
2046 sk->sk_state_change = xs_tcp_state_change;
2047 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04002048 sk->sk_error_report = xs_error_report;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05002049 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07002050
2051 /* socket options */
2052 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
2053 sock_reset_flag(sk, SOCK_LINGER);
2054 tcp_sk(sk)->linger2 = 0;
2055 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002056
2057 xprt_clear_connected(xprt);
2058
2059 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002060 transport->sock = sock;
2061 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002062
2063 write_unlock_bh(&sk->sk_callback_lock);
2064 }
2065
Trond Myklebust01d37c42009-03-11 14:09:39 -04002066 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002067 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002068
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002069 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05002070 xprt->stat.connect_count++;
2071 xprt->stat.connect_start = jiffies;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002072 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2073 switch (ret) {
2074 case 0:
2075 case -EINPROGRESS:
2076 /* SYN_SENT! */
2077 xprt->connect_cookie++;
2078 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2079 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2080 }
2081out:
2082 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002083}
2084
2085/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002086 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2087 * @xprt: RPC transport to connect
2088 * @transport: socket transport to connect
2089 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04002090 *
2091 * Invoked by a work queue tasklet.
2092 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002093static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002094{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002095 struct sock_xprt *transport =
2096 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002097 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002098 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002099 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002100
Trond Myklebust01d37c42009-03-11 14:09:39 -04002101 if (xprt->shutdown)
Chuck Lever16be2d22007-08-06 11:57:38 -04002102 goto out;
2103
2104 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002105 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002106 sock = xs_create_sock(xprt, transport,
2107 xs_addr(xprt)->sa_family, SOCK_STREAM, IPPROTO_TCP);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002108 if (IS_ERR(sock)) {
2109 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002110 goto out;
2111 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002112 } else {
2113 int abort_and_exit;
2114
2115 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
2116 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04002117 /* "close" the socket, preserving the local port */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002118 xs_tcp_reuse_connection(transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04002119
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002120 if (abort_and_exit)
2121 goto out_eagain;
2122 }
2123
Chuck Leverc740eff2009-08-09 15:09:46 -04002124 dprintk("RPC: worker connecting xprt %p via %s to "
2125 "%s (port %s)\n", xprt,
2126 xprt->address_strings[RPC_DISPLAY_PROTO],
2127 xprt->address_strings[RPC_DISPLAY_ADDR],
2128 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002129
2130 status = xs_tcp_finish_connecting(xprt, sock);
Chuck Lever46121cf2007-01-31 12:14:08 -05002131 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2132 xprt, -status, xprt_connected(xprt),
2133 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002134 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002135 default:
2136 printk("%s: connect returned unhandled error %d\n",
2137 __func__, status);
2138 case -EADDRNOTAVAIL:
2139 /* We're probably in TIME_WAIT. Get rid of existing socket,
2140 * and retry
2141 */
2142 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
2143 xprt_force_disconnect(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002144 break;
Trond Myklebust8a2cec22009-03-11 14:38:01 -04002145 case -ECONNREFUSED:
2146 case -ECONNRESET:
2147 case -ENETUNREACH:
2148 /* retry with existing socket, after a delay */
Trond Myklebust2a491992009-03-11 14:38:00 -04002149 case 0:
2150 case -EINPROGRESS:
2151 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002152 xprt_clear_connecting(xprt);
2153 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002154 case -EINVAL:
2155 /* Happens, for instance, if the user specified a link
2156 * local IPv6 address without a scope-id.
2157 */
2158 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002159 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002160out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04002161 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002162out:
Chuck Lever2226feb2005-08-11 16:25:38 -04002163 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002164 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04002165}
2166
Chuck Lever68e220b2007-08-06 11:57:48 -04002167/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002168 * xs_connect - connect a socket to a remote endpoint
2169 * @task: address of RPC task that manages state of connect request
2170 *
2171 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002172 *
2173 * UDP socket connects are synchronous, but we use a work queue anyway
2174 * to guarantee that even unprivileged user processes can set up a
2175 * socket on a privileged port.
2176 *
2177 * If a UDP socket connect fails, the delay behavior here prevents
2178 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002179 */
2180static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002181{
2182 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002183 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002184
Chuck Lever09a21c42009-12-03 15:58:56 -05002185 if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002186 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2187 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002188 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002189 queue_delayed_work(rpciod_workqueue,
2190 &transport->connect_worker,
2191 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07002192 xprt->reestablish_timeout <<= 1;
Neil Brown61d0a8e2009-09-23 14:36:37 -04002193 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2194 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002195 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2196 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002197 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05002198 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002199 queue_delayed_work(rpciod_workqueue,
2200 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002201 }
2202}
2203
Chuck Lever262ca072006-03-20 13:44:16 -05002204/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002205 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2206 * @xprt: rpc_xprt struct containing statistics
2207 * @seq: output file
2208 *
2209 */
2210static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2211{
2212 long idle_time = 0;
2213
2214 if (xprt_connected(xprt))
2215 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2216
2217 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
2218 "%llu %llu\n",
2219 xprt->stat.bind_count,
2220 xprt->stat.connect_count,
2221 xprt->stat.connect_time,
2222 idle_time,
2223 xprt->stat.sends,
2224 xprt->stat.recvs,
2225 xprt->stat.bad_xids,
2226 xprt->stat.req_u,
2227 xprt->stat.bklog_u);
2228}
2229
2230/**
Chuck Lever262ca072006-03-20 13:44:16 -05002231 * xs_udp_print_stats - display UDP socket-specifc stats
2232 * @xprt: rpc_xprt struct containing statistics
2233 * @seq: output file
2234 *
2235 */
2236static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2237{
Chuck Leverc8475462006-12-05 16:35:26 -05002238 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2239
Chuck Lever262ca072006-03-20 13:44:16 -05002240 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002241 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002242 xprt->stat.bind_count,
2243 xprt->stat.sends,
2244 xprt->stat.recvs,
2245 xprt->stat.bad_xids,
2246 xprt->stat.req_u,
2247 xprt->stat.bklog_u);
2248}
2249
2250/**
2251 * xs_tcp_print_stats - display TCP socket-specifc stats
2252 * @xprt: rpc_xprt struct containing statistics
2253 * @seq: output file
2254 *
2255 */
2256static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2257{
Chuck Leverc8475462006-12-05 16:35:26 -05002258 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002259 long idle_time = 0;
2260
2261 if (xprt_connected(xprt))
2262 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2263
2264 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002265 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002266 xprt->stat.bind_count,
2267 xprt->stat.connect_count,
2268 xprt->stat.connect_time,
2269 idle_time,
2270 xprt->stat.sends,
2271 xprt->stat.recvs,
2272 xprt->stat.bad_xids,
2273 xprt->stat.req_u,
2274 xprt->stat.bklog_u);
2275}
2276
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002277/*
2278 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2279 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2280 * to use the server side send routines.
2281 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002282static void *bc_malloc(struct rpc_task *task, size_t size)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002283{
2284 struct page *page;
2285 struct rpc_buffer *buf;
2286
2287 BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2288 page = alloc_page(GFP_KERNEL);
2289
2290 if (!page)
2291 return NULL;
2292
2293 buf = page_address(page);
2294 buf->len = PAGE_SIZE;
2295
2296 return buf->data;
2297}
2298
2299/*
2300 * Free the space allocated in the bc_alloc routine
2301 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002302static void bc_free(void *buffer)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002303{
2304 struct rpc_buffer *buf;
2305
2306 if (!buffer)
2307 return;
2308
2309 buf = container_of(buffer, struct rpc_buffer, data);
2310 free_page((unsigned long)buf);
2311}
2312
2313/*
2314 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2315 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2316 */
2317static int bc_sendto(struct rpc_rqst *req)
2318{
2319 int len;
2320 struct xdr_buf *xbufp = &req->rq_snd_buf;
2321 struct rpc_xprt *xprt = req->rq_xprt;
2322 struct sock_xprt *transport =
2323 container_of(xprt, struct sock_xprt, xprt);
2324 struct socket *sock = transport->sock;
2325 unsigned long headoff;
2326 unsigned long tailoff;
2327
Chuck Lever61677ee2011-05-09 15:22:34 -04002328 xs_encode_stream_record_marker(xbufp);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002329
2330 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2331 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2332 len = svc_send_common(sock, xbufp,
2333 virt_to_page(xbufp->head[0].iov_base), headoff,
2334 xbufp->tail[0].iov_base, tailoff);
2335
2336 if (len != xbufp->len) {
2337 printk(KERN_NOTICE "Error sending entire callback!\n");
2338 len = -EAGAIN;
2339 }
2340
2341 return len;
2342}
2343
2344/*
2345 * The send routine. Borrows from svc_send
2346 */
2347static int bc_send_request(struct rpc_task *task)
2348{
2349 struct rpc_rqst *req = task->tk_rqstp;
2350 struct svc_xprt *xprt;
2351 struct svc_sock *svsk;
2352 u32 len;
2353
2354 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2355 /*
2356 * Get the server socket associated with this callback xprt
2357 */
2358 xprt = req->rq_xprt->bc_xprt;
2359 svsk = container_of(xprt, struct svc_sock, sk_xprt);
2360
2361 /*
2362 * Grab the mutex to serialize data as the connection is shared
2363 * with the fore channel
2364 */
2365 if (!mutex_trylock(&xprt->xpt_mutex)) {
2366 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2367 if (!mutex_trylock(&xprt->xpt_mutex))
2368 return -EAGAIN;
2369 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2370 }
2371 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2372 len = -ENOTCONN;
2373 else
2374 len = bc_sendto(req);
2375 mutex_unlock(&xprt->xpt_mutex);
2376
2377 if (len > 0)
2378 len = 0;
2379
2380 return len;
2381}
2382
2383/*
2384 * The close routine. Since this is client initiated, we do nothing
2385 */
2386
2387static void bc_close(struct rpc_xprt *xprt)
2388{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002389}
2390
2391/*
2392 * The xprt destroy routine. Again, because this connection is client
2393 * initiated, we do nothing
2394 */
2395
2396static void bc_destroy(struct rpc_xprt *xprt)
2397{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002398}
2399
Chuck Lever176e21e2011-05-09 15:22:44 -04002400static struct rpc_xprt_ops xs_local_ops = {
2401 .reserve_xprt = xprt_reserve_xprt,
2402 .release_xprt = xs_tcp_release_xprt,
2403 .rpcbind = xs_local_rpcbind,
2404 .set_port = xs_local_set_port,
2405 .connect = xs_connect,
2406 .buf_alloc = rpc_malloc,
2407 .buf_free = rpc_free,
2408 .send_request = xs_local_send_request,
2409 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2410 .close = xs_close,
2411 .destroy = xs_destroy,
2412 .print_stats = xs_local_print_stats,
2413};
2414
Chuck Lever262965f2005-08-11 16:25:56 -04002415static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002416 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002417 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002418 .release_xprt = xprt_release_xprt_cong,
Chuck Lever45160d62007-07-01 12:13:17 -04002419 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002420 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002421 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002422 .buf_alloc = rpc_malloc,
2423 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002424 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002425 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002426 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002427 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002428 .close = xs_close,
2429 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002430 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04002431};
2432
2433static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002434 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002435 .release_xprt = xs_tcp_release_xprt,
Chuck Lever45160d62007-07-01 12:13:17 -04002436 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002437 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002438 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002439 .buf_alloc = rpc_malloc,
2440 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002441 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002442 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustf75e6742009-04-21 17:18:20 -04002443 .close = xs_tcp_close,
Chuck Lever9903cd12005-08-11 16:25:26 -04002444 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002445 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002446};
2447
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002448/*
2449 * The rpc_xprt_ops for the server backchannel
2450 */
2451
2452static struct rpc_xprt_ops bc_tcp_ops = {
2453 .reserve_xprt = xprt_reserve_xprt,
2454 .release_xprt = xprt_release_xprt,
2455 .buf_alloc = bc_malloc,
2456 .buf_free = bc_free,
2457 .send_request = bc_send_request,
2458 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2459 .close = bc_close,
2460 .destroy = bc_destroy,
2461 .print_stats = xs_tcp_print_stats,
2462};
2463
Chuck Lever92476852010-10-20 11:53:01 -04002464static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2465{
2466 static const struct sockaddr_in sin = {
2467 .sin_family = AF_INET,
2468 .sin_addr.s_addr = htonl(INADDR_ANY),
2469 };
2470 static const struct sockaddr_in6 sin6 = {
2471 .sin6_family = AF_INET6,
2472 .sin6_addr = IN6ADDR_ANY_INIT,
2473 };
2474
2475 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002476 case AF_LOCAL:
2477 break;
Chuck Lever92476852010-10-20 11:53:01 -04002478 case AF_INET:
2479 memcpy(sap, &sin, sizeof(sin));
2480 break;
2481 case AF_INET6:
2482 memcpy(sap, &sin6, sizeof(sin6));
2483 break;
2484 default:
2485 dprintk("RPC: %s: Bad address family\n", __func__);
2486 return -EAFNOSUPPORT;
2487 }
2488 return 0;
2489}
2490
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002491static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002492 unsigned int slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002493{
2494 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002495 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002496
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002497 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002498 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002499 return ERR_PTR(-EBADF);
2500 }
2501
Pavel Emelyanov37aa2132010-09-29 16:05:43 +04002502 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002503 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002504 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2505 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002506 return ERR_PTR(-ENOMEM);
2507 }
2508
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002509 new = container_of(xprt, struct sock_xprt, xprt);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002510 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2511 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002512 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002513 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002514 else {
2515 int err;
2516 err = xs_init_anyaddr(args->dstaddr->sa_family,
2517 (struct sockaddr *)&new->srcaddr);
2518 if (err != 0)
2519 return ERR_PTR(err);
2520 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002521
2522 return xprt;
2523}
2524
Chuck Lever176e21e2011-05-09 15:22:44 -04002525static const struct rpc_timeout xs_local_default_timeout = {
2526 .to_initval = 10 * HZ,
2527 .to_maxval = 10 * HZ,
2528 .to_retries = 2,
2529};
2530
2531/**
2532 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2533 * @args: rpc transport creation arguments
2534 *
2535 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2536 */
2537static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2538{
2539 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2540 struct sock_xprt *transport;
2541 struct rpc_xprt *xprt;
2542 struct rpc_xprt *ret;
2543
2544 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2545 if (IS_ERR(xprt))
2546 return xprt;
2547 transport = container_of(xprt, struct sock_xprt, xprt);
2548
2549 xprt->prot = 0;
2550 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2551 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2552
2553 xprt->bind_timeout = XS_BIND_TO;
2554 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2555 xprt->idle_timeout = XS_IDLE_DISC_TO;
2556
2557 xprt->ops = &xs_local_ops;
2558 xprt->timeout = &xs_local_default_timeout;
2559
2560 switch (sun->sun_family) {
2561 case AF_LOCAL:
2562 if (sun->sun_path[0] != '/') {
2563 dprintk("RPC: bad AF_LOCAL address: %s\n",
2564 sun->sun_path);
2565 ret = ERR_PTR(-EINVAL);
2566 goto out_err;
2567 }
2568 xprt_set_bound(xprt);
2569 INIT_DELAYED_WORK(&transport->connect_worker,
2570 xs_local_setup_socket);
2571 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
2572 break;
2573 default:
2574 ret = ERR_PTR(-EAFNOSUPPORT);
2575 goto out_err;
2576 }
2577
2578 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2579 xprt->address_strings[RPC_DISPLAY_ADDR]);
2580
2581 if (try_module_get(THIS_MODULE))
2582 return xprt;
2583 ret = ERR_PTR(-EINVAL);
2584out_err:
2585 xprt_free(xprt);
2586 return ret;
2587}
2588
Trond Myklebust2881ae72007-12-20 16:03:54 -05002589static const struct rpc_timeout xs_udp_default_timeout = {
2590 .to_initval = 5 * HZ,
2591 .to_maxval = 30 * HZ,
2592 .to_increment = 5 * HZ,
2593 .to_retries = 5,
2594};
2595
Chuck Lever9903cd12005-08-11 16:25:26 -04002596/**
2597 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002598 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002599 *
2600 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002601static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002602{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002603 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002604 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002605 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002606 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002607
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002608 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002609 if (IS_ERR(xprt))
2610 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002611 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002612
Chuck Leverec739ef2006-08-22 20:06:15 -04002613 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002614 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002615 /* XXX: header size can vary due to auth type, IPv6, etc. */
2616 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2617
Chuck Lever03bf4b72005-08-25 16:25:55 -07002618 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002619 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2620 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002621
Chuck Lever262965f2005-08-11 16:25:56 -04002622 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002623
Trond Myklebustba7392b2007-12-20 16:03:55 -05002624 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002625
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002626 switch (addr->sa_family) {
2627 case AF_INET:
2628 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2629 xprt_set_bound(xprt);
2630
2631 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002632 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002633 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002634 break;
2635 case AF_INET6:
2636 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2637 xprt_set_bound(xprt);
2638
2639 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002640 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002641 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002642 break;
2643 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002644 ret = ERR_PTR(-EAFNOSUPPORT);
2645 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002646 }
2647
Chuck Leverc740eff2009-08-09 15:09:46 -04002648 if (xprt_bound(xprt))
2649 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2650 xprt->address_strings[RPC_DISPLAY_ADDR],
2651 xprt->address_strings[RPC_DISPLAY_PORT],
2652 xprt->address_strings[RPC_DISPLAY_PROTO]);
2653 else
2654 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2655 xprt->address_strings[RPC_DISPLAY_ADDR],
2656 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002657
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002658 if (try_module_get(THIS_MODULE))
2659 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002660 ret = ERR_PTR(-EINVAL);
2661out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002662 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002663 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002664}
2665
Trond Myklebust2881ae72007-12-20 16:03:54 -05002666static const struct rpc_timeout xs_tcp_default_timeout = {
2667 .to_initval = 60 * HZ,
2668 .to_maxval = 60 * HZ,
2669 .to_retries = 2,
2670};
2671
Chuck Lever9903cd12005-08-11 16:25:26 -04002672/**
2673 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002674 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002675 *
2676 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002677static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002678{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002679 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002680 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002681 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002682 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002683
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002684 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002685 if (IS_ERR(xprt))
2686 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002687 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002688
Chuck Leverec739ef2006-08-22 20:06:15 -04002689 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002690 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002691 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002692
Chuck Lever03bf4b72005-08-25 16:25:55 -07002693 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002694 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2695 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002696
Chuck Lever262965f2005-08-11 16:25:56 -04002697 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002698 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002699
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002700 switch (addr->sa_family) {
2701 case AF_INET:
2702 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2703 xprt_set_bound(xprt);
2704
Chuck Lever9dc3b092009-08-09 15:09:46 -04002705 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002706 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002707 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002708 break;
2709 case AF_INET6:
2710 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2711 xprt_set_bound(xprt);
2712
Chuck Lever9dc3b092009-08-09 15:09:46 -04002713 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002714 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002715 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002716 break;
2717 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002718 ret = ERR_PTR(-EAFNOSUPPORT);
2719 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002720 }
2721
Chuck Leverc740eff2009-08-09 15:09:46 -04002722 if (xprt_bound(xprt))
2723 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2724 xprt->address_strings[RPC_DISPLAY_ADDR],
2725 xprt->address_strings[RPC_DISPLAY_PORT],
2726 xprt->address_strings[RPC_DISPLAY_PROTO]);
2727 else
2728 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2729 xprt->address_strings[RPC_DISPLAY_ADDR],
2730 xprt->address_strings[RPC_DISPLAY_PROTO]);
2731
Chuck Leveredb267a2006-08-22 20:06:18 -04002732
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002733 if (try_module_get(THIS_MODULE))
2734 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002735 ret = ERR_PTR(-EINVAL);
2736out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002737 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002738 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002739}
Chuck Lever282b32e2006-12-05 16:35:51 -05002740
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002741/**
2742 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2743 * @args: rpc transport creation arguments
2744 *
2745 */
2746static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2747{
2748 struct sockaddr *addr = args->dstaddr;
2749 struct rpc_xprt *xprt;
2750 struct sock_xprt *transport;
2751 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002752 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002753
J. Bruce Fieldsf0418aa2010-12-08 13:48:19 -05002754 if (args->bc_xprt->xpt_bc_xprt) {
2755 /*
2756 * This server connection already has a backchannel
2757 * export; we can't create a new one, as we wouldn't be
2758 * able to match replies based on xid any more. So,
2759 * reuse the already-existing one:
2760 */
2761 return args->bc_xprt->xpt_bc_xprt;
2762 }
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002763 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2764 if (IS_ERR(xprt))
2765 return xprt;
2766 transport = container_of(xprt, struct sock_xprt, xprt);
2767
2768 xprt->prot = IPPROTO_TCP;
2769 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2770 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2771 xprt->timeout = &xs_tcp_default_timeout;
2772
2773 /* backchannel */
2774 xprt_set_bound(xprt);
2775 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002776 xprt->reestablish_timeout = 0;
2777 xprt->idle_timeout = 0;
2778
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002779 xprt->ops = &bc_tcp_ops;
2780
2781 switch (addr->sa_family) {
2782 case AF_INET:
2783 xs_format_peer_addresses(xprt, "tcp",
2784 RPCBIND_NETID_TCP);
2785 break;
2786 case AF_INET6:
2787 xs_format_peer_addresses(xprt, "tcp",
2788 RPCBIND_NETID_TCP6);
2789 break;
2790 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002791 ret = ERR_PTR(-EAFNOSUPPORT);
2792 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002793 }
2794
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04002795 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2796 xprt->address_strings[RPC_DISPLAY_ADDR],
2797 xprt->address_strings[RPC_DISPLAY_PORT],
2798 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002799
2800 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002801 * Once we've associated a backchannel xprt with a connection,
2802 * we want to keep it around as long as long as the connection
2803 * lasts, in case we need to start using it for a backchannel
2804 * again; this reference won't be dropped until bc_xprt is
2805 * destroyed.
2806 */
2807 xprt_get(xprt);
2808 args->bc_xprt->xpt_bc_xprt = xprt;
2809 xprt->bc_xprt = args->bc_xprt;
2810 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2811 transport->sock = bc_sock->sk_sock;
2812 transport->inet = bc_sock->sk_sk;
2813
2814 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002815 * Since we don't want connections for the backchannel, we set
2816 * the xprt status to connected
2817 */
2818 xprt_set_connected(xprt);
2819
2820
2821 if (try_module_get(THIS_MODULE))
2822 return xprt;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002823 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002824 ret = ERR_PTR(-EINVAL);
2825out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002826 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002827 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002828}
2829
Chuck Lever176e21e2011-05-09 15:22:44 -04002830static struct xprt_class xs_local_transport = {
2831 .list = LIST_HEAD_INIT(xs_local_transport.list),
2832 .name = "named UNIX socket",
2833 .owner = THIS_MODULE,
2834 .ident = XPRT_TRANSPORT_LOCAL,
2835 .setup = xs_setup_local,
2836};
2837
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002838static struct xprt_class xs_udp_transport = {
2839 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2840 .name = "udp",
2841 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002842 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002843 .setup = xs_setup_udp,
2844};
2845
2846static struct xprt_class xs_tcp_transport = {
2847 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2848 .name = "tcp",
2849 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002850 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002851 .setup = xs_setup_tcp,
2852};
2853
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002854static struct xprt_class xs_bc_tcp_transport = {
2855 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2856 .name = "tcp NFSv4.1 backchannel",
2857 .owner = THIS_MODULE,
2858 .ident = XPRT_TRANSPORT_BC_TCP,
2859 .setup = xs_setup_bc_tcp,
2860};
2861
Chuck Lever282b32e2006-12-05 16:35:51 -05002862/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002863 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002864 *
2865 */
2866int init_socket_xprt(void)
2867{
Chuck Leverfbf76682006-12-05 16:35:54 -05002868#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002869 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002870 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002871#endif
2872
Chuck Lever176e21e2011-05-09 15:22:44 -04002873 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002874 xprt_register_transport(&xs_udp_transport);
2875 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002876 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002877
Chuck Lever282b32e2006-12-05 16:35:51 -05002878 return 0;
2879}
2880
2881/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002882 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05002883 *
2884 */
2885void cleanup_socket_xprt(void)
2886{
Chuck Leverfbf76682006-12-05 16:35:54 -05002887#ifdef RPC_DEBUG
2888 if (sunrpc_table_header) {
2889 unregister_sysctl_table(sunrpc_table_header);
2890 sunrpc_table_header = NULL;
2891 }
2892#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002893
Chuck Lever176e21e2011-05-09 15:22:44 -04002894 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002895 xprt_unregister_transport(&xs_udp_transport);
2896 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002897 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05002898}
Trond Myklebustcbf11072009-08-09 15:06:19 -04002899
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002900static int param_set_uint_minmax(const char *val,
2901 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04002902 unsigned int min, unsigned int max)
2903{
2904 unsigned long num;
2905 int ret;
2906
2907 if (!val)
2908 return -EINVAL;
2909 ret = strict_strtoul(val, 0, &num);
2910 if (ret == -EINVAL || num < min || num > max)
2911 return -EINVAL;
2912 *((unsigned int *)kp->arg) = num;
2913 return 0;
2914}
2915
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002916static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04002917{
2918 return param_set_uint_minmax(val, kp,
2919 RPC_MIN_RESVPORT,
2920 RPC_MAX_RESVPORT);
2921}
2922
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002923static struct kernel_param_ops param_ops_portnr = {
2924 .set = param_set_portnr,
2925 .get = param_get_uint,
2926};
2927
Trond Myklebustcbf11072009-08-09 15:06:19 -04002928#define param_check_portnr(name, p) \
2929 __param_check(name, p, unsigned int);
2930
2931module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
2932module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
2933
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002934static int param_set_slot_table_size(const char *val,
2935 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04002936{
2937 return param_set_uint_minmax(val, kp,
2938 RPC_MIN_SLOT_TABLE,
2939 RPC_MAX_SLOT_TABLE);
2940}
2941
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002942static struct kernel_param_ops param_ops_slot_table_size = {
2943 .set = param_set_slot_table_size,
2944 .get = param_get_uint,
2945};
2946
Trond Myklebustcbf11072009-08-09 15:06:19 -04002947#define param_check_slot_table_size(name, p) \
2948 __param_check(name, p, unsigned int);
2949
2950module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
2951 slot_table_size, 0644);
2952module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
2953 slot_table_size, 0644);
2954