blob: 0137af1c0916cdf25547f89ed20b62588fc5c0b2 [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,
Frank Sorensone08ea3a2016-07-08 16:35:24 -0500127 .extra2 = &xprt_max_resvport
Chuck Leverfbf76682006-12-05 16:35:54 -0500128 },
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,
Frank Sorensone08ea3a2016-07-08 16:35:24 -0500135 .extra1 = &xprt_min_resvport,
Chuck Leverfbf76682006-12-05 16:35:54 -0500136 .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)
Chuck Lever03bf4b72005-08-25 16:25:55 -0700180
181/*
182 * TCP idle timeout; client drops the transport socket if it is idle
183 * for this long. Note that we also timeout UDP sockets to prevent
184 * holding port numbers when there is no RPC traffic.
185 */
186#define XS_IDLE_DISC_TO (5U * 60 * HZ)
187
Jeff Laytonf895b252014-11-17 16:58:04 -0500188#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Levera246b012005-08-11 16:25:23 -0400189# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400190# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400191#endif
192
Chuck Levera246b012005-08-11 16:25:23 -0400193#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400194static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400195{
Chuck Lever9903cd12005-08-11 16:25:26 -0400196 u8 *buf = (u8 *) packet;
197 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400198
Chuck Lever46121cf2007-01-31 12:14:08 -0500199 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400200 for (j = 0; j < count && j < 128; j += 4) {
201 if (!(j & 31)) {
202 if (j)
203 dprintk("\n");
204 dprintk("0x%04x ", j);
205 }
206 dprintk("%02x%02x%02x%02x ",
207 buf[j], buf[j+1], buf[j+2], buf[j+3]);
208 }
209 dprintk("\n");
210}
211#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400212static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400213{
214 /* NOP */
215}
216#endif
217
Trond Myklebust21180712013-12-31 13:22:59 -0500218static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
219{
220 return (struct rpc_xprt *) sk->sk_user_data;
221}
222
Chuck Lever95392c52007-08-06 11:57:58 -0400223static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400224{
Chuck Lever95392c52007-08-06 11:57:58 -0400225 return (struct sockaddr *) &xprt->addr;
226}
227
Chuck Lever176e21e2011-05-09 15:22:44 -0400228static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
229{
230 return (struct sockaddr_un *) &xprt->addr;
231}
232
Chuck Lever95392c52007-08-06 11:57:58 -0400233static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
234{
235 return (struct sockaddr_in *) &xprt->addr;
236}
237
238static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
239{
240 return (struct sockaddr_in6 *) &xprt->addr;
241}
242
Chuck Leverc877b842009-08-09 15:09:36 -0400243static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400244{
Chuck Leverc877b842009-08-09 15:09:36 -0400245 struct sockaddr *sap = xs_addr(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400246 struct sockaddr_in6 *sin6;
247 struct sockaddr_in *sin;
Chuck Lever176e21e2011-05-09 15:22:44 -0400248 struct sockaddr_un *sun;
Chuck Leverc877b842009-08-09 15:09:36 -0400249 char buf[128];
Chuck Leveredb267a2006-08-22 20:06:18 -0400250
Chuck Lever9dc3b092009-08-09 15:09:46 -0400251 switch (sap->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400252 case AF_LOCAL:
253 sun = xs_addr_un(xprt);
254 strlcpy(buf, sun->sun_path, sizeof(buf));
255 xprt->address_strings[RPC_DISPLAY_ADDR] =
256 kstrdup(buf, GFP_KERNEL);
257 break;
Chuck Lever9dc3b092009-08-09 15:09:46 -0400258 case AF_INET:
Chuck Lever176e21e2011-05-09 15:22:44 -0400259 (void)rpc_ntop(sap, buf, sizeof(buf));
260 xprt->address_strings[RPC_DISPLAY_ADDR] =
261 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400262 sin = xs_addr_in(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800263 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
Chuck Lever9dc3b092009-08-09 15:09:46 -0400264 break;
265 case AF_INET6:
Chuck Lever176e21e2011-05-09 15:22:44 -0400266 (void)rpc_ntop(sap, buf, sizeof(buf));
267 xprt->address_strings[RPC_DISPLAY_ADDR] =
268 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400269 sin6 = xs_addr_in6(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800270 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400271 break;
272 default:
273 BUG();
Chuck Leveredb267a2006-08-22 20:06:18 -0400274 }
Chuck Lever176e21e2011-05-09 15:22:44 -0400275
Chuck Lever9dc3b092009-08-09 15:09:46 -0400276 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
Chuck Leveredb267a2006-08-22 20:06:18 -0400277}
278
Chuck Lever9dc3b092009-08-09 15:09:46 -0400279static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400280{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400281 struct sockaddr *sap = xs_addr(xprt);
282 char buf[128];
Chuck Lever4b6473f2007-08-06 11:57:12 -0400283
Joe Perches81160e662010-03-08 12:15:59 -0800284 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400285 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400286
Joe Perches81160e662010-03-08 12:15:59 -0800287 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400288 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
289}
Chuck Lever4b6473f2007-08-06 11:57:12 -0400290
Chuck Lever9dc3b092009-08-09 15:09:46 -0400291static void xs_format_peer_addresses(struct rpc_xprt *xprt,
292 const char *protocol,
293 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400294{
Chuck Leverb454ae92008-01-07 18:34:48 -0500295 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leverb454ae92008-01-07 18:34:48 -0500296 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leverc877b842009-08-09 15:09:36 -0400297 xs_format_common_peer_addresses(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400298 xs_format_common_peer_ports(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400299}
300
Chuck Lever9dc3b092009-08-09 15:09:46 -0400301static void xs_update_peer_port(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400302{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400303 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
304 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400305
Chuck Lever9dc3b092009-08-09 15:09:46 -0400306 xs_format_common_peer_ports(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400307}
308
309static void xs_free_peer_addresses(struct rpc_xprt *xprt)
310{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500311 unsigned int i;
312
313 for (i = 0; i < RPC_DISPLAY_MAX; i++)
314 switch (i) {
315 case RPC_DISPLAY_PROTO:
316 case RPC_DISPLAY_NETID:
317 continue;
318 default:
319 kfree(xprt->address_strings[i]);
320 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400321}
322
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400323#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
324
Trond Myklebust24c56842006-10-17 15:06:22 -0400325static 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 -0400326{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400327 struct msghdr msg = {
328 .msg_name = addr,
329 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400330 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
331 };
332 struct kvec iov = {
333 .iov_base = vec->iov_base + base,
334 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400335 };
336
Trond Myklebust24c56842006-10-17 15:06:22 -0400337 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400338 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
339 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
340}
341
Jason Baronf279cd02014-09-24 18:08:00 +0000342static 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 -0400343{
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500344 ssize_t (*do_sendpage)(struct socket *sock, struct page *page,
345 int offset, size_t size, int flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400346 struct page **ppage;
347 unsigned int remainder;
Jason Baronf279cd02014-09-24 18:08:00 +0000348 int err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400349
Trond Myklebust24c56842006-10-17 15:06:22 -0400350 remainder = xdr->page_len - base;
351 base += xdr->page_base;
352 ppage = xdr->pages + (base >> PAGE_SHIFT);
353 base &= ~PAGE_MASK;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500354 do_sendpage = sock->ops->sendpage;
355 if (!zerocopy)
356 do_sendpage = sock_no_sendpage;
Trond Myklebust24c56842006-10-17 15:06:22 -0400357 for(;;) {
358 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
359 int flags = XS_SENDMSG_FLAGS;
360
361 remainder -= len;
Trond Myklebust31303d62015-10-06 15:59:20 -0400362 if (more)
Trond Myklebust24c56842006-10-17 15:06:22 -0400363 flags |= MSG_MORE;
Trond Myklebust31303d62015-10-06 15:59:20 -0400364 if (remainder != 0)
365 flags |= MSG_SENDPAGE_NOTLAST | MSG_MORE;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500366 err = do_sendpage(sock, *ppage, base, len, flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400367 if (remainder == 0 || err != len)
368 break;
Jason Baronf279cd02014-09-24 18:08:00 +0000369 *sent_p += err;
Trond Myklebust24c56842006-10-17 15:06:22 -0400370 ppage++;
371 base = 0;
372 }
Jason Baronf279cd02014-09-24 18:08:00 +0000373 if (err > 0) {
374 *sent_p += err;
375 err = 0;
376 }
377 return err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400378}
379
Chuck Lever9903cd12005-08-11 16:25:26 -0400380/**
381 * xs_sendpages - write pages directly to a socket
382 * @sock: socket to send on
383 * @addr: UDP only -- address of destination
384 * @addrlen: UDP only -- length of destination address
385 * @xdr: buffer containing this request
386 * @base: starting position in the buffer
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500387 * @zerocopy: true if it is safe to use sendpage()
Jason Baronf279cd02014-09-24 18:08:00 +0000388 * @sent_p: return the total number of bytes successfully queued for sending
Chuck Lever9903cd12005-08-11 16:25:26 -0400389 *
Chuck Levera246b012005-08-11 16:25:23 -0400390 */
Jason Baronf279cd02014-09-24 18:08:00 +0000391static 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 -0400392{
Trond Myklebust24c56842006-10-17 15:06:22 -0400393 unsigned int remainder = xdr->len - base;
Jason Baronf279cd02014-09-24 18:08:00 +0000394 int err = 0;
395 int sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400396
Chuck Lever262965f2005-08-11 16:25:56 -0400397 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400398 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400399
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--;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400443}
444
Chuck Lever9903cd12005-08-11 16:25:26 -0400445/**
Chuck Lever262965f2005-08-11 16:25:56 -0400446 * xs_nospace - place task on wait queue if transmit was incomplete
447 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400448 *
Chuck Levera246b012005-08-11 16:25:23 -0400449 */
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400450static int xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400451{
Chuck Lever262965f2005-08-11 16:25:56 -0400452 struct rpc_rqst *req = task->tk_rqstp;
453 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500454 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500455 struct sock *sk = transport->inet;
Trond Myklebust24ca9a82011-11-22 14:44:28 +0200456 int ret = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400457
Chuck Lever46121cf2007-01-31 12:14:08 -0500458 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400459 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
460 req->rq_slen);
461
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400462 /* Protect against races with write_space */
463 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400464
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400465 /* Don't race with disconnect */
466 if (xprt_connected(xprt)) {
Trond Myklebust13331a52016-01-06 08:57:06 -0500467 /* wait for more buffer space */
468 sk->sk_write_pending++;
469 xprt_wait_for_buffer_space(task, xs_nospace_callback);
470 } else
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400471 ret = -ENOTCONN;
Chuck Lever262965f2005-08-11 16:25:56 -0400472
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400473 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500474
475 /* Race breaker in case memory is freed before above code is called */
David Vrabeld48f9ce2016-09-19 13:58:30 +0100476 if (ret == -EAGAIN) {
477 struct socket_wq *wq;
478
479 rcu_read_lock();
480 wq = rcu_dereference(sk->sk_wq);
481 set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags);
482 rcu_read_unlock();
483
484 sk->sk_write_space(sk);
485 }
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400486 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400487}
488
Chuck Lever61677ee2011-05-09 15:22:34 -0400489/*
490 * Construct a stream transport record marker in @buf.
491 */
492static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
493{
494 u32 reclen = buf->len - sizeof(rpc_fraghdr);
495 rpc_fraghdr *base = buf->head[0].iov_base;
496 *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
497}
498
Chuck Lever262965f2005-08-11 16:25:56 -0400499/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400500 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
501 * @task: RPC task that manages the state of an RPC request
502 *
503 * Return values:
504 * 0: The request has been sent
505 * EAGAIN: The socket was blocked, please call again later to
506 * complete the request
507 * ENOTCONN: Caller needs to invoke connect logic then call again
508 * other: Some other error occured, the request was not sent
509 */
510static int xs_local_send_request(struct rpc_task *task)
511{
512 struct rpc_rqst *req = task->tk_rqstp;
513 struct rpc_xprt *xprt = req->rq_xprt;
514 struct sock_xprt *transport =
515 container_of(xprt, struct sock_xprt, xprt);
516 struct xdr_buf *xdr = &req->rq_snd_buf;
517 int status;
Jason Baronf279cd02014-09-24 18:08:00 +0000518 int sent = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -0400519
520 xs_encode_stream_record_marker(&req->rq_snd_buf);
521
522 xs_pktdump("packet data:",
523 req->rq_svec->iov_base, req->rq_svec->iov_len);
524
Jason Baronf279cd02014-09-24 18:08:00 +0000525 status = xs_sendpages(transport->sock, NULL, 0, xdr, req->rq_bytes_sent,
526 true, &sent);
Chuck Lever176e21e2011-05-09 15:22:44 -0400527 dprintk("RPC: %s(%u) = %d\n",
528 __func__, xdr->len - req->rq_bytes_sent, status);
NeilBrown743c69e2015-07-27 10:55:35 +1000529
530 if (status == -EAGAIN && sock_writeable(transport->inet))
531 status = -ENOBUFS;
532
Jason Baronf279cd02014-09-24 18:08:00 +0000533 if (likely(sent > 0) || status == 0) {
534 req->rq_bytes_sent += sent;
535 req->rq_xmit_bytes_sent += sent;
Chuck Lever176e21e2011-05-09 15:22:44 -0400536 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
537 req->rq_bytes_sent = 0;
538 return 0;
539 }
540 status = -EAGAIN;
541 }
542
543 switch (status) {
Trond Myklebust3601c4a2014-06-30 13:42:19 -0400544 case -ENOBUFS:
Trond Myklebustb5872f02015-07-03 09:32:23 -0400545 break;
Chuck Lever176e21e2011-05-09 15:22:44 -0400546 case -EAGAIN:
547 status = xs_nospace(task);
548 break;
549 default:
550 dprintk("RPC: sendmsg returned unrecognized error %d\n",
551 -status);
552 case -EPIPE:
553 xs_close(xprt);
554 status = -ENOTCONN;
555 }
556
557 return status;
558}
559
560/**
Chuck Lever262965f2005-08-11 16:25:56 -0400561 * xs_udp_send_request - write an RPC request to a UDP socket
562 * @task: address of RPC task that manages the state of an RPC request
563 *
564 * Return values:
565 * 0: The request has been sent
566 * EAGAIN: The socket was blocked, please call again later to
567 * complete the request
568 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300569 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -0400570 */
571static int xs_udp_send_request(struct rpc_task *task)
572{
573 struct rpc_rqst *req = task->tk_rqstp;
574 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500575 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400576 struct xdr_buf *xdr = &req->rq_snd_buf;
Jason Baronf279cd02014-09-24 18:08:00 +0000577 int sent = 0;
Chuck Lever262965f2005-08-11 16:25:56 -0400578 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400579
Chuck Lever9903cd12005-08-11 16:25:26 -0400580 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400581 req->rq_svec->iov_base,
582 req->rq_svec->iov_len);
583
Trond Myklebust01d37c42009-03-11 14:09:39 -0400584 if (!xprt_bound(xprt))
585 return -ENOTCONN;
Jason Baronf279cd02014-09-24 18:08:00 +0000586 status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen,
587 xdr, req->rq_bytes_sent, true, &sent);
Chuck Levera246b012005-08-11 16:25:23 -0400588
Chuck Lever46121cf2007-01-31 12:14:08 -0500589 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400590 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400591
Jason Baron3dedbb52014-09-24 18:08:04 +0000592 /* firewall is blocking us, don't return -EAGAIN or we end up looping */
593 if (status == -EPERM)
594 goto process_status;
595
NeilBrown743c69e2015-07-27 10:55:35 +1000596 if (status == -EAGAIN && sock_writeable(transport->inet))
597 status = -ENOBUFS;
598
Jason Baronf279cd02014-09-24 18:08:00 +0000599 if (sent > 0 || status == 0) {
600 req->rq_xmit_bytes_sent += sent;
601 if (sent >= req->rq_slen)
Trond Myklebust21997002007-10-01 11:43:37 -0400602 return 0;
603 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400604 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400605 }
Chuck Levera246b012005-08-11 16:25:23 -0400606
Jason Baron3dedbb52014-09-24 18:08:04 +0000607process_status:
Chuck Lever262965f2005-08-11 16:25:56 -0400608 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400609 case -ENOTSOCK:
610 status = -ENOTCONN;
611 /* Should we call xs_close() here? */
612 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400613 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400614 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400615 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400616 case -ENETUNREACH:
Trond Myklebust3601c4a2014-06-30 13:42:19 -0400617 case -ENOBUFS:
Chuck Lever262965f2005-08-11 16:25:56 -0400618 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400619 case -ECONNREFUSED:
Jason Baron3dedbb52014-09-24 18:08:04 +0000620 case -EPERM:
Chuck Levera246b012005-08-11 16:25:23 -0400621 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400622 * prompts ECONNREFUSED. */
Trond Myklebust13331a52016-01-06 08:57:06 -0500623 break;
624 default:
625 dprintk("RPC: sendmsg returned unrecognized error %d\n",
626 -status);
Chuck Levera246b012005-08-11 16:25:23 -0400627 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800628
Chuck Lever262965f2005-08-11 16:25:56 -0400629 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400630}
631
Trond Myklebuste06799f2007-11-05 15:44:12 -0500632/**
Chuck Lever262965f2005-08-11 16:25:56 -0400633 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400634 * @task: address of RPC task that manages the state of an RPC request
635 *
636 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400637 * 0: The request has been sent
638 * EAGAIN: The socket was blocked, please call again later to
639 * complete the request
640 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300641 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400642 *
643 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400644 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400645 */
Chuck Lever262965f2005-08-11 16:25:56 -0400646static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400647{
648 struct rpc_rqst *req = task->tk_rqstp;
649 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500650 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400651 struct xdr_buf *xdr = &req->rq_snd_buf;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500652 bool zerocopy = true;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -0400653 bool vm_wait = false;
Chuck Leverb595bb12007-08-06 11:56:42 -0400654 int status;
Jason Baronf279cd02014-09-24 18:08:00 +0000655 int sent;
Chuck Levera246b012005-08-11 16:25:23 -0400656
Chuck Lever61677ee2011-05-09 15:22:34 -0400657 xs_encode_stream_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400658
Chuck Lever262965f2005-08-11 16:25:56 -0400659 xs_pktdump("packet data:",
660 req->rq_svec->iov_base,
661 req->rq_svec->iov_len);
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500662 /* Don't use zero copy if this is a resend. If the RPC call
663 * completes while the socket holds a reference to the pages,
664 * then we may end up resending corrupted data.
665 */
666 if (task->tk_flags & RPC_TASK_SENT)
667 zerocopy = false;
Chuck Levera246b012005-08-11 16:25:23 -0400668
669 /* Continue transmitting the packet/record. We must be careful
670 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400671 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400672 while (1) {
Jason Baronf279cd02014-09-24 18:08:00 +0000673 sent = 0;
674 status = xs_sendpages(transport->sock, NULL, 0, xdr,
675 req->rq_bytes_sent, zerocopy, &sent);
Chuck Levera246b012005-08-11 16:25:23 -0400676
Chuck Lever46121cf2007-01-31 12:14:08 -0500677 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400678 xdr->len - req->rq_bytes_sent, status);
679
Chuck Lever262965f2005-08-11 16:25:56 -0400680 /* If we've sent the entire packet, immediately
681 * reset the count of bytes sent. */
Jason Baronf279cd02014-09-24 18:08:00 +0000682 req->rq_bytes_sent += sent;
683 req->rq_xmit_bytes_sent += sent;
Chuck Lever262965f2005-08-11 16:25:56 -0400684 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
685 req->rq_bytes_sent = 0;
686 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400687 }
688
Trond Myklebust9ffadfb2016-05-29 00:42:03 -0400689 WARN_ON_ONCE(sent == 0 && status == 0);
690
691 if (status == -EAGAIN ) {
692 /*
693 * Return EAGAIN if we're sure we're hitting the
694 * socket send buffer limits.
695 */
696 if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
697 break;
698 /*
699 * Did we hit a memory allocation failure?
700 */
701 if (sent == 0) {
702 status = -ENOBUFS;
703 if (vm_wait)
704 break;
705 /* Retry, knowing now that we're below the
706 * socket send buffer limit
707 */
708 vm_wait = true;
709 }
710 continue;
711 }
Trond Myklebustf580dd02015-07-11 17:48:52 +0200712 if (status < 0)
713 break;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -0400714 vm_wait = false;
Chuck Levera246b012005-08-11 16:25:23 -0400715 }
716
Chuck Lever262965f2005-08-11 16:25:56 -0400717 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400718 case -ENOTSOCK:
719 status = -ENOTCONN;
720 /* Should we call xs_close() here? */
721 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400722 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400723 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400724 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400725 case -ECONNRESET:
Chuck Lever262965f2005-08-11 16:25:56 -0400726 case -ECONNREFUSED:
727 case -ENOTCONN:
Trond Myklebust3913c782015-02-08 21:44:04 -0500728 case -EADDRINUSE:
Trond Myklebustb5872f02015-07-03 09:32:23 -0400729 case -ENOBUFS:
Trond Myklebustb9d2bb22012-10-23 11:40:02 -0400730 case -EPIPE:
Trond Myklebust13331a52016-01-06 08:57:06 -0500731 break;
732 default:
733 dprintk("RPC: sendmsg returned unrecognized error %d\n",
734 -status);
Chuck Levera246b012005-08-11 16:25:23 -0400735 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800736
Chuck Levera246b012005-08-11 16:25:23 -0400737 return status;
738}
739
Chuck Lever9903cd12005-08-11 16:25:26 -0400740/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400741 * xs_tcp_release_xprt - clean up after a tcp transmission
742 * @xprt: transport
743 * @task: rpc task
744 *
745 * This cleans up if an error causes us to abort the transmission of a request.
746 * In this case, the socket may need to be reset in order to avoid confusing
747 * the server.
748 */
749static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
750{
751 struct rpc_rqst *req;
752
753 if (task != xprt->snd_task)
754 return;
755 if (task == NULL)
756 goto out_release;
757 req = task->tk_rqstp;
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400758 if (req == NULL)
759 goto out_release;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400760 if (req->rq_bytes_sent == 0)
761 goto out_release;
762 if (req->rq_bytes_sent == req->rq_snd_buf.len)
763 goto out_release;
Trond Myklebusta4f08352013-01-08 09:10:21 -0500764 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400765out_release:
766 xprt_release_xprt(xprt, task);
767}
768
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400769static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
770{
771 transport->old_data_ready = sk->sk_data_ready;
772 transport->old_state_change = sk->sk_state_change;
773 transport->old_write_space = sk->sk_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -0500774 transport->old_error_report = sk->sk_error_report;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400775}
776
777static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
778{
779 sk->sk_data_ready = transport->old_data_ready;
780 sk->sk_state_change = transport->old_state_change;
781 sk->sk_write_space = transport->old_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -0500782 sk->sk_error_report = transport->old_error_report;
783}
784
Trond Myklebust42d42a52016-05-23 09:24:55 -0400785static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
786{
787 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
788
789 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
790}
791
Trond Myklebustb70ae912015-02-09 09:41:32 -0500792static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
793{
794 smp_mb__before_atomic();
795 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
796 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust42d42a52016-05-23 09:24:55 -0400797 xs_sock_reset_state_flags(xprt);
Trond Myklebustb70ae912015-02-09 09:41:32 -0500798 smp_mb__after_atomic();
799}
800
801static void xs_sock_mark_closed(struct rpc_xprt *xprt)
802{
803 xs_sock_reset_connection_flags(xprt);
804 /* Mark transport as closed and wake up all pending tasks */
805 xprt_disconnect_done(xprt);
806}
807
Trond Myklebust21180712013-12-31 13:22:59 -0500808/**
809 * xs_error_report - callback to handle TCP socket state errors
810 * @sk: socket
811 *
812 * Note: we don't call sock_error() since there may be a rpc_task
813 * using the socket, and so we don't want to clear sk->sk_err.
814 */
815static void xs_error_report(struct sock *sk)
816{
817 struct rpc_xprt *xprt;
818 int err;
819
820 read_lock_bh(&sk->sk_callback_lock);
821 if (!(xprt = xprt_from_sock(sk)))
822 goto out;
823
824 err = -sk->sk_err;
825 if (err == 0)
826 goto out;
Trond Myklebustb70ae912015-02-09 09:41:32 -0500827 /* Is this a reset event? */
828 if (sk->sk_state == TCP_CLOSE)
829 xs_sock_mark_closed(xprt);
Trond Myklebust21180712013-12-31 13:22:59 -0500830 dprintk("RPC: xs_error_report client %p, error=%d...\n",
831 xprt, -err);
Trond Myklebuste8353c762013-12-31 13:39:22 -0500832 trace_rpc_socket_error(xprt, sk->sk_socket, err);
Trond Myklebust21180712013-12-31 13:22:59 -0500833 xprt_wake_pending_tasks(xprt, err);
834 out:
835 read_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400836}
837
Chuck Leverfe315e72009-03-11 14:10:21 -0400838static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400839{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500840 struct socket *sock = transport->sock;
841 struct sock *sk = transport->inet;
Trond Myklebust6cc7e902015-02-08 18:35:25 -0500842 struct rpc_xprt *xprt = &transport->xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400843
Chuck Leverfe315e72009-03-11 14:10:21 -0400844 if (sk == NULL)
845 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400846
Jeff Layton264d1df2015-06-03 16:14:27 -0400847 if (atomic_read(&transport->xprt.swapper))
848 sk_clear_memalloc(sk);
849
Trond Myklebust09939202015-08-29 19:11:21 -0700850 kernel_sock_shutdown(sock, SHUT_RDWR);
851
Trond Myklebustedc1b012015-10-05 10:53:49 -0400852 mutex_lock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -0400853 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500854 transport->inet = NULL;
855 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400856
Chuck Lever9903cd12005-08-11 16:25:26 -0400857 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400858
859 xs_restore_old_callbacks(transport, sk);
Trond Myklebust0c787892015-08-29 13:36:30 -0700860 xprt_clear_connected(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400861 write_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust6cc7e902015-02-08 18:35:25 -0500862 xs_sock_reset_connection_flags(xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -0400863 mutex_unlock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -0400864
Trond Myklebust6cc7e902015-02-08 18:35:25 -0500865 trace_rpc_socket_close(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -0400866 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -0400867}
868
869/**
870 * xs_close - close a socket
871 * @xprt: transport
872 *
873 * This is used when all requests are complete; ie, no DRC state remains
874 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400875 *
876 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
877 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e72009-03-11 14:10:21 -0400878 */
879static void xs_close(struct rpc_xprt *xprt)
880{
881 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
882
883 dprintk("RPC: xs_close xprt %p\n", xprt);
884
885 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -0400886 xprt->reestablish_timeout = 0;
Chuck Leverfe315e72009-03-11 14:10:21 -0400887
Trond Myklebust62da3b22007-11-06 18:44:20 -0500888 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400889}
890
Chuck Lever4a068252015-05-11 14:02:25 -0400891static void xs_inject_disconnect(struct rpc_xprt *xprt)
892{
893 dprintk("RPC: injecting transport disconnect on xprt=%p\n",
894 xprt);
895 xprt_disconnect_done(xprt);
896}
897
Kinglong Mee315f3812014-03-24 11:07:22 +0800898static void xs_xprt_free(struct rpc_xprt *xprt)
899{
900 xs_free_peer_addresses(xprt);
901 xprt_free(xprt);
902}
903
Chuck Lever9903cd12005-08-11 16:25:26 -0400904/**
905 * xs_destroy - prepare to shutdown a transport
906 * @xprt: doomed transport
907 *
908 */
909static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400910{
Trond Myklebust03c788272015-09-17 10:42:27 -0400911 struct sock_xprt *transport = container_of(xprt,
912 struct sock_xprt, xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -0500913 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400914
Trond Myklebust03c788272015-09-17 10:42:27 -0400915 cancel_delayed_work_sync(&transport->connect_worker);
Trond Myklebusta1311d82013-10-31 09:18:49 -0400916 xs_close(xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -0400917 cancel_work_sync(&transport->recv_worker);
Kinglong Mee315f3812014-03-24 11:07:22 +0800918 xs_xprt_free(xprt);
Trond Myklebusta1311d82013-10-31 09:18:49 -0400919 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400920}
921
Chuck Lever176e21e2011-05-09 15:22:44 -0400922static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
923{
924 struct xdr_skb_reader desc = {
925 .skb = skb,
926 .offset = sizeof(rpc_fraghdr),
927 .count = skb->len - sizeof(rpc_fraghdr),
928 };
929
930 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
931 return -1;
932 if (desc.count)
933 return -1;
934 return 0;
935}
936
937/**
Trond Myklebusta2648092015-10-06 17:03:00 -0400938 * xs_local_data_read_skb
939 * @xprt: transport
940 * @sk: socket
941 * @skb: skbuff
Chuck Lever176e21e2011-05-09 15:22:44 -0400942 *
943 * Currently this assumes we can read the whole reply in a single gulp.
944 */
Trond Myklebusta2648092015-10-06 17:03:00 -0400945static void xs_local_data_read_skb(struct rpc_xprt *xprt,
946 struct sock *sk,
947 struct sk_buff *skb)
Chuck Lever176e21e2011-05-09 15:22:44 -0400948{
949 struct rpc_task *task;
Chuck Lever176e21e2011-05-09 15:22:44 -0400950 struct rpc_rqst *rovr;
Trond Myklebusta2648092015-10-06 17:03:00 -0400951 int repsize, copied;
Chuck Lever176e21e2011-05-09 15:22:44 -0400952 u32 _xid;
953 __be32 *xp;
954
Chuck Lever176e21e2011-05-09 15:22:44 -0400955 repsize = skb->len - sizeof(rpc_fraghdr);
956 if (repsize < 4) {
957 dprintk("RPC: impossible RPC reply size %d\n", repsize);
Trond Myklebusta2648092015-10-06 17:03:00 -0400958 return;
Chuck Lever176e21e2011-05-09 15:22:44 -0400959 }
960
961 /* Copy the XID from the skb... */
962 xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
963 if (xp == NULL)
Trond Myklebusta2648092015-10-06 17:03:00 -0400964 return;
Chuck Lever176e21e2011-05-09 15:22:44 -0400965
966 /* Look up and lock the request corresponding to the given XID */
Trond Myklebusta2648092015-10-06 17:03:00 -0400967 spin_lock_bh(&xprt->transport_lock);
Chuck Lever176e21e2011-05-09 15:22:44 -0400968 rovr = xprt_lookup_rqst(xprt, *xp);
969 if (!rovr)
970 goto out_unlock;
971 task = rovr->rq_task;
972
973 copied = rovr->rq_private_buf.buflen;
974 if (copied > repsize)
975 copied = repsize;
976
977 if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
978 dprintk("RPC: sk_buff copy failed\n");
979 goto out_unlock;
980 }
981
982 xprt_complete_rqst(task, copied);
983
984 out_unlock:
Trond Myklebusta2648092015-10-06 17:03:00 -0400985 spin_unlock_bh(&xprt->transport_lock);
986}
987
988static void xs_local_data_receive(struct sock_xprt *transport)
989{
990 struct sk_buff *skb;
991 struct sock *sk;
992 int err;
993
994 mutex_lock(&transport->recv_mutex);
995 sk = transport->inet;
996 if (sk == NULL)
997 goto out;
998 for (;;) {
999 skb = skb_recv_datagram(sk, 0, 1, &err);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001000 if (skb != NULL) {
1001 xs_local_data_read_skb(&transport->xprt, sk, skb);
1002 skb_free_datagram(sk, skb);
1003 continue;
1004 }
1005 if (!test_and_clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebusta2648092015-10-06 17:03:00 -04001006 break;
Trond Myklebusta2648092015-10-06 17:03:00 -04001007 }
1008out:
1009 mutex_unlock(&transport->recv_mutex);
1010}
1011
1012static void xs_local_data_receive_workfn(struct work_struct *work)
1013{
1014 struct sock_xprt *transport =
1015 container_of(work, struct sock_xprt, recv_worker);
1016 xs_local_data_receive(transport);
Chuck Lever176e21e2011-05-09 15:22:44 -04001017}
1018
Chuck Lever9903cd12005-08-11 16:25:26 -04001019/**
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001020 * xs_udp_data_read_skb - receive callback for UDP sockets
1021 * @xprt: transport
1022 * @sk: socket
1023 * @skb: skbuff
Chuck Lever9903cd12005-08-11 16:25:26 -04001024 *
1025 */
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001026static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1027 struct sock *sk,
1028 struct sk_buff *skb)
Chuck Lever9903cd12005-08-11 16:25:26 -04001029{
1030 struct rpc_task *task;
Chuck Levera246b012005-08-11 16:25:23 -04001031 struct rpc_rqst *rovr;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001032 int repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001033 u32 _xid;
1034 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -04001035
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001036 repsize = skb->len;
Chuck Levera246b012005-08-11 16:25:23 -04001037 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001038 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001039 return;
Chuck Levera246b012005-08-11 16:25:23 -04001040 }
1041
1042 /* Copy the XID from the skb... */
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001043 xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
Chuck Levera246b012005-08-11 16:25:23 -04001044 if (xp == NULL)
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001045 return;
Chuck Levera246b012005-08-11 16:25:23 -04001046
1047 /* Look up and lock the request corresponding to the given XID */
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001048 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001049 rovr = xprt_lookup_rqst(xprt, *xp);
1050 if (!rovr)
1051 goto out_unlock;
1052 task = rovr->rq_task;
1053
Chuck Levera246b012005-08-11 16:25:23 -04001054 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1055 copied = repsize;
1056
1057 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001058 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
Eric Dumazet02c22342016-04-27 16:44:30 -07001059 __UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -04001060 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001061 }
1062
Eric Dumazet02c22342016-04-27 16:44:30 -07001063 __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -04001064
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001065 xprt_adjust_cwnd(xprt, task, copied);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001066 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -04001067
1068 out_unlock:
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001069 spin_unlock_bh(&xprt->transport_lock);
1070}
1071
1072static void xs_udp_data_receive(struct sock_xprt *transport)
1073{
1074 struct sk_buff *skb;
1075 struct sock *sk;
1076 int err;
1077
1078 mutex_lock(&transport->recv_mutex);
1079 sk = transport->inet;
1080 if (sk == NULL)
1081 goto out;
1082 for (;;) {
1083 skb = skb_recv_datagram(sk, 0, 1, &err);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001084 if (skb != NULL) {
1085 xs_udp_data_read_skb(&transport->xprt, sk, skb);
Paolo Abenia41bd252016-08-25 18:42:35 +02001086 skb_free_datagram_locked(sk, skb);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001087 continue;
1088 }
1089 if (!test_and_clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001090 break;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001091 }
1092out:
1093 mutex_unlock(&transport->recv_mutex);
1094}
1095
1096static void xs_udp_data_receive_workfn(struct work_struct *work)
1097{
1098 struct sock_xprt *transport =
1099 container_of(work, struct sock_xprt, recv_worker);
1100 xs_udp_data_receive(transport);
1101}
1102
1103/**
1104 * xs_data_ready - "data ready" callback for UDP sockets
1105 * @sk: socket with data to read
1106 *
1107 */
1108static void xs_data_ready(struct sock *sk)
1109{
1110 struct rpc_xprt *xprt;
1111
1112 read_lock_bh(&sk->sk_callback_lock);
1113 dprintk("RPC: xs_data_ready...\n");
1114 xprt = xprt_from_sock(sk);
1115 if (xprt != NULL) {
1116 struct sock_xprt *transport = container_of(xprt,
1117 struct sock_xprt, xprt);
Trond Myklebust5157b952016-05-29 10:13:24 -04001118 transport->old_data_ready(sk);
1119 /* Any data means we had a useful conversation, so
1120 * then we don't need to delay the next reconnect
1121 */
1122 if (xprt->reestablish_timeout)
1123 xprt->reestablish_timeout = 0;
Trond Myklebust42d42a52016-05-23 09:24:55 -04001124 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebust40a5f1b2016-05-27 10:39:50 -04001125 queue_work(xprtiod_workqueue, &transport->recv_worker);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001126 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001127 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001128}
1129
Trond Myklebusta519fc72012-09-12 16:49:15 -04001130/*
1131 * Helper function to force a TCP close if the server is sending
1132 * junk and/or it has put us in CLOSE_WAIT
1133 */
1134static void xs_tcp_force_close(struct rpc_xprt *xprt)
1135{
Trond Myklebusta519fc72012-09-12 16:49:15 -04001136 xprt_force_disconnect(xprt);
1137}
1138
Chuck Leverdd456472006-12-05 16:35:44 -05001139static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001140{
Chuck Lever51971132006-12-05 16:35:19 -05001141 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001142 size_t len, used;
1143 char *p;
1144
Chuck Lever51971132006-12-05 16:35:19 -05001145 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
1146 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001147 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001148 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001149 if (used != len)
1150 return;
Chuck Lever808012f2005-08-25 16:25:49 -07001151
Chuck Lever51971132006-12-05 16:35:19 -05001152 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
1153 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -05001154 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -04001155 else
Chuck Levere136d092006-12-05 16:35:23 -05001156 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -05001157 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -07001158
Chuck Levere136d092006-12-05 16:35:23 -05001159 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001160 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -07001161
Chuck Levera246b012005-08-11 16:25:23 -04001162 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001163 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001164 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebusta519fc72012-09-12 16:49:15 -04001165 xs_tcp_force_close(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001166 return;
Chuck Levera246b012005-08-11 16:25:23 -04001167 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001168 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -05001169 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001170}
1171
Chuck Lever51971132006-12-05 16:35:19 -05001172static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001173{
Chuck Lever51971132006-12-05 16:35:19 -05001174 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001175 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001176 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001177 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
1178 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1179 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001180 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001181 }
1182 }
1183}
1184
Chuck Leverdd456472006-12-05 16:35:44 -05001185static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001186{
1187 size_t len, used;
1188 char *p;
1189
Chuck Lever51971132006-12-05 16:35:19 -05001190 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -05001191 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001192 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001193 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001194 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001195 if (used != len)
1196 return;
Chuck Levere136d092006-12-05 16:35:23 -05001197 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001198 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -05001199 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001200 dprintk("RPC: reading %s XID %08x\n",
1201 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1202 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -05001203 ntohl(transport->tcp_xid));
1204 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001205}
1206
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001207static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1208 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001209{
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001210 size_t len, used;
1211 u32 offset;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001212 char *p;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001213
1214 /*
1215 * We want transport->tcp_offset to be 8 at the end of this routine
1216 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1217 * When this function is called for the first time,
1218 * transport->tcp_offset is 4 (after having already read the xid).
1219 */
1220 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001221 len = sizeof(transport->tcp_calldir) - offset;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001222 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001223 p = ((char *) &transport->tcp_calldir) + offset;
1224 used = xdr_skb_read_bits(desc, p, len);
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001225 transport->tcp_offset += used;
1226 if (used != len)
1227 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001228 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001229 /*
1230 * We don't yet have the XDR buffer, so we will write the calldir
1231 * out after we get the buffer from the 'struct rpc_rqst'
1232 */
Trond Myklebustb76ce562010-06-16 13:57:32 -04001233 switch (ntohl(transport->tcp_calldir)) {
1234 case RPC_REPLY:
1235 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1236 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001237 transport->tcp_flags |= TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001238 break;
1239 case RPC_CALL:
1240 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1241 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001242 transport->tcp_flags &= ~TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001243 break;
1244 default:
1245 dprintk("RPC: invalid request message type\n");
Trond Myklebusta519fc72012-09-12 16:49:15 -04001246 xs_tcp_force_close(&transport->xprt);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001247 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001248 xs_tcp_check_fraghdr(transport);
1249}
1250
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001251static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
1252 struct xdr_skb_reader *desc,
1253 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001254{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001255 struct sock_xprt *transport =
1256 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001257 struct xdr_buf *rcvbuf;
1258 size_t len;
1259 ssize_t r;
1260
Chuck Levera246b012005-08-11 16:25:23 -04001261 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001262
1263 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1264 /*
1265 * Save the RPC direction in the XDR buffer
1266 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001267 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
Trond Myklebustb76ce562010-06-16 13:57:32 -04001268 &transport->tcp_calldir,
1269 sizeof(transport->tcp_calldir));
1270 transport->tcp_copied += sizeof(transport->tcp_calldir);
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001271 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -04001272 }
1273
Chuck Levera246b012005-08-11 16:25:23 -04001274 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -05001275 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -05001276 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -04001277
Chuck Lever51971132006-12-05 16:35:19 -05001278 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001279 memcpy(&my_desc, desc, sizeof(my_desc));
1280 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -05001281 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001282 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001283 desc->count -= r;
1284 desc->offset += r;
1285 } else
Chuck Lever51971132006-12-05 16:35:19 -05001286 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001287 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001288
1289 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001290 transport->tcp_copied += r;
1291 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001292 }
1293 if (r != len) {
1294 /* Error when copying to the receive buffer,
1295 * usually because we weren't able to allocate
1296 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001297 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001298 * will not receive any additional updates,
1299 * and time out.
1300 * Any remaining data from this record will
1301 * be discarded.
1302 */
Chuck Levere136d092006-12-05 16:35:23 -05001303 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001304 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001305 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001306 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1307 "tcp_offset = %u, tcp_reclen = %u\n",
1308 xprt, transport->tcp_copied,
1309 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001310 return;
Chuck Levera246b012005-08-11 16:25:23 -04001311 }
1312
Chuck Lever46121cf2007-01-31 12:14:08 -05001313 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001314 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001315 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1316 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1317 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001318
Chuck Lever51971132006-12-05 16:35:19 -05001319 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001320 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001321 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001322 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1323 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001324 }
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001325}
1326
1327/*
1328 * Finds the request corresponding to the RPC xid and invokes the common
1329 * tcp read code to read the data.
1330 */
1331static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1332 struct xdr_skb_reader *desc)
1333{
1334 struct sock_xprt *transport =
1335 container_of(xprt, struct sock_xprt, xprt);
1336 struct rpc_rqst *req;
1337
1338 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1339
1340 /* Find and lock the request corresponding to this xid */
Trond Myklebustedc1b012015-10-05 10:53:49 -04001341 spin_lock_bh(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001342 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1343 if (!req) {
1344 dprintk("RPC: XID %08x request not found!\n",
1345 ntohl(transport->tcp_xid));
Trond Myklebustedc1b012015-10-05 10:53:49 -04001346 spin_unlock_bh(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001347 return -1;
1348 }
1349
1350 xs_tcp_read_common(xprt, desc, req);
1351
Chuck Levere136d092006-12-05 16:35:23 -05001352 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001353 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001354
Trond Myklebustedc1b012015-10-05 10:53:49 -04001355 spin_unlock_bh(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001356 return 0;
1357}
1358
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001359#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001360/*
1361 * Obtains an rpc_rqst previously allocated and invokes the common
1362 * tcp read code to read the data. The result is placed in the callback
1363 * queue.
1364 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1365 * connection and return -1.
1366 */
Trond Myklebust2ea24492014-02-10 11:18:39 -05001367static int xs_tcp_read_callback(struct rpc_xprt *xprt,
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001368 struct xdr_skb_reader *desc)
1369{
1370 struct sock_xprt *transport =
1371 container_of(xprt, struct sock_xprt, xprt);
1372 struct rpc_rqst *req;
1373
Trond Myklebust2ea24492014-02-10 11:18:39 -05001374 /* Look up and lock the request corresponding to the given XID */
Trond Myklebustedc1b012015-10-05 10:53:49 -04001375 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust2ea24492014-02-10 11:18:39 -05001376 req = xprt_lookup_bc_request(xprt, transport->tcp_xid);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001377 if (req == NULL) {
Trond Myklebustedc1b012015-10-05 10:53:49 -04001378 spin_unlock_bh(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001379 printk(KERN_WARNING "Callback slot table overflowed\n");
1380 xprt_force_disconnect(xprt);
1381 return -1;
1382 }
1383
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001384 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1385 xs_tcp_read_common(xprt, desc, req);
1386
Trond Myklebust2ea24492014-02-10 11:18:39 -05001387 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
1388 xprt_complete_bc_request(req, transport->tcp_copied);
Trond Myklebustedc1b012015-10-05 10:53:49 -04001389 spin_unlock_bh(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001390
1391 return 0;
1392}
1393
1394static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1395 struct xdr_skb_reader *desc)
1396{
1397 struct sock_xprt *transport =
1398 container_of(xprt, struct sock_xprt, xprt);
1399
1400 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1401 xs_tcp_read_reply(xprt, desc) :
1402 xs_tcp_read_callback(xprt, desc);
1403}
Chuck Lever76566772015-10-24 17:28:32 -04001404
1405static int xs_tcp_bc_up(struct svc_serv *serv, struct net *net)
1406{
1407 int ret;
1408
1409 ret = svc_create_xprt(serv, "tcp-bc", net, PF_INET, 0,
1410 SVC_SOCK_ANONYMOUS);
1411 if (ret < 0)
1412 return ret;
1413 return 0;
1414}
Chuck Lever6b26cc82016-05-02 14:40:40 -04001415
1416static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
1417{
1418 return PAGE_SIZE;
1419}
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001420#else
1421static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1422 struct xdr_skb_reader *desc)
1423{
1424 return xs_tcp_read_reply(xprt, desc);
1425}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001426#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001427
1428/*
1429 * Read data off the transport. This can be either an RPC_CALL or an
1430 * RPC_REPLY. Relay the processing to helper functions.
1431 */
1432static void xs_tcp_read_data(struct rpc_xprt *xprt,
1433 struct xdr_skb_reader *desc)
1434{
1435 struct sock_xprt *transport =
1436 container_of(xprt, struct sock_xprt, xprt);
1437
1438 if (_xs_tcp_read_data(xprt, desc) == 0)
1439 xs_tcp_check_fraghdr(transport);
1440 else {
1441 /*
1442 * The transport_lock protects the request handling.
1443 * There's no need to hold it to update the tcp_flags.
1444 */
1445 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1446 }
Chuck Levera246b012005-08-11 16:25:23 -04001447}
1448
Chuck Leverdd456472006-12-05 16:35:44 -05001449static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001450{
1451 size_t len;
1452
Chuck Lever51971132006-12-05 16:35:19 -05001453 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001454 if (len > desc->count)
1455 len = desc->count;
1456 desc->count -= len;
1457 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001458 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001459 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001460 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001461}
1462
Chuck Lever9903cd12005-08-11 16:25:26 -04001463static 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 -04001464{
1465 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001466 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001467 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001468 .skb = skb,
1469 .offset = offset,
1470 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001471 };
Chuck Levera246b012005-08-11 16:25:23 -04001472
Chuck Lever46121cf2007-01-31 12:14:08 -05001473 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001474 do {
Jeff Layton1a867a02014-10-28 14:24:14 -04001475 trace_xs_tcp_data_recv(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001476 /* Read in a new fragment marker if necessary */
1477 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001478 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001479 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001480 continue;
1481 }
1482 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001483 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001484 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001485 continue;
1486 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001487 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001488 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001489 xs_tcp_read_calldir(transport, &desc);
1490 continue;
1491 }
Chuck Levera246b012005-08-11 16:25:23 -04001492 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001493 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001494 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001495 continue;
1496 }
1497 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001498 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001499 } while (desc.count);
Jeff Layton1a867a02014-10-28 14:24:14 -04001500 trace_xs_tcp_data_recv(transport);
Chuck Lever46121cf2007-01-31 12:14:08 -05001501 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001502 return len - desc.count;
1503}
1504
Trond Myklebust66d7a562015-10-05 11:19:20 -04001505static void xs_tcp_data_receive(struct sock_xprt *transport)
1506{
1507 struct rpc_xprt *xprt = &transport->xprt;
1508 struct sock *sk;
1509 read_descriptor_t rd_desc = {
1510 .count = 2*1024*1024,
1511 .arg.data = xprt,
1512 };
1513 unsigned long total = 0;
1514 int read = 0;
1515
Trond Myklebustedc1b012015-10-05 10:53:49 -04001516 mutex_lock(&transport->recv_mutex);
Trond Myklebust66d7a562015-10-05 11:19:20 -04001517 sk = transport->inet;
Trond Myklebustedc1b012015-10-05 10:53:49 -04001518 if (sk == NULL)
1519 goto out;
Trond Myklebust66d7a562015-10-05 11:19:20 -04001520
1521 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1522 for (;;) {
Trond Myklebustedc1b012015-10-05 10:53:49 -04001523 lock_sock(sk);
Trond Myklebust66d7a562015-10-05 11:19:20 -04001524 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001525 if (read <= 0) {
1526 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
1527 release_sock(sk);
1528 if (!test_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
1529 break;
1530 } else {
1531 release_sock(sk);
1532 total += read;
1533 }
Trond Myklebust66d7a562015-10-05 11:19:20 -04001534 rd_desc.count = 65536;
1535 }
Trond Myklebustedc1b012015-10-05 10:53:49 -04001536out:
1537 mutex_unlock(&transport->recv_mutex);
Trond Myklebust66d7a562015-10-05 11:19:20 -04001538 trace_xs_tcp_data_ready(xprt, read, total);
1539}
1540
Trond Myklebustedc1b012015-10-05 10:53:49 -04001541static void xs_tcp_data_receive_workfn(struct work_struct *work)
1542{
1543 struct sock_xprt *transport =
1544 container_of(work, struct sock_xprt, recv_worker);
1545 xs_tcp_data_receive(transport);
1546}
1547
Chuck Lever9903cd12005-08-11 16:25:26 -04001548/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001549 * xs_tcp_state_change - callback to handle TCP socket state changes
1550 * @sk: socket whose state has changed
1551 *
1552 */
1553static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001554{
Chuck Lever9903cd12005-08-11 16:25:26 -04001555 struct rpc_xprt *xprt;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001556 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001557
Eric Dumazetf064af12010-09-22 12:43:39 +00001558 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001559 if (!(xprt = xprt_from_sock(sk)))
1560 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001561 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001562 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001563 sk->sk_state, xprt_connected(xprt),
1564 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001565 sock_flag(sk, SOCK_ZAPPED),
1566 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001567
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001568 transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001569 trace_rpc_socket_state_change(xprt, sk->sk_socket);
Chuck Levera246b012005-08-11 16:25:23 -04001570 switch (sk->sk_state) {
1571 case TCP_ESTABLISHED:
Eric Dumazetf064af12010-09-22 12:43:39 +00001572 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001573 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001574
Chuck Levera246b012005-08-11 16:25:23 -04001575 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001576 transport->tcp_offset = 0;
1577 transport->tcp_reclen = 0;
1578 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001579 transport->tcp_flags =
1580 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Trond Myklebust8b717982013-09-26 10:18:04 -04001581 xprt->connect_cookie++;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001582 clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
1583 xprt_clear_connecting(xprt);
Chuck Lever51971132006-12-05 16:35:19 -05001584
Trond Myklebust2a491992009-03-11 14:38:00 -04001585 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001586 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001587 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001588 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001589 case TCP_FIN_WAIT1:
1590 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001591 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001592 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001593 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001594 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001595 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001596 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001597 smp_mb__after_atomic();
Chuck Levera246b012005-08-11 16:25:23 -04001598 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001599 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001600 /* The server initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001601 xprt->connect_cookie++;
Trond Myklebustd0bea452012-10-23 11:35:47 -04001602 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebusta519fc72012-09-12 16:49:15 -04001603 xs_tcp_force_close(xprt);
Trond Myklebust663b8852008-01-01 18:42:12 -05001604 case TCP_CLOSING:
1605 /*
1606 * If the server closed down the connection, make sure that
1607 * we back off before reconnecting
1608 */
1609 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1610 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001611 break;
1612 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001613 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001614 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001615 clear_bit(XPRT_CONNECTED, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001616 smp_mb__after_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001617 break;
1618 case TCP_CLOSE:
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001619 if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
1620 &transport->sock_state))
1621 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001622 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001623 }
1624 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001625 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001626}
1627
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001628static void xs_write_space(struct sock *sk)
1629{
Trond Myklebust13331a52016-01-06 08:57:06 -05001630 struct socket_wq *wq;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001631 struct rpc_xprt *xprt;
1632
Trond Myklebust13331a52016-01-06 08:57:06 -05001633 if (!sk->sk_socket)
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001634 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001635 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001636
1637 if (unlikely(!(xprt = xprt_from_sock(sk))))
1638 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001639 rcu_read_lock();
1640 wq = rcu_dereference(sk->sk_wq);
1641 if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0)
1642 goto out;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001643
1644 xprt_write_space(xprt);
Trond Myklebust13331a52016-01-06 08:57:06 -05001645out:
1646 rcu_read_unlock();
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001647}
1648
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001649/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001650 * xs_udp_write_space - callback invoked when socket buffer space
1651 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001652 * @sk: socket whose state has changed
1653 *
Chuck Levera246b012005-08-11 16:25:23 -04001654 * Called when more output buffer space is available for this socket.
1655 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001656 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001657 * with a bunch of small requests.
1658 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001659static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001660{
Eric Dumazetf064af12010-09-22 12:43:39 +00001661 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001662
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001663 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001664 if (sock_writeable(sk))
1665 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001666
Eric Dumazetf064af12010-09-22 12:43:39 +00001667 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001668}
Chuck Levera246b012005-08-11 16:25:23 -04001669
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001670/**
1671 * xs_tcp_write_space - callback invoked when socket buffer space
1672 * becomes available
1673 * @sk: socket whose state has changed
1674 *
1675 * Called when more output buffer space is available for this socket.
1676 * We try not to wake our writers until they can make "significant"
1677 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1678 * with a bunch of small requests.
1679 */
1680static void xs_tcp_write_space(struct sock *sk)
1681{
Eric Dumazetf064af12010-09-22 12:43:39 +00001682 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001683
1684 /* from net/core/stream.c:sk_stream_write_space */
Eric Dumazet64dc6132013-07-22 20:26:31 -07001685 if (sk_stream_is_writeable(sk))
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001686 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001687
Eric Dumazetf064af12010-09-22 12:43:39 +00001688 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001689}
1690
Chuck Lever470056c2005-08-25 16:25:56 -07001691static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001692{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001693 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1694 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001695
Chuck Lever7c6e0662006-12-05 16:35:30 -05001696 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001697 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001698 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001699 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001700 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001701 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001702 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001703 sk->sk_write_space(sk);
1704 }
1705}
1706
Chuck Lever43118c22005-08-25 16:25:49 -07001707/**
Chuck Lever470056c2005-08-25 16:25:56 -07001708 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001709 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001710 * @sndsize: requested size of send buffer, in bytes
1711 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001712 *
Chuck Lever470056c2005-08-25 16:25:56 -07001713 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001714 */
Chuck Lever470056c2005-08-25 16:25:56 -07001715static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001716{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001717 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1718
1719 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001720 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001721 transport->sndsize = sndsize + 1024;
1722 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001723 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001724 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001725
1726 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001727}
1728
Chuck Lever46c0ee82005-08-25 16:25:52 -07001729/**
1730 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1731 * @task: task that timed out
1732 *
1733 * Adjust the congestion window after a retransmit timeout has occurred.
1734 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001735static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever46c0ee82005-08-25 16:25:52 -07001736{
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001737 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
Chuck Lever46c0ee82005-08-25 16:25:52 -07001738}
1739
Chuck Leverb85d8802006-05-25 01:40:49 -04001740static unsigned short xs_get_random_port(void)
1741{
Frank Sorenson5d718992016-07-08 16:35:23 -05001742 unsigned short range = xprt_max_resvport - xprt_min_resvport + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001743 unsigned short rand = (unsigned short) prandom_u32() % range;
Chuck Leverb85d8802006-05-25 01:40:49 -04001744 return rand + xprt_min_resvport;
1745}
1746
Chuck Lever92200412006-01-03 09:55:51 +01001747/**
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001748 * xs_set_reuseaddr_port - set the socket's port and address reuse options
1749 * @sock: socket
1750 *
1751 * Note that this function has to be called on all sockets that share the
1752 * same port, and it must be called before binding.
1753 */
1754static void xs_sock_set_reuseport(struct socket *sock)
1755{
Trond Myklebust402e23b2015-02-09 17:20:14 -05001756 int opt = 1;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001757
Trond Myklebust402e23b2015-02-09 17:20:14 -05001758 kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
1759 (char *)&opt, sizeof(opt));
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001760}
1761
1762static unsigned short xs_sock_getport(struct socket *sock)
1763{
1764 struct sockaddr_storage buf;
1765 int buflen;
1766 unsigned short port = 0;
1767
1768 if (kernel_getsockname(sock, (struct sockaddr *)&buf, &buflen) < 0)
1769 goto out;
1770 switch (buf.ss_family) {
1771 case AF_INET6:
1772 port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
1773 break;
1774 case AF_INET:
1775 port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
1776 }
1777out:
1778 return port;
1779}
1780
1781/**
Chuck Lever92200412006-01-03 09:55:51 +01001782 * xs_set_port - reset the port number in the remote endpoint address
1783 * @xprt: generic transport
1784 * @port: new port number
1785 *
1786 */
1787static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1788{
Chuck Lever46121cf2007-01-31 12:14:08 -05001789 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001790
Chuck Lever9dc3b092009-08-09 15:09:46 -04001791 rpc_set_port(xs_addr(xprt), port);
1792 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001793}
1794
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001795static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
1796{
1797 if (transport->srcport == 0)
1798 transport->srcport = xs_sock_getport(sock);
1799}
1800
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001801static unsigned short xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001802{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001803 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001804
1805 if (port == 0 && transport->xprt.resvport)
1806 port = xs_get_random_port();
1807 return port;
1808}
1809
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001810static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001811{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001812 if (transport->srcport != 0)
1813 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001814 if (!transport->xprt.resvport)
1815 return 0;
1816 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1817 return xprt_max_resvport;
1818 return --port;
1819}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001820static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001821{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001822 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001823 int err, nloop = 0;
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001824 unsigned short port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001825 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001826
Chris Perl0f7a6222014-09-05 15:40:21 -04001827 /*
1828 * If we are asking for any ephemeral port (i.e. port == 0 &&
1829 * transport->xprt.resvport == 0), don't bind. Let the local
1830 * port selection happen implicitly when the socket is used
1831 * (for example at connect time).
1832 *
1833 * This ensures that we can continue to establish TCP
1834 * connections even when all local ephemeral ports are already
1835 * a part of some TCP connection. This makes no difference
1836 * for UDP sockets, but also doens't harm them.
1837 *
1838 * If we're asking for any reserved port (i.e. port == 0 &&
1839 * transport->xprt.resvport == 1) xs_get_srcport above will
1840 * ensure that port is non-zero and we will bind as needed.
1841 */
1842 if (port == 0)
1843 return 0;
1844
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001845 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001846 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001847 rpc_set_port((struct sockaddr *)&myaddr, port);
1848 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1849 transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001850 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001851 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001852 break;
Chuck Levera246b012005-08-11 16:25:23 -04001853 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001854 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001855 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001856 if (port > last)
1857 nloop++;
1858 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001859
Chuck Lever4232e862010-10-20 11:52:51 -04001860 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001861 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1862 &((struct sockaddr_in *)&myaddr)->sin_addr,
1863 port, err ? "failed" : "ok", err);
1864 else
1865 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1866 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1867 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001868 return err;
1869}
1870
Chuck Lever176e21e2011-05-09 15:22:44 -04001871/*
1872 * We don't support autobind on AF_LOCAL sockets
1873 */
1874static void xs_local_rpcbind(struct rpc_task *task)
1875{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001876 xprt_set_bound(task->tk_xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04001877}
1878
1879static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1880{
1881}
Chuck Levera246b012005-08-11 16:25:23 -04001882
Peter Zijlstraed075362006-12-06 20:35:24 -08001883#ifdef CONFIG_DEBUG_LOCK_ALLOC
1884static struct lock_class_key xs_key[2];
1885static struct lock_class_key xs_slock_key[2];
1886
Chuck Lever176e21e2011-05-09 15:22:44 -04001887static inline void xs_reclassify_socketu(struct socket *sock)
1888{
1889 struct sock *sk = sock->sk;
1890
Chuck Lever176e21e2011-05-09 15:22:44 -04001891 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1892 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1893}
1894
Chuck Lever8945ee52007-08-06 11:58:04 -04001895static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001896{
1897 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001898
Chuck Lever8945ee52007-08-06 11:58:04 -04001899 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1900 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1901}
Peter Zijlstraed075362006-12-06 20:35:24 -08001902
Chuck Lever8945ee52007-08-06 11:58:04 -04001903static inline void xs_reclassify_socket6(struct socket *sock)
1904{
1905 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001906
Chuck Lever8945ee52007-08-06 11:58:04 -04001907 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1908 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001909}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001910
1911static inline void xs_reclassify_socket(int family, struct socket *sock)
1912{
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02001913 if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -04001914 return;
1915
Chuck Lever4232e862010-10-20 11:52:51 -04001916 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001917 case AF_LOCAL:
1918 xs_reclassify_socketu(sock);
1919 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001920 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001921 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001922 break;
1923 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001924 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001925 break;
1926 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001927}
Peter Zijlstraed075362006-12-06 20:35:24 -08001928#else
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001929static inline void xs_reclassify_socket(int family, struct socket *sock)
1930{
1931}
Peter Zijlstraed075362006-12-06 20:35:24 -08001932#endif
1933
NeilBrown93dc41b2013-10-31 16:14:36 +11001934static void xs_dummy_setup_socket(struct work_struct *work)
1935{
1936}
1937
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001938static struct socket *xs_create_sock(struct rpc_xprt *xprt,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001939 struct sock_xprt *transport, int family, int type,
1940 int protocol, bool reuseport)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001941{
1942 struct socket *sock;
1943 int err;
1944
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001945 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001946 if (err < 0) {
1947 dprintk("RPC: can't create %d transport socket (%d).\n",
1948 protocol, -err);
1949 goto out;
1950 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001951 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001952
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001953 if (reuseport)
1954 xs_sock_set_reuseport(sock);
1955
Ben Hutchings4cea2882011-02-22 21:54:34 +00001956 err = xs_bind(transport, sock);
1957 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001958 sock_release(sock);
1959 goto out;
1960 }
1961
1962 return sock;
1963out:
1964 return ERR_PTR(err);
1965}
1966
Chuck Lever176e21e2011-05-09 15:22:44 -04001967static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1968 struct socket *sock)
1969{
1970 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1971 xprt);
1972
1973 if (!transport->inet) {
1974 struct sock *sk = sock->sk;
1975
1976 write_lock_bh(&sk->sk_callback_lock);
1977
1978 xs_save_old_callbacks(transport, sk);
1979
1980 sk->sk_user_data = xprt;
Trond Myklebusta2648092015-10-06 17:03:00 -04001981 sk->sk_data_ready = xs_data_ready;
Chuck Lever176e21e2011-05-09 15:22:44 -04001982 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07001983 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05001984 sk->sk_error_report = xs_error_report;
Trond Myklebustc2126152015-08-19 21:46:15 -05001985 sk->sk_allocation = GFP_NOIO;
Chuck Lever176e21e2011-05-09 15:22:44 -04001986
1987 xprt_clear_connected(xprt);
1988
1989 /* Reset to new socket */
1990 transport->sock = sock;
1991 transport->inet = sk;
1992
1993 write_unlock_bh(&sk->sk_callback_lock);
1994 }
1995
1996 /* Tell the socket layer to start connecting... */
1997 xprt->stat.connect_count++;
1998 xprt->stat.connect_start = jiffies;
1999 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
2000}
2001
2002/**
2003 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
Chuck Lever176e21e2011-05-09 15:22:44 -04002004 * @transport: socket transport to connect
Chuck Lever176e21e2011-05-09 15:22:44 -04002005 */
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002006static int xs_local_setup_socket(struct sock_xprt *transport)
Chuck Lever176e21e2011-05-09 15:22:44 -04002007{
Chuck Lever176e21e2011-05-09 15:22:44 -04002008 struct rpc_xprt *xprt = &transport->xprt;
2009 struct socket *sock;
2010 int status = -EIO;
2011
Chuck Lever176e21e2011-05-09 15:22:44 -04002012 status = __sock_create(xprt->xprt_net, AF_LOCAL,
2013 SOCK_STREAM, 0, &sock, 1);
2014 if (status < 0) {
2015 dprintk("RPC: can't create AF_LOCAL "
2016 "transport socket (%d).\n", -status);
2017 goto out;
2018 }
Stefan Hajnoczid1358912015-12-02 14:17:52 +08002019 xs_reclassify_socket(AF_LOCAL, sock);
Chuck Lever176e21e2011-05-09 15:22:44 -04002020
2021 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
2022 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2023
2024 status = xs_local_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002025 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever176e21e2011-05-09 15:22:44 -04002026 switch (status) {
2027 case 0:
2028 dprintk("RPC: xprt %p connected to %s\n",
2029 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2030 xprt_set_connected(xprt);
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002031 case -ENOBUFS:
Chuck Lever176e21e2011-05-09 15:22:44 -04002032 break;
2033 case -ENOENT:
2034 dprintk("RPC: xprt %p: socket %s does not exist\n",
2035 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2036 break;
Trond Myklebust4a20a982012-12-15 17:02:29 -05002037 case -ECONNREFUSED:
2038 dprintk("RPC: xprt %p: connection refused for %s\n",
2039 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2040 break;
Chuck Lever176e21e2011-05-09 15:22:44 -04002041 default:
2042 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
2043 __func__, -status,
2044 xprt->address_strings[RPC_DISPLAY_ADDR]);
2045 }
2046
2047out:
2048 xprt_clear_connecting(xprt);
2049 xprt_wake_pending_tasks(xprt, status);
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002050 return status;
2051}
2052
Linus Torvaldsb6669732013-02-28 18:02:55 -08002053static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002054{
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002055 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2056 int ret;
2057
2058 if (RPC_IS_ASYNC(task)) {
2059 /*
2060 * We want the AF_LOCAL connect to be resolved in the
2061 * filesystem namespace of the process making the rpc
2062 * call. Thus we connect synchronously.
2063 *
2064 * If we want to support asynchronous AF_LOCAL calls,
2065 * we'll need to figure out how to pass a namespace to
2066 * connect.
2067 */
2068 rpc_exit(task, -ENOTCONN);
2069 return;
2070 }
2071 ret = xs_local_setup_socket(transport);
2072 if (ret && !RPC_IS_SOFTCONN(task))
2073 msleep_interruptible(15000);
Chuck Lever176e21e2011-05-09 15:22:44 -04002074}
2075
Jeff Layton3c87ef62015-06-03 16:14:25 -04002076#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Jeff Laytond6e971d2015-06-03 16:14:28 -04002077/*
2078 * Note that this should be called with XPRT_LOCKED held (or when we otherwise
2079 * know that we have exclusive access to the socket), to guard against
2080 * races with xs_reset_transport.
2081 */
Mel Gormana564b8f2012-07-31 16:45:12 -07002082static void xs_set_memalloc(struct rpc_xprt *xprt)
2083{
2084 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2085 xprt);
2086
Jeff Laytond6e971d2015-06-03 16:14:28 -04002087 /*
2088 * If there's no sock, then we have nothing to set. The
2089 * reconnecting process will get it for us.
2090 */
2091 if (!transport->inet)
2092 return;
Jeff Layton8e228132015-06-03 16:14:26 -04002093 if (atomic_read(&xprt->swapper))
Mel Gormana564b8f2012-07-31 16:45:12 -07002094 sk_set_memalloc(transport->inet);
2095}
2096
2097/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002098 * xs_enable_swap - Tag this transport as being used for swap.
Mel Gormana564b8f2012-07-31 16:45:12 -07002099 * @xprt: transport to tag
Mel Gormana564b8f2012-07-31 16:45:12 -07002100 *
Jeff Layton8e228132015-06-03 16:14:26 -04002101 * Take a reference to this transport on behalf of the rpc_clnt, and
2102 * optionally mark it for swapping if it wasn't already.
Mel Gormana564b8f2012-07-31 16:45:12 -07002103 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002104static int
2105xs_enable_swap(struct rpc_xprt *xprt)
Mel Gormana564b8f2012-07-31 16:45:12 -07002106{
Jeff Laytond6e971d2015-06-03 16:14:28 -04002107 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Mel Gormana564b8f2012-07-31 16:45:12 -07002108
Jeff Laytond6e971d2015-06-03 16:14:28 -04002109 if (atomic_inc_return(&xprt->swapper) != 1)
2110 return 0;
2111 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2112 return -ERESTARTSYS;
2113 if (xs->inet)
2114 sk_set_memalloc(xs->inet);
2115 xprt_release_xprt(xprt, NULL);
Jeff Layton8e228132015-06-03 16:14:26 -04002116 return 0;
Mel Gormana564b8f2012-07-31 16:45:12 -07002117}
Jeff Layton8e228132015-06-03 16:14:26 -04002118
2119/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002120 * xs_disable_swap - Untag this transport as being used for swap.
Jeff Layton8e228132015-06-03 16:14:26 -04002121 * @xprt: transport to tag
2122 *
2123 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
2124 * swapper refcount goes to 0, untag the socket as a memalloc socket.
2125 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002126static void
2127xs_disable_swap(struct rpc_xprt *xprt)
Jeff Layton8e228132015-06-03 16:14:26 -04002128{
Jeff Laytond6e971d2015-06-03 16:14:28 -04002129 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Jeff Layton8e228132015-06-03 16:14:26 -04002130
Jeff Laytond6e971d2015-06-03 16:14:28 -04002131 if (!atomic_dec_and_test(&xprt->swapper))
2132 return;
2133 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2134 return;
2135 if (xs->inet)
2136 sk_clear_memalloc(xs->inet);
2137 xprt_release_xprt(xprt, NULL);
Mel Gormana564b8f2012-07-31 16:45:12 -07002138}
Mel Gormana564b8f2012-07-31 16:45:12 -07002139#else
2140static void xs_set_memalloc(struct rpc_xprt *xprt)
2141{
2142}
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002143
2144static int
2145xs_enable_swap(struct rpc_xprt *xprt)
2146{
2147 return -EINVAL;
2148}
2149
2150static void
2151xs_disable_swap(struct rpc_xprt *xprt)
2152{
2153}
Mel Gormana564b8f2012-07-31 16:45:12 -07002154#endif
2155
Chuck Lever16be2d22007-08-06 11:57:38 -04002156static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04002157{
Chuck Lever16be2d22007-08-06 11:57:38 -04002158 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04002159
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002160 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002161 struct sock *sk = sock->sk;
2162
2163 write_lock_bh(&sk->sk_callback_lock);
2164
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002165 xs_save_old_callbacks(transport, sk);
2166
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002167 sk->sk_user_data = xprt;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002168 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002169 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002170 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebustc2126152015-08-19 21:46:15 -05002171 sk->sk_allocation = GFP_NOIO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002172
2173 xprt_set_connected(xprt);
2174
2175 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002176 transport->sock = sock;
2177 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002178
Mel Gormana564b8f2012-07-31 16:45:12 -07002179 xs_set_memalloc(xprt);
2180
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002181 write_unlock_bh(&sk->sk_callback_lock);
2182 }
Chuck Lever470056c2005-08-25 16:25:56 -07002183 xs_udp_do_set_buffer_size(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002184
2185 xprt->stat.connect_start = jiffies;
Chuck Lever16be2d22007-08-06 11:57:38 -04002186}
2187
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002188static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04002189{
2190 struct sock_xprt *transport =
2191 container_of(work, struct sock_xprt, connect_worker.work);
2192 struct rpc_xprt *xprt = &transport->xprt;
2193 struct socket *sock = transport->sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002194 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04002195
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002196 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002197 xs_addr(xprt)->sa_family, SOCK_DGRAM,
2198 IPPROTO_UDP, false);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002199 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04002200 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04002201
Chuck Leverc740eff2009-08-09 15:09:46 -04002202 dprintk("RPC: worker connecting xprt %p via %s to "
2203 "%s (port %s)\n", xprt,
2204 xprt->address_strings[RPC_DISPLAY_PROTO],
2205 xprt->address_strings[RPC_DISPLAY_ADDR],
2206 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04002207
2208 xs_udp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002209 trace_rpc_socket_connect(xprt, sock, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002210 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002211out:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002212 xprt_unlock_connect(xprt, transport);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002213 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002214 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002215}
2216
Trond Myklebust4876cc72015-06-19 16:17:57 -04002217/**
2218 * xs_tcp_shutdown - gracefully shut down a TCP socket
2219 * @xprt: transport
2220 *
2221 * Initiates a graceful shutdown of the TCP socket by calling the
2222 * equivalent of shutdown(SHUT_RDWR);
2223 */
2224static void xs_tcp_shutdown(struct rpc_xprt *xprt)
2225{
2226 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2227 struct socket *sock = transport->sock;
2228
2229 if (sock == NULL)
2230 return;
2231 if (xprt_connected(xprt)) {
2232 kernel_sock_shutdown(sock, SHUT_RDWR);
2233 trace_rpc_socket_shutdown(xprt, sock);
2234 } else
2235 xs_reset_transport(transport);
2236}
2237
Chuck Lever16be2d22007-08-06 11:57:38 -04002238static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002239{
Chuck Lever16be2d22007-08-06 11:57:38 -04002240 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002241 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002242
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002243 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002244 struct sock *sk = sock->sk;
Trond Myklebust7f260e82013-09-24 11:25:22 -04002245 unsigned int keepidle = xprt->timeout->to_initval / HZ;
2246 unsigned int keepcnt = xprt->timeout->to_retries + 1;
2247 unsigned int opt_on = 1;
Trond Myklebust775f06a2015-06-20 15:31:54 -04002248 unsigned int timeo;
NeilBrownd88e4d82016-08-04 16:24:28 +10002249 unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC;
Trond Myklebust7f260e82013-09-24 11:25:22 -04002250
2251 /* TCP Keepalive options */
2252 kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
2253 (char *)&opt_on, sizeof(opt_on));
2254 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
2255 (char *)&keepidle, sizeof(keepidle));
2256 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
2257 (char *)&keepidle, sizeof(keepidle));
2258 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
2259 (char *)&keepcnt, sizeof(keepcnt));
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002260
NeilBrownd88e4d82016-08-04 16:24:28 +10002261 /* Avoid temporary address, they are bad for long-lived
2262 * connections such as NFS mounts.
2263 * RFC4941, section 3.6 suggests that:
2264 * Individual applications, which have specific
2265 * knowledge about the normal duration of connections,
2266 * MAY override this as appropriate.
2267 */
2268 kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES,
2269 (char *)&addr_pref, sizeof(addr_pref));
2270
Trond Myklebust775f06a2015-06-20 15:31:54 -04002271 /* TCP user timeout (see RFC5482) */
2272 timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
2273 (xprt->timeout->to_retries + 1);
2274 kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT,
2275 (char *)&timeo, sizeof(timeo));
2276
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002277 write_lock_bh(&sk->sk_callback_lock);
2278
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002279 xs_save_old_callbacks(transport, sk);
2280
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002281 sk->sk_user_data = xprt;
Trond Myklebust5157b952016-05-29 10:13:24 -04002282 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002283 sk->sk_state_change = xs_tcp_state_change;
2284 sk->sk_write_space = xs_tcp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002285 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05002286 sk->sk_error_report = xs_error_report;
Trond Myklebustc2126152015-08-19 21:46:15 -05002287 sk->sk_allocation = GFP_NOIO;
Chuck Lever3167e122005-08-25 16:25:55 -07002288
2289 /* socket options */
Chuck Lever3167e122005-08-25 16:25:55 -07002290 sock_reset_flag(sk, SOCK_LINGER);
Chuck Lever3167e122005-08-25 16:25:55 -07002291 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002292
2293 xprt_clear_connected(xprt);
2294
2295 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002296 transport->sock = sock;
2297 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002298
2299 write_unlock_bh(&sk->sk_callback_lock);
2300 }
2301
Trond Myklebust01d37c42009-03-11 14:09:39 -04002302 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002303 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002304
Mel Gormana564b8f2012-07-31 16:45:12 -07002305 xs_set_memalloc(xprt);
2306
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002307 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05002308 xprt->stat.connect_count++;
2309 xprt->stat.connect_start = jiffies;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04002310 set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002311 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2312 switch (ret) {
2313 case 0:
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002314 xs_set_srcport(transport, sock);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002315 case -EINPROGRESS:
2316 /* SYN_SENT! */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002317 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2318 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust1f4c17a2016-08-01 13:36:08 -04002319 break;
2320 case -EADDRNOTAVAIL:
2321 /* Source port number is unavailable. Try a new one! */
2322 transport->srcport = 0;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002323 }
2324out:
2325 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002326}
2327
2328/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002329 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
Chuck Lever16be2d22007-08-06 11:57:38 -04002330 *
2331 * Invoked by a work queue tasklet.
2332 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002333static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002334{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002335 struct sock_xprt *transport =
2336 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002337 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002338 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002339 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002340
Chuck Lever16be2d22007-08-06 11:57:38 -04002341 if (!sock) {
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002342 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002343 xs_addr(xprt)->sa_family, SOCK_STREAM,
2344 IPPROTO_TCP, true);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002345 if (IS_ERR(sock)) {
2346 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002347 goto out;
2348 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002349 }
2350
Chuck Leverc740eff2009-08-09 15:09:46 -04002351 dprintk("RPC: worker connecting xprt %p via %s to "
2352 "%s (port %s)\n", xprt,
2353 xprt->address_strings[RPC_DISPLAY_PROTO],
2354 xprt->address_strings[RPC_DISPLAY_ADDR],
2355 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002356
2357 status = xs_tcp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002358 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever46121cf2007-01-31 12:14:08 -05002359 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2360 xprt, -status, xprt_connected(xprt),
2361 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002362 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002363 default:
2364 printk("%s: connect returned unhandled error %d\n",
2365 __func__, status);
2366 case -EADDRNOTAVAIL:
2367 /* We're probably in TIME_WAIT. Get rid of existing socket,
2368 * and retry
2369 */
Trond Myklebusta519fc72012-09-12 16:49:15 -04002370 xs_tcp_force_close(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002371 break;
Trond Myklebust2a491992009-03-11 14:38:00 -04002372 case 0:
2373 case -EINPROGRESS:
2374 case -EALREADY:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002375 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002376 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002377 case -EINVAL:
2378 /* Happens, for instance, if the user specified a link
2379 * local IPv6 address without a scope-id.
2380 */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002381 case -ECONNREFUSED:
2382 case -ECONNRESET:
2383 case -ENETUNREACH:
Trond Myklebust3913c782015-02-08 21:44:04 -05002384 case -EADDRINUSE:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002385 case -ENOBUFS:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002386 /* retry with existing socket, after a delay */
Trond Myklebust4efdd922015-02-08 15:34:28 -05002387 xs_tcp_force_close(xprt);
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002388 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002389 }
Trond Myklebust2a491992009-03-11 14:38:00 -04002390 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002391out:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002392 xprt_unlock_connect(xprt, transport);
Chuck Lever2226feb2005-08-11 16:25:38 -04002393 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002394 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04002395}
2396
Trond Myklebust02910172016-08-04 00:00:33 -04002397static unsigned long xs_reconnect_delay(const struct rpc_xprt *xprt)
2398{
2399 unsigned long start, now = jiffies;
2400
2401 start = xprt->stat.connect_start + xprt->reestablish_timeout;
2402 if (time_after(start, now))
2403 return start - now;
2404 return 0;
2405}
2406
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002407static void xs_reconnect_backoff(struct rpc_xprt *xprt)
2408{
2409 xprt->reestablish_timeout <<= 1;
2410 if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
2411 xprt->reestablish_timeout = xprt->max_reconnect_timeout;
2412 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2413 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2414}
2415
Chuck Lever68e220b2007-08-06 11:57:48 -04002416/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002417 * xs_connect - connect a socket to a remote endpoint
Trond Myklebust1b092092013-01-08 09:26:49 -05002418 * @xprt: pointer to transport structure
Chuck Lever9903cd12005-08-11 16:25:26 -04002419 * @task: address of RPC task that manages state of connect request
2420 *
2421 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002422 *
2423 * UDP socket connects are synchronous, but we use a work queue anyway
2424 * to guarantee that even unprivileged user processes can set up a
2425 * socket on a privileged port.
2426 *
2427 * If a UDP socket connect fails, the delay behavior here prevents
2428 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002429 */
Trond Myklebust1b092092013-01-08 09:26:49 -05002430static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002431{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002432 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002433 unsigned long delay = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002434
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002435 WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2436
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002437 if (transport->sock != NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002438 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2439 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002440 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002441
2442 /* Start by resetting any existing state */
2443 xs_reset_transport(transport);
2444
Trond Myklebust02910172016-08-04 00:00:33 -04002445 delay = xs_reconnect_delay(xprt);
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002446 xs_reconnect_backoff(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002447
Trond Myklebust02910172016-08-04 00:00:33 -04002448 } else
Chuck Lever46121cf2007-01-31 12:14:08 -05002449 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002450
2451 queue_delayed_work(xprtiod_workqueue,
2452 &transport->connect_worker,
2453 delay);
Chuck Levera246b012005-08-11 16:25:23 -04002454}
2455
Chuck Lever262ca072006-03-20 13:44:16 -05002456/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002457 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2458 * @xprt: rpc_xprt struct containing statistics
2459 * @seq: output file
2460 *
2461 */
2462static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2463{
2464 long idle_time = 0;
2465
2466 if (xprt_connected(xprt))
2467 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2468
2469 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
Andy Adamson15a45202012-02-14 16:19:18 -05002470 "%llu %llu %lu %llu %llu\n",
Chuck Lever176e21e2011-05-09 15:22:44 -04002471 xprt->stat.bind_count,
2472 xprt->stat.connect_count,
2473 xprt->stat.connect_time,
2474 idle_time,
2475 xprt->stat.sends,
2476 xprt->stat.recvs,
2477 xprt->stat.bad_xids,
2478 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002479 xprt->stat.bklog_u,
2480 xprt->stat.max_slots,
2481 xprt->stat.sending_u,
2482 xprt->stat.pending_u);
Chuck Lever176e21e2011-05-09 15:22:44 -04002483}
2484
2485/**
Chuck Lever262ca072006-03-20 13:44:16 -05002486 * xs_udp_print_stats - display UDP socket-specifc stats
2487 * @xprt: rpc_xprt struct containing statistics
2488 * @seq: output file
2489 *
2490 */
2491static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2492{
Chuck Leverc8475462006-12-05 16:35:26 -05002493 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2494
Andy Adamson15a45202012-02-14 16:19:18 -05002495 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2496 "%lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002497 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002498 xprt->stat.bind_count,
2499 xprt->stat.sends,
2500 xprt->stat.recvs,
2501 xprt->stat.bad_xids,
2502 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002503 xprt->stat.bklog_u,
2504 xprt->stat.max_slots,
2505 xprt->stat.sending_u,
2506 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002507}
2508
2509/**
2510 * xs_tcp_print_stats - display TCP socket-specifc stats
2511 * @xprt: rpc_xprt struct containing statistics
2512 * @seq: output file
2513 *
2514 */
2515static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2516{
Chuck Leverc8475462006-12-05 16:35:26 -05002517 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002518 long idle_time = 0;
2519
2520 if (xprt_connected(xprt))
2521 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2522
Andy Adamson15a45202012-02-14 16:19:18 -05002523 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2524 "%llu %llu %lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002525 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002526 xprt->stat.bind_count,
2527 xprt->stat.connect_count,
2528 xprt->stat.connect_time,
2529 idle_time,
2530 xprt->stat.sends,
2531 xprt->stat.recvs,
2532 xprt->stat.bad_xids,
2533 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002534 xprt->stat.bklog_u,
2535 xprt->stat.max_slots,
2536 xprt->stat.sending_u,
2537 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002538}
2539
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002540/*
2541 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2542 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2543 * to use the server side send routines.
2544 */
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002545static int bc_malloc(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002546{
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002547 struct rpc_rqst *rqst = task->tk_rqstp;
2548 size_t size = rqst->rq_callsize;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002549 struct page *page;
2550 struct rpc_buffer *buf;
2551
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002552 if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
2553 WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
2554 size);
2555 return -EINVAL;
2556 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002557
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002558 page = alloc_page(GFP_KERNEL);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002559 if (!page)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002560 return -ENOMEM;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002561
2562 buf = page_address(page);
2563 buf->len = PAGE_SIZE;
2564
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002565 rqst->rq_buffer = buf->data;
2566 return 0;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002567}
2568
2569/*
2570 * Free the space allocated in the bc_alloc routine
2571 */
Chuck Lever3435c742016-09-15 10:55:29 -04002572static void bc_free(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002573{
Chuck Lever3435c742016-09-15 10:55:29 -04002574 void *buffer = task->tk_rqstp->rq_buffer;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002575 struct rpc_buffer *buf;
2576
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002577 buf = container_of(buffer, struct rpc_buffer, data);
2578 free_page((unsigned long)buf);
2579}
2580
2581/*
2582 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2583 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2584 */
2585static int bc_sendto(struct rpc_rqst *req)
2586{
2587 int len;
2588 struct xdr_buf *xbufp = &req->rq_snd_buf;
2589 struct rpc_xprt *xprt = req->rq_xprt;
2590 struct sock_xprt *transport =
2591 container_of(xprt, struct sock_xprt, xprt);
2592 struct socket *sock = transport->sock;
2593 unsigned long headoff;
2594 unsigned long tailoff;
2595
Chuck Lever61677ee2011-05-09 15:22:34 -04002596 xs_encode_stream_record_marker(xbufp);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002597
2598 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2599 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2600 len = svc_send_common(sock, xbufp,
2601 virt_to_page(xbufp->head[0].iov_base), headoff,
2602 xbufp->tail[0].iov_base, tailoff);
2603
2604 if (len != xbufp->len) {
2605 printk(KERN_NOTICE "Error sending entire callback!\n");
2606 len = -EAGAIN;
2607 }
2608
2609 return len;
2610}
2611
2612/*
2613 * The send routine. Borrows from svc_send
2614 */
2615static int bc_send_request(struct rpc_task *task)
2616{
2617 struct rpc_rqst *req = task->tk_rqstp;
2618 struct svc_xprt *xprt;
Andrzej Hajda7fc56132015-09-24 16:00:09 +02002619 int len;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002620
2621 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2622 /*
2623 * Get the server socket associated with this callback xprt
2624 */
2625 xprt = req->rq_xprt->bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002626
2627 /*
2628 * Grab the mutex to serialize data as the connection is shared
2629 * with the fore channel
2630 */
2631 if (!mutex_trylock(&xprt->xpt_mutex)) {
2632 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2633 if (!mutex_trylock(&xprt->xpt_mutex))
2634 return -EAGAIN;
2635 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2636 }
2637 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2638 len = -ENOTCONN;
2639 else
2640 len = bc_sendto(req);
2641 mutex_unlock(&xprt->xpt_mutex);
2642
2643 if (len > 0)
2644 len = 0;
2645
2646 return len;
2647}
2648
2649/*
2650 * The close routine. Since this is client initiated, we do nothing
2651 */
2652
2653static void bc_close(struct rpc_xprt *xprt)
2654{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002655}
2656
2657/*
2658 * The xprt destroy routine. Again, because this connection is client
2659 * initiated, we do nothing
2660 */
2661
2662static void bc_destroy(struct rpc_xprt *xprt)
2663{
Kinglong Mee47f72ef2014-03-24 11:58:16 +08002664 dprintk("RPC: bc_destroy xprt %p\n", xprt);
2665
2666 xs_xprt_free(xprt);
2667 module_put(THIS_MODULE);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002668}
2669
Chuck Lever176e21e2011-05-09 15:22:44 -04002670static struct rpc_xprt_ops xs_local_ops = {
2671 .reserve_xprt = xprt_reserve_xprt,
2672 .release_xprt = xs_tcp_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002673 .alloc_slot = xprt_alloc_slot,
Chuck Lever176e21e2011-05-09 15:22:44 -04002674 .rpcbind = xs_local_rpcbind,
2675 .set_port = xs_local_set_port,
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002676 .connect = xs_local_connect,
Chuck Lever176e21e2011-05-09 15:22:44 -04002677 .buf_alloc = rpc_malloc,
2678 .buf_free = rpc_free,
2679 .send_request = xs_local_send_request,
2680 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2681 .close = xs_close,
Trond Myklebusta1311d82013-10-31 09:18:49 -04002682 .destroy = xs_destroy,
Chuck Lever176e21e2011-05-09 15:22:44 -04002683 .print_stats = xs_local_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002684 .enable_swap = xs_enable_swap,
2685 .disable_swap = xs_disable_swap,
Chuck Lever176e21e2011-05-09 15:22:44 -04002686};
2687
Chuck Lever262965f2005-08-11 16:25:56 -04002688static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002689 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002690 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002691 .release_xprt = xprt_release_xprt_cong,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002692 .alloc_slot = xprt_alloc_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002693 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002694 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002695 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002696 .buf_alloc = rpc_malloc,
2697 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002698 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002699 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002700 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002701 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002702 .close = xs_close,
2703 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002704 .print_stats = xs_udp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002705 .enable_swap = xs_enable_swap,
2706 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002707 .inject_disconnect = xs_inject_disconnect,
Chuck Lever262965f2005-08-11 16:25:56 -04002708};
2709
2710static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002711 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002712 .release_xprt = xs_tcp_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002713 .alloc_slot = xprt_lock_and_alloc_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002714 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002715 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002716 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002717 .buf_alloc = rpc_malloc,
2718 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002719 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002720 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustc627d312015-02-10 11:06:04 -05002721 .close = xs_tcp_shutdown,
Chuck Lever9903cd12005-08-11 16:25:26 -04002722 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002723 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002724 .enable_swap = xs_enable_swap,
2725 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002726 .inject_disconnect = xs_inject_disconnect,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002727#ifdef CONFIG_SUNRPC_BACKCHANNEL
2728 .bc_setup = xprt_setup_bc,
Chuck Lever76566772015-10-24 17:28:32 -04002729 .bc_up = xs_tcp_bc_up,
Chuck Lever6b26cc82016-05-02 14:40:40 -04002730 .bc_maxpayload = xs_tcp_bc_maxpayload,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002731 .bc_free_rqst = xprt_free_bc_rqst,
2732 .bc_destroy = xprt_destroy_bc,
2733#endif
Chuck Levera246b012005-08-11 16:25:23 -04002734};
2735
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002736/*
2737 * The rpc_xprt_ops for the server backchannel
2738 */
2739
2740static struct rpc_xprt_ops bc_tcp_ops = {
2741 .reserve_xprt = xprt_reserve_xprt,
2742 .release_xprt = xprt_release_xprt,
Bryan Schumaker84e28a32012-09-24 13:39:01 -04002743 .alloc_slot = xprt_alloc_slot,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002744 .buf_alloc = bc_malloc,
2745 .buf_free = bc_free,
2746 .send_request = bc_send_request,
2747 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2748 .close = bc_close,
2749 .destroy = bc_destroy,
2750 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002751 .enable_swap = xs_enable_swap,
2752 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002753 .inject_disconnect = xs_inject_disconnect,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002754};
2755
Chuck Lever92476852010-10-20 11:53:01 -04002756static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2757{
2758 static const struct sockaddr_in sin = {
2759 .sin_family = AF_INET,
2760 .sin_addr.s_addr = htonl(INADDR_ANY),
2761 };
2762 static const struct sockaddr_in6 sin6 = {
2763 .sin6_family = AF_INET6,
2764 .sin6_addr = IN6ADDR_ANY_INIT,
2765 };
2766
2767 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002768 case AF_LOCAL:
2769 break;
Chuck Lever92476852010-10-20 11:53:01 -04002770 case AF_INET:
2771 memcpy(sap, &sin, sizeof(sin));
2772 break;
2773 case AF_INET6:
2774 memcpy(sap, &sin6, sizeof(sin6));
2775 break;
2776 default:
2777 dprintk("RPC: %s: Bad address family\n", __func__);
2778 return -EAFNOSUPPORT;
2779 }
2780 return 0;
2781}
2782
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002783static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002784 unsigned int slot_table_size,
2785 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002786{
2787 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002788 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002789
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002790 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002791 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002792 return ERR_PTR(-EBADF);
2793 }
2794
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002795 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2796 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002797 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002798 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2799 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002800 return ERR_PTR(-ENOMEM);
2801 }
2802
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002803 new = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002804 mutex_init(&new->recv_mutex);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002805 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2806 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002807 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002808 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002809 else {
2810 int err;
2811 err = xs_init_anyaddr(args->dstaddr->sa_family,
2812 (struct sockaddr *)&new->srcaddr);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002813 if (err != 0) {
2814 xprt_free(xprt);
Chuck Lever92476852010-10-20 11:53:01 -04002815 return ERR_PTR(err);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002816 }
Chuck Lever92476852010-10-20 11:53:01 -04002817 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002818
2819 return xprt;
2820}
2821
Chuck Lever176e21e2011-05-09 15:22:44 -04002822static const struct rpc_timeout xs_local_default_timeout = {
2823 .to_initval = 10 * HZ,
2824 .to_maxval = 10 * HZ,
2825 .to_retries = 2,
2826};
2827
2828/**
2829 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2830 * @args: rpc transport creation arguments
2831 *
2832 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2833 */
2834static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2835{
2836 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2837 struct sock_xprt *transport;
2838 struct rpc_xprt *xprt;
2839 struct rpc_xprt *ret;
2840
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002841 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2842 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002843 if (IS_ERR(xprt))
2844 return xprt;
2845 transport = container_of(xprt, struct sock_xprt, xprt);
2846
2847 xprt->prot = 0;
2848 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2849 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2850
2851 xprt->bind_timeout = XS_BIND_TO;
2852 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2853 xprt->idle_timeout = XS_IDLE_DISC_TO;
2854
2855 xprt->ops = &xs_local_ops;
2856 xprt->timeout = &xs_local_default_timeout;
2857
Trond Myklebusta2648092015-10-06 17:03:00 -04002858 INIT_WORK(&transport->recv_worker, xs_local_data_receive_workfn);
NeilBrown93dc41b2013-10-31 16:14:36 +11002859 INIT_DELAYED_WORK(&transport->connect_worker,
2860 xs_dummy_setup_socket);
2861
Chuck Lever176e21e2011-05-09 15:22:44 -04002862 switch (sun->sun_family) {
2863 case AF_LOCAL:
2864 if (sun->sun_path[0] != '/') {
2865 dprintk("RPC: bad AF_LOCAL address: %s\n",
2866 sun->sun_path);
2867 ret = ERR_PTR(-EINVAL);
2868 goto out_err;
2869 }
2870 xprt_set_bound(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002871 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
J. Bruce Fields7073ea82013-02-21 10:14:22 -05002872 ret = ERR_PTR(xs_local_setup_socket(transport));
2873 if (ret)
2874 goto out_err;
Chuck Lever176e21e2011-05-09 15:22:44 -04002875 break;
2876 default:
2877 ret = ERR_PTR(-EAFNOSUPPORT);
2878 goto out_err;
2879 }
2880
2881 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2882 xprt->address_strings[RPC_DISPLAY_ADDR]);
2883
2884 if (try_module_get(THIS_MODULE))
2885 return xprt;
2886 ret = ERR_PTR(-EINVAL);
2887out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002888 xs_xprt_free(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002889 return ret;
2890}
2891
Trond Myklebust2881ae72007-12-20 16:03:54 -05002892static const struct rpc_timeout xs_udp_default_timeout = {
2893 .to_initval = 5 * HZ,
2894 .to_maxval = 30 * HZ,
2895 .to_increment = 5 * HZ,
2896 .to_retries = 5,
2897};
2898
Chuck Lever9903cd12005-08-11 16:25:26 -04002899/**
2900 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002901 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002902 *
2903 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002904static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002905{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002906 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002907 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002908 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002909 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002910
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002911 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2912 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002913 if (IS_ERR(xprt))
2914 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002915 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002916
Chuck Leverec739ef2006-08-22 20:06:15 -04002917 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002918 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002919 /* XXX: header size can vary due to auth type, IPv6, etc. */
2920 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2921
Chuck Lever03bf4b72005-08-25 16:25:55 -07002922 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002923 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2924 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002925
Chuck Lever262965f2005-08-11 16:25:56 -04002926 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002927
Trond Myklebustba7392b2007-12-20 16:03:55 -05002928 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002929
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002930 INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002931 INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2932
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002933 switch (addr->sa_family) {
2934 case AF_INET:
2935 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2936 xprt_set_bound(xprt);
2937
Chuck Lever9dc3b092009-08-09 15:09:46 -04002938 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002939 break;
2940 case AF_INET6:
2941 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2942 xprt_set_bound(xprt);
2943
Chuck Lever9dc3b092009-08-09 15:09:46 -04002944 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002945 break;
2946 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002947 ret = ERR_PTR(-EAFNOSUPPORT);
2948 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002949 }
2950
Chuck Leverc740eff2009-08-09 15:09:46 -04002951 if (xprt_bound(xprt))
2952 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2953 xprt->address_strings[RPC_DISPLAY_ADDR],
2954 xprt->address_strings[RPC_DISPLAY_PORT],
2955 xprt->address_strings[RPC_DISPLAY_PROTO]);
2956 else
2957 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2958 xprt->address_strings[RPC_DISPLAY_ADDR],
2959 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002960
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002961 if (try_module_get(THIS_MODULE))
2962 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002963 ret = ERR_PTR(-EINVAL);
2964out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002965 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002966 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002967}
2968
Trond Myklebust2881ae72007-12-20 16:03:54 -05002969static const struct rpc_timeout xs_tcp_default_timeout = {
2970 .to_initval = 60 * HZ,
2971 .to_maxval = 60 * HZ,
2972 .to_retries = 2,
2973};
2974
Chuck Lever9903cd12005-08-11 16:25:26 -04002975/**
2976 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002977 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002978 *
2979 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002980static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002981{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002982 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002983 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002984 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002985 struct rpc_xprt *ret;
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002986 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2987
2988 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2989 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Levera246b012005-08-11 16:25:23 -04002990
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002991 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002992 max_slot_table_size);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002993 if (IS_ERR(xprt))
2994 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002995 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002996
Chuck Leverec739ef2006-08-22 20:06:15 -04002997 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002998 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002999 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04003000
Chuck Lever03bf4b72005-08-25 16:25:55 -07003001 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07003002 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
3003 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04003004
Chuck Lever262965f2005-08-11 16:25:56 -04003005 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05003006 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04003007
Trond Myklebust3851f1c2016-08-04 00:08:45 -04003008 xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
3009
Trond Myklebustedc1b012015-10-05 10:53:49 -04003010 INIT_WORK(&transport->recv_worker, xs_tcp_data_receive_workfn);
3011 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
3012
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003013 switch (addr->sa_family) {
3014 case AF_INET:
3015 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
3016 xprt_set_bound(xprt);
3017
Chuck Lever9dc3b092009-08-09 15:09:46 -04003018 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003019 break;
3020 case AF_INET6:
3021 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
3022 xprt_set_bound(xprt);
3023
Chuck Lever9dc3b092009-08-09 15:09:46 -04003024 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003025 break;
3026 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003027 ret = ERR_PTR(-EAFNOSUPPORT);
3028 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003029 }
3030
Chuck Leverc740eff2009-08-09 15:09:46 -04003031 if (xprt_bound(xprt))
3032 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3033 xprt->address_strings[RPC_DISPLAY_ADDR],
3034 xprt->address_strings[RPC_DISPLAY_PORT],
3035 xprt->address_strings[RPC_DISPLAY_PROTO]);
3036 else
3037 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
3038 xprt->address_strings[RPC_DISPLAY_ADDR],
3039 xprt->address_strings[RPC_DISPLAY_PROTO]);
3040
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003041 if (try_module_get(THIS_MODULE))
3042 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003043 ret = ERR_PTR(-EINVAL);
3044out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003045 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003046 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04003047}
Chuck Lever282b32e2006-12-05 16:35:51 -05003048
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003049/**
3050 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3051 * @args: rpc transport creation arguments
3052 *
3053 */
3054static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
3055{
3056 struct sockaddr *addr = args->dstaddr;
3057 struct rpc_xprt *xprt;
3058 struct sock_xprt *transport;
3059 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003060 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003061
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003062 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3063 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003064 if (IS_ERR(xprt))
3065 return xprt;
3066 transport = container_of(xprt, struct sock_xprt, xprt);
3067
3068 xprt->prot = IPPROTO_TCP;
3069 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
3070 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3071 xprt->timeout = &xs_tcp_default_timeout;
3072
3073 /* backchannel */
3074 xprt_set_bound(xprt);
3075 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003076 xprt->reestablish_timeout = 0;
3077 xprt->idle_timeout = 0;
3078
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003079 xprt->ops = &bc_tcp_ops;
3080
3081 switch (addr->sa_family) {
3082 case AF_INET:
3083 xs_format_peer_addresses(xprt, "tcp",
3084 RPCBIND_NETID_TCP);
3085 break;
3086 case AF_INET6:
3087 xs_format_peer_addresses(xprt, "tcp",
3088 RPCBIND_NETID_TCP6);
3089 break;
3090 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003091 ret = ERR_PTR(-EAFNOSUPPORT);
3092 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003093 }
3094
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04003095 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3096 xprt->address_strings[RPC_DISPLAY_ADDR],
3097 xprt->address_strings[RPC_DISPLAY_PORT],
3098 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003099
3100 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003101 * Once we've associated a backchannel xprt with a connection,
Weng Meiling28303ca2013-11-30 17:56:44 +08003102 * we want to keep it around as long as the connection lasts,
3103 * in case we need to start using it for a backchannel again;
3104 * this reference won't be dropped until bc_xprt is destroyed.
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003105 */
3106 xprt_get(xprt);
3107 args->bc_xprt->xpt_bc_xprt = xprt;
3108 xprt->bc_xprt = args->bc_xprt;
3109 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
3110 transport->sock = bc_sock->sk_sock;
3111 transport->inet = bc_sock->sk_sk;
3112
3113 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003114 * Since we don't want connections for the backchannel, we set
3115 * the xprt status to connected
3116 */
3117 xprt_set_connected(xprt);
3118
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003119 if (try_module_get(THIS_MODULE))
3120 return xprt;
Kinglong Mee642aab52014-03-24 12:00:28 +08003121
3122 args->bc_xprt->xpt_bc_xprt = NULL;
J. Bruce Fields39a9bea2016-05-17 12:38:21 -04003123 args->bc_xprt->xpt_bc_xps = NULL;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003124 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003125 ret = ERR_PTR(-EINVAL);
3126out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003127 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003128 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003129}
3130
Chuck Lever176e21e2011-05-09 15:22:44 -04003131static struct xprt_class xs_local_transport = {
3132 .list = LIST_HEAD_INIT(xs_local_transport.list),
3133 .name = "named UNIX socket",
3134 .owner = THIS_MODULE,
3135 .ident = XPRT_TRANSPORT_LOCAL,
3136 .setup = xs_setup_local,
3137};
3138
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003139static struct xprt_class xs_udp_transport = {
3140 .list = LIST_HEAD_INIT(xs_udp_transport.list),
3141 .name = "udp",
3142 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003143 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003144 .setup = xs_setup_udp,
3145};
3146
3147static struct xprt_class xs_tcp_transport = {
3148 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
3149 .name = "tcp",
3150 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003151 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003152 .setup = xs_setup_tcp,
3153};
3154
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003155static struct xprt_class xs_bc_tcp_transport = {
3156 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3157 .name = "tcp NFSv4.1 backchannel",
3158 .owner = THIS_MODULE,
3159 .ident = XPRT_TRANSPORT_BC_TCP,
3160 .setup = xs_setup_bc_tcp,
3161};
3162
Chuck Lever282b32e2006-12-05 16:35:51 -05003163/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003164 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05003165 *
3166 */
3167int init_socket_xprt(void)
3168{
Jeff Laytonf895b252014-11-17 16:58:04 -05003169#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08003170 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08003171 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05003172#endif
3173
Chuck Lever176e21e2011-05-09 15:22:44 -04003174 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003175 xprt_register_transport(&xs_udp_transport);
3176 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003177 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003178
Chuck Lever282b32e2006-12-05 16:35:51 -05003179 return 0;
3180}
3181
3182/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003183 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05003184 *
3185 */
3186void cleanup_socket_xprt(void)
3187{
Jeff Laytonf895b252014-11-17 16:58:04 -05003188#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Leverfbf76682006-12-05 16:35:54 -05003189 if (sunrpc_table_header) {
3190 unregister_sysctl_table(sunrpc_table_header);
3191 sunrpc_table_header = NULL;
3192 }
3193#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003194
Chuck Lever176e21e2011-05-09 15:22:44 -04003195 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003196 xprt_unregister_transport(&xs_udp_transport);
3197 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003198 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05003199}
Trond Myklebustcbf11072009-08-09 15:06:19 -04003200
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003201static int param_set_uint_minmax(const char *val,
3202 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003203 unsigned int min, unsigned int max)
3204{
Daniel Walter00cfaa92014-06-21 13:06:38 +01003205 unsigned int num;
Trond Myklebustcbf11072009-08-09 15:06:19 -04003206 int ret;
3207
3208 if (!val)
3209 return -EINVAL;
Daniel Walter00cfaa92014-06-21 13:06:38 +01003210 ret = kstrtouint(val, 0, &num);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003211 if (ret == -EINVAL || num < min || num > max)
3212 return -EINVAL;
3213 *((unsigned int *)kp->arg) = num;
3214 return 0;
3215}
3216
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003217static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003218{
Frank Sorensonffb6ca32016-07-08 16:35:25 -05003219 if (kp->arg == &xprt_min_resvport)
3220 return param_set_uint_minmax(val, kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003221 RPC_MIN_RESVPORT,
Frank Sorensonffb6ca32016-07-08 16:35:25 -05003222 xprt_max_resvport);
3223 return param_set_uint_minmax(val, kp,
3224 xprt_min_resvport,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003225 RPC_MAX_RESVPORT);
3226}
3227
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303228static const struct kernel_param_ops param_ops_portnr = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003229 .set = param_set_portnr,
3230 .get = param_get_uint,
3231};
3232
Trond Myklebustcbf11072009-08-09 15:06:19 -04003233#define param_check_portnr(name, p) \
3234 __param_check(name, p, unsigned int);
3235
3236module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3237module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3238
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003239static int param_set_slot_table_size(const char *val,
3240 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003241{
3242 return param_set_uint_minmax(val, kp,
3243 RPC_MIN_SLOT_TABLE,
3244 RPC_MAX_SLOT_TABLE);
3245}
3246
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303247static const struct kernel_param_ops param_ops_slot_table_size = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003248 .set = param_set_slot_table_size,
3249 .get = param_get_uint,
3250};
3251
Trond Myklebustcbf11072009-08-09 15:06:19 -04003252#define param_check_slot_table_size(name, p) \
3253 __param_check(name, p, unsigned int);
3254
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003255static int param_set_max_slot_table_size(const char *val,
3256 const struct kernel_param *kp)
3257{
3258 return param_set_uint_minmax(val, kp,
3259 RPC_MIN_SLOT_TABLE,
3260 RPC_MAX_SLOT_TABLE_LIMIT);
3261}
3262
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303263static const struct kernel_param_ops param_ops_max_slot_table_size = {
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003264 .set = param_set_max_slot_table_size,
3265 .get = param_get_uint,
3266};
3267
3268#define param_check_max_slot_table_size(name, p) \
3269 __param_check(name, p, unsigned int);
3270
Trond Myklebustcbf11072009-08-09 15:06:19 -04003271module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3272 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003273module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3274 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003275module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3276 slot_table_size, 0644);
3277