blob: adaa54c6a09adb172bbf31fe07855bf86d18b450 [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>
Trond Myklebust9e00abc2011-07-13 19:20:49 -040040#ifdef CONFIG_SUNRPC_BACKCHANNEL
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -040041#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;
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400758 if (req == NULL)
759 goto out_release;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400760 if (req->rq_bytes_sent == 0)
761 goto out_release;
762 if (req->rq_bytes_sent == req->rq_snd_buf.len)
763 goto out_release;
764 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
765out_release:
766 xprt_release_xprt(xprt, task);
767}
768
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400769static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
770{
771 transport->old_data_ready = sk->sk_data_ready;
772 transport->old_state_change = sk->sk_state_change;
773 transport->old_write_space = sk->sk_write_space;
774 transport->old_error_report = sk->sk_error_report;
775}
776
777static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
778{
779 sk->sk_data_ready = transport->old_data_ready;
780 sk->sk_state_change = transport->old_state_change;
781 sk->sk_write_space = transport->old_write_space;
782 sk->sk_error_report = transport->old_error_report;
783}
784
Chuck Leverfe315e72009-03-11 14:10:21 -0400785static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400786{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500787 struct socket *sock = transport->sock;
788 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400789
Chuck Leverfe315e72009-03-11 14:10:21 -0400790 if (sk == NULL)
791 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400792
Trond Myklebust246408d2011-03-22 18:40:10 -0400793 transport->srcport = 0;
794
Chuck Levera246b012005-08-11 16:25:23 -0400795 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500796 transport->inet = NULL;
797 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400798
Chuck Lever9903cd12005-08-11 16:25:26 -0400799 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400800
801 xs_restore_old_callbacks(transport, sk);
Chuck Levera246b012005-08-11 16:25:23 -0400802 write_unlock_bh(&sk->sk_callback_lock);
803
Chuck Lever9903cd12005-08-11 16:25:26 -0400804 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400805
806 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -0400807}
808
809/**
810 * xs_close - close a socket
811 * @xprt: transport
812 *
813 * This is used when all requests are complete; ie, no DRC state remains
814 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400815 *
816 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
817 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e72009-03-11 14:10:21 -0400818 */
819static void xs_close(struct rpc_xprt *xprt)
820{
821 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
822
823 dprintk("RPC: xs_close xprt %p\n", xprt);
824
825 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -0400826 xprt->reestablish_timeout = 0;
Chuck Leverfe315e72009-03-11 14:10:21 -0400827
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100828 smp_mb__before_clear_bit();
Trond Myklebust7d1e8252009-03-11 14:38:03 -0400829 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100830 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -0500831 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100832 smp_mb__after_clear_bit();
Trond Myklebust62da3b22007-11-06 18:44:20 -0500833 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400834}
835
Trond Myklebustf75e6742009-04-21 17:18:20 -0400836static void xs_tcp_close(struct rpc_xprt *xprt)
837{
838 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
839 xs_close(xprt);
840 else
841 xs_tcp_shutdown(xprt);
842}
843
Chuck Lever9903cd12005-08-11 16:25:26 -0400844/**
845 * xs_destroy - prepare to shutdown a transport
846 * @xprt: doomed transport
847 *
848 */
849static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400850{
Chuck Leverc8475462006-12-05 16:35:26 -0500851 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
852
Chuck Lever46121cf2007-01-31 12:14:08 -0500853 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400854
Tejun Heoafe2c512010-12-14 16:21:17 +0100855 cancel_delayed_work_sync(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400856
Chuck Lever9903cd12005-08-11 16:25:26 -0400857 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400858 xs_free_peer_addresses(xprt);
Pavel Emelyanove204e622010-09-29 16:03:13 +0400859 xprt_free(xprt);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -0400860 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400861}
862
Chuck Lever9903cd12005-08-11 16:25:26 -0400863static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400864{
Chuck Lever9903cd12005-08-11 16:25:26 -0400865 return (struct rpc_xprt *) sk->sk_user_data;
866}
867
Chuck Lever176e21e2011-05-09 15:22:44 -0400868static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
869{
870 struct xdr_skb_reader desc = {
871 .skb = skb,
872 .offset = sizeof(rpc_fraghdr),
873 .count = skb->len - sizeof(rpc_fraghdr),
874 };
875
876 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
877 return -1;
878 if (desc.count)
879 return -1;
880 return 0;
881}
882
883/**
884 * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets
885 * @sk: socket with data to read
886 * @len: how much data to read
887 *
888 * Currently this assumes we can read the whole reply in a single gulp.
889 */
890static void xs_local_data_ready(struct sock *sk, int len)
891{
892 struct rpc_task *task;
893 struct rpc_xprt *xprt;
894 struct rpc_rqst *rovr;
895 struct sk_buff *skb;
896 int err, repsize, copied;
897 u32 _xid;
898 __be32 *xp;
899
900 read_lock_bh(&sk->sk_callback_lock);
901 dprintk("RPC: %s...\n", __func__);
902 xprt = xprt_from_sock(sk);
903 if (xprt == NULL)
904 goto out;
905
906 skb = skb_recv_datagram(sk, 0, 1, &err);
907 if (skb == NULL)
908 goto out;
909
910 if (xprt->shutdown)
911 goto dropit;
912
913 repsize = skb->len - sizeof(rpc_fraghdr);
914 if (repsize < 4) {
915 dprintk("RPC: impossible RPC reply size %d\n", repsize);
916 goto dropit;
917 }
918
919 /* Copy the XID from the skb... */
920 xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
921 if (xp == NULL)
922 goto dropit;
923
924 /* Look up and lock the request corresponding to the given XID */
925 spin_lock(&xprt->transport_lock);
926 rovr = xprt_lookup_rqst(xprt, *xp);
927 if (!rovr)
928 goto out_unlock;
929 task = rovr->rq_task;
930
931 copied = rovr->rq_private_buf.buflen;
932 if (copied > repsize)
933 copied = repsize;
934
935 if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
936 dprintk("RPC: sk_buff copy failed\n");
937 goto out_unlock;
938 }
939
940 xprt_complete_rqst(task, copied);
941
942 out_unlock:
943 spin_unlock(&xprt->transport_lock);
944 dropit:
945 skb_free_datagram(sk, skb);
946 out:
947 read_unlock_bh(&sk->sk_callback_lock);
948}
949
Chuck Lever9903cd12005-08-11 16:25:26 -0400950/**
951 * xs_udp_data_ready - "data ready" callback for UDP sockets
952 * @sk: socket with data to read
953 * @len: how much data to read
954 *
955 */
956static void xs_udp_data_ready(struct sock *sk, int len)
957{
958 struct rpc_task *task;
959 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400960 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400961 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400962 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700963 u32 _xid;
964 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400965
Eric Dumazetf064af12010-09-22 12:43:39 +0000966 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500967 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400968 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400969 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400970
971 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
972 goto out;
973
974 if (xprt->shutdown)
975 goto dropit;
976
977 repsize = skb->len - sizeof(struct udphdr);
978 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500979 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400980 goto dropit;
981 }
982
983 /* Copy the XID from the skb... */
984 xp = skb_header_pointer(skb, sizeof(struct udphdr),
985 sizeof(_xid), &_xid);
986 if (xp == NULL)
987 goto dropit;
988
989 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400990 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400991 rovr = xprt_lookup_rqst(xprt, *xp);
992 if (!rovr)
993 goto out_unlock;
994 task = rovr->rq_task;
995
Chuck Levera246b012005-08-11 16:25:23 -0400996 if ((copied = rovr->rq_private_buf.buflen) > repsize)
997 copied = repsize;
998
999 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001000 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
1001 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -04001002 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001003 }
1004
1005 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -04001006
1007 /* Something worked... */
Eric Dumazetadf30902009-06-02 05:19:30 +00001008 dst_confirm(skb_dst(skb));
Chuck Levera246b012005-08-11 16:25:23 -04001009
Chuck Lever1570c1e2005-08-25 16:25:52 -07001010 xprt_adjust_cwnd(task, copied);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001011 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -04001012
1013 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001014 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001015 dropit:
1016 skb_free_datagram(sk, skb);
1017 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001018 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001019}
1020
Chuck Leverdd456472006-12-05 16:35:44 -05001021static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001022{
Chuck Lever51971132006-12-05 16:35:19 -05001023 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001024 size_t len, used;
1025 char *p;
1026
Chuck Lever51971132006-12-05 16:35:19 -05001027 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
1028 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001029 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001030 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001031 if (used != len)
1032 return;
Chuck Lever808012f2005-08-25 16:25:49 -07001033
Chuck Lever51971132006-12-05 16:35:19 -05001034 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
1035 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -05001036 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -04001037 else
Chuck Levere136d092006-12-05 16:35:23 -05001038 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -05001039 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -07001040
Chuck Levere136d092006-12-05 16:35:23 -05001041 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001042 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -07001043
Chuck Levera246b012005-08-11 16:25:23 -04001044 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001045 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001046 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebust3ebb0672007-11-06 18:40:12 -05001047 xprt_force_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001048 return;
Chuck Levera246b012005-08-11 16:25:23 -04001049 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001050 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -05001051 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001052}
1053
Chuck Lever51971132006-12-05 16:35:19 -05001054static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001055{
Chuck Lever51971132006-12-05 16:35:19 -05001056 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001057 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001058 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001059 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
1060 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1061 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001062 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001063 }
1064 }
1065}
1066
Chuck Leverdd456472006-12-05 16:35:44 -05001067static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001068{
1069 size_t len, used;
1070 char *p;
1071
Chuck Lever51971132006-12-05 16:35:19 -05001072 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -05001073 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001074 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001075 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001076 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001077 if (used != len)
1078 return;
Chuck Levere136d092006-12-05 16:35:23 -05001079 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001080 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -05001081 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001082 dprintk("RPC: reading %s XID %08x\n",
1083 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1084 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -05001085 ntohl(transport->tcp_xid));
1086 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001087}
1088
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001089static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1090 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001091{
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001092 size_t len, used;
1093 u32 offset;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001094 char *p;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001095
1096 /*
1097 * We want transport->tcp_offset to be 8 at the end of this routine
1098 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1099 * When this function is called for the first time,
1100 * transport->tcp_offset is 4 (after having already read the xid).
1101 */
1102 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001103 len = sizeof(transport->tcp_calldir) - offset;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001104 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001105 p = ((char *) &transport->tcp_calldir) + offset;
1106 used = xdr_skb_read_bits(desc, p, len);
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001107 transport->tcp_offset += used;
1108 if (used != len)
1109 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001110 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001111 /*
1112 * We don't yet have the XDR buffer, so we will write the calldir
1113 * out after we get the buffer from the 'struct rpc_rqst'
1114 */
Trond Myklebustb76ce562010-06-16 13:57:32 -04001115 switch (ntohl(transport->tcp_calldir)) {
1116 case RPC_REPLY:
1117 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1118 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001119 transport->tcp_flags |= TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001120 break;
1121 case RPC_CALL:
1122 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1123 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001124 transport->tcp_flags &= ~TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001125 break;
1126 default:
1127 dprintk("RPC: invalid request message type\n");
1128 xprt_force_disconnect(&transport->xprt);
1129 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001130 xs_tcp_check_fraghdr(transport);
1131}
1132
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001133static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
1134 struct xdr_skb_reader *desc,
1135 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001136{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001137 struct sock_xprt *transport =
1138 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001139 struct xdr_buf *rcvbuf;
1140 size_t len;
1141 ssize_t r;
1142
Chuck Levera246b012005-08-11 16:25:23 -04001143 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001144
1145 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1146 /*
1147 * Save the RPC direction in the XDR buffer
1148 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001149 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
Trond Myklebustb76ce562010-06-16 13:57:32 -04001150 &transport->tcp_calldir,
1151 sizeof(transport->tcp_calldir));
1152 transport->tcp_copied += sizeof(transport->tcp_calldir);
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001153 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -04001154 }
1155
Chuck Levera246b012005-08-11 16:25:23 -04001156 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -05001157 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -05001158 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -04001159
Chuck Lever51971132006-12-05 16:35:19 -05001160 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001161 memcpy(&my_desc, desc, sizeof(my_desc));
1162 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -05001163 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001164 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001165 desc->count -= r;
1166 desc->offset += r;
1167 } else
Chuck Lever51971132006-12-05 16:35:19 -05001168 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001169 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001170
1171 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001172 transport->tcp_copied += r;
1173 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001174 }
1175 if (r != len) {
1176 /* Error when copying to the receive buffer,
1177 * usually because we weren't able to allocate
1178 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001179 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001180 * will not receive any additional updates,
1181 * and time out.
1182 * Any remaining data from this record will
1183 * be discarded.
1184 */
Chuck Levere136d092006-12-05 16:35:23 -05001185 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001186 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001187 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001188 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1189 "tcp_offset = %u, tcp_reclen = %u\n",
1190 xprt, transport->tcp_copied,
1191 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001192 return;
Chuck Levera246b012005-08-11 16:25:23 -04001193 }
1194
Chuck Lever46121cf2007-01-31 12:14:08 -05001195 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001196 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001197 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1198 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1199 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001200
Chuck Lever51971132006-12-05 16:35:19 -05001201 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001202 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001203 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001204 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1205 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001206 }
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001207}
1208
1209/*
1210 * Finds the request corresponding to the RPC xid and invokes the common
1211 * tcp read code to read the data.
1212 */
1213static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1214 struct xdr_skb_reader *desc)
1215{
1216 struct sock_xprt *transport =
1217 container_of(xprt, struct sock_xprt, xprt);
1218 struct rpc_rqst *req;
1219
1220 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1221
1222 /* Find and lock the request corresponding to this xid */
1223 spin_lock(&xprt->transport_lock);
1224 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1225 if (!req) {
1226 dprintk("RPC: XID %08x request not found!\n",
1227 ntohl(transport->tcp_xid));
1228 spin_unlock(&xprt->transport_lock);
1229 return -1;
1230 }
1231
1232 xs_tcp_read_common(xprt, desc, req);
1233
Chuck Levere136d092006-12-05 16:35:23 -05001234 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001235 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001236
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001237 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001238 return 0;
1239}
1240
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001241#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001242/*
1243 * Obtains an rpc_rqst previously allocated and invokes the common
1244 * tcp read code to read the data. The result is placed in the callback
1245 * queue.
1246 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1247 * connection and return -1.
1248 */
1249static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1250 struct xdr_skb_reader *desc)
1251{
1252 struct sock_xprt *transport =
1253 container_of(xprt, struct sock_xprt, xprt);
1254 struct rpc_rqst *req;
1255
1256 req = xprt_alloc_bc_request(xprt);
1257 if (req == NULL) {
1258 printk(KERN_WARNING "Callback slot table overflowed\n");
1259 xprt_force_disconnect(xprt);
1260 return -1;
1261 }
1262
1263 req->rq_xid = transport->tcp_xid;
1264 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1265 xs_tcp_read_common(xprt, desc, req);
1266
1267 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1268 struct svc_serv *bc_serv = xprt->bc_serv;
1269
1270 /*
1271 * Add callback request to callback list. The callback
1272 * service sleeps on the sv_cb_waitq waiting for new
1273 * requests. Wake it up after adding enqueing the
1274 * request.
1275 */
1276 dprintk("RPC: add callback request to list\n");
1277 spin_lock(&bc_serv->sv_cb_lock);
1278 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1279 spin_unlock(&bc_serv->sv_cb_lock);
1280 wake_up(&bc_serv->sv_cb_waitq);
1281 }
1282
1283 req->rq_private_buf.len = transport->tcp_copied;
1284
1285 return 0;
1286}
1287
1288static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1289 struct xdr_skb_reader *desc)
1290{
1291 struct sock_xprt *transport =
1292 container_of(xprt, struct sock_xprt, xprt);
1293
1294 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1295 xs_tcp_read_reply(xprt, desc) :
1296 xs_tcp_read_callback(xprt, desc);
1297}
1298#else
1299static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1300 struct xdr_skb_reader *desc)
1301{
1302 return xs_tcp_read_reply(xprt, desc);
1303}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001304#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001305
1306/*
1307 * Read data off the transport. This can be either an RPC_CALL or an
1308 * RPC_REPLY. Relay the processing to helper functions.
1309 */
1310static void xs_tcp_read_data(struct rpc_xprt *xprt,
1311 struct xdr_skb_reader *desc)
1312{
1313 struct sock_xprt *transport =
1314 container_of(xprt, struct sock_xprt, xprt);
1315
1316 if (_xs_tcp_read_data(xprt, desc) == 0)
1317 xs_tcp_check_fraghdr(transport);
1318 else {
1319 /*
1320 * The transport_lock protects the request handling.
1321 * There's no need to hold it to update the tcp_flags.
1322 */
1323 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1324 }
Chuck Levera246b012005-08-11 16:25:23 -04001325}
1326
Chuck Leverdd456472006-12-05 16:35:44 -05001327static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001328{
1329 size_t len;
1330
Chuck Lever51971132006-12-05 16:35:19 -05001331 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001332 if (len > desc->count)
1333 len = desc->count;
1334 desc->count -= len;
1335 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001336 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001337 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001338 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001339}
1340
Chuck Lever9903cd12005-08-11 16:25:26 -04001341static 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 -04001342{
1343 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001344 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001345 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001346 .skb = skb,
1347 .offset = offset,
1348 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001349 };
Chuck Levera246b012005-08-11 16:25:23 -04001350
Chuck Lever46121cf2007-01-31 12:14:08 -05001351 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001352 do {
1353 /* Read in a new fragment marker if necessary */
1354 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001355 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001356 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001357 continue;
1358 }
1359 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001360 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001361 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001362 continue;
1363 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001364 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001365 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001366 xs_tcp_read_calldir(transport, &desc);
1367 continue;
1368 }
Chuck Levera246b012005-08-11 16:25:23 -04001369 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001370 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001371 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001372 continue;
1373 }
1374 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001375 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001376 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001377 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001378 return len - desc.count;
1379}
1380
Chuck Lever9903cd12005-08-11 16:25:26 -04001381/**
1382 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1383 * @sk: socket with data to read
1384 * @bytes: how much data to read
1385 *
1386 */
1387static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001388{
1389 struct rpc_xprt *xprt;
1390 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001391 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001392
Chuck Lever46121cf2007-01-31 12:14:08 -05001393 dprintk("RPC: xs_tcp_data_ready...\n");
1394
Eric Dumazetf064af12010-09-22 12:43:39 +00001395 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001396 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001397 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001398 if (xprt->shutdown)
1399 goto out;
1400
Neil Brown61d0a8e2009-09-23 14:36:37 -04001401 /* Any data means we had a useful conversation, so
1402 * the we don't need to delay the next reconnect
1403 */
1404 if (xprt->reestablish_timeout)
1405 xprt->reestablish_timeout = 0;
1406
Chuck Lever9903cd12005-08-11 16:25:26 -04001407 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001408 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001409 do {
1410 rd_desc.count = 65536;
1411 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1412 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001413out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001414 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001415}
1416
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001417/*
1418 * Do the equivalent of linger/linger2 handling for dealing with
1419 * broken servers that don't close the socket in a timely
1420 * fashion
1421 */
1422static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1423 unsigned long timeout)
1424{
1425 struct sock_xprt *transport;
1426
1427 if (xprt_test_and_set_connecting(xprt))
1428 return;
1429 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1430 transport = container_of(xprt, struct sock_xprt, xprt);
1431 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1432 timeout);
1433}
1434
1435static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1436{
1437 struct sock_xprt *transport;
1438
1439 transport = container_of(xprt, struct sock_xprt, xprt);
1440
1441 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1442 !cancel_delayed_work(&transport->connect_worker))
1443 return;
1444 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1445 xprt_clear_connecting(xprt);
1446}
1447
1448static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1449{
1450 smp_mb__before_clear_bit();
1451 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1452 clear_bit(XPRT_CLOSING, &xprt->state);
1453 smp_mb__after_clear_bit();
1454 /* Mark transport as closed and wake up all pending tasks */
1455 xprt_disconnect_done(xprt);
1456}
1457
Chuck Lever9903cd12005-08-11 16:25:26 -04001458/**
1459 * xs_tcp_state_change - callback to handle TCP socket state changes
1460 * @sk: socket whose state has changed
1461 *
1462 */
1463static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001464{
Chuck Lever9903cd12005-08-11 16:25:26 -04001465 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001466
Eric Dumazetf064af12010-09-22 12:43:39 +00001467 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001468 if (!(xprt = xprt_from_sock(sk)))
1469 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001470 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001471 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001472 sk->sk_state, xprt_connected(xprt),
1473 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001474 sock_flag(sk, SOCK_ZAPPED),
1475 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001476
1477 switch (sk->sk_state) {
1478 case TCP_ESTABLISHED:
Eric Dumazetf064af12010-09-22 12:43:39 +00001479 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001480 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001481 struct sock_xprt *transport = container_of(xprt,
1482 struct sock_xprt, xprt);
1483
Chuck Levera246b012005-08-11 16:25:23 -04001484 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001485 transport->tcp_offset = 0;
1486 transport->tcp_reclen = 0;
1487 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001488 transport->tcp_flags =
1489 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001490
Trond Myklebust2a491992009-03-11 14:38:00 -04001491 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001492 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001493 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001494 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001495 case TCP_FIN_WAIT1:
1496 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001497 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001498 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001499 set_bit(XPRT_CLOSING, &xprt->state);
1500 smp_mb__before_clear_bit();
1501 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001502 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001503 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001504 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001505 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001506 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001507 /* The server initiated a shutdown of the socket */
Trond Myklebust66af1e52007-11-06 10:18:36 -05001508 xprt_force_disconnect(xprt);
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001509 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001510 case TCP_CLOSING:
1511 /*
1512 * If the server closed down the connection, make sure that
1513 * we back off before reconnecting
1514 */
1515 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1516 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001517 break;
1518 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001519 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001520 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001521 smp_mb__before_clear_bit();
1522 clear_bit(XPRT_CONNECTED, &xprt->state);
1523 smp_mb__after_clear_bit();
1524 break;
1525 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001526 xs_tcp_cancel_linger_timeout(xprt);
1527 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001528 }
1529 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001530 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001531}
1532
Chuck Lever9903cd12005-08-11 16:25:26 -04001533/**
Trond Myklebust482f32e2009-03-11 14:38:00 -04001534 * xs_error_report - callback mainly for catching socket errors
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001535 * @sk: socket
1536 */
Trond Myklebust482f32e2009-03-11 14:38:00 -04001537static void xs_error_report(struct sock *sk)
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001538{
1539 struct rpc_xprt *xprt;
1540
Eric Dumazetf064af12010-09-22 12:43:39 +00001541 read_lock_bh(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001542 if (!(xprt = xprt_from_sock(sk)))
1543 goto out;
1544 dprintk("RPC: %s client %p...\n"
1545 "RPC: error %d\n",
1546 __func__, xprt, sk->sk_err);
Trond Myklebust482f32e2009-03-11 14:38:00 -04001547 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001548out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001549 read_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001550}
1551
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001552static void xs_write_space(struct sock *sk)
1553{
1554 struct socket *sock;
1555 struct rpc_xprt *xprt;
1556
1557 if (unlikely(!(sock = sk->sk_socket)))
1558 return;
1559 clear_bit(SOCK_NOSPACE, &sock->flags);
1560
1561 if (unlikely(!(xprt = xprt_from_sock(sk))))
1562 return;
1563 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1564 return;
1565
1566 xprt_write_space(xprt);
1567}
1568
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001569/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001570 * xs_udp_write_space - callback invoked when socket buffer space
1571 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001572 * @sk: socket whose state has changed
1573 *
Chuck Levera246b012005-08-11 16:25:23 -04001574 * Called when more output buffer space is available for this socket.
1575 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001576 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001577 * with a bunch of small requests.
1578 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001579static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001580{
Eric Dumazetf064af12010-09-22 12:43:39 +00001581 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001582
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001583 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001584 if (sock_writeable(sk))
1585 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001586
Eric Dumazetf064af12010-09-22 12:43:39 +00001587 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001588}
Chuck Levera246b012005-08-11 16:25:23 -04001589
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001590/**
1591 * xs_tcp_write_space - callback invoked when socket buffer space
1592 * becomes available
1593 * @sk: socket whose state has changed
1594 *
1595 * Called when more output buffer space is available for this socket.
1596 * We try not to wake our writers until they can make "significant"
1597 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1598 * with a bunch of small requests.
1599 */
1600static void xs_tcp_write_space(struct sock *sk)
1601{
Eric Dumazetf064af12010-09-22 12:43:39 +00001602 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001603
1604 /* from net/core/stream.c:sk_stream_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001605 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1606 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001607
Eric Dumazetf064af12010-09-22 12:43:39 +00001608 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001609}
1610
Chuck Lever470056c2005-08-25 16:25:56 -07001611static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001612{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001613 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1614 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001615
Chuck Lever7c6e0662006-12-05 16:35:30 -05001616 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001617 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001618 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001619 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001620 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001621 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001622 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001623 sk->sk_write_space(sk);
1624 }
1625}
1626
Chuck Lever43118c22005-08-25 16:25:49 -07001627/**
Chuck Lever470056c2005-08-25 16:25:56 -07001628 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001629 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001630 * @sndsize: requested size of send buffer, in bytes
1631 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001632 *
Chuck Lever470056c2005-08-25 16:25:56 -07001633 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001634 */
Chuck Lever470056c2005-08-25 16:25:56 -07001635static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001636{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001637 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1638
1639 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001640 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001641 transport->sndsize = sndsize + 1024;
1642 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001643 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001644 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001645
1646 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001647}
1648
Chuck Lever46c0ee82005-08-25 16:25:52 -07001649/**
1650 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1651 * @task: task that timed out
1652 *
1653 * Adjust the congestion window after a retransmit timeout has occurred.
1654 */
1655static void xs_udp_timer(struct rpc_task *task)
1656{
1657 xprt_adjust_cwnd(task, -ETIMEDOUT);
1658}
1659
Chuck Leverb85d8802006-05-25 01:40:49 -04001660static unsigned short xs_get_random_port(void)
1661{
1662 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1663 unsigned short rand = (unsigned short) net_random() % range;
1664 return rand + xprt_min_resvport;
1665}
1666
Chuck Lever92200412006-01-03 09:55:51 +01001667/**
1668 * xs_set_port - reset the port number in the remote endpoint address
1669 * @xprt: generic transport
1670 * @port: new port number
1671 *
1672 */
1673static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1674{
Chuck Lever46121cf2007-01-31 12:14:08 -05001675 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001676
Chuck Lever9dc3b092009-08-09 15:09:46 -04001677 rpc_set_port(xs_addr(xprt), port);
1678 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001679}
1680
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001681static unsigned short xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001682{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001683 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001684
1685 if (port == 0 && transport->xprt.resvport)
1686 port = xs_get_random_port();
1687 return port;
1688}
1689
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001690static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001691{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001692 if (transport->srcport != 0)
1693 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001694 if (!transport->xprt.resvport)
1695 return 0;
1696 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1697 return xprt_max_resvport;
1698 return --port;
1699}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001700static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001701{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001702 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001703 int err, nloop = 0;
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001704 unsigned short port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001705 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001706
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001707 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001708 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001709 rpc_set_port((struct sockaddr *)&myaddr, port);
1710 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1711 transport->xprt.addrlen);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001712 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001713 break;
Chuck Levera246b012005-08-11 16:25:23 -04001714 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001715 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001716 break;
Chuck Levera246b012005-08-11 16:25:23 -04001717 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001718 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001719 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001720 if (port > last)
1721 nloop++;
1722 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001723
Chuck Lever4232e862010-10-20 11:52:51 -04001724 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001725 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1726 &((struct sockaddr_in *)&myaddr)->sin_addr,
1727 port, err ? "failed" : "ok", err);
1728 else
1729 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1730 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1731 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001732 return err;
1733}
1734
Chuck Lever176e21e2011-05-09 15:22:44 -04001735/*
1736 * We don't support autobind on AF_LOCAL sockets
1737 */
1738static void xs_local_rpcbind(struct rpc_task *task)
1739{
1740 xprt_set_bound(task->tk_xprt);
1741}
1742
1743static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1744{
1745}
Chuck Levera246b012005-08-11 16:25:23 -04001746
Peter Zijlstraed075362006-12-06 20:35:24 -08001747#ifdef CONFIG_DEBUG_LOCK_ALLOC
1748static struct lock_class_key xs_key[2];
1749static struct lock_class_key xs_slock_key[2];
1750
Chuck Lever176e21e2011-05-09 15:22:44 -04001751static inline void xs_reclassify_socketu(struct socket *sock)
1752{
1753 struct sock *sk = sock->sk;
1754
1755 BUG_ON(sock_owned_by_user(sk));
1756 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1757 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1758}
1759
Chuck Lever8945ee52007-08-06 11:58:04 -04001760static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001761{
1762 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001763
John Heffner02b3d342007-09-12 10:42:12 +02001764 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001765 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1766 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1767}
Peter Zijlstraed075362006-12-06 20:35:24 -08001768
Chuck Lever8945ee52007-08-06 11:58:04 -04001769static inline void xs_reclassify_socket6(struct socket *sock)
1770{
1771 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001772
Linus Torvaldsf4921af2007-10-15 10:46:05 -07001773 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001774 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1775 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001776}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001777
1778static inline void xs_reclassify_socket(int family, struct socket *sock)
1779{
Chuck Lever4232e862010-10-20 11:52:51 -04001780 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001781 case AF_LOCAL:
1782 xs_reclassify_socketu(sock);
1783 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001784 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001785 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001786 break;
1787 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001788 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001789 break;
1790 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001791}
Peter Zijlstraed075362006-12-06 20:35:24 -08001792#else
Chuck Lever176e21e2011-05-09 15:22:44 -04001793static inline void xs_reclassify_socketu(struct socket *sock)
1794{
1795}
1796
Chuck Lever8945ee52007-08-06 11:58:04 -04001797static inline void xs_reclassify_socket4(struct socket *sock)
1798{
1799}
1800
1801static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001802{
1803}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001804
1805static inline void xs_reclassify_socket(int family, struct socket *sock)
1806{
1807}
Peter Zijlstraed075362006-12-06 20:35:24 -08001808#endif
1809
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001810static struct socket *xs_create_sock(struct rpc_xprt *xprt,
1811 struct sock_xprt *transport, int family, int type, int protocol)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001812{
1813 struct socket *sock;
1814 int err;
1815
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001816 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001817 if (err < 0) {
1818 dprintk("RPC: can't create %d transport socket (%d).\n",
1819 protocol, -err);
1820 goto out;
1821 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001822 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001823
Ben Hutchings4cea2882011-02-22 21:54:34 +00001824 err = xs_bind(transport, sock);
1825 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001826 sock_release(sock);
1827 goto out;
1828 }
1829
1830 return sock;
1831out:
1832 return ERR_PTR(err);
1833}
1834
Chuck Lever176e21e2011-05-09 15:22:44 -04001835static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1836 struct socket *sock)
1837{
1838 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1839 xprt);
1840
1841 if (!transport->inet) {
1842 struct sock *sk = sock->sk;
1843
1844 write_lock_bh(&sk->sk_callback_lock);
1845
1846 xs_save_old_callbacks(transport, sk);
1847
1848 sk->sk_user_data = xprt;
1849 sk->sk_data_ready = xs_local_data_ready;
1850 sk->sk_write_space = xs_udp_write_space;
1851 sk->sk_error_report = xs_error_report;
1852 sk->sk_allocation = GFP_ATOMIC;
1853
1854 xprt_clear_connected(xprt);
1855
1856 /* Reset to new socket */
1857 transport->sock = sock;
1858 transport->inet = sk;
1859
1860 write_unlock_bh(&sk->sk_callback_lock);
1861 }
1862
1863 /* Tell the socket layer to start connecting... */
1864 xprt->stat.connect_count++;
1865 xprt->stat.connect_start = jiffies;
1866 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1867}
1868
1869/**
1870 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1871 * @xprt: RPC transport to connect
1872 * @transport: socket transport to connect
1873 * @create_sock: function to create a socket of the correct type
1874 *
1875 * Invoked by a work queue tasklet.
1876 */
1877static void xs_local_setup_socket(struct work_struct *work)
1878{
1879 struct sock_xprt *transport =
1880 container_of(work, struct sock_xprt, connect_worker.work);
1881 struct rpc_xprt *xprt = &transport->xprt;
1882 struct socket *sock;
1883 int status = -EIO;
1884
1885 if (xprt->shutdown)
1886 goto out;
1887
1888 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1889 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1890 SOCK_STREAM, 0, &sock, 1);
1891 if (status < 0) {
1892 dprintk("RPC: can't create AF_LOCAL "
1893 "transport socket (%d).\n", -status);
1894 goto out;
1895 }
1896 xs_reclassify_socketu(sock);
1897
1898 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1899 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1900
1901 status = xs_local_finish_connecting(xprt, sock);
1902 switch (status) {
1903 case 0:
1904 dprintk("RPC: xprt %p connected to %s\n",
1905 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1906 xprt_set_connected(xprt);
1907 break;
1908 case -ENOENT:
1909 dprintk("RPC: xprt %p: socket %s does not exist\n",
1910 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1911 break;
1912 default:
1913 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1914 __func__, -status,
1915 xprt->address_strings[RPC_DISPLAY_ADDR]);
1916 }
1917
1918out:
1919 xprt_clear_connecting(xprt);
1920 xprt_wake_pending_tasks(xprt, status);
1921}
1922
Chuck Lever16be2d22007-08-06 11:57:38 -04001923static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001924{
Chuck Lever16be2d22007-08-06 11:57:38 -04001925 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001926
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001927 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001928 struct sock *sk = sock->sk;
1929
1930 write_lock_bh(&sk->sk_callback_lock);
1931
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001932 xs_save_old_callbacks(transport, sk);
1933
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001934 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001935 sk->sk_data_ready = xs_udp_data_ready;
1936 sk->sk_write_space = xs_udp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001937 sk->sk_error_report = xs_error_report;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001938 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001939 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001940
1941 xprt_set_connected(xprt);
1942
1943 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001944 transport->sock = sock;
1945 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001946
1947 write_unlock_bh(&sk->sk_callback_lock);
1948 }
Chuck Lever470056c2005-08-25 16:25:56 -07001949 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04001950}
1951
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04001952static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04001953{
1954 struct sock_xprt *transport =
1955 container_of(work, struct sock_xprt, connect_worker.work);
1956 struct rpc_xprt *xprt = &transport->xprt;
1957 struct socket *sock = transport->sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04001958 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04001959
Trond Myklebust01d37c42009-03-11 14:09:39 -04001960 if (xprt->shutdown)
Chuck Lever9903cd12005-08-11 16:25:26 -04001961 goto out;
1962
Chuck Levera246b012005-08-11 16:25:23 -04001963 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001964 xs_reset_transport(transport);
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04001965 sock = xs_create_sock(xprt, transport,
1966 xs_addr(xprt)->sa_family, SOCK_DGRAM, IPPROTO_UDP);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04001967 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04001968 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04001969
Chuck Leverc740eff2009-08-09 15:09:46 -04001970 dprintk("RPC: worker connecting xprt %p via %s to "
1971 "%s (port %s)\n", xprt,
1972 xprt->address_strings[RPC_DISPLAY_PROTO],
1973 xprt->address_strings[RPC_DISPLAY_ADDR],
1974 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04001975
1976 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001977 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001978out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001979 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001980 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001981}
1982
Chuck Lever3167e122005-08-25 16:25:55 -07001983/*
1984 * We need to preserve the port number so the reply cache on the server can
1985 * find our cached RPC replies when we get around to reconnecting.
1986 */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04001987static void xs_abort_connection(struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07001988{
1989 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07001990 struct sockaddr any;
1991
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04001992 dprintk("RPC: disconnecting xprt %p to reuse port\n", transport);
Chuck Lever3167e122005-08-25 16:25:55 -07001993
1994 /*
1995 * Disconnect the transport socket by doing a connect operation
1996 * with AF_UNSPEC. This should return immediately...
1997 */
1998 memset(&any, 0, sizeof(any));
1999 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002000 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002001 if (!result)
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002002 xs_sock_mark_closed(&transport->xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002003 else
Chuck Lever46121cf2007-01-31 12:14:08 -05002004 dprintk("RPC: AF_UNSPEC connect return code %d\n",
Chuck Lever3167e122005-08-25 16:25:55 -07002005 result);
2006}
2007
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002008static void xs_tcp_reuse_connection(struct sock_xprt *transport)
Trond Myklebust40d25492009-03-11 14:37:58 -04002009{
2010 unsigned int state = transport->inet->sk_state;
2011
Andy Chittenden669502f2010-08-10 10:19:53 -04002012 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) {
2013 /* we don't need to abort the connection if the socket
2014 * hasn't undergone a shutdown
2015 */
2016 if (transport->inet->sk_shutdown == 0)
2017 return;
2018 dprintk("RPC: %s: TCP_CLOSEd and sk_shutdown set to %d\n",
2019 __func__, transport->inet->sk_shutdown);
2020 }
2021 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) {
2022 /* we don't need to abort the connection if the socket
2023 * hasn't undergone a shutdown
2024 */
2025 if (transport->inet->sk_shutdown == 0)
2026 return;
2027 dprintk("RPC: %s: ESTABLISHED/SYN_SENT "
2028 "sk_shutdown set to %d\n",
2029 __func__, transport->inet->sk_shutdown);
2030 }
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002031 xs_abort_connection(transport);
Trond Myklebust40d25492009-03-11 14:37:58 -04002032}
2033
Chuck Lever16be2d22007-08-06 11:57:38 -04002034static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002035{
Chuck Lever16be2d22007-08-06 11:57:38 -04002036 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002037 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002038
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002039 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002040 struct sock *sk = sock->sk;
2041
2042 write_lock_bh(&sk->sk_callback_lock);
2043
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002044 xs_save_old_callbacks(transport, sk);
2045
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002046 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002047 sk->sk_data_ready = xs_tcp_data_ready;
2048 sk->sk_state_change = xs_tcp_state_change;
2049 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04002050 sk->sk_error_report = xs_error_report;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05002051 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07002052
2053 /* socket options */
2054 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
2055 sock_reset_flag(sk, SOCK_LINGER);
2056 tcp_sk(sk)->linger2 = 0;
2057 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002058
2059 xprt_clear_connected(xprt);
2060
2061 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002062 transport->sock = sock;
2063 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002064
2065 write_unlock_bh(&sk->sk_callback_lock);
2066 }
2067
Trond Myklebust01d37c42009-03-11 14:09:39 -04002068 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002069 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002070
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002071 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05002072 xprt->stat.connect_count++;
2073 xprt->stat.connect_start = jiffies;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002074 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2075 switch (ret) {
2076 case 0:
2077 case -EINPROGRESS:
2078 /* SYN_SENT! */
2079 xprt->connect_cookie++;
2080 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2081 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2082 }
2083out:
2084 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002085}
2086
2087/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002088 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2089 * @xprt: RPC transport to connect
2090 * @transport: socket transport to connect
2091 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04002092 *
2093 * Invoked by a work queue tasklet.
2094 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002095static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002096{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002097 struct sock_xprt *transport =
2098 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002099 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002100 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002101 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002102
Trond Myklebust01d37c42009-03-11 14:09:39 -04002103 if (xprt->shutdown)
Chuck Lever16be2d22007-08-06 11:57:38 -04002104 goto out;
2105
2106 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002107 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002108 sock = xs_create_sock(xprt, transport,
2109 xs_addr(xprt)->sa_family, SOCK_STREAM, IPPROTO_TCP);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002110 if (IS_ERR(sock)) {
2111 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002112 goto out;
2113 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002114 } else {
2115 int abort_and_exit;
2116
2117 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
2118 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04002119 /* "close" the socket, preserving the local port */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002120 xs_tcp_reuse_connection(transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04002121
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002122 if (abort_and_exit)
2123 goto out_eagain;
2124 }
2125
Chuck Leverc740eff2009-08-09 15:09:46 -04002126 dprintk("RPC: worker connecting xprt %p via %s to "
2127 "%s (port %s)\n", xprt,
2128 xprt->address_strings[RPC_DISPLAY_PROTO],
2129 xprt->address_strings[RPC_DISPLAY_ADDR],
2130 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002131
2132 status = xs_tcp_finish_connecting(xprt, sock);
Chuck Lever46121cf2007-01-31 12:14:08 -05002133 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2134 xprt, -status, xprt_connected(xprt),
2135 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002136 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002137 default:
2138 printk("%s: connect returned unhandled error %d\n",
2139 __func__, status);
2140 case -EADDRNOTAVAIL:
2141 /* We're probably in TIME_WAIT. Get rid of existing socket,
2142 * and retry
2143 */
2144 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
2145 xprt_force_disconnect(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002146 break;
Trond Myklebust8a2cec22009-03-11 14:38:01 -04002147 case -ECONNREFUSED:
2148 case -ECONNRESET:
2149 case -ENETUNREACH:
2150 /* retry with existing socket, after a delay */
Trond Myklebust2a491992009-03-11 14:38:00 -04002151 case 0:
2152 case -EINPROGRESS:
2153 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002154 xprt_clear_connecting(xprt);
2155 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002156 case -EINVAL:
2157 /* Happens, for instance, if the user specified a link
2158 * local IPv6 address without a scope-id.
2159 */
2160 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002161 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002162out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04002163 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002164out:
Chuck Lever2226feb2005-08-11 16:25:38 -04002165 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002166 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04002167}
2168
Chuck Lever68e220b2007-08-06 11:57:48 -04002169/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002170 * xs_connect - connect a socket to a remote endpoint
2171 * @task: address of RPC task that manages state of connect request
2172 *
2173 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002174 *
2175 * UDP socket connects are synchronous, but we use a work queue anyway
2176 * to guarantee that even unprivileged user processes can set up a
2177 * socket on a privileged port.
2178 *
2179 * If a UDP socket connect fails, the delay behavior here prevents
2180 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002181 */
2182static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002183{
2184 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002185 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002186
Chuck Lever09a21c42009-12-03 15:58:56 -05002187 if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002188 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2189 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002190 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002191 queue_delayed_work(rpciod_workqueue,
2192 &transport->connect_worker,
2193 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07002194 xprt->reestablish_timeout <<= 1;
Neil Brown61d0a8e2009-09-23 14:36:37 -04002195 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2196 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002197 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2198 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002199 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05002200 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002201 queue_delayed_work(rpciod_workqueue,
2202 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002203 }
2204}
2205
Chuck Lever262ca072006-03-20 13:44:16 -05002206/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002207 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2208 * @xprt: rpc_xprt struct containing statistics
2209 * @seq: output file
2210 *
2211 */
2212static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2213{
2214 long idle_time = 0;
2215
2216 if (xprt_connected(xprt))
2217 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2218
2219 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
2220 "%llu %llu\n",
2221 xprt->stat.bind_count,
2222 xprt->stat.connect_count,
2223 xprt->stat.connect_time,
2224 idle_time,
2225 xprt->stat.sends,
2226 xprt->stat.recvs,
2227 xprt->stat.bad_xids,
2228 xprt->stat.req_u,
2229 xprt->stat.bklog_u);
2230}
2231
2232/**
Chuck Lever262ca072006-03-20 13:44:16 -05002233 * xs_udp_print_stats - display UDP socket-specifc stats
2234 * @xprt: rpc_xprt struct containing statistics
2235 * @seq: output file
2236 *
2237 */
2238static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2239{
Chuck Leverc8475462006-12-05 16:35:26 -05002240 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2241
Chuck Lever262ca072006-03-20 13:44:16 -05002242 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002243 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002244 xprt->stat.bind_count,
2245 xprt->stat.sends,
2246 xprt->stat.recvs,
2247 xprt->stat.bad_xids,
2248 xprt->stat.req_u,
2249 xprt->stat.bklog_u);
2250}
2251
2252/**
2253 * xs_tcp_print_stats - display TCP socket-specifc stats
2254 * @xprt: rpc_xprt struct containing statistics
2255 * @seq: output file
2256 *
2257 */
2258static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2259{
Chuck Leverc8475462006-12-05 16:35:26 -05002260 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002261 long idle_time = 0;
2262
2263 if (xprt_connected(xprt))
2264 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2265
2266 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002267 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002268 xprt->stat.bind_count,
2269 xprt->stat.connect_count,
2270 xprt->stat.connect_time,
2271 idle_time,
2272 xprt->stat.sends,
2273 xprt->stat.recvs,
2274 xprt->stat.bad_xids,
2275 xprt->stat.req_u,
2276 xprt->stat.bklog_u);
2277}
2278
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002279/*
2280 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2281 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2282 * to use the server side send routines.
2283 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002284static void *bc_malloc(struct rpc_task *task, size_t size)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002285{
2286 struct page *page;
2287 struct rpc_buffer *buf;
2288
2289 BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2290 page = alloc_page(GFP_KERNEL);
2291
2292 if (!page)
2293 return NULL;
2294
2295 buf = page_address(page);
2296 buf->len = PAGE_SIZE;
2297
2298 return buf->data;
2299}
2300
2301/*
2302 * Free the space allocated in the bc_alloc routine
2303 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002304static void bc_free(void *buffer)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002305{
2306 struct rpc_buffer *buf;
2307
2308 if (!buffer)
2309 return;
2310
2311 buf = container_of(buffer, struct rpc_buffer, data);
2312 free_page((unsigned long)buf);
2313}
2314
2315/*
2316 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2317 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2318 */
2319static int bc_sendto(struct rpc_rqst *req)
2320{
2321 int len;
2322 struct xdr_buf *xbufp = &req->rq_snd_buf;
2323 struct rpc_xprt *xprt = req->rq_xprt;
2324 struct sock_xprt *transport =
2325 container_of(xprt, struct sock_xprt, xprt);
2326 struct socket *sock = transport->sock;
2327 unsigned long headoff;
2328 unsigned long tailoff;
2329
Chuck Lever61677ee2011-05-09 15:22:34 -04002330 xs_encode_stream_record_marker(xbufp);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002331
2332 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2333 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2334 len = svc_send_common(sock, xbufp,
2335 virt_to_page(xbufp->head[0].iov_base), headoff,
2336 xbufp->tail[0].iov_base, tailoff);
2337
2338 if (len != xbufp->len) {
2339 printk(KERN_NOTICE "Error sending entire callback!\n");
2340 len = -EAGAIN;
2341 }
2342
2343 return len;
2344}
2345
2346/*
2347 * The send routine. Borrows from svc_send
2348 */
2349static int bc_send_request(struct rpc_task *task)
2350{
2351 struct rpc_rqst *req = task->tk_rqstp;
2352 struct svc_xprt *xprt;
2353 struct svc_sock *svsk;
2354 u32 len;
2355
2356 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2357 /*
2358 * Get the server socket associated with this callback xprt
2359 */
2360 xprt = req->rq_xprt->bc_xprt;
2361 svsk = container_of(xprt, struct svc_sock, sk_xprt);
2362
2363 /*
2364 * Grab the mutex to serialize data as the connection is shared
2365 * with the fore channel
2366 */
2367 if (!mutex_trylock(&xprt->xpt_mutex)) {
2368 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2369 if (!mutex_trylock(&xprt->xpt_mutex))
2370 return -EAGAIN;
2371 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2372 }
2373 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2374 len = -ENOTCONN;
2375 else
2376 len = bc_sendto(req);
2377 mutex_unlock(&xprt->xpt_mutex);
2378
2379 if (len > 0)
2380 len = 0;
2381
2382 return len;
2383}
2384
2385/*
2386 * The close routine. Since this is client initiated, we do nothing
2387 */
2388
2389static void bc_close(struct rpc_xprt *xprt)
2390{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002391}
2392
2393/*
2394 * The xprt destroy routine. Again, because this connection is client
2395 * initiated, we do nothing
2396 */
2397
2398static void bc_destroy(struct rpc_xprt *xprt)
2399{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002400}
2401
Chuck Lever176e21e2011-05-09 15:22:44 -04002402static struct rpc_xprt_ops xs_local_ops = {
2403 .reserve_xprt = xprt_reserve_xprt,
2404 .release_xprt = xs_tcp_release_xprt,
2405 .rpcbind = xs_local_rpcbind,
2406 .set_port = xs_local_set_port,
2407 .connect = xs_connect,
2408 .buf_alloc = rpc_malloc,
2409 .buf_free = rpc_free,
2410 .send_request = xs_local_send_request,
2411 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2412 .close = xs_close,
2413 .destroy = xs_destroy,
2414 .print_stats = xs_local_print_stats,
2415};
2416
Chuck Lever262965f2005-08-11 16:25:56 -04002417static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002418 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002419 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002420 .release_xprt = xprt_release_xprt_cong,
Chuck Lever45160d62007-07-01 12:13:17 -04002421 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002422 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002423 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002424 .buf_alloc = rpc_malloc,
2425 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002426 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002427 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002428 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002429 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002430 .close = xs_close,
2431 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002432 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04002433};
2434
2435static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002436 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002437 .release_xprt = xs_tcp_release_xprt,
Chuck Lever45160d62007-07-01 12:13:17 -04002438 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002439 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002440 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002441 .buf_alloc = rpc_malloc,
2442 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002443 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002444 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustf75e6742009-04-21 17:18:20 -04002445 .close = xs_tcp_close,
Chuck Lever9903cd12005-08-11 16:25:26 -04002446 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002447 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002448};
2449
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002450/*
2451 * The rpc_xprt_ops for the server backchannel
2452 */
2453
2454static struct rpc_xprt_ops bc_tcp_ops = {
2455 .reserve_xprt = xprt_reserve_xprt,
2456 .release_xprt = xprt_release_xprt,
2457 .buf_alloc = bc_malloc,
2458 .buf_free = bc_free,
2459 .send_request = bc_send_request,
2460 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2461 .close = bc_close,
2462 .destroy = bc_destroy,
2463 .print_stats = xs_tcp_print_stats,
2464};
2465
Chuck Lever92476852010-10-20 11:53:01 -04002466static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2467{
2468 static const struct sockaddr_in sin = {
2469 .sin_family = AF_INET,
2470 .sin_addr.s_addr = htonl(INADDR_ANY),
2471 };
2472 static const struct sockaddr_in6 sin6 = {
2473 .sin6_family = AF_INET6,
2474 .sin6_addr = IN6ADDR_ANY_INIT,
2475 };
2476
2477 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002478 case AF_LOCAL:
2479 break;
Chuck Lever92476852010-10-20 11:53:01 -04002480 case AF_INET:
2481 memcpy(sap, &sin, sizeof(sin));
2482 break;
2483 case AF_INET6:
2484 memcpy(sap, &sin6, sizeof(sin6));
2485 break;
2486 default:
2487 dprintk("RPC: %s: Bad address family\n", __func__);
2488 return -EAFNOSUPPORT;
2489 }
2490 return 0;
2491}
2492
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002493static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002494 unsigned int slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002495{
2496 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002497 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002498
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002499 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002500 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002501 return ERR_PTR(-EBADF);
2502 }
2503
Pavel Emelyanov37aa2132010-09-29 16:05:43 +04002504 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002505 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002506 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2507 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002508 return ERR_PTR(-ENOMEM);
2509 }
2510
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002511 new = container_of(xprt, struct sock_xprt, xprt);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002512 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2513 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002514 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002515 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002516 else {
2517 int err;
2518 err = xs_init_anyaddr(args->dstaddr->sa_family,
2519 (struct sockaddr *)&new->srcaddr);
2520 if (err != 0)
2521 return ERR_PTR(err);
2522 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002523
2524 return xprt;
2525}
2526
Chuck Lever176e21e2011-05-09 15:22:44 -04002527static const struct rpc_timeout xs_local_default_timeout = {
2528 .to_initval = 10 * HZ,
2529 .to_maxval = 10 * HZ,
2530 .to_retries = 2,
2531};
2532
2533/**
2534 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2535 * @args: rpc transport creation arguments
2536 *
2537 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2538 */
2539static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2540{
2541 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2542 struct sock_xprt *transport;
2543 struct rpc_xprt *xprt;
2544 struct rpc_xprt *ret;
2545
2546 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2547 if (IS_ERR(xprt))
2548 return xprt;
2549 transport = container_of(xprt, struct sock_xprt, xprt);
2550
2551 xprt->prot = 0;
2552 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2553 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2554
2555 xprt->bind_timeout = XS_BIND_TO;
2556 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2557 xprt->idle_timeout = XS_IDLE_DISC_TO;
2558
2559 xprt->ops = &xs_local_ops;
2560 xprt->timeout = &xs_local_default_timeout;
2561
2562 switch (sun->sun_family) {
2563 case AF_LOCAL:
2564 if (sun->sun_path[0] != '/') {
2565 dprintk("RPC: bad AF_LOCAL address: %s\n",
2566 sun->sun_path);
2567 ret = ERR_PTR(-EINVAL);
2568 goto out_err;
2569 }
2570 xprt_set_bound(xprt);
2571 INIT_DELAYED_WORK(&transport->connect_worker,
2572 xs_local_setup_socket);
2573 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
2574 break;
2575 default:
2576 ret = ERR_PTR(-EAFNOSUPPORT);
2577 goto out_err;
2578 }
2579
2580 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2581 xprt->address_strings[RPC_DISPLAY_ADDR]);
2582
2583 if (try_module_get(THIS_MODULE))
2584 return xprt;
2585 ret = ERR_PTR(-EINVAL);
2586out_err:
2587 xprt_free(xprt);
2588 return ret;
2589}
2590
Trond Myklebust2881ae72007-12-20 16:03:54 -05002591static const struct rpc_timeout xs_udp_default_timeout = {
2592 .to_initval = 5 * HZ,
2593 .to_maxval = 30 * HZ,
2594 .to_increment = 5 * HZ,
2595 .to_retries = 5,
2596};
2597
Chuck Lever9903cd12005-08-11 16:25:26 -04002598/**
2599 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002600 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002601 *
2602 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002603static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002604{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002605 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002606 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002607 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002608 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002609
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002610 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002611 if (IS_ERR(xprt))
2612 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002613 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002614
Chuck Leverec739ef2006-08-22 20:06:15 -04002615 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002616 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002617 /* XXX: header size can vary due to auth type, IPv6, etc. */
2618 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2619
Chuck Lever03bf4b72005-08-25 16:25:55 -07002620 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002621 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2622 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002623
Chuck Lever262965f2005-08-11 16:25:56 -04002624 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002625
Trond Myklebustba7392b2007-12-20 16:03:55 -05002626 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002627
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002628 switch (addr->sa_family) {
2629 case AF_INET:
2630 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2631 xprt_set_bound(xprt);
2632
2633 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002634 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002635 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002636 break;
2637 case AF_INET6:
2638 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2639 xprt_set_bound(xprt);
2640
2641 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002642 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002643 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002644 break;
2645 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002646 ret = ERR_PTR(-EAFNOSUPPORT);
2647 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002648 }
2649
Chuck Leverc740eff2009-08-09 15:09:46 -04002650 if (xprt_bound(xprt))
2651 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2652 xprt->address_strings[RPC_DISPLAY_ADDR],
2653 xprt->address_strings[RPC_DISPLAY_PORT],
2654 xprt->address_strings[RPC_DISPLAY_PROTO]);
2655 else
2656 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2657 xprt->address_strings[RPC_DISPLAY_ADDR],
2658 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002659
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002660 if (try_module_get(THIS_MODULE))
2661 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002662 ret = ERR_PTR(-EINVAL);
2663out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002664 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002665 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002666}
2667
Trond Myklebust2881ae72007-12-20 16:03:54 -05002668static const struct rpc_timeout xs_tcp_default_timeout = {
2669 .to_initval = 60 * HZ,
2670 .to_maxval = 60 * HZ,
2671 .to_retries = 2,
2672};
2673
Chuck Lever9903cd12005-08-11 16:25:26 -04002674/**
2675 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002676 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002677 *
2678 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002679static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002680{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002681 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002682 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002683 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002684 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002685
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002686 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002687 if (IS_ERR(xprt))
2688 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002689 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002690
Chuck Leverec739ef2006-08-22 20:06:15 -04002691 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002692 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002693 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002694
Chuck Lever03bf4b72005-08-25 16:25:55 -07002695 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002696 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2697 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002698
Chuck Lever262965f2005-08-11 16:25:56 -04002699 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002700 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002701
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002702 switch (addr->sa_family) {
2703 case AF_INET:
2704 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2705 xprt_set_bound(xprt);
2706
Chuck Lever9dc3b092009-08-09 15:09:46 -04002707 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002708 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002709 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002710 break;
2711 case AF_INET6:
2712 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2713 xprt_set_bound(xprt);
2714
Chuck Lever9dc3b092009-08-09 15:09:46 -04002715 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002716 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002717 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002718 break;
2719 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002720 ret = ERR_PTR(-EAFNOSUPPORT);
2721 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002722 }
2723
Chuck Leverc740eff2009-08-09 15:09:46 -04002724 if (xprt_bound(xprt))
2725 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2726 xprt->address_strings[RPC_DISPLAY_ADDR],
2727 xprt->address_strings[RPC_DISPLAY_PORT],
2728 xprt->address_strings[RPC_DISPLAY_PROTO]);
2729 else
2730 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2731 xprt->address_strings[RPC_DISPLAY_ADDR],
2732 xprt->address_strings[RPC_DISPLAY_PROTO]);
2733
Chuck Leveredb267a2006-08-22 20:06:18 -04002734
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002735 if (try_module_get(THIS_MODULE))
2736 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002737 ret = ERR_PTR(-EINVAL);
2738out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002739 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002740 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002741}
Chuck Lever282b32e2006-12-05 16:35:51 -05002742
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002743/**
2744 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2745 * @args: rpc transport creation arguments
2746 *
2747 */
2748static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2749{
2750 struct sockaddr *addr = args->dstaddr;
2751 struct rpc_xprt *xprt;
2752 struct sock_xprt *transport;
2753 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002754 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002755
J. Bruce Fieldsf0418aa2010-12-08 13:48:19 -05002756 if (args->bc_xprt->xpt_bc_xprt) {
2757 /*
2758 * This server connection already has a backchannel
2759 * export; we can't create a new one, as we wouldn't be
2760 * able to match replies based on xid any more. So,
2761 * reuse the already-existing one:
2762 */
2763 return args->bc_xprt->xpt_bc_xprt;
2764 }
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002765 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2766 if (IS_ERR(xprt))
2767 return xprt;
2768 transport = container_of(xprt, struct sock_xprt, xprt);
2769
2770 xprt->prot = IPPROTO_TCP;
2771 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2772 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2773 xprt->timeout = &xs_tcp_default_timeout;
2774
2775 /* backchannel */
2776 xprt_set_bound(xprt);
2777 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002778 xprt->reestablish_timeout = 0;
2779 xprt->idle_timeout = 0;
2780
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002781 xprt->ops = &bc_tcp_ops;
2782
2783 switch (addr->sa_family) {
2784 case AF_INET:
2785 xs_format_peer_addresses(xprt, "tcp",
2786 RPCBIND_NETID_TCP);
2787 break;
2788 case AF_INET6:
2789 xs_format_peer_addresses(xprt, "tcp",
2790 RPCBIND_NETID_TCP6);
2791 break;
2792 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002793 ret = ERR_PTR(-EAFNOSUPPORT);
2794 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002795 }
2796
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04002797 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2798 xprt->address_strings[RPC_DISPLAY_ADDR],
2799 xprt->address_strings[RPC_DISPLAY_PORT],
2800 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002801
2802 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002803 * Once we've associated a backchannel xprt with a connection,
2804 * we want to keep it around as long as long as the connection
2805 * lasts, in case we need to start using it for a backchannel
2806 * again; this reference won't be dropped until bc_xprt is
2807 * destroyed.
2808 */
2809 xprt_get(xprt);
2810 args->bc_xprt->xpt_bc_xprt = xprt;
2811 xprt->bc_xprt = args->bc_xprt;
2812 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2813 transport->sock = bc_sock->sk_sock;
2814 transport->inet = bc_sock->sk_sk;
2815
2816 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002817 * Since we don't want connections for the backchannel, we set
2818 * the xprt status to connected
2819 */
2820 xprt_set_connected(xprt);
2821
2822
2823 if (try_module_get(THIS_MODULE))
2824 return xprt;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002825 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002826 ret = ERR_PTR(-EINVAL);
2827out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002828 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002829 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002830}
2831
Chuck Lever176e21e2011-05-09 15:22:44 -04002832static struct xprt_class xs_local_transport = {
2833 .list = LIST_HEAD_INIT(xs_local_transport.list),
2834 .name = "named UNIX socket",
2835 .owner = THIS_MODULE,
2836 .ident = XPRT_TRANSPORT_LOCAL,
2837 .setup = xs_setup_local,
2838};
2839
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002840static struct xprt_class xs_udp_transport = {
2841 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2842 .name = "udp",
2843 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002844 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002845 .setup = xs_setup_udp,
2846};
2847
2848static struct xprt_class xs_tcp_transport = {
2849 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2850 .name = "tcp",
2851 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002852 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002853 .setup = xs_setup_tcp,
2854};
2855
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002856static struct xprt_class xs_bc_tcp_transport = {
2857 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2858 .name = "tcp NFSv4.1 backchannel",
2859 .owner = THIS_MODULE,
2860 .ident = XPRT_TRANSPORT_BC_TCP,
2861 .setup = xs_setup_bc_tcp,
2862};
2863
Chuck Lever282b32e2006-12-05 16:35:51 -05002864/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002865 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002866 *
2867 */
2868int init_socket_xprt(void)
2869{
Chuck Leverfbf76682006-12-05 16:35:54 -05002870#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002871 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002872 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002873#endif
2874
Chuck Lever176e21e2011-05-09 15:22:44 -04002875 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002876 xprt_register_transport(&xs_udp_transport);
2877 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002878 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002879
Chuck Lever282b32e2006-12-05 16:35:51 -05002880 return 0;
2881}
2882
2883/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002884 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05002885 *
2886 */
2887void cleanup_socket_xprt(void)
2888{
Chuck Leverfbf76682006-12-05 16:35:54 -05002889#ifdef RPC_DEBUG
2890 if (sunrpc_table_header) {
2891 unregister_sysctl_table(sunrpc_table_header);
2892 sunrpc_table_header = NULL;
2893 }
2894#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002895
Chuck Lever176e21e2011-05-09 15:22:44 -04002896 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002897 xprt_unregister_transport(&xs_udp_transport);
2898 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002899 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05002900}
Trond Myklebustcbf11072009-08-09 15:06:19 -04002901
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002902static int param_set_uint_minmax(const char *val,
2903 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04002904 unsigned int min, unsigned int max)
2905{
2906 unsigned long num;
2907 int ret;
2908
2909 if (!val)
2910 return -EINVAL;
2911 ret = strict_strtoul(val, 0, &num);
2912 if (ret == -EINVAL || num < min || num > max)
2913 return -EINVAL;
2914 *((unsigned int *)kp->arg) = num;
2915 return 0;
2916}
2917
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002918static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04002919{
2920 return param_set_uint_minmax(val, kp,
2921 RPC_MIN_RESVPORT,
2922 RPC_MAX_RESVPORT);
2923}
2924
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002925static struct kernel_param_ops param_ops_portnr = {
2926 .set = param_set_portnr,
2927 .get = param_get_uint,
2928};
2929
Trond Myklebustcbf11072009-08-09 15:06:19 -04002930#define param_check_portnr(name, p) \
2931 __param_check(name, p, unsigned int);
2932
2933module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
2934module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
2935
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002936static int param_set_slot_table_size(const char *val,
2937 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04002938{
2939 return param_set_uint_minmax(val, kp,
2940 RPC_MIN_SLOT_TABLE,
2941 RPC_MAX_SLOT_TABLE);
2942}
2943
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002944static struct kernel_param_ops param_ops_slot_table_size = {
2945 .set = param_set_slot_table_size,
2946 .get = param_get_uint,
2947};
2948
Trond Myklebustcbf11072009-08-09 15:06:19 -04002949#define param_check_slot_table_size(name, p) \
2950 __param_check(name, p, unsigned int);
2951
2952module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
2953 slot_table_size, 0644);
2954module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
2955 slot_table_size, 0644);
2956