blob: 44c1927b68c7b50bd9645bdd04dcc78dd1b338a5 [file] [log] [blame]
Chuck Levera246b012005-08-11 16:25:23 -04001/*
2 * linux/net/sunrpc/xprtsock.c
3 *
4 * Client-side transport implementation for sockets.
5 *
Alan Cox113aa832008-10-13 19:01:08 -07006 * TCP callback races fixes (C) 1998 Red Hat
7 * TCP send fixes (C) 1998 Red Hat
Chuck Levera246b012005-08-11 16:25:23 -04008 * TCP NFS related read + write fixes
9 * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
10 *
11 * Rewrite of larges part of the code in order to stabilize TCP stuff.
12 * Fix behaviour when socket buffer is full.
13 * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
Chuck Lever55aa4f52005-08-11 16:25:47 -040014 *
15 * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
Chuck Lever8f9d5b12007-08-06 11:57:53 -040016 *
17 * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
18 * <gilles.quillard@bull.net>
Chuck Levera246b012005-08-11 16:25:23 -040019 */
20
21#include <linux/types.h>
Chuck Lever176e21e2011-05-09 15:22:44 -040022#include <linux/string.h>
Chuck Levera246b012005-08-11 16:25:23 -040023#include <linux/slab.h>
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -040024#include <linux/module.h>
Chuck Levera246b012005-08-11 16:25:23 -040025#include <linux/capability.h>
Chuck Levera246b012005-08-11 16:25:23 -040026#include <linux/pagemap.h>
27#include <linux/errno.h>
28#include <linux/socket.h>
29#include <linux/in.h>
30#include <linux/net.h>
31#include <linux/mm.h>
Chuck Lever176e21e2011-05-09 15:22:44 -040032#include <linux/un.h>
Chuck Levera246b012005-08-11 16:25:23 -040033#include <linux/udp.h>
34#include <linux/tcp.h>
35#include <linux/sunrpc/clnt.h>
Jeff Layton59766872013-02-04 12:50:00 -050036#include <linux/sunrpc/addr.h>
Chuck Lever02107142006-01-03 09:55:49 +010037#include <linux/sunrpc/sched.h>
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030038#include <linux/sunrpc/svcsock.h>
\"Talpey, Thomas\49c36fc2007-09-10 13:47:31 -040039#include <linux/sunrpc/xprtsock.h>
Chuck Levera246b012005-08-11 16:25:23 -040040#include <linux/file.h>
Trond Myklebust9e00abc2011-07-13 19:20:49 -040041#ifdef CONFIG_SUNRPC_BACKCHANNEL
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -040042#include <linux/sunrpc/bc_xprt.h>
43#endif
Chuck Levera246b012005-08-11 16:25:23 -040044
45#include <net/sock.h>
46#include <net/checksum.h>
47#include <net/udp.h>
48#include <net/tcp.h>
49
Trond Myklebust40b5ea02013-09-04 12:16:23 -040050#include <trace/events/sunrpc.h>
51
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030052#include "sunrpc.h"
Chuck Lever176e21e2011-05-09 15:22:44 -040053
54static void xs_close(struct rpc_xprt *xprt);
55
Chuck Lever9903cd12005-08-11 16:25:26 -040056/*
Chuck Leverc556b752005-11-01 12:24:48 -050057 * xprtsock tunables
58 */
Trond Myklebust09acfea2012-03-11 15:22:54 -040059static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
60static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
61static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
Chuck Leverc556b752005-11-01 12:24:48 -050062
Trond Myklebust09acfea2012-03-11 15:22:54 -040063static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
64static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
Chuck Leverc556b752005-11-01 12:24:48 -050065
Trond Myklebust54c09872015-02-09 11:01:02 -050066#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
67
Trond Myklebust7d1e8252009-03-11 14:38:03 -040068#define XS_TCP_LINGER_TO (15U * HZ)
Trond Myklebust25fe6142009-03-11 14:38:03 -040069static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
Trond Myklebust7d1e8252009-03-11 14:38:03 -040070
Chuck Leverc556b752005-11-01 12:24:48 -050071/*
Chuck Leverfbf76682006-12-05 16:35:54 -050072 * We can register our own files under /proc/sys/sunrpc by
73 * calling register_sysctl_table() again. The files in that
74 * directory become the union of all files registered there.
75 *
76 * We simply need to make sure that we don't collide with
77 * someone else's file names!
78 */
79
Chuck Leverfbf76682006-12-05 16:35:54 -050080static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
81static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
Trond Myklebustd9ba1312011-07-17 18:11:30 -040082static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Leverfbf76682006-12-05 16:35:54 -050083static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
84static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
85
86static struct ctl_table_header *sunrpc_table_header;
87
88/*
89 * FIXME: changing the UDP slot table size should also resize the UDP
90 * socket buffers for existing UDP transports
91 */
Joe Perchesfe2c6332013-06-11 23:04:25 -070092static struct ctl_table xs_tunables_table[] = {
Chuck Leverfbf76682006-12-05 16:35:54 -050093 {
Chuck Leverfbf76682006-12-05 16:35:54 -050094 .procname = "udp_slot_table_entries",
95 .data = &xprt_udp_slot_table_entries,
96 .maxlen = sizeof(unsigned int),
97 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080098 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -050099 .extra1 = &min_slot_table_size,
100 .extra2 = &max_slot_table_size
101 },
102 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500103 .procname = "tcp_slot_table_entries",
104 .data = &xprt_tcp_slot_table_entries,
105 .maxlen = sizeof(unsigned int),
106 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800107 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500108 .extra1 = &min_slot_table_size,
109 .extra2 = &max_slot_table_size
110 },
111 {
Trond Myklebustd9ba1312011-07-17 18:11:30 -0400112 .procname = "tcp_max_slot_table_entries",
113 .data = &xprt_max_tcp_slot_table_entries,
114 .maxlen = sizeof(unsigned int),
115 .mode = 0644,
116 .proc_handler = proc_dointvec_minmax,
117 .extra1 = &min_slot_table_size,
118 .extra2 = &max_tcp_slot_table_limit
119 },
120 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500121 .procname = "min_resvport",
122 .data = &xprt_min_resvport,
123 .maxlen = sizeof(unsigned int),
124 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800125 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500126 .extra1 = &xprt_min_resvport_limit,
127 .extra2 = &xprt_max_resvport_limit
128 },
129 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500130 .procname = "max_resvport",
131 .data = &xprt_max_resvport,
132 .maxlen = sizeof(unsigned int),
133 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800134 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500135 .extra1 = &xprt_min_resvport_limit,
136 .extra2 = &xprt_max_resvport_limit
137 },
138 {
Trond Myklebust25fe6142009-03-11 14:38:03 -0400139 .procname = "tcp_fin_timeout",
140 .data = &xs_tcp_fin_timeout,
141 .maxlen = sizeof(xs_tcp_fin_timeout),
142 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800143 .proc_handler = proc_dointvec_jiffies,
Trond Myklebust25fe6142009-03-11 14:38:03 -0400144 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800145 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500146};
147
Joe Perchesfe2c6332013-06-11 23:04:25 -0700148static struct ctl_table sunrpc_table[] = {
Chuck Leverfbf76682006-12-05 16:35:54 -0500149 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500150 .procname = "sunrpc",
151 .mode = 0555,
152 .child = xs_tunables_table
153 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800154 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500155};
156
157#endif
158
159/*
Chuck Lever03bf4b72005-08-25 16:25:55 -0700160 * Wait duration for a reply from the RPC portmapper.
161 */
162#define XS_BIND_TO (60U * HZ)
163
164/*
165 * Delay if a UDP socket connect error occurs. This is most likely some
166 * kind of resource problem on the local host.
167 */
168#define XS_UDP_REEST_TO (2U * HZ)
169
170/*
171 * The reestablish timeout allows clients to delay for a bit before attempting
172 * to reconnect to a server that just dropped our connection.
173 *
174 * We implement an exponential backoff when trying to reestablish a TCP
175 * transport connection with the server. Some servers like to drop a TCP
176 * connection when they are overworked, so we start with a short timeout and
177 * increase over time if the server is down or not responding.
178 */
179#define XS_TCP_INIT_REEST_TO (3U * HZ)
180#define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
181
182/*
183 * TCP idle timeout; client drops the transport socket if it is idle
184 * for this long. Note that we also timeout UDP sockets to prevent
185 * holding port numbers when there is no RPC traffic.
186 */
187#define XS_IDLE_DISC_TO (5U * 60 * HZ)
188
Jeff Laytonf895b252014-11-17 16:58:04 -0500189#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Levera246b012005-08-11 16:25:23 -0400190# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400191# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400192#endif
193
Chuck Levera246b012005-08-11 16:25:23 -0400194#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400195static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400196{
Chuck Lever9903cd12005-08-11 16:25:26 -0400197 u8 *buf = (u8 *) packet;
198 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400199
Chuck Lever46121cf2007-01-31 12:14:08 -0500200 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400201 for (j = 0; j < count && j < 128; j += 4) {
202 if (!(j & 31)) {
203 if (j)
204 dprintk("\n");
205 dprintk("0x%04x ", j);
206 }
207 dprintk("%02x%02x%02x%02x ",
208 buf[j], buf[j+1], buf[j+2], buf[j+3]);
209 }
210 dprintk("\n");
211}
212#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400213static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400214{
215 /* NOP */
216}
217#endif
218
Trond Myklebust21180712013-12-31 13:22:59 -0500219static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
220{
221 return (struct rpc_xprt *) sk->sk_user_data;
222}
223
Chuck Lever95392c52007-08-06 11:57:58 -0400224static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400225{
Chuck Lever95392c52007-08-06 11:57:58 -0400226 return (struct sockaddr *) &xprt->addr;
227}
228
Chuck Lever176e21e2011-05-09 15:22:44 -0400229static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
230{
231 return (struct sockaddr_un *) &xprt->addr;
232}
233
Chuck Lever95392c52007-08-06 11:57:58 -0400234static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
235{
236 return (struct sockaddr_in *) &xprt->addr;
237}
238
239static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
240{
241 return (struct sockaddr_in6 *) &xprt->addr;
242}
243
Chuck Leverc877b842009-08-09 15:09:36 -0400244static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400245{
Chuck Leverc877b842009-08-09 15:09:36 -0400246 struct sockaddr *sap = xs_addr(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400247 struct sockaddr_in6 *sin6;
248 struct sockaddr_in *sin;
Chuck Lever176e21e2011-05-09 15:22:44 -0400249 struct sockaddr_un *sun;
Chuck Leverc877b842009-08-09 15:09:36 -0400250 char buf[128];
Chuck Leveredb267a2006-08-22 20:06:18 -0400251
Chuck Lever9dc3b092009-08-09 15:09:46 -0400252 switch (sap->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400253 case AF_LOCAL:
254 sun = xs_addr_un(xprt);
255 strlcpy(buf, sun->sun_path, sizeof(buf));
256 xprt->address_strings[RPC_DISPLAY_ADDR] =
257 kstrdup(buf, GFP_KERNEL);
258 break;
Chuck Lever9dc3b092009-08-09 15:09:46 -0400259 case AF_INET:
Chuck Lever176e21e2011-05-09 15:22:44 -0400260 (void)rpc_ntop(sap, buf, sizeof(buf));
261 xprt->address_strings[RPC_DISPLAY_ADDR] =
262 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400263 sin = xs_addr_in(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800264 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
Chuck Lever9dc3b092009-08-09 15:09:46 -0400265 break;
266 case AF_INET6:
Chuck Lever176e21e2011-05-09 15:22:44 -0400267 (void)rpc_ntop(sap, buf, sizeof(buf));
268 xprt->address_strings[RPC_DISPLAY_ADDR] =
269 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400270 sin6 = xs_addr_in6(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800271 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400272 break;
273 default:
274 BUG();
Chuck Leveredb267a2006-08-22 20:06:18 -0400275 }
Chuck Lever176e21e2011-05-09 15:22:44 -0400276
Chuck Lever9dc3b092009-08-09 15:09:46 -0400277 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
Chuck Leveredb267a2006-08-22 20:06:18 -0400278}
279
Chuck Lever9dc3b092009-08-09 15:09:46 -0400280static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400281{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400282 struct sockaddr *sap = xs_addr(xprt);
283 char buf[128];
Chuck Lever4b6473f2007-08-06 11:57:12 -0400284
Joe Perches81160e662010-03-08 12:15:59 -0800285 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400286 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400287
Joe Perches81160e662010-03-08 12:15:59 -0800288 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400289 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
290}
Chuck Lever4b6473f2007-08-06 11:57:12 -0400291
Chuck Lever9dc3b092009-08-09 15:09:46 -0400292static void xs_format_peer_addresses(struct rpc_xprt *xprt,
293 const char *protocol,
294 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400295{
Chuck Leverb454ae92008-01-07 18:34:48 -0500296 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leverb454ae92008-01-07 18:34:48 -0500297 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leverc877b842009-08-09 15:09:36 -0400298 xs_format_common_peer_addresses(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400299 xs_format_common_peer_ports(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400300}
301
Chuck Lever9dc3b092009-08-09 15:09:46 -0400302static void xs_update_peer_port(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400303{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400304 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
305 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400306
Chuck Lever9dc3b092009-08-09 15:09:46 -0400307 xs_format_common_peer_ports(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400308}
309
310static void xs_free_peer_addresses(struct rpc_xprt *xprt)
311{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500312 unsigned int i;
313
314 for (i = 0; i < RPC_DISPLAY_MAX; i++)
315 switch (i) {
316 case RPC_DISPLAY_PROTO:
317 case RPC_DISPLAY_NETID:
318 continue;
319 default:
320 kfree(xprt->address_strings[i]);
321 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400322}
323
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400324#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
325
Trond Myklebust24c56842006-10-17 15:06:22 -0400326static 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 -0400327{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400328 struct msghdr msg = {
329 .msg_name = addr,
330 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400331 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
332 };
333 struct kvec iov = {
334 .iov_base = vec->iov_base + base,
335 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400336 };
337
Trond Myklebust24c56842006-10-17 15:06:22 -0400338 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400339 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
340 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
341}
342
Jason Baronf279cd02014-09-24 18:08:00 +0000343static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more, bool zerocopy, int *sent_p)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400344{
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500345 ssize_t (*do_sendpage)(struct socket *sock, struct page *page,
346 int offset, size_t size, int flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400347 struct page **ppage;
348 unsigned int remainder;
Jason Baronf279cd02014-09-24 18:08:00 +0000349 int err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400350
Trond Myklebust24c56842006-10-17 15:06:22 -0400351 remainder = xdr->page_len - base;
352 base += xdr->page_base;
353 ppage = xdr->pages + (base >> PAGE_SHIFT);
354 base &= ~PAGE_MASK;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500355 do_sendpage = sock->ops->sendpage;
356 if (!zerocopy)
357 do_sendpage = sock_no_sendpage;
Trond Myklebust24c56842006-10-17 15:06:22 -0400358 for(;;) {
359 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
360 int flags = XS_SENDMSG_FLAGS;
361
362 remainder -= len;
363 if (remainder != 0 || more)
364 flags |= MSG_MORE;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500365 err = do_sendpage(sock, *ppage, base, len, flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400366 if (remainder == 0 || err != len)
367 break;
Jason Baronf279cd02014-09-24 18:08:00 +0000368 *sent_p += err;
Trond Myklebust24c56842006-10-17 15:06:22 -0400369 ppage++;
370 base = 0;
371 }
Jason Baronf279cd02014-09-24 18:08:00 +0000372 if (err > 0) {
373 *sent_p += err;
374 err = 0;
375 }
376 return err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400377}
378
Chuck Lever9903cd12005-08-11 16:25:26 -0400379/**
380 * xs_sendpages - write pages directly to a socket
381 * @sock: socket to send on
382 * @addr: UDP only -- address of destination
383 * @addrlen: UDP only -- length of destination address
384 * @xdr: buffer containing this request
385 * @base: starting position in the buffer
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500386 * @zerocopy: true if it is safe to use sendpage()
Jason Baronf279cd02014-09-24 18:08:00 +0000387 * @sent_p: return the total number of bytes successfully queued for sending
Chuck Lever9903cd12005-08-11 16:25:26 -0400388 *
Chuck Levera246b012005-08-11 16:25:23 -0400389 */
Jason Baronf279cd02014-09-24 18:08:00 +0000390static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, bool zerocopy, int *sent_p)
Chuck Levera246b012005-08-11 16:25:23 -0400391{
Trond Myklebust24c56842006-10-17 15:06:22 -0400392 unsigned int remainder = xdr->len - base;
Jason Baronf279cd02014-09-24 18:08:00 +0000393 int err = 0;
394 int sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400395
Chuck Lever262965f2005-08-11 16:25:56 -0400396 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400397 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400398
399 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400400 if (base != 0) {
401 addr = NULL;
402 addrlen = 0;
403 }
Chuck Lever262965f2005-08-11 16:25:56 -0400404
Trond Myklebust24c56842006-10-17 15:06:22 -0400405 if (base < xdr->head[0].iov_len || addr != NULL) {
406 unsigned int len = xdr->head[0].iov_len - base;
407 remainder -= len;
408 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
409 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400410 goto out;
Jason Baronf279cd02014-09-24 18:08:00 +0000411 *sent_p += err;
Chuck Levera246b012005-08-11 16:25:23 -0400412 base = 0;
413 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400414 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400415
Trond Myklebust24c56842006-10-17 15:06:22 -0400416 if (base < xdr->page_len) {
417 unsigned int len = xdr->page_len - base;
418 remainder -= len;
Jason Baronf279cd02014-09-24 18:08:00 +0000419 err = xs_send_pagedata(sock, xdr, base, remainder != 0, zerocopy, &sent);
420 *sent_p += sent;
421 if (remainder == 0 || sent != len)
Chuck Levera246b012005-08-11 16:25:23 -0400422 goto out;
423 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400424 } else
425 base -= xdr->page_len;
426
427 if (base >= xdr->tail[0].iov_len)
Jason Baronf279cd02014-09-24 18:08:00 +0000428 return 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400429 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400430out:
Jason Baronf279cd02014-09-24 18:08:00 +0000431 if (err > 0) {
432 *sent_p += err;
433 err = 0;
434 }
435 return err;
Chuck Levera246b012005-08-11 16:25:23 -0400436}
437
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400438static void xs_nospace_callback(struct rpc_task *task)
439{
440 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
441
442 transport->inet->sk_write_pending--;
443 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
444}
445
Chuck Lever9903cd12005-08-11 16:25:26 -0400446/**
Chuck Lever262965f2005-08-11 16:25:56 -0400447 * xs_nospace - place task on wait queue if transmit was incomplete
448 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400449 *
Chuck Levera246b012005-08-11 16:25:23 -0400450 */
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400451static int xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400452{
Chuck Lever262965f2005-08-11 16:25:56 -0400453 struct rpc_rqst *req = task->tk_rqstp;
454 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500455 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500456 struct sock *sk = transport->inet;
Trond Myklebust24ca9a82011-11-22 14:44:28 +0200457 int ret = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400458
Chuck Lever46121cf2007-01-31 12:14:08 -0500459 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400460 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
461 req->rq_slen);
462
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400463 /* Protect against races with write_space */
464 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400465
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400466 /* Don't race with disconnect */
467 if (xprt_connected(xprt)) {
468 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
469 /*
470 * Notify TCP that we're limited by the application
471 * window size
472 */
473 set_bit(SOCK_NOSPACE, &transport->sock->flags);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500474 sk->sk_write_pending++;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400475 /* ...and wait for more buffer space */
476 xprt_wait_for_buffer_space(task, xs_nospace_callback);
477 }
478 } else {
479 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400480 ret = -ENOTCONN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400481 }
Chuck Lever262965f2005-08-11 16:25:56 -0400482
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400483 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500484
485 /* Race breaker in case memory is freed before above code is called */
486 sk->sk_write_space(sk);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400487 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400488}
489
Chuck Lever61677ee2011-05-09 15:22:34 -0400490/*
491 * Construct a stream transport record marker in @buf.
492 */
493static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
494{
495 u32 reclen = buf->len - sizeof(rpc_fraghdr);
496 rpc_fraghdr *base = buf->head[0].iov_base;
497 *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
498}
499
Chuck Lever262965f2005-08-11 16:25:56 -0400500/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400501 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
502 * @task: RPC task that manages the state of an RPC request
503 *
504 * Return values:
505 * 0: The request has been sent
506 * EAGAIN: The socket was blocked, please call again later to
507 * complete the request
508 * ENOTCONN: Caller needs to invoke connect logic then call again
509 * other: Some other error occured, the request was not sent
510 */
511static int xs_local_send_request(struct rpc_task *task)
512{
513 struct rpc_rqst *req = task->tk_rqstp;
514 struct rpc_xprt *xprt = req->rq_xprt;
515 struct sock_xprt *transport =
516 container_of(xprt, struct sock_xprt, xprt);
517 struct xdr_buf *xdr = &req->rq_snd_buf;
518 int status;
Jason Baronf279cd02014-09-24 18:08:00 +0000519 int sent = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -0400520
521 xs_encode_stream_record_marker(&req->rq_snd_buf);
522
523 xs_pktdump("packet data:",
524 req->rq_svec->iov_base, req->rq_svec->iov_len);
525
Jason Baronf279cd02014-09-24 18:08:00 +0000526 status = xs_sendpages(transport->sock, NULL, 0, xdr, req->rq_bytes_sent,
527 true, &sent);
Chuck Lever176e21e2011-05-09 15:22:44 -0400528 dprintk("RPC: %s(%u) = %d\n",
529 __func__, xdr->len - req->rq_bytes_sent, status);
Jason Baronf279cd02014-09-24 18:08:00 +0000530 if (likely(sent > 0) || status == 0) {
531 req->rq_bytes_sent += sent;
532 req->rq_xmit_bytes_sent += sent;
Chuck Lever176e21e2011-05-09 15:22:44 -0400533 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
534 req->rq_bytes_sent = 0;
535 return 0;
536 }
537 status = -EAGAIN;
538 }
539
540 switch (status) {
Trond Myklebust3601c4a2014-06-30 13:42:19 -0400541 case -ENOBUFS:
Trond Myklebustb5872f02015-07-03 09:32:23 -0400542 break;
Chuck Lever176e21e2011-05-09 15:22:44 -0400543 case -EAGAIN:
544 status = xs_nospace(task);
545 break;
546 default:
547 dprintk("RPC: sendmsg returned unrecognized error %d\n",
548 -status);
549 case -EPIPE:
550 xs_close(xprt);
551 status = -ENOTCONN;
552 }
553
554 return status;
555}
556
557/**
Chuck Lever262965f2005-08-11 16:25:56 -0400558 * xs_udp_send_request - write an RPC request to a UDP socket
559 * @task: address of RPC task that manages the state of an RPC request
560 *
561 * Return values:
562 * 0: The request has been sent
563 * EAGAIN: The socket was blocked, please call again later to
564 * complete the request
565 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300566 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -0400567 */
568static int xs_udp_send_request(struct rpc_task *task)
569{
570 struct rpc_rqst *req = task->tk_rqstp;
571 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500572 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400573 struct xdr_buf *xdr = &req->rq_snd_buf;
Jason Baronf279cd02014-09-24 18:08:00 +0000574 int sent = 0;
Chuck Lever262965f2005-08-11 16:25:56 -0400575 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400576
Chuck Lever9903cd12005-08-11 16:25:26 -0400577 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400578 req->rq_svec->iov_base,
579 req->rq_svec->iov_len);
580
Trond Myklebust01d37c42009-03-11 14:09:39 -0400581 if (!xprt_bound(xprt))
582 return -ENOTCONN;
Jason Baronf279cd02014-09-24 18:08:00 +0000583 status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen,
584 xdr, req->rq_bytes_sent, true, &sent);
Chuck Levera246b012005-08-11 16:25:23 -0400585
Chuck Lever46121cf2007-01-31 12:14:08 -0500586 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400587 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400588
Jason Baron3dedbb52014-09-24 18:08:04 +0000589 /* firewall is blocking us, don't return -EAGAIN or we end up looping */
590 if (status == -EPERM)
591 goto process_status;
592
Jason Baronf279cd02014-09-24 18:08:00 +0000593 if (sent > 0 || status == 0) {
594 req->rq_xmit_bytes_sent += sent;
595 if (sent >= req->rq_slen)
Trond Myklebust21997002007-10-01 11:43:37 -0400596 return 0;
597 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400598 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400599 }
Chuck Levera246b012005-08-11 16:25:23 -0400600
Jason Baron3dedbb52014-09-24 18:08:04 +0000601process_status:
Chuck Lever262965f2005-08-11 16:25:56 -0400602 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400603 case -ENOTSOCK:
604 status = -ENOTCONN;
605 /* Should we call xs_close() here? */
606 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400607 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400608 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400609 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400610 default:
611 dprintk("RPC: sendmsg returned unrecognized error %d\n",
612 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400613 case -ENETUNREACH:
Trond Myklebust3601c4a2014-06-30 13:42:19 -0400614 case -ENOBUFS:
Chuck Lever262965f2005-08-11 16:25:56 -0400615 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400616 case -ECONNREFUSED:
Jason Baron3dedbb52014-09-24 18:08:04 +0000617 case -EPERM:
Chuck Levera246b012005-08-11 16:25:23 -0400618 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400619 * prompts ECONNREFUSED. */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400620 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400621 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800622
Chuck Lever262965f2005-08-11 16:25:56 -0400623 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400624}
625
Trond Myklebuste06799f2007-11-05 15:44:12 -0500626/**
Chuck Lever262965f2005-08-11 16:25:56 -0400627 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400628 * @task: address of RPC task that manages the state of an RPC request
629 *
630 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400631 * 0: The request has been sent
632 * EAGAIN: The socket was blocked, please call again later to
633 * complete the request
634 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300635 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400636 *
637 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400638 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400639 */
Chuck Lever262965f2005-08-11 16:25:56 -0400640static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400641{
642 struct rpc_rqst *req = task->tk_rqstp;
643 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500644 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400645 struct xdr_buf *xdr = &req->rq_snd_buf;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500646 bool zerocopy = true;
Chuck Leverb595bb12007-08-06 11:56:42 -0400647 int status;
Jason Baronf279cd02014-09-24 18:08:00 +0000648 int sent;
Chuck Levera246b012005-08-11 16:25:23 -0400649
Chuck Lever61677ee2011-05-09 15:22:34 -0400650 xs_encode_stream_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400651
Chuck Lever262965f2005-08-11 16:25:56 -0400652 xs_pktdump("packet data:",
653 req->rq_svec->iov_base,
654 req->rq_svec->iov_len);
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500655 /* Don't use zero copy if this is a resend. If the RPC call
656 * completes while the socket holds a reference to the pages,
657 * then we may end up resending corrupted data.
658 */
659 if (task->tk_flags & RPC_TASK_SENT)
660 zerocopy = false;
Chuck Levera246b012005-08-11 16:25:23 -0400661
662 /* Continue transmitting the packet/record. We must be careful
663 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400664 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400665 while (1) {
Jason Baronf279cd02014-09-24 18:08:00 +0000666 sent = 0;
667 status = xs_sendpages(transport->sock, NULL, 0, xdr,
668 req->rq_bytes_sent, zerocopy, &sent);
Chuck Levera246b012005-08-11 16:25:23 -0400669
Chuck Lever46121cf2007-01-31 12:14:08 -0500670 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400671 xdr->len - req->rq_bytes_sent, status);
672
Jason Baronf279cd02014-09-24 18:08:00 +0000673 if (unlikely(sent == 0 && status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400674 break;
675
Chuck Lever262965f2005-08-11 16:25:56 -0400676 /* If we've sent the entire packet, immediately
677 * reset the count of bytes sent. */
Jason Baronf279cd02014-09-24 18:08:00 +0000678 req->rq_bytes_sent += sent;
679 req->rq_xmit_bytes_sent += sent;
Chuck Lever262965f2005-08-11 16:25:56 -0400680 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
681 req->rq_bytes_sent = 0;
682 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400683 }
684
Jason Baronf279cd02014-09-24 18:08:00 +0000685 if (sent != 0)
Trond Myklebust06b4b682008-04-16 16:51:38 -0400686 continue;
Chuck Levera246b012005-08-11 16:25:23 -0400687 status = -EAGAIN;
Trond Myklebust06b4b682008-04-16 16:51:38 -0400688 break;
Chuck Levera246b012005-08-11 16:25:23 -0400689 }
690
Chuck Lever262965f2005-08-11 16:25:56 -0400691 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400692 case -ENOTSOCK:
693 status = -ENOTCONN;
694 /* Should we call xs_close() here? */
695 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400696 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400697 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400698 break;
699 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500700 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400701 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400702 case -ECONNRESET:
Chuck Lever262965f2005-08-11 16:25:56 -0400703 case -ECONNREFUSED:
704 case -ENOTCONN:
Trond Myklebust3913c782015-02-08 21:44:04 -0500705 case -EADDRINUSE:
Trond Myklebustb5872f02015-07-03 09:32:23 -0400706 case -ENOBUFS:
Trond Myklebustb9d2bb22012-10-23 11:40:02 -0400707 case -EPIPE:
Chuck Lever262965f2005-08-11 16:25:56 -0400708 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400709 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800710
Chuck Levera246b012005-08-11 16:25:23 -0400711 return status;
712}
713
Chuck Lever9903cd12005-08-11 16:25:26 -0400714/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400715 * xs_tcp_release_xprt - clean up after a tcp transmission
716 * @xprt: transport
717 * @task: rpc task
718 *
719 * This cleans up if an error causes us to abort the transmission of a request.
720 * In this case, the socket may need to be reset in order to avoid confusing
721 * the server.
722 */
723static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
724{
725 struct rpc_rqst *req;
726
727 if (task != xprt->snd_task)
728 return;
729 if (task == NULL)
730 goto out_release;
731 req = task->tk_rqstp;
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400732 if (req == NULL)
733 goto out_release;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400734 if (req->rq_bytes_sent == 0)
735 goto out_release;
736 if (req->rq_bytes_sent == req->rq_snd_buf.len)
737 goto out_release;
Trond Myklebusta4f08352013-01-08 09:10:21 -0500738 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400739out_release:
740 xprt_release_xprt(xprt, task);
741}
742
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400743static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
744{
745 transport->old_data_ready = sk->sk_data_ready;
746 transport->old_state_change = sk->sk_state_change;
747 transport->old_write_space = sk->sk_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -0500748 transport->old_error_report = sk->sk_error_report;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400749}
750
751static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
752{
753 sk->sk_data_ready = transport->old_data_ready;
754 sk->sk_state_change = transport->old_state_change;
755 sk->sk_write_space = transport->old_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -0500756 sk->sk_error_report = transport->old_error_report;
757}
758
Trond Myklebustb70ae912015-02-09 09:41:32 -0500759static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
760{
761 smp_mb__before_atomic();
762 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
763 clear_bit(XPRT_CLOSING, &xprt->state);
764 smp_mb__after_atomic();
765}
766
767static void xs_sock_mark_closed(struct rpc_xprt *xprt)
768{
769 xs_sock_reset_connection_flags(xprt);
770 /* Mark transport as closed and wake up all pending tasks */
771 xprt_disconnect_done(xprt);
Trond Myklebust4876cc72015-06-19 16:17:57 -0400772 xprt_force_disconnect(xprt);
Trond Myklebustb70ae912015-02-09 09:41:32 -0500773}
774
Trond Myklebust21180712013-12-31 13:22:59 -0500775/**
776 * xs_error_report - callback to handle TCP socket state errors
777 * @sk: socket
778 *
779 * Note: we don't call sock_error() since there may be a rpc_task
780 * using the socket, and so we don't want to clear sk->sk_err.
781 */
782static void xs_error_report(struct sock *sk)
783{
784 struct rpc_xprt *xprt;
785 int err;
786
787 read_lock_bh(&sk->sk_callback_lock);
788 if (!(xprt = xprt_from_sock(sk)))
789 goto out;
790
791 err = -sk->sk_err;
792 if (err == 0)
793 goto out;
Trond Myklebustb70ae912015-02-09 09:41:32 -0500794 /* Is this a reset event? */
795 if (sk->sk_state == TCP_CLOSE)
796 xs_sock_mark_closed(xprt);
Trond Myklebust21180712013-12-31 13:22:59 -0500797 dprintk("RPC: xs_error_report client %p, error=%d...\n",
798 xprt, -err);
Trond Myklebuste8353c762013-12-31 13:39:22 -0500799 trace_rpc_socket_error(xprt, sk->sk_socket, err);
Trond Myklebust21180712013-12-31 13:22:59 -0500800 xprt_wake_pending_tasks(xprt, err);
801 out:
802 read_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400803}
804
Chuck Leverfe315e72009-03-11 14:10:21 -0400805static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400806{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500807 struct socket *sock = transport->sock;
808 struct sock *sk = transport->inet;
Trond Myklebust6cc7e902015-02-08 18:35:25 -0500809 struct rpc_xprt *xprt = &transport->xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400810
Chuck Leverfe315e72009-03-11 14:10:21 -0400811 if (sk == NULL)
812 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400813
Jeff Layton264d1df2015-06-03 16:14:27 -0400814 if (atomic_read(&transport->xprt.swapper))
815 sk_clear_memalloc(sk);
816
Chuck Levera246b012005-08-11 16:25:23 -0400817 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500818 transport->inet = NULL;
819 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400820
Chuck Lever9903cd12005-08-11 16:25:26 -0400821 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400822
823 xs_restore_old_callbacks(transport, sk);
Chuck Levera246b012005-08-11 16:25:23 -0400824 write_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust6cc7e902015-02-08 18:35:25 -0500825 xs_sock_reset_connection_flags(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400826
Trond Myklebust6cc7e902015-02-08 18:35:25 -0500827 trace_rpc_socket_close(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -0400828 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -0400829}
830
831/**
832 * xs_close - close a socket
833 * @xprt: transport
834 *
835 * This is used when all requests are complete; ie, no DRC state remains
836 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400837 *
838 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
839 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e72009-03-11 14:10:21 -0400840 */
841static void xs_close(struct rpc_xprt *xprt)
842{
843 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
844
845 dprintk("RPC: xs_close xprt %p\n", xprt);
846
847 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -0400848 xprt->reestablish_timeout = 0;
Chuck Leverfe315e72009-03-11 14:10:21 -0400849
Trond Myklebust62da3b22007-11-06 18:44:20 -0500850 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400851}
852
Chuck Lever4a068252015-05-11 14:02:25 -0400853static void xs_inject_disconnect(struct rpc_xprt *xprt)
854{
855 dprintk("RPC: injecting transport disconnect on xprt=%p\n",
856 xprt);
857 xprt_disconnect_done(xprt);
858}
859
Kinglong Mee315f3812014-03-24 11:07:22 +0800860static void xs_xprt_free(struct rpc_xprt *xprt)
861{
862 xs_free_peer_addresses(xprt);
863 xprt_free(xprt);
864}
865
Chuck Lever9903cd12005-08-11 16:25:26 -0400866/**
867 * xs_destroy - prepare to shutdown a transport
868 * @xprt: doomed transport
869 *
870 */
871static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400872{
Chuck Lever46121cf2007-01-31 12:14:08 -0500873 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400874
Trond Myklebusta1311d82013-10-31 09:18:49 -0400875 xs_close(xprt);
Kinglong Mee315f3812014-03-24 11:07:22 +0800876 xs_xprt_free(xprt);
Trond Myklebusta1311d82013-10-31 09:18:49 -0400877 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400878}
879
Chuck Lever176e21e2011-05-09 15:22:44 -0400880static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
881{
882 struct xdr_skb_reader desc = {
883 .skb = skb,
884 .offset = sizeof(rpc_fraghdr),
885 .count = skb->len - sizeof(rpc_fraghdr),
886 };
887
888 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
889 return -1;
890 if (desc.count)
891 return -1;
892 return 0;
893}
894
895/**
896 * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets
897 * @sk: socket with data to read
Chuck Lever176e21e2011-05-09 15:22:44 -0400898 *
899 * Currently this assumes we can read the whole reply in a single gulp.
900 */
David S. Miller676d2362014-04-11 16:15:36 -0400901static void xs_local_data_ready(struct sock *sk)
Chuck Lever176e21e2011-05-09 15:22:44 -0400902{
903 struct rpc_task *task;
904 struct rpc_xprt *xprt;
905 struct rpc_rqst *rovr;
906 struct sk_buff *skb;
907 int err, repsize, copied;
908 u32 _xid;
909 __be32 *xp;
910
911 read_lock_bh(&sk->sk_callback_lock);
912 dprintk("RPC: %s...\n", __func__);
913 xprt = xprt_from_sock(sk);
914 if (xprt == NULL)
915 goto out;
916
917 skb = skb_recv_datagram(sk, 0, 1, &err);
918 if (skb == NULL)
919 goto out;
920
Chuck Lever176e21e2011-05-09 15:22:44 -0400921 repsize = skb->len - sizeof(rpc_fraghdr);
922 if (repsize < 4) {
923 dprintk("RPC: impossible RPC reply size %d\n", repsize);
924 goto dropit;
925 }
926
927 /* Copy the XID from the skb... */
928 xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
929 if (xp == NULL)
930 goto dropit;
931
932 /* Look up and lock the request corresponding to the given XID */
933 spin_lock(&xprt->transport_lock);
934 rovr = xprt_lookup_rqst(xprt, *xp);
935 if (!rovr)
936 goto out_unlock;
937 task = rovr->rq_task;
938
939 copied = rovr->rq_private_buf.buflen;
940 if (copied > repsize)
941 copied = repsize;
942
943 if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
944 dprintk("RPC: sk_buff copy failed\n");
945 goto out_unlock;
946 }
947
948 xprt_complete_rqst(task, copied);
949
950 out_unlock:
951 spin_unlock(&xprt->transport_lock);
952 dropit:
953 skb_free_datagram(sk, skb);
954 out:
955 read_unlock_bh(&sk->sk_callback_lock);
956}
957
Chuck Lever9903cd12005-08-11 16:25:26 -0400958/**
959 * xs_udp_data_ready - "data ready" callback for UDP sockets
960 * @sk: socket with data to read
Chuck Lever9903cd12005-08-11 16:25:26 -0400961 *
962 */
David S. Miller676d2362014-04-11 16:15:36 -0400963static void xs_udp_data_ready(struct sock *sk)
Chuck Lever9903cd12005-08-11 16:25:26 -0400964{
965 struct rpc_task *task;
966 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400967 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400968 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400969 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700970 u32 _xid;
971 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400972
Eric Dumazetf064af12010-09-22 12:43:39 +0000973 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500974 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400975 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400976 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400977
978 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
979 goto out;
980
Chuck Levera246b012005-08-11 16:25:23 -0400981 repsize = skb->len - sizeof(struct udphdr);
982 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500983 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400984 goto dropit;
985 }
986
987 /* Copy the XID from the skb... */
988 xp = skb_header_pointer(skb, sizeof(struct udphdr),
989 sizeof(_xid), &_xid);
990 if (xp == NULL)
991 goto dropit;
992
993 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400994 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400995 rovr = xprt_lookup_rqst(xprt, *xp);
996 if (!rovr)
997 goto out_unlock;
998 task = rovr->rq_task;
999
Chuck Levera246b012005-08-11 16:25:23 -04001000 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1001 copied = repsize;
1002
1003 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001004 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
1005 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -04001006 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001007 }
1008
1009 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -04001010
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001011 xprt_adjust_cwnd(xprt, task, copied);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001012 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -04001013
1014 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001015 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001016 dropit:
1017 skb_free_datagram(sk, skb);
1018 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001019 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001020}
1021
Trond Myklebusta519fc72012-09-12 16:49:15 -04001022/*
1023 * Helper function to force a TCP close if the server is sending
1024 * junk and/or it has put us in CLOSE_WAIT
1025 */
1026static void xs_tcp_force_close(struct rpc_xprt *xprt)
1027{
Trond Myklebusta519fc72012-09-12 16:49:15 -04001028 xprt_force_disconnect(xprt);
1029}
1030
Chuck Leverdd456472006-12-05 16:35:44 -05001031static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001032{
Chuck Lever51971132006-12-05 16:35:19 -05001033 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001034 size_t len, used;
1035 char *p;
1036
Chuck Lever51971132006-12-05 16:35:19 -05001037 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
1038 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001039 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001040 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001041 if (used != len)
1042 return;
Chuck Lever808012f2005-08-25 16:25:49 -07001043
Chuck Lever51971132006-12-05 16:35:19 -05001044 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
1045 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -05001046 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -04001047 else
Chuck Levere136d092006-12-05 16:35:23 -05001048 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -05001049 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -07001050
Chuck Levere136d092006-12-05 16:35:23 -05001051 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001052 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -07001053
Chuck Levera246b012005-08-11 16:25:23 -04001054 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001055 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001056 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebusta519fc72012-09-12 16:49:15 -04001057 xs_tcp_force_close(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001058 return;
Chuck Levera246b012005-08-11 16:25:23 -04001059 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001060 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -05001061 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001062}
1063
Chuck Lever51971132006-12-05 16:35:19 -05001064static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001065{
Chuck Lever51971132006-12-05 16:35:19 -05001066 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001067 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001068 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001069 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
1070 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1071 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001072 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001073 }
1074 }
1075}
1076
Chuck Leverdd456472006-12-05 16:35:44 -05001077static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001078{
1079 size_t len, used;
1080 char *p;
1081
Chuck Lever51971132006-12-05 16:35:19 -05001082 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -05001083 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001084 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001085 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001086 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001087 if (used != len)
1088 return;
Chuck Levere136d092006-12-05 16:35:23 -05001089 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001090 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -05001091 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001092 dprintk("RPC: reading %s XID %08x\n",
1093 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1094 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -05001095 ntohl(transport->tcp_xid));
1096 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001097}
1098
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001099static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1100 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001101{
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001102 size_t len, used;
1103 u32 offset;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001104 char *p;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001105
1106 /*
1107 * We want transport->tcp_offset to be 8 at the end of this routine
1108 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1109 * When this function is called for the first time,
1110 * transport->tcp_offset is 4 (after having already read the xid).
1111 */
1112 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001113 len = sizeof(transport->tcp_calldir) - offset;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001114 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001115 p = ((char *) &transport->tcp_calldir) + offset;
1116 used = xdr_skb_read_bits(desc, p, len);
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001117 transport->tcp_offset += used;
1118 if (used != len)
1119 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001120 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001121 /*
1122 * We don't yet have the XDR buffer, so we will write the calldir
1123 * out after we get the buffer from the 'struct rpc_rqst'
1124 */
Trond Myklebustb76ce562010-06-16 13:57:32 -04001125 switch (ntohl(transport->tcp_calldir)) {
1126 case RPC_REPLY:
1127 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1128 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001129 transport->tcp_flags |= TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001130 break;
1131 case RPC_CALL:
1132 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1133 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001134 transport->tcp_flags &= ~TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001135 break;
1136 default:
1137 dprintk("RPC: invalid request message type\n");
Trond Myklebusta519fc72012-09-12 16:49:15 -04001138 xs_tcp_force_close(&transport->xprt);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001139 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001140 xs_tcp_check_fraghdr(transport);
1141}
1142
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001143static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
1144 struct xdr_skb_reader *desc,
1145 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001146{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001147 struct sock_xprt *transport =
1148 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001149 struct xdr_buf *rcvbuf;
1150 size_t len;
1151 ssize_t r;
1152
Chuck Levera246b012005-08-11 16:25:23 -04001153 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001154
1155 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1156 /*
1157 * Save the RPC direction in the XDR buffer
1158 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001159 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
Trond Myklebustb76ce562010-06-16 13:57:32 -04001160 &transport->tcp_calldir,
1161 sizeof(transport->tcp_calldir));
1162 transport->tcp_copied += sizeof(transport->tcp_calldir);
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001163 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -04001164 }
1165
Chuck Levera246b012005-08-11 16:25:23 -04001166 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -05001167 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -05001168 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -04001169
Chuck Lever51971132006-12-05 16:35:19 -05001170 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001171 memcpy(&my_desc, desc, sizeof(my_desc));
1172 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -05001173 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001174 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001175 desc->count -= r;
1176 desc->offset += r;
1177 } else
Chuck Lever51971132006-12-05 16:35:19 -05001178 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001179 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001180
1181 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001182 transport->tcp_copied += r;
1183 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001184 }
1185 if (r != len) {
1186 /* Error when copying to the receive buffer,
1187 * usually because we weren't able to allocate
1188 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001189 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001190 * will not receive any additional updates,
1191 * and time out.
1192 * Any remaining data from this record will
1193 * be discarded.
1194 */
Chuck Levere136d092006-12-05 16:35:23 -05001195 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001196 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001197 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001198 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1199 "tcp_offset = %u, tcp_reclen = %u\n",
1200 xprt, transport->tcp_copied,
1201 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001202 return;
Chuck Levera246b012005-08-11 16:25:23 -04001203 }
1204
Chuck Lever46121cf2007-01-31 12:14:08 -05001205 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001206 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001207 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1208 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1209 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001210
Chuck Lever51971132006-12-05 16:35:19 -05001211 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001212 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001213 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001214 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1215 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001216 }
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001217}
1218
1219/*
1220 * Finds the request corresponding to the RPC xid and invokes the common
1221 * tcp read code to read the data.
1222 */
1223static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1224 struct xdr_skb_reader *desc)
1225{
1226 struct sock_xprt *transport =
1227 container_of(xprt, struct sock_xprt, xprt);
1228 struct rpc_rqst *req;
1229
1230 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1231
1232 /* Find and lock the request corresponding to this xid */
1233 spin_lock(&xprt->transport_lock);
1234 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1235 if (!req) {
1236 dprintk("RPC: XID %08x request not found!\n",
1237 ntohl(transport->tcp_xid));
1238 spin_unlock(&xprt->transport_lock);
1239 return -1;
1240 }
1241
1242 xs_tcp_read_common(xprt, desc, req);
1243
Chuck Levere136d092006-12-05 16:35:23 -05001244 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001245 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001246
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001247 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001248 return 0;
1249}
1250
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001251#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001252/*
1253 * Obtains an rpc_rqst previously allocated and invokes the common
1254 * tcp read code to read the data. The result is placed in the callback
1255 * queue.
1256 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1257 * connection and return -1.
1258 */
Trond Myklebust2ea24492014-02-10 11:18:39 -05001259static int xs_tcp_read_callback(struct rpc_xprt *xprt,
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001260 struct xdr_skb_reader *desc)
1261{
1262 struct sock_xprt *transport =
1263 container_of(xprt, struct sock_xprt, xprt);
1264 struct rpc_rqst *req;
1265
Trond Myklebust2ea24492014-02-10 11:18:39 -05001266 /* Look up and lock the request corresponding to the given XID */
1267 spin_lock(&xprt->transport_lock);
1268 req = xprt_lookup_bc_request(xprt, transport->tcp_xid);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001269 if (req == NULL) {
Trond Myklebust2ea24492014-02-10 11:18:39 -05001270 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001271 printk(KERN_WARNING "Callback slot table overflowed\n");
1272 xprt_force_disconnect(xprt);
1273 return -1;
1274 }
1275
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001276 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1277 xs_tcp_read_common(xprt, desc, req);
1278
Trond Myklebust2ea24492014-02-10 11:18:39 -05001279 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
1280 xprt_complete_bc_request(req, transport->tcp_copied);
1281 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001282
1283 return 0;
1284}
1285
1286static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1287 struct xdr_skb_reader *desc)
1288{
1289 struct sock_xprt *transport =
1290 container_of(xprt, struct sock_xprt, xprt);
1291
1292 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1293 xs_tcp_read_reply(xprt, desc) :
1294 xs_tcp_read_callback(xprt, desc);
1295}
1296#else
1297static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1298 struct xdr_skb_reader *desc)
1299{
1300 return xs_tcp_read_reply(xprt, desc);
1301}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001302#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001303
1304/*
1305 * Read data off the transport. This can be either an RPC_CALL or an
1306 * RPC_REPLY. Relay the processing to helper functions.
1307 */
1308static void xs_tcp_read_data(struct rpc_xprt *xprt,
1309 struct xdr_skb_reader *desc)
1310{
1311 struct sock_xprt *transport =
1312 container_of(xprt, struct sock_xprt, xprt);
1313
1314 if (_xs_tcp_read_data(xprt, desc) == 0)
1315 xs_tcp_check_fraghdr(transport);
1316 else {
1317 /*
1318 * The transport_lock protects the request handling.
1319 * There's no need to hold it to update the tcp_flags.
1320 */
1321 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1322 }
Chuck Levera246b012005-08-11 16:25:23 -04001323}
1324
Chuck Leverdd456472006-12-05 16:35:44 -05001325static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001326{
1327 size_t len;
1328
Chuck Lever51971132006-12-05 16:35:19 -05001329 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001330 if (len > desc->count)
1331 len = desc->count;
1332 desc->count -= len;
1333 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001334 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001335 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001336 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001337}
1338
Chuck Lever9903cd12005-08-11 16:25:26 -04001339static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
Chuck Levera246b012005-08-11 16:25:23 -04001340{
1341 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001342 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001343 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001344 .skb = skb,
1345 .offset = offset,
1346 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001347 };
Chuck Levera246b012005-08-11 16:25:23 -04001348
Chuck Lever46121cf2007-01-31 12:14:08 -05001349 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001350 do {
Jeff Layton1a867a02014-10-28 14:24:14 -04001351 trace_xs_tcp_data_recv(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001352 /* Read in a new fragment marker if necessary */
1353 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001354 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001355 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001356 continue;
1357 }
1358 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001359 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001360 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001361 continue;
1362 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001363 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001364 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001365 xs_tcp_read_calldir(transport, &desc);
1366 continue;
1367 }
Chuck Levera246b012005-08-11 16:25:23 -04001368 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001369 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001370 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001371 continue;
1372 }
1373 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001374 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001375 } while (desc.count);
Jeff Layton1a867a02014-10-28 14:24:14 -04001376 trace_xs_tcp_data_recv(transport);
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
Chuck Lever9903cd12005-08-11 16:25:26 -04001384 *
1385 */
David S. Miller676d2362014-04-11 16:15:36 -04001386static void xs_tcp_data_ready(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001387{
1388 struct rpc_xprt *xprt;
1389 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001390 int read;
Jeff Layton3705ad62014-10-28 14:24:13 -04001391 unsigned long total = 0;
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);
Jeff Layton3705ad62014-10-28 14:24:13 -04001396 if (!(xprt = xprt_from_sock(sk))) {
1397 read = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001398 goto out;
Jeff Layton3705ad62014-10-28 14:24:13 -04001399 }
Neil Brown61d0a8e2009-09-23 14:36:37 -04001400 /* Any data means we had a useful conversation, so
1401 * the we don't need to delay the next reconnect
1402 */
1403 if (xprt->reestablish_timeout)
1404 xprt->reestablish_timeout = 0;
1405
Chuck Lever9903cd12005-08-11 16:25:26 -04001406 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001407 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001408 do {
1409 rd_desc.count = 65536;
1410 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
Jeff Layton3705ad62014-10-28 14:24:13 -04001411 if (read > 0)
1412 total += read;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001413 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001414out:
Jeff Layton3705ad62014-10-28 14:24:13 -04001415 trace_xs_tcp_data_ready(xprt, read, total);
Eric Dumazetf064af12010-09-22 12:43:39 +00001416 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001417}
1418
Chuck Lever9903cd12005-08-11 16:25:26 -04001419/**
1420 * xs_tcp_state_change - callback to handle TCP socket state changes
1421 * @sk: socket whose state has changed
1422 *
1423 */
1424static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001425{
Chuck Lever9903cd12005-08-11 16:25:26 -04001426 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001427
Eric Dumazetf064af12010-09-22 12:43:39 +00001428 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001429 if (!(xprt = xprt_from_sock(sk)))
1430 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001431 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001432 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001433 sk->sk_state, xprt_connected(xprt),
1434 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001435 sock_flag(sk, SOCK_ZAPPED),
1436 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001437
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001438 trace_rpc_socket_state_change(xprt, sk->sk_socket);
Chuck Levera246b012005-08-11 16:25:23 -04001439 switch (sk->sk_state) {
1440 case TCP_ESTABLISHED:
Eric Dumazetf064af12010-09-22 12:43:39 +00001441 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001442 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001443 struct sock_xprt *transport = container_of(xprt,
1444 struct sock_xprt, xprt);
1445
Chuck Levera246b012005-08-11 16:25:23 -04001446 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001447 transport->tcp_offset = 0;
1448 transport->tcp_reclen = 0;
1449 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001450 transport->tcp_flags =
1451 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Trond Myklebust8b717982013-09-26 10:18:04 -04001452 xprt->connect_cookie++;
Chuck Lever51971132006-12-05 16:35:19 -05001453
Trond Myklebust2a491992009-03-11 14:38:00 -04001454 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001455 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001456 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001457 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001458 case TCP_FIN_WAIT1:
1459 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001460 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001461 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001462 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001463 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001464 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001465 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001466 smp_mb__after_atomic();
Chuck Levera246b012005-08-11 16:25:23 -04001467 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001468 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001469 /* The server initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001470 xprt->connect_cookie++;
Trond Myklebustd0bea452012-10-23 11:35:47 -04001471 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebusta519fc72012-09-12 16:49:15 -04001472 xs_tcp_force_close(xprt);
Trond Myklebust663b8852008-01-01 18:42:12 -05001473 case TCP_CLOSING:
1474 /*
1475 * If the server closed down the connection, make sure that
1476 * we back off before reconnecting
1477 */
1478 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1479 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001480 break;
1481 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001482 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001483 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001484 clear_bit(XPRT_CONNECTED, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001485 smp_mb__after_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001486 break;
1487 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001488 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001489 }
1490 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001491 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001492}
1493
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001494static void xs_write_space(struct sock *sk)
1495{
1496 struct socket *sock;
1497 struct rpc_xprt *xprt;
1498
1499 if (unlikely(!(sock = sk->sk_socket)))
1500 return;
1501 clear_bit(SOCK_NOSPACE, &sock->flags);
1502
1503 if (unlikely(!(xprt = xprt_from_sock(sk))))
1504 return;
1505 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1506 return;
1507
1508 xprt_write_space(xprt);
1509}
1510
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001511/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001512 * xs_udp_write_space - callback invoked when socket buffer space
1513 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001514 * @sk: socket whose state has changed
1515 *
Chuck Levera246b012005-08-11 16:25:23 -04001516 * Called when more output buffer space is available for this socket.
1517 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001518 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001519 * with a bunch of small requests.
1520 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001521static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001522{
Eric Dumazetf064af12010-09-22 12:43:39 +00001523 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001524
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001525 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001526 if (sock_writeable(sk))
1527 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001528
Eric Dumazetf064af12010-09-22 12:43:39 +00001529 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001530}
Chuck Levera246b012005-08-11 16:25:23 -04001531
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001532/**
1533 * xs_tcp_write_space - callback invoked when socket buffer space
1534 * becomes available
1535 * @sk: socket whose state has changed
1536 *
1537 * Called when more output buffer space is available for this socket.
1538 * We try not to wake our writers until they can make "significant"
1539 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1540 * with a bunch of small requests.
1541 */
1542static void xs_tcp_write_space(struct sock *sk)
1543{
Eric Dumazetf064af12010-09-22 12:43:39 +00001544 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001545
1546 /* from net/core/stream.c:sk_stream_write_space */
Eric Dumazet64dc6132013-07-22 20:26:31 -07001547 if (sk_stream_is_writeable(sk))
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001548 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001549
Eric Dumazetf064af12010-09-22 12:43:39 +00001550 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001551}
1552
Chuck Lever470056c2005-08-25 16:25:56 -07001553static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001554{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001555 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1556 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001557
Chuck Lever7c6e0662006-12-05 16:35:30 -05001558 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001559 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001560 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001561 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001562 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001563 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001564 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001565 sk->sk_write_space(sk);
1566 }
1567}
1568
Chuck Lever43118c22005-08-25 16:25:49 -07001569/**
Chuck Lever470056c2005-08-25 16:25:56 -07001570 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001571 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001572 * @sndsize: requested size of send buffer, in bytes
1573 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001574 *
Chuck Lever470056c2005-08-25 16:25:56 -07001575 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001576 */
Chuck Lever470056c2005-08-25 16:25:56 -07001577static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001578{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001579 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1580
1581 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001582 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001583 transport->sndsize = sndsize + 1024;
1584 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001585 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001586 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001587
1588 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001589}
1590
Chuck Lever46c0ee82005-08-25 16:25:52 -07001591/**
1592 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1593 * @task: task that timed out
1594 *
1595 * Adjust the congestion window after a retransmit timeout has occurred.
1596 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001597static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever46c0ee82005-08-25 16:25:52 -07001598{
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001599 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
Chuck Lever46c0ee82005-08-25 16:25:52 -07001600}
1601
Chuck Leverb85d8802006-05-25 01:40:49 -04001602static unsigned short xs_get_random_port(void)
1603{
1604 unsigned short range = xprt_max_resvport - xprt_min_resvport;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001605 unsigned short rand = (unsigned short) prandom_u32() % range;
Chuck Leverb85d8802006-05-25 01:40:49 -04001606 return rand + xprt_min_resvport;
1607}
1608
Chuck Lever92200412006-01-03 09:55:51 +01001609/**
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001610 * xs_set_reuseaddr_port - set the socket's port and address reuse options
1611 * @sock: socket
1612 *
1613 * Note that this function has to be called on all sockets that share the
1614 * same port, and it must be called before binding.
1615 */
1616static void xs_sock_set_reuseport(struct socket *sock)
1617{
Trond Myklebust402e23b2015-02-09 17:20:14 -05001618 int opt = 1;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001619
Trond Myklebust402e23b2015-02-09 17:20:14 -05001620 kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
1621 (char *)&opt, sizeof(opt));
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001622}
1623
1624static unsigned short xs_sock_getport(struct socket *sock)
1625{
1626 struct sockaddr_storage buf;
1627 int buflen;
1628 unsigned short port = 0;
1629
1630 if (kernel_getsockname(sock, (struct sockaddr *)&buf, &buflen) < 0)
1631 goto out;
1632 switch (buf.ss_family) {
1633 case AF_INET6:
1634 port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
1635 break;
1636 case AF_INET:
1637 port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
1638 }
1639out:
1640 return port;
1641}
1642
1643/**
Chuck Lever92200412006-01-03 09:55:51 +01001644 * xs_set_port - reset the port number in the remote endpoint address
1645 * @xprt: generic transport
1646 * @port: new port number
1647 *
1648 */
1649static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1650{
Chuck Lever46121cf2007-01-31 12:14:08 -05001651 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001652
Chuck Lever9dc3b092009-08-09 15:09:46 -04001653 rpc_set_port(xs_addr(xprt), port);
1654 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001655}
1656
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001657static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
1658{
1659 if (transport->srcport == 0)
1660 transport->srcport = xs_sock_getport(sock);
1661}
1662
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001663static unsigned short xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001664{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001665 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001666
1667 if (port == 0 && transport->xprt.resvport)
1668 port = xs_get_random_port();
1669 return port;
1670}
1671
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001672static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001673{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001674 if (transport->srcport != 0)
1675 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001676 if (!transport->xprt.resvport)
1677 return 0;
1678 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1679 return xprt_max_resvport;
1680 return --port;
1681}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001682static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001683{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001684 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001685 int err, nloop = 0;
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001686 unsigned short port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001687 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001688
Chris Perl0f7a6222014-09-05 15:40:21 -04001689 /*
1690 * If we are asking for any ephemeral port (i.e. port == 0 &&
1691 * transport->xprt.resvport == 0), don't bind. Let the local
1692 * port selection happen implicitly when the socket is used
1693 * (for example at connect time).
1694 *
1695 * This ensures that we can continue to establish TCP
1696 * connections even when all local ephemeral ports are already
1697 * a part of some TCP connection. This makes no difference
1698 * for UDP sockets, but also doens't harm them.
1699 *
1700 * If we're asking for any reserved port (i.e. port == 0 &&
1701 * transport->xprt.resvport == 1) xs_get_srcport above will
1702 * ensure that port is non-zero and we will bind as needed.
1703 */
1704 if (port == 0)
1705 return 0;
1706
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);
Chuck Levera246b012005-08-11 16:25:23 -04001712 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001713 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001714 break;
Chuck Levera246b012005-08-11 16:25:23 -04001715 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001716 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001717 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001718 if (port > last)
1719 nloop++;
1720 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001721
Chuck Lever4232e862010-10-20 11:52:51 -04001722 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001723 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1724 &((struct sockaddr_in *)&myaddr)->sin_addr,
1725 port, err ? "failed" : "ok", err);
1726 else
1727 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1728 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1729 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001730 return err;
1731}
1732
Chuck Lever176e21e2011-05-09 15:22:44 -04001733/*
1734 * We don't support autobind on AF_LOCAL sockets
1735 */
1736static void xs_local_rpcbind(struct rpc_task *task)
1737{
Trond Myklebust3dc0da22013-01-08 09:31:13 -05001738 rcu_read_lock();
1739 xprt_set_bound(rcu_dereference(task->tk_client->cl_xprt));
1740 rcu_read_unlock();
Chuck Lever176e21e2011-05-09 15:22:44 -04001741}
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
Chuck Lever176e21e2011-05-09 15:22:44 -04001755 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1756 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1757}
1758
Chuck Lever8945ee52007-08-06 11:58:04 -04001759static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001760{
1761 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001762
Chuck Lever8945ee52007-08-06 11:58:04 -04001763 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1764 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1765}
Peter Zijlstraed075362006-12-06 20:35:24 -08001766
Chuck Lever8945ee52007-08-06 11:58:04 -04001767static inline void xs_reclassify_socket6(struct socket *sock)
1768{
1769 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001770
Chuck Lever8945ee52007-08-06 11:58:04 -04001771 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1772 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001773}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001774
1775static inline void xs_reclassify_socket(int family, struct socket *sock)
1776{
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -04001777 WARN_ON_ONCE(sock_owned_by_user(sock->sk));
1778 if (sock_owned_by_user(sock->sk))
1779 return;
1780
Chuck Lever4232e862010-10-20 11:52:51 -04001781 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001782 case AF_LOCAL:
1783 xs_reclassify_socketu(sock);
1784 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001785 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001786 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001787 break;
1788 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001789 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001790 break;
1791 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001792}
Peter Zijlstraed075362006-12-06 20:35:24 -08001793#else
Chuck Lever176e21e2011-05-09 15:22:44 -04001794static inline void xs_reclassify_socketu(struct socket *sock)
1795{
1796}
1797
Chuck Lever8945ee52007-08-06 11:58:04 -04001798static inline void xs_reclassify_socket4(struct socket *sock)
1799{
1800}
1801
1802static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001803{
1804}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001805
1806static inline void xs_reclassify_socket(int family, struct socket *sock)
1807{
1808}
Peter Zijlstraed075362006-12-06 20:35:24 -08001809#endif
1810
NeilBrown93dc41b2013-10-31 16:14:36 +11001811static void xs_dummy_setup_socket(struct work_struct *work)
1812{
1813}
1814
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001815static struct socket *xs_create_sock(struct rpc_xprt *xprt,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001816 struct sock_xprt *transport, int family, int type,
1817 int protocol, bool reuseport)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001818{
1819 struct socket *sock;
1820 int err;
1821
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001822 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001823 if (err < 0) {
1824 dprintk("RPC: can't create %d transport socket (%d).\n",
1825 protocol, -err);
1826 goto out;
1827 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001828 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001829
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001830 if (reuseport)
1831 xs_sock_set_reuseport(sock);
1832
Ben Hutchings4cea2882011-02-22 21:54:34 +00001833 err = xs_bind(transport, sock);
1834 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001835 sock_release(sock);
1836 goto out;
1837 }
1838
1839 return sock;
1840out:
1841 return ERR_PTR(err);
1842}
1843
Chuck Lever176e21e2011-05-09 15:22:44 -04001844static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1845 struct socket *sock)
1846{
1847 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1848 xprt);
1849
1850 if (!transport->inet) {
1851 struct sock *sk = sock->sk;
1852
1853 write_lock_bh(&sk->sk_callback_lock);
1854
1855 xs_save_old_callbacks(transport, sk);
1856
1857 sk->sk_user_data = xprt;
1858 sk->sk_data_ready = xs_local_data_ready;
1859 sk->sk_write_space = xs_udp_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -05001860 sk->sk_error_report = xs_error_report;
Chuck Lever176e21e2011-05-09 15:22:44 -04001861 sk->sk_allocation = GFP_ATOMIC;
1862
1863 xprt_clear_connected(xprt);
1864
1865 /* Reset to new socket */
1866 transport->sock = sock;
1867 transport->inet = sk;
1868
1869 write_unlock_bh(&sk->sk_callback_lock);
1870 }
1871
1872 /* Tell the socket layer to start connecting... */
1873 xprt->stat.connect_count++;
1874 xprt->stat.connect_start = jiffies;
1875 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1876}
1877
1878/**
1879 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
Chuck Lever176e21e2011-05-09 15:22:44 -04001880 * @transport: socket transport to connect
Chuck Lever176e21e2011-05-09 15:22:44 -04001881 */
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001882static int xs_local_setup_socket(struct sock_xprt *transport)
Chuck Lever176e21e2011-05-09 15:22:44 -04001883{
Chuck Lever176e21e2011-05-09 15:22:44 -04001884 struct rpc_xprt *xprt = &transport->xprt;
1885 struct socket *sock;
1886 int status = -EIO;
1887
Chuck Lever176e21e2011-05-09 15:22:44 -04001888 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1889 SOCK_STREAM, 0, &sock, 1);
1890 if (status < 0) {
1891 dprintk("RPC: can't create AF_LOCAL "
1892 "transport socket (%d).\n", -status);
1893 goto out;
1894 }
1895 xs_reclassify_socketu(sock);
1896
1897 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1898 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1899
1900 status = xs_local_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001901 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever176e21e2011-05-09 15:22:44 -04001902 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);
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001907 case -ENOBUFS:
Chuck Lever176e21e2011-05-09 15:22:44 -04001908 break;
1909 case -ENOENT:
1910 dprintk("RPC: xprt %p: socket %s does not exist\n",
1911 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1912 break;
Trond Myklebust4a20a982012-12-15 17:02:29 -05001913 case -ECONNREFUSED:
1914 dprintk("RPC: xprt %p: connection refused for %s\n",
1915 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1916 break;
Chuck Lever176e21e2011-05-09 15:22:44 -04001917 default:
1918 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1919 __func__, -status,
1920 xprt->address_strings[RPC_DISPLAY_ADDR]);
1921 }
1922
1923out:
1924 xprt_clear_connecting(xprt);
1925 xprt_wake_pending_tasks(xprt, status);
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001926 return status;
1927}
1928
Linus Torvaldsb6669732013-02-28 18:02:55 -08001929static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001930{
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001931 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1932 int ret;
1933
1934 if (RPC_IS_ASYNC(task)) {
1935 /*
1936 * We want the AF_LOCAL connect to be resolved in the
1937 * filesystem namespace of the process making the rpc
1938 * call. Thus we connect synchronously.
1939 *
1940 * If we want to support asynchronous AF_LOCAL calls,
1941 * we'll need to figure out how to pass a namespace to
1942 * connect.
1943 */
1944 rpc_exit(task, -ENOTCONN);
1945 return;
1946 }
1947 ret = xs_local_setup_socket(transport);
1948 if (ret && !RPC_IS_SOFTCONN(task))
1949 msleep_interruptible(15000);
Chuck Lever176e21e2011-05-09 15:22:44 -04001950}
1951
Jeff Layton3c87ef62015-06-03 16:14:25 -04001952#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Jeff Laytond6e971d2015-06-03 16:14:28 -04001953/*
1954 * Note that this should be called with XPRT_LOCKED held (or when we otherwise
1955 * know that we have exclusive access to the socket), to guard against
1956 * races with xs_reset_transport.
1957 */
Mel Gormana564b8f2012-07-31 16:45:12 -07001958static void xs_set_memalloc(struct rpc_xprt *xprt)
1959{
1960 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1961 xprt);
1962
Jeff Laytond6e971d2015-06-03 16:14:28 -04001963 /*
1964 * If there's no sock, then we have nothing to set. The
1965 * reconnecting process will get it for us.
1966 */
1967 if (!transport->inet)
1968 return;
Jeff Layton8e228132015-06-03 16:14:26 -04001969 if (atomic_read(&xprt->swapper))
Mel Gormana564b8f2012-07-31 16:45:12 -07001970 sk_set_memalloc(transport->inet);
1971}
1972
1973/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04001974 * xs_enable_swap - Tag this transport as being used for swap.
Mel Gormana564b8f2012-07-31 16:45:12 -07001975 * @xprt: transport to tag
Mel Gormana564b8f2012-07-31 16:45:12 -07001976 *
Jeff Layton8e228132015-06-03 16:14:26 -04001977 * Take a reference to this transport on behalf of the rpc_clnt, and
1978 * optionally mark it for swapping if it wasn't already.
Mel Gormana564b8f2012-07-31 16:45:12 -07001979 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04001980static int
1981xs_enable_swap(struct rpc_xprt *xprt)
Mel Gormana564b8f2012-07-31 16:45:12 -07001982{
Jeff Laytond6e971d2015-06-03 16:14:28 -04001983 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Mel Gormana564b8f2012-07-31 16:45:12 -07001984
Jeff Laytond6e971d2015-06-03 16:14:28 -04001985 if (atomic_inc_return(&xprt->swapper) != 1)
1986 return 0;
1987 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
1988 return -ERESTARTSYS;
1989 if (xs->inet)
1990 sk_set_memalloc(xs->inet);
1991 xprt_release_xprt(xprt, NULL);
Jeff Layton8e228132015-06-03 16:14:26 -04001992 return 0;
1993}
1994
1995/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04001996 * xs_disable_swap - Untag this transport as being used for swap.
Jeff Layton8e228132015-06-03 16:14:26 -04001997 * @xprt: transport to tag
1998 *
1999 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
2000 * swapper refcount goes to 0, untag the socket as a memalloc socket.
2001 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002002static void
2003xs_disable_swap(struct rpc_xprt *xprt)
Jeff Layton8e228132015-06-03 16:14:26 -04002004{
Jeff Laytond6e971d2015-06-03 16:14:28 -04002005 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Jeff Layton8e228132015-06-03 16:14:26 -04002006
Jeff Laytond6e971d2015-06-03 16:14:28 -04002007 if (!atomic_dec_and_test(&xprt->swapper))
2008 return;
2009 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2010 return;
2011 if (xs->inet)
2012 sk_clear_memalloc(xs->inet);
2013 xprt_release_xprt(xprt, NULL);
Mel Gormana564b8f2012-07-31 16:45:12 -07002014}
Mel Gormana564b8f2012-07-31 16:45:12 -07002015#else
2016static void xs_set_memalloc(struct rpc_xprt *xprt)
2017{
2018}
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002019
2020static int
2021xs_enable_swap(struct rpc_xprt *xprt)
2022{
2023 return -EINVAL;
2024}
2025
2026static void
2027xs_disable_swap(struct rpc_xprt *xprt)
2028{
2029}
Mel Gormana564b8f2012-07-31 16:45:12 -07002030#endif
2031
Chuck Lever16be2d22007-08-06 11:57:38 -04002032static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04002033{
Chuck Lever16be2d22007-08-06 11:57:38 -04002034 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04002035
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002036 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002037 struct sock *sk = sock->sk;
2038
2039 write_lock_bh(&sk->sk_callback_lock);
2040
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002041 xs_save_old_callbacks(transport, sk);
2042
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002043 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002044 sk->sk_data_ready = xs_udp_data_ready;
2045 sk->sk_write_space = xs_udp_write_space;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05002046 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002047
2048 xprt_set_connected(xprt);
2049
2050 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002051 transport->sock = sock;
2052 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002053
Mel Gormana564b8f2012-07-31 16:45:12 -07002054 xs_set_memalloc(xprt);
2055
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002056 write_unlock_bh(&sk->sk_callback_lock);
2057 }
Chuck Lever470056c2005-08-25 16:25:56 -07002058 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04002059}
2060
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002061static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04002062{
2063 struct sock_xprt *transport =
2064 container_of(work, struct sock_xprt, connect_worker.work);
2065 struct rpc_xprt *xprt = &transport->xprt;
2066 struct socket *sock = transport->sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002067 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04002068
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002069 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002070 xs_addr(xprt)->sa_family, SOCK_DGRAM,
2071 IPPROTO_UDP, false);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002072 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04002073 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04002074
Chuck Leverc740eff2009-08-09 15:09:46 -04002075 dprintk("RPC: worker connecting xprt %p via %s to "
2076 "%s (port %s)\n", xprt,
2077 xprt->address_strings[RPC_DISPLAY_PROTO],
2078 xprt->address_strings[RPC_DISPLAY_ADDR],
2079 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04002080
2081 xs_udp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002082 trace_rpc_socket_connect(xprt, sock, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002083 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002084out:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002085 xprt_unlock_connect(xprt, transport);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002086 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002087 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002088}
2089
Trond Myklebust4876cc72015-06-19 16:17:57 -04002090/**
2091 * xs_tcp_shutdown - gracefully shut down a TCP socket
2092 * @xprt: transport
2093 *
2094 * Initiates a graceful shutdown of the TCP socket by calling the
2095 * equivalent of shutdown(SHUT_RDWR);
2096 */
2097static void xs_tcp_shutdown(struct rpc_xprt *xprt)
2098{
2099 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2100 struct socket *sock = transport->sock;
2101
2102 if (sock == NULL)
2103 return;
2104 if (xprt_connected(xprt)) {
2105 kernel_sock_shutdown(sock, SHUT_RDWR);
2106 trace_rpc_socket_shutdown(xprt, sock);
2107 } else
2108 xs_reset_transport(transport);
2109}
2110
Chuck Lever16be2d22007-08-06 11:57:38 -04002111static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002112{
Chuck Lever16be2d22007-08-06 11:57:38 -04002113 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002114 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002115
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002116 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002117 struct sock *sk = sock->sk;
Trond Myklebust7f260e82013-09-24 11:25:22 -04002118 unsigned int keepidle = xprt->timeout->to_initval / HZ;
2119 unsigned int keepcnt = xprt->timeout->to_retries + 1;
2120 unsigned int opt_on = 1;
Trond Myklebust775f06a2015-06-20 15:31:54 -04002121 unsigned int timeo;
Trond Myklebust7f260e82013-09-24 11:25:22 -04002122
2123 /* TCP Keepalive options */
2124 kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
2125 (char *)&opt_on, sizeof(opt_on));
2126 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
2127 (char *)&keepidle, sizeof(keepidle));
2128 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
2129 (char *)&keepidle, sizeof(keepidle));
2130 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
2131 (char *)&keepcnt, sizeof(keepcnt));
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002132
Trond Myklebust775f06a2015-06-20 15:31:54 -04002133 /* TCP user timeout (see RFC5482) */
2134 timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
2135 (xprt->timeout->to_retries + 1);
2136 kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT,
2137 (char *)&timeo, sizeof(timeo));
2138
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002139 write_lock_bh(&sk->sk_callback_lock);
2140
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002141 xs_save_old_callbacks(transport, sk);
2142
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002143 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002144 sk->sk_data_ready = xs_tcp_data_ready;
2145 sk->sk_state_change = xs_tcp_state_change;
2146 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -05002147 sk->sk_error_report = xs_error_report;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05002148 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07002149
2150 /* socket options */
Chuck Lever3167e122005-08-25 16:25:55 -07002151 sock_reset_flag(sk, SOCK_LINGER);
Chuck Lever3167e122005-08-25 16:25:55 -07002152 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002153
2154 xprt_clear_connected(xprt);
2155
2156 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002157 transport->sock = sock;
2158 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002159
2160 write_unlock_bh(&sk->sk_callback_lock);
2161 }
2162
Trond Myklebust01d37c42009-03-11 14:09:39 -04002163 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002164 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002165
Mel Gormana564b8f2012-07-31 16:45:12 -07002166 xs_set_memalloc(xprt);
2167
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002168 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05002169 xprt->stat.connect_count++;
2170 xprt->stat.connect_start = jiffies;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002171 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2172 switch (ret) {
2173 case 0:
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002174 xs_set_srcport(transport, sock);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002175 case -EINPROGRESS:
2176 /* SYN_SENT! */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002177 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2178 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2179 }
2180out:
2181 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002182}
2183
2184/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002185 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
Chuck Lever16be2d22007-08-06 11:57:38 -04002186 *
2187 * Invoked by a work queue tasklet.
2188 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002189static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002190{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002191 struct sock_xprt *transport =
2192 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002193 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002194 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002195 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002196
Chuck Lever16be2d22007-08-06 11:57:38 -04002197 if (!sock) {
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002198 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002199 xs_addr(xprt)->sa_family, SOCK_STREAM,
2200 IPPROTO_TCP, true);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002201 if (IS_ERR(sock)) {
2202 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002203 goto out;
2204 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002205 }
2206
Chuck Leverc740eff2009-08-09 15:09:46 -04002207 dprintk("RPC: worker connecting xprt %p via %s to "
2208 "%s (port %s)\n", xprt,
2209 xprt->address_strings[RPC_DISPLAY_PROTO],
2210 xprt->address_strings[RPC_DISPLAY_ADDR],
2211 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002212
2213 status = xs_tcp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002214 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever46121cf2007-01-31 12:14:08 -05002215 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2216 xprt, -status, xprt_connected(xprt),
2217 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002218 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002219 default:
2220 printk("%s: connect returned unhandled error %d\n",
2221 __func__, status);
2222 case -EADDRNOTAVAIL:
2223 /* We're probably in TIME_WAIT. Get rid of existing socket,
2224 * and retry
2225 */
Trond Myklebusta519fc72012-09-12 16:49:15 -04002226 xs_tcp_force_close(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002227 break;
Trond Myklebust2a491992009-03-11 14:38:00 -04002228 case 0:
2229 case -EINPROGRESS:
2230 case -EALREADY:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002231 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002232 xprt_clear_connecting(xprt);
2233 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002234 case -EINVAL:
2235 /* Happens, for instance, if the user specified a link
2236 * local IPv6 address without a scope-id.
2237 */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002238 case -ECONNREFUSED:
2239 case -ECONNRESET:
2240 case -ENETUNREACH:
Trond Myklebust3913c782015-02-08 21:44:04 -05002241 case -EADDRINUSE:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002242 case -ENOBUFS:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002243 /* retry with existing socket, after a delay */
Trond Myklebust4efdd922015-02-08 15:34:28 -05002244 xs_tcp_force_close(xprt);
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002245 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002246 }
Trond Myklebust2a491992009-03-11 14:38:00 -04002247 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002248out:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002249 xprt_unlock_connect(xprt, transport);
Chuck Lever2226feb2005-08-11 16:25:38 -04002250 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002251 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04002252}
2253
Chuck Lever68e220b2007-08-06 11:57:48 -04002254/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002255 * xs_connect - connect a socket to a remote endpoint
Trond Myklebust1b092092013-01-08 09:26:49 -05002256 * @xprt: pointer to transport structure
Chuck Lever9903cd12005-08-11 16:25:26 -04002257 * @task: address of RPC task that manages state of connect request
2258 *
2259 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002260 *
2261 * UDP socket connects are synchronous, but we use a work queue anyway
2262 * to guarantee that even unprivileged user processes can set up a
2263 * socket on a privileged port.
2264 *
2265 * If a UDP socket connect fails, the delay behavior here prevents
2266 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002267 */
Trond Myklebust1b092092013-01-08 09:26:49 -05002268static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002269{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002270 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002271
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002272 WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2273
Trond Myklebustde84d892015-02-08 16:49:48 -05002274 /* Start by resetting any existing state */
2275 xs_reset_transport(transport);
2276
Chuck Lever09a21c42009-12-03 15:58:56 -05002277 if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002278 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2279 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002280 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002281 queue_delayed_work(rpciod_workqueue,
2282 &transport->connect_worker,
2283 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07002284 xprt->reestablish_timeout <<= 1;
Neil Brown61d0a8e2009-09-23 14:36:37 -04002285 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2286 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002287 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2288 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002289 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05002290 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002291 queue_delayed_work(rpciod_workqueue,
2292 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002293 }
2294}
2295
Chuck Lever262ca072006-03-20 13:44:16 -05002296/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002297 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2298 * @xprt: rpc_xprt struct containing statistics
2299 * @seq: output file
2300 *
2301 */
2302static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2303{
2304 long idle_time = 0;
2305
2306 if (xprt_connected(xprt))
2307 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2308
2309 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
Andy Adamson15a45202012-02-14 16:19:18 -05002310 "%llu %llu %lu %llu %llu\n",
Chuck Lever176e21e2011-05-09 15:22:44 -04002311 xprt->stat.bind_count,
2312 xprt->stat.connect_count,
2313 xprt->stat.connect_time,
2314 idle_time,
2315 xprt->stat.sends,
2316 xprt->stat.recvs,
2317 xprt->stat.bad_xids,
2318 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002319 xprt->stat.bklog_u,
2320 xprt->stat.max_slots,
2321 xprt->stat.sending_u,
2322 xprt->stat.pending_u);
Chuck Lever176e21e2011-05-09 15:22:44 -04002323}
2324
2325/**
Chuck Lever262ca072006-03-20 13:44:16 -05002326 * xs_udp_print_stats - display UDP socket-specifc stats
2327 * @xprt: rpc_xprt struct containing statistics
2328 * @seq: output file
2329 *
2330 */
2331static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2332{
Chuck Leverc8475462006-12-05 16:35:26 -05002333 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2334
Andy Adamson15a45202012-02-14 16:19:18 -05002335 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2336 "%lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002337 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002338 xprt->stat.bind_count,
2339 xprt->stat.sends,
2340 xprt->stat.recvs,
2341 xprt->stat.bad_xids,
2342 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002343 xprt->stat.bklog_u,
2344 xprt->stat.max_slots,
2345 xprt->stat.sending_u,
2346 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002347}
2348
2349/**
2350 * xs_tcp_print_stats - display TCP socket-specifc stats
2351 * @xprt: rpc_xprt struct containing statistics
2352 * @seq: output file
2353 *
2354 */
2355static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2356{
Chuck Leverc8475462006-12-05 16:35:26 -05002357 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002358 long idle_time = 0;
2359
2360 if (xprt_connected(xprt))
2361 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2362
Andy Adamson15a45202012-02-14 16:19:18 -05002363 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2364 "%llu %llu %lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002365 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002366 xprt->stat.bind_count,
2367 xprt->stat.connect_count,
2368 xprt->stat.connect_time,
2369 idle_time,
2370 xprt->stat.sends,
2371 xprt->stat.recvs,
2372 xprt->stat.bad_xids,
2373 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002374 xprt->stat.bklog_u,
2375 xprt->stat.max_slots,
2376 xprt->stat.sending_u,
2377 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002378}
2379
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002380/*
2381 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2382 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2383 * to use the server side send routines.
2384 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002385static void *bc_malloc(struct rpc_task *task, size_t size)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002386{
2387 struct page *page;
2388 struct rpc_buffer *buf;
2389
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002390 WARN_ON_ONCE(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2391 if (size > PAGE_SIZE - sizeof(struct rpc_buffer))
2392 return NULL;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002393
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002394 page = alloc_page(GFP_KERNEL);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002395 if (!page)
2396 return NULL;
2397
2398 buf = page_address(page);
2399 buf->len = PAGE_SIZE;
2400
2401 return buf->data;
2402}
2403
2404/*
2405 * Free the space allocated in the bc_alloc routine
2406 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002407static void bc_free(void *buffer)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002408{
2409 struct rpc_buffer *buf;
2410
2411 if (!buffer)
2412 return;
2413
2414 buf = container_of(buffer, struct rpc_buffer, data);
2415 free_page((unsigned long)buf);
2416}
2417
2418/*
2419 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2420 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2421 */
2422static int bc_sendto(struct rpc_rqst *req)
2423{
2424 int len;
2425 struct xdr_buf *xbufp = &req->rq_snd_buf;
2426 struct rpc_xprt *xprt = req->rq_xprt;
2427 struct sock_xprt *transport =
2428 container_of(xprt, struct sock_xprt, xprt);
2429 struct socket *sock = transport->sock;
2430 unsigned long headoff;
2431 unsigned long tailoff;
2432
Chuck Lever61677ee2011-05-09 15:22:34 -04002433 xs_encode_stream_record_marker(xbufp);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002434
2435 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2436 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2437 len = svc_send_common(sock, xbufp,
2438 virt_to_page(xbufp->head[0].iov_base), headoff,
2439 xbufp->tail[0].iov_base, tailoff);
2440
2441 if (len != xbufp->len) {
2442 printk(KERN_NOTICE "Error sending entire callback!\n");
2443 len = -EAGAIN;
2444 }
2445
2446 return len;
2447}
2448
2449/*
2450 * The send routine. Borrows from svc_send
2451 */
2452static int bc_send_request(struct rpc_task *task)
2453{
2454 struct rpc_rqst *req = task->tk_rqstp;
2455 struct svc_xprt *xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002456 u32 len;
2457
2458 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2459 /*
2460 * Get the server socket associated with this callback xprt
2461 */
2462 xprt = req->rq_xprt->bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002463
2464 /*
2465 * Grab the mutex to serialize data as the connection is shared
2466 * with the fore channel
2467 */
2468 if (!mutex_trylock(&xprt->xpt_mutex)) {
2469 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2470 if (!mutex_trylock(&xprt->xpt_mutex))
2471 return -EAGAIN;
2472 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2473 }
2474 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2475 len = -ENOTCONN;
2476 else
2477 len = bc_sendto(req);
2478 mutex_unlock(&xprt->xpt_mutex);
2479
2480 if (len > 0)
2481 len = 0;
2482
2483 return len;
2484}
2485
2486/*
2487 * The close routine. Since this is client initiated, we do nothing
2488 */
2489
2490static void bc_close(struct rpc_xprt *xprt)
2491{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002492}
2493
2494/*
2495 * The xprt destroy routine. Again, because this connection is client
2496 * initiated, we do nothing
2497 */
2498
2499static void bc_destroy(struct rpc_xprt *xprt)
2500{
Kinglong Mee47f72ef2014-03-24 11:58:16 +08002501 dprintk("RPC: bc_destroy xprt %p\n", xprt);
2502
2503 xs_xprt_free(xprt);
2504 module_put(THIS_MODULE);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002505}
2506
Chuck Lever176e21e2011-05-09 15:22:44 -04002507static struct rpc_xprt_ops xs_local_ops = {
2508 .reserve_xprt = xprt_reserve_xprt,
2509 .release_xprt = xs_tcp_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002510 .alloc_slot = xprt_alloc_slot,
Chuck Lever176e21e2011-05-09 15:22:44 -04002511 .rpcbind = xs_local_rpcbind,
2512 .set_port = xs_local_set_port,
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002513 .connect = xs_local_connect,
Chuck Lever176e21e2011-05-09 15:22:44 -04002514 .buf_alloc = rpc_malloc,
2515 .buf_free = rpc_free,
2516 .send_request = xs_local_send_request,
2517 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2518 .close = xs_close,
Trond Myklebusta1311d82013-10-31 09:18:49 -04002519 .destroy = xs_destroy,
Chuck Lever176e21e2011-05-09 15:22:44 -04002520 .print_stats = xs_local_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002521 .enable_swap = xs_enable_swap,
2522 .disable_swap = xs_disable_swap,
Chuck Lever176e21e2011-05-09 15:22:44 -04002523};
2524
Chuck Lever262965f2005-08-11 16:25:56 -04002525static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002526 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002527 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002528 .release_xprt = xprt_release_xprt_cong,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002529 .alloc_slot = xprt_alloc_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002530 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002531 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002532 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002533 .buf_alloc = rpc_malloc,
2534 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002535 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002536 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002537 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002538 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002539 .close = xs_close,
2540 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002541 .print_stats = xs_udp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002542 .enable_swap = xs_enable_swap,
2543 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002544 .inject_disconnect = xs_inject_disconnect,
Chuck Lever262965f2005-08-11 16:25:56 -04002545};
2546
2547static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002548 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002549 .release_xprt = xs_tcp_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002550 .alloc_slot = xprt_lock_and_alloc_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002551 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002552 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002553 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002554 .buf_alloc = rpc_malloc,
2555 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002556 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002557 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustc627d312015-02-10 11:06:04 -05002558 .close = xs_tcp_shutdown,
Chuck Lever9903cd12005-08-11 16:25:26 -04002559 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002560 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002561 .enable_swap = xs_enable_swap,
2562 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002563 .inject_disconnect = xs_inject_disconnect,
Chuck Levera246b012005-08-11 16:25:23 -04002564};
2565
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002566/*
2567 * The rpc_xprt_ops for the server backchannel
2568 */
2569
2570static struct rpc_xprt_ops bc_tcp_ops = {
2571 .reserve_xprt = xprt_reserve_xprt,
2572 .release_xprt = xprt_release_xprt,
Bryan Schumaker84e28a32012-09-24 13:39:01 -04002573 .alloc_slot = xprt_alloc_slot,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002574 .buf_alloc = bc_malloc,
2575 .buf_free = bc_free,
2576 .send_request = bc_send_request,
2577 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2578 .close = bc_close,
2579 .destroy = bc_destroy,
2580 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002581 .enable_swap = xs_enable_swap,
2582 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002583 .inject_disconnect = xs_inject_disconnect,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002584};
2585
Chuck Lever92476852010-10-20 11:53:01 -04002586static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2587{
2588 static const struct sockaddr_in sin = {
2589 .sin_family = AF_INET,
2590 .sin_addr.s_addr = htonl(INADDR_ANY),
2591 };
2592 static const struct sockaddr_in6 sin6 = {
2593 .sin6_family = AF_INET6,
2594 .sin6_addr = IN6ADDR_ANY_INIT,
2595 };
2596
2597 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002598 case AF_LOCAL:
2599 break;
Chuck Lever92476852010-10-20 11:53:01 -04002600 case AF_INET:
2601 memcpy(sap, &sin, sizeof(sin));
2602 break;
2603 case AF_INET6:
2604 memcpy(sap, &sin6, sizeof(sin6));
2605 break;
2606 default:
2607 dprintk("RPC: %s: Bad address family\n", __func__);
2608 return -EAFNOSUPPORT;
2609 }
2610 return 0;
2611}
2612
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002613static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002614 unsigned int slot_table_size,
2615 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002616{
2617 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002618 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002619
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002620 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002621 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002622 return ERR_PTR(-EBADF);
2623 }
2624
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002625 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2626 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002627 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002628 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2629 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002630 return ERR_PTR(-ENOMEM);
2631 }
2632
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002633 new = container_of(xprt, struct sock_xprt, xprt);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002634 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2635 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002636 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002637 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002638 else {
2639 int err;
2640 err = xs_init_anyaddr(args->dstaddr->sa_family,
2641 (struct sockaddr *)&new->srcaddr);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002642 if (err != 0) {
2643 xprt_free(xprt);
Chuck Lever92476852010-10-20 11:53:01 -04002644 return ERR_PTR(err);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002645 }
Chuck Lever92476852010-10-20 11:53:01 -04002646 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002647
2648 return xprt;
2649}
2650
Chuck Lever176e21e2011-05-09 15:22:44 -04002651static const struct rpc_timeout xs_local_default_timeout = {
2652 .to_initval = 10 * HZ,
2653 .to_maxval = 10 * HZ,
2654 .to_retries = 2,
2655};
2656
2657/**
2658 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2659 * @args: rpc transport creation arguments
2660 *
2661 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2662 */
2663static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2664{
2665 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2666 struct sock_xprt *transport;
2667 struct rpc_xprt *xprt;
2668 struct rpc_xprt *ret;
2669
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002670 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2671 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002672 if (IS_ERR(xprt))
2673 return xprt;
2674 transport = container_of(xprt, struct sock_xprt, xprt);
2675
2676 xprt->prot = 0;
2677 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2678 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2679
2680 xprt->bind_timeout = XS_BIND_TO;
2681 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2682 xprt->idle_timeout = XS_IDLE_DISC_TO;
2683
2684 xprt->ops = &xs_local_ops;
2685 xprt->timeout = &xs_local_default_timeout;
2686
NeilBrown93dc41b2013-10-31 16:14:36 +11002687 INIT_DELAYED_WORK(&transport->connect_worker,
2688 xs_dummy_setup_socket);
2689
Chuck Lever176e21e2011-05-09 15:22:44 -04002690 switch (sun->sun_family) {
2691 case AF_LOCAL:
2692 if (sun->sun_path[0] != '/') {
2693 dprintk("RPC: bad AF_LOCAL address: %s\n",
2694 sun->sun_path);
2695 ret = ERR_PTR(-EINVAL);
2696 goto out_err;
2697 }
2698 xprt_set_bound(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002699 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
J. Bruce Fields7073ea82013-02-21 10:14:22 -05002700 ret = ERR_PTR(xs_local_setup_socket(transport));
2701 if (ret)
2702 goto out_err;
Chuck Lever176e21e2011-05-09 15:22:44 -04002703 break;
2704 default:
2705 ret = ERR_PTR(-EAFNOSUPPORT);
2706 goto out_err;
2707 }
2708
2709 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2710 xprt->address_strings[RPC_DISPLAY_ADDR]);
2711
2712 if (try_module_get(THIS_MODULE))
2713 return xprt;
2714 ret = ERR_PTR(-EINVAL);
2715out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002716 xs_xprt_free(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002717 return ret;
2718}
2719
Trond Myklebust2881ae72007-12-20 16:03:54 -05002720static const struct rpc_timeout xs_udp_default_timeout = {
2721 .to_initval = 5 * HZ,
2722 .to_maxval = 30 * HZ,
2723 .to_increment = 5 * HZ,
2724 .to_retries = 5,
2725};
2726
Chuck Lever9903cd12005-08-11 16:25:26 -04002727/**
2728 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002729 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002730 *
2731 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002732static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002733{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002734 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002735 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002736 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002737 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002738
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002739 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2740 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002741 if (IS_ERR(xprt))
2742 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002743 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002744
Chuck Leverec739ef2006-08-22 20:06:15 -04002745 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002746 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002747 /* XXX: header size can vary due to auth type, IPv6, etc. */
2748 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2749
Chuck Lever03bf4b72005-08-25 16:25:55 -07002750 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002751 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2752 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002753
Chuck Lever262965f2005-08-11 16:25:56 -04002754 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002755
Trond Myklebustba7392b2007-12-20 16:03:55 -05002756 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002757
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002758 switch (addr->sa_family) {
2759 case AF_INET:
2760 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2761 xprt_set_bound(xprt);
2762
2763 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002764 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002765 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002766 break;
2767 case AF_INET6:
2768 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2769 xprt_set_bound(xprt);
2770
2771 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002772 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002773 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002774 break;
2775 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002776 ret = ERR_PTR(-EAFNOSUPPORT);
2777 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002778 }
2779
Chuck Leverc740eff2009-08-09 15:09:46 -04002780 if (xprt_bound(xprt))
2781 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2782 xprt->address_strings[RPC_DISPLAY_ADDR],
2783 xprt->address_strings[RPC_DISPLAY_PORT],
2784 xprt->address_strings[RPC_DISPLAY_PROTO]);
2785 else
2786 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2787 xprt->address_strings[RPC_DISPLAY_ADDR],
2788 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002789
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002790 if (try_module_get(THIS_MODULE))
2791 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002792 ret = ERR_PTR(-EINVAL);
2793out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002794 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002795 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002796}
2797
Trond Myklebust2881ae72007-12-20 16:03:54 -05002798static const struct rpc_timeout xs_tcp_default_timeout = {
2799 .to_initval = 60 * HZ,
2800 .to_maxval = 60 * HZ,
2801 .to_retries = 2,
2802};
2803
Chuck Lever9903cd12005-08-11 16:25:26 -04002804/**
2805 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002806 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002807 *
2808 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002809static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002810{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002811 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002812 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002813 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002814 struct rpc_xprt *ret;
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002815 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2816
2817 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2818 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Levera246b012005-08-11 16:25:23 -04002819
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002820 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002821 max_slot_table_size);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002822 if (IS_ERR(xprt))
2823 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002824 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002825
Chuck Leverec739ef2006-08-22 20:06:15 -04002826 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002827 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002828 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002829
Chuck Lever03bf4b72005-08-25 16:25:55 -07002830 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002831 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2832 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002833
Chuck Lever262965f2005-08-11 16:25:56 -04002834 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002835 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002836
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002837 switch (addr->sa_family) {
2838 case AF_INET:
2839 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2840 xprt_set_bound(xprt);
2841
Chuck Lever9dc3b092009-08-09 15:09:46 -04002842 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002843 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002844 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002845 break;
2846 case AF_INET6:
2847 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2848 xprt_set_bound(xprt);
2849
Chuck Lever9dc3b092009-08-09 15:09:46 -04002850 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002851 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002852 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002853 break;
2854 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002855 ret = ERR_PTR(-EAFNOSUPPORT);
2856 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002857 }
2858
Chuck Leverc740eff2009-08-09 15:09:46 -04002859 if (xprt_bound(xprt))
2860 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2861 xprt->address_strings[RPC_DISPLAY_ADDR],
2862 xprt->address_strings[RPC_DISPLAY_PORT],
2863 xprt->address_strings[RPC_DISPLAY_PROTO]);
2864 else
2865 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2866 xprt->address_strings[RPC_DISPLAY_ADDR],
2867 xprt->address_strings[RPC_DISPLAY_PROTO]);
2868
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002869 if (try_module_get(THIS_MODULE))
2870 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002871 ret = ERR_PTR(-EINVAL);
2872out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002873 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002874 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002875}
Chuck Lever282b32e2006-12-05 16:35:51 -05002876
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002877/**
2878 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2879 * @args: rpc transport creation arguments
2880 *
2881 */
2882static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2883{
2884 struct sockaddr *addr = args->dstaddr;
2885 struct rpc_xprt *xprt;
2886 struct sock_xprt *transport;
2887 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002888 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002889
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002890 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2891 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002892 if (IS_ERR(xprt))
2893 return xprt;
2894 transport = container_of(xprt, struct sock_xprt, xprt);
2895
2896 xprt->prot = IPPROTO_TCP;
2897 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2898 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2899 xprt->timeout = &xs_tcp_default_timeout;
2900
2901 /* backchannel */
2902 xprt_set_bound(xprt);
2903 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002904 xprt->reestablish_timeout = 0;
2905 xprt->idle_timeout = 0;
2906
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002907 xprt->ops = &bc_tcp_ops;
2908
2909 switch (addr->sa_family) {
2910 case AF_INET:
2911 xs_format_peer_addresses(xprt, "tcp",
2912 RPCBIND_NETID_TCP);
2913 break;
2914 case AF_INET6:
2915 xs_format_peer_addresses(xprt, "tcp",
2916 RPCBIND_NETID_TCP6);
2917 break;
2918 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002919 ret = ERR_PTR(-EAFNOSUPPORT);
2920 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002921 }
2922
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04002923 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2924 xprt->address_strings[RPC_DISPLAY_ADDR],
2925 xprt->address_strings[RPC_DISPLAY_PORT],
2926 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002927
2928 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002929 * Once we've associated a backchannel xprt with a connection,
Weng Meiling28303ca2013-11-30 17:56:44 +08002930 * we want to keep it around as long as the connection lasts,
2931 * in case we need to start using it for a backchannel again;
2932 * this reference won't be dropped until bc_xprt is destroyed.
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002933 */
2934 xprt_get(xprt);
2935 args->bc_xprt->xpt_bc_xprt = xprt;
2936 xprt->bc_xprt = args->bc_xprt;
2937 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2938 transport->sock = bc_sock->sk_sock;
2939 transport->inet = bc_sock->sk_sk;
2940
2941 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002942 * Since we don't want connections for the backchannel, we set
2943 * the xprt status to connected
2944 */
2945 xprt_set_connected(xprt);
2946
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002947 if (try_module_get(THIS_MODULE))
2948 return xprt;
Kinglong Mee642aab52014-03-24 12:00:28 +08002949
2950 args->bc_xprt->xpt_bc_xprt = NULL;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002951 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002952 ret = ERR_PTR(-EINVAL);
2953out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002954 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002955 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002956}
2957
Chuck Lever176e21e2011-05-09 15:22:44 -04002958static struct xprt_class xs_local_transport = {
2959 .list = LIST_HEAD_INIT(xs_local_transport.list),
2960 .name = "named UNIX socket",
2961 .owner = THIS_MODULE,
2962 .ident = XPRT_TRANSPORT_LOCAL,
2963 .setup = xs_setup_local,
2964};
2965
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002966static struct xprt_class xs_udp_transport = {
2967 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2968 .name = "udp",
2969 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002970 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002971 .setup = xs_setup_udp,
2972};
2973
2974static struct xprt_class xs_tcp_transport = {
2975 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2976 .name = "tcp",
2977 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002978 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002979 .setup = xs_setup_tcp,
2980};
2981
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002982static struct xprt_class xs_bc_tcp_transport = {
2983 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2984 .name = "tcp NFSv4.1 backchannel",
2985 .owner = THIS_MODULE,
2986 .ident = XPRT_TRANSPORT_BC_TCP,
2987 .setup = xs_setup_bc_tcp,
2988};
2989
Chuck Lever282b32e2006-12-05 16:35:51 -05002990/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002991 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002992 *
2993 */
2994int init_socket_xprt(void)
2995{
Jeff Laytonf895b252014-11-17 16:58:04 -05002996#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002997 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002998 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002999#endif
3000
Chuck Lever176e21e2011-05-09 15:22:44 -04003001 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003002 xprt_register_transport(&xs_udp_transport);
3003 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003004 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003005
Chuck Lever282b32e2006-12-05 16:35:51 -05003006 return 0;
3007}
3008
3009/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003010 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05003011 *
3012 */
3013void cleanup_socket_xprt(void)
3014{
Jeff Laytonf895b252014-11-17 16:58:04 -05003015#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Leverfbf76682006-12-05 16:35:54 -05003016 if (sunrpc_table_header) {
3017 unregister_sysctl_table(sunrpc_table_header);
3018 sunrpc_table_header = NULL;
3019 }
3020#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003021
Chuck Lever176e21e2011-05-09 15:22:44 -04003022 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003023 xprt_unregister_transport(&xs_udp_transport);
3024 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003025 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05003026}
Trond Myklebustcbf11072009-08-09 15:06:19 -04003027
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003028static int param_set_uint_minmax(const char *val,
3029 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003030 unsigned int min, unsigned int max)
3031{
Daniel Walter00cfaa92014-06-21 13:06:38 +01003032 unsigned int num;
Trond Myklebustcbf11072009-08-09 15:06:19 -04003033 int ret;
3034
3035 if (!val)
3036 return -EINVAL;
Daniel Walter00cfaa92014-06-21 13:06:38 +01003037 ret = kstrtouint(val, 0, &num);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003038 if (ret == -EINVAL || num < min || num > max)
3039 return -EINVAL;
3040 *((unsigned int *)kp->arg) = num;
3041 return 0;
3042}
3043
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003044static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003045{
3046 return param_set_uint_minmax(val, kp,
3047 RPC_MIN_RESVPORT,
3048 RPC_MAX_RESVPORT);
3049}
3050
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003051static struct kernel_param_ops param_ops_portnr = {
3052 .set = param_set_portnr,
3053 .get = param_get_uint,
3054};
3055
Trond Myklebustcbf11072009-08-09 15:06:19 -04003056#define param_check_portnr(name, p) \
3057 __param_check(name, p, unsigned int);
3058
3059module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3060module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3061
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003062static int param_set_slot_table_size(const char *val,
3063 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003064{
3065 return param_set_uint_minmax(val, kp,
3066 RPC_MIN_SLOT_TABLE,
3067 RPC_MAX_SLOT_TABLE);
3068}
3069
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003070static struct kernel_param_ops param_ops_slot_table_size = {
3071 .set = param_set_slot_table_size,
3072 .get = param_get_uint,
3073};
3074
Trond Myklebustcbf11072009-08-09 15:06:19 -04003075#define param_check_slot_table_size(name, p) \
3076 __param_check(name, p, unsigned int);
3077
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003078static int param_set_max_slot_table_size(const char *val,
3079 const struct kernel_param *kp)
3080{
3081 return param_set_uint_minmax(val, kp,
3082 RPC_MIN_SLOT_TABLE,
3083 RPC_MAX_SLOT_TABLE_LIMIT);
3084}
3085
3086static struct kernel_param_ops param_ops_max_slot_table_size = {
3087 .set = param_set_max_slot_table_size,
3088 .get = param_get_uint,
3089};
3090
3091#define param_check_max_slot_table_size(name, p) \
3092 __param_check(name, p, unsigned int);
3093
Trond Myklebustcbf11072009-08-09 15:06:19 -04003094module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3095 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003096module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3097 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003098module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3099 slot_table_size, 0644);
3100