blob: 9cc850c2719e7da3216f3fdba66b752e99152b76 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Chuck Levera246b012005-08-11 16:25:23 -04002/*
3 * linux/net/sunrpc/xprtsock.c
4 *
5 * Client-side transport implementation for sockets.
6 *
Alan Cox113aa832008-10-13 19:01:08 -07007 * TCP callback races fixes (C) 1998 Red Hat
8 * TCP send fixes (C) 1998 Red Hat
Chuck Levera246b012005-08-11 16:25:23 -04009 * TCP NFS related read + write fixes
10 * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
11 *
12 * Rewrite of larges part of the code in order to stabilize TCP stuff.
13 * Fix behaviour when socket buffer is full.
14 * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
Chuck Lever55aa4f52005-08-11 16:25:47 -040015 *
16 * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
Chuck Lever8f9d5b12007-08-06 11:57:53 -040017 *
18 * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
19 * <gilles.quillard@bull.net>
Chuck Levera246b012005-08-11 16:25:23 -040020 */
21
22#include <linux/types.h>
Chuck Lever176e21e2011-05-09 15:22:44 -040023#include <linux/string.h>
Chuck Levera246b012005-08-11 16:25:23 -040024#include <linux/slab.h>
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -040025#include <linux/module.h>
Chuck Levera246b012005-08-11 16:25:23 -040026#include <linux/capability.h>
Chuck Levera246b012005-08-11 16:25:23 -040027#include <linux/pagemap.h>
28#include <linux/errno.h>
29#include <linux/socket.h>
30#include <linux/in.h>
31#include <linux/net.h>
32#include <linux/mm.h>
Chuck Lever176e21e2011-05-09 15:22:44 -040033#include <linux/un.h>
Chuck Levera246b012005-08-11 16:25:23 -040034#include <linux/udp.h>
35#include <linux/tcp.h>
36#include <linux/sunrpc/clnt.h>
Jeff Layton59766872013-02-04 12:50:00 -050037#include <linux/sunrpc/addr.h>
Chuck Lever02107142006-01-03 09:55:49 +010038#include <linux/sunrpc/sched.h>
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030039#include <linux/sunrpc/svcsock.h>
\"Talpey, Thomas\49c36fc2007-09-10 13:47:31 -040040#include <linux/sunrpc/xprtsock.h>
Chuck Levera246b012005-08-11 16:25:23 -040041#include <linux/file.h>
Trond Myklebust9e00abc2011-07-13 19:20:49 -040042#ifdef CONFIG_SUNRPC_BACKCHANNEL
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -040043#include <linux/sunrpc/bc_xprt.h>
44#endif
Chuck Levera246b012005-08-11 16:25:23 -040045
46#include <net/sock.h>
47#include <net/checksum.h>
48#include <net/udp.h>
49#include <net/tcp.h>
50
Trond Myklebust40b5ea02013-09-04 12:16:23 -040051#include <trace/events/sunrpc.h>
52
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030053#include "sunrpc.h"
Chuck Lever176e21e2011-05-09 15:22:44 -040054
55static void xs_close(struct rpc_xprt *xprt);
Trond Myklebust7196dbb2017-02-08 11:17:54 -050056static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
57 struct socket *sock);
Chuck Lever176e21e2011-05-09 15:22:44 -040058
Chuck Lever9903cd12005-08-11 16:25:26 -040059/*
Chuck Leverc556b752005-11-01 12:24:48 -050060 * xprtsock tunables
61 */
Trond Myklebust09acfea2012-03-11 15:22:54 -040062static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
63static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
64static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
Chuck Leverc556b752005-11-01 12:24:48 -050065
Trond Myklebust09acfea2012-03-11 15:22:54 -040066static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
67static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
Chuck Leverc556b752005-11-01 12:24:48 -050068
Trond Myklebust54c09872015-02-09 11:01:02 -050069#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
70
Trond Myklebust7d1e8252009-03-11 14:38:03 -040071#define XS_TCP_LINGER_TO (15U * HZ)
Trond Myklebust25fe6142009-03-11 14:38:03 -040072static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
Trond Myklebust7d1e8252009-03-11 14:38:03 -040073
Chuck Leverc556b752005-11-01 12:24:48 -050074/*
Chuck Leverfbf76682006-12-05 16:35:54 -050075 * We can register our own files under /proc/sys/sunrpc by
76 * calling register_sysctl_table() again. The files in that
77 * directory become the union of all files registered there.
78 *
79 * We simply need to make sure that we don't collide with
80 * someone else's file names!
81 */
82
Chuck Leverfbf76682006-12-05 16:35:54 -050083static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
84static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
Trond Myklebustd9ba1312011-07-17 18:11:30 -040085static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Leverfbf76682006-12-05 16:35:54 -050086static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
87static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
88
89static struct ctl_table_header *sunrpc_table_header;
90
91/*
92 * FIXME: changing the UDP slot table size should also resize the UDP
93 * socket buffers for existing UDP transports
94 */
Joe Perchesfe2c6332013-06-11 23:04:25 -070095static struct ctl_table xs_tunables_table[] = {
Chuck Leverfbf76682006-12-05 16:35:54 -050096 {
Chuck Leverfbf76682006-12-05 16:35:54 -050097 .procname = "udp_slot_table_entries",
98 .data = &xprt_udp_slot_table_entries,
99 .maxlen = sizeof(unsigned int),
100 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800101 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500102 .extra1 = &min_slot_table_size,
103 .extra2 = &max_slot_table_size
104 },
105 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500106 .procname = "tcp_slot_table_entries",
107 .data = &xprt_tcp_slot_table_entries,
108 .maxlen = sizeof(unsigned int),
109 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800110 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500111 .extra1 = &min_slot_table_size,
112 .extra2 = &max_slot_table_size
113 },
114 {
Trond Myklebustd9ba1312011-07-17 18:11:30 -0400115 .procname = "tcp_max_slot_table_entries",
116 .data = &xprt_max_tcp_slot_table_entries,
117 .maxlen = sizeof(unsigned int),
118 .mode = 0644,
119 .proc_handler = proc_dointvec_minmax,
120 .extra1 = &min_slot_table_size,
121 .extra2 = &max_tcp_slot_table_limit
122 },
123 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500124 .procname = "min_resvport",
125 .data = &xprt_min_resvport,
126 .maxlen = sizeof(unsigned int),
127 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800128 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500129 .extra1 = &xprt_min_resvport_limit,
Frank Sorensone08ea3a2016-07-08 16:35:24 -0500130 .extra2 = &xprt_max_resvport
Chuck Leverfbf76682006-12-05 16:35:54 -0500131 },
132 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500133 .procname = "max_resvport",
134 .data = &xprt_max_resvport,
135 .maxlen = sizeof(unsigned int),
136 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800137 .proc_handler = proc_dointvec_minmax,
Frank Sorensone08ea3a2016-07-08 16:35:24 -0500138 .extra1 = &xprt_min_resvport,
Chuck Leverfbf76682006-12-05 16:35:54 -0500139 .extra2 = &xprt_max_resvport_limit
140 },
141 {
Trond Myklebust25fe6142009-03-11 14:38:03 -0400142 .procname = "tcp_fin_timeout",
143 .data = &xs_tcp_fin_timeout,
144 .maxlen = sizeof(xs_tcp_fin_timeout),
145 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800146 .proc_handler = proc_dointvec_jiffies,
Trond Myklebust25fe6142009-03-11 14:38:03 -0400147 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800148 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500149};
150
Joe Perchesfe2c6332013-06-11 23:04:25 -0700151static struct ctl_table sunrpc_table[] = {
Chuck Leverfbf76682006-12-05 16:35:54 -0500152 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500153 .procname = "sunrpc",
154 .mode = 0555,
155 .child = xs_tunables_table
156 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800157 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500158};
159
160#endif
161
162/*
Chuck Lever03bf4b72005-08-25 16:25:55 -0700163 * Wait duration for a reply from the RPC portmapper.
164 */
165#define XS_BIND_TO (60U * HZ)
166
167/*
168 * Delay if a UDP socket connect error occurs. This is most likely some
169 * kind of resource problem on the local host.
170 */
171#define XS_UDP_REEST_TO (2U * HZ)
172
173/*
174 * The reestablish timeout allows clients to delay for a bit before attempting
175 * to reconnect to a server that just dropped our connection.
176 *
177 * We implement an exponential backoff when trying to reestablish a TCP
178 * transport connection with the server. Some servers like to drop a TCP
179 * connection when they are overworked, so we start with a short timeout and
180 * increase over time if the server is down or not responding.
181 */
182#define XS_TCP_INIT_REEST_TO (3U * HZ)
Chuck Lever03bf4b72005-08-25 16:25:55 -0700183
184/*
185 * TCP idle timeout; client drops the transport socket if it is idle
186 * for this long. Note that we also timeout UDP sockets to prevent
187 * holding port numbers when there is no RPC traffic.
188 */
189#define XS_IDLE_DISC_TO (5U * 60 * HZ)
190
Jeff Laytonf895b252014-11-17 16:58:04 -0500191#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Levera246b012005-08-11 16:25:23 -0400192# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400193# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400194#endif
195
Chuck Levera246b012005-08-11 16:25:23 -0400196#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400197static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400198{
Chuck Lever9903cd12005-08-11 16:25:26 -0400199 u8 *buf = (u8 *) packet;
200 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400201
Chuck Lever46121cf2007-01-31 12:14:08 -0500202 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400203 for (j = 0; j < count && j < 128; j += 4) {
204 if (!(j & 31)) {
205 if (j)
206 dprintk("\n");
207 dprintk("0x%04x ", j);
208 }
209 dprintk("%02x%02x%02x%02x ",
210 buf[j], buf[j+1], buf[j+2], buf[j+3]);
211 }
212 dprintk("\n");
213}
214#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400215static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400216{
217 /* NOP */
218}
219#endif
220
Trond Myklebust21180712013-12-31 13:22:59 -0500221static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
222{
223 return (struct rpc_xprt *) sk->sk_user_data;
224}
225
Chuck Lever95392c52007-08-06 11:57:58 -0400226static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400227{
Chuck Lever95392c52007-08-06 11:57:58 -0400228 return (struct sockaddr *) &xprt->addr;
229}
230
Chuck Lever176e21e2011-05-09 15:22:44 -0400231static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
232{
233 return (struct sockaddr_un *) &xprt->addr;
234}
235
Chuck Lever95392c52007-08-06 11:57:58 -0400236static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
237{
238 return (struct sockaddr_in *) &xprt->addr;
239}
240
241static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
242{
243 return (struct sockaddr_in6 *) &xprt->addr;
244}
245
Chuck Leverc877b842009-08-09 15:09:36 -0400246static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400247{
Chuck Leverc877b842009-08-09 15:09:36 -0400248 struct sockaddr *sap = xs_addr(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400249 struct sockaddr_in6 *sin6;
250 struct sockaddr_in *sin;
Chuck Lever176e21e2011-05-09 15:22:44 -0400251 struct sockaddr_un *sun;
Chuck Leverc877b842009-08-09 15:09:36 -0400252 char buf[128];
Chuck Leveredb267a2006-08-22 20:06:18 -0400253
Chuck Lever9dc3b092009-08-09 15:09:46 -0400254 switch (sap->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400255 case AF_LOCAL:
256 sun = xs_addr_un(xprt);
257 strlcpy(buf, sun->sun_path, sizeof(buf));
258 xprt->address_strings[RPC_DISPLAY_ADDR] =
259 kstrdup(buf, GFP_KERNEL);
260 break;
Chuck Lever9dc3b092009-08-09 15:09:46 -0400261 case AF_INET:
Chuck Lever176e21e2011-05-09 15:22:44 -0400262 (void)rpc_ntop(sap, buf, sizeof(buf));
263 xprt->address_strings[RPC_DISPLAY_ADDR] =
264 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400265 sin = xs_addr_in(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800266 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
Chuck Lever9dc3b092009-08-09 15:09:46 -0400267 break;
268 case AF_INET6:
Chuck Lever176e21e2011-05-09 15:22:44 -0400269 (void)rpc_ntop(sap, buf, sizeof(buf));
270 xprt->address_strings[RPC_DISPLAY_ADDR] =
271 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400272 sin6 = xs_addr_in6(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800273 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400274 break;
275 default:
276 BUG();
Chuck Leveredb267a2006-08-22 20:06:18 -0400277 }
Chuck Lever176e21e2011-05-09 15:22:44 -0400278
Chuck Lever9dc3b092009-08-09 15:09:46 -0400279 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
Chuck Leveredb267a2006-08-22 20:06:18 -0400280}
281
Chuck Lever9dc3b092009-08-09 15:09:46 -0400282static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400283{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400284 struct sockaddr *sap = xs_addr(xprt);
285 char buf[128];
Chuck Lever4b6473f2007-08-06 11:57:12 -0400286
Joe Perches81160e662010-03-08 12:15:59 -0800287 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400288 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400289
Joe Perches81160e662010-03-08 12:15:59 -0800290 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400291 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
292}
Chuck Lever4b6473f2007-08-06 11:57:12 -0400293
Chuck Lever9dc3b092009-08-09 15:09:46 -0400294static void xs_format_peer_addresses(struct rpc_xprt *xprt,
295 const char *protocol,
296 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400297{
Chuck Leverb454ae92008-01-07 18:34:48 -0500298 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leverb454ae92008-01-07 18:34:48 -0500299 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leverc877b842009-08-09 15:09:36 -0400300 xs_format_common_peer_addresses(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400301 xs_format_common_peer_ports(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400302}
303
Chuck Lever9dc3b092009-08-09 15:09:46 -0400304static void xs_update_peer_port(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400305{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400306 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
307 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400308
Chuck Lever9dc3b092009-08-09 15:09:46 -0400309 xs_format_common_peer_ports(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400310}
311
312static void xs_free_peer_addresses(struct rpc_xprt *xprt)
313{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500314 unsigned int i;
315
316 for (i = 0; i < RPC_DISPLAY_MAX; i++)
317 switch (i) {
318 case RPC_DISPLAY_PROTO:
319 case RPC_DISPLAY_NETID:
320 continue;
321 default:
322 kfree(xprt->address_strings[i]);
323 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400324}
325
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400326#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
327
Trond Myklebust24c56842006-10-17 15:06:22 -0400328static 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 -0400329{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400330 struct msghdr msg = {
331 .msg_name = addr,
332 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400333 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
334 };
335 struct kvec iov = {
336 .iov_base = vec->iov_base + base,
337 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400338 };
339
Trond Myklebust24c56842006-10-17 15:06:22 -0400340 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400341 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
342 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
343}
344
Jason Baronf279cd02014-09-24 18:08:00 +0000345static 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 -0400346{
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500347 ssize_t (*do_sendpage)(struct socket *sock, struct page *page,
348 int offset, size_t size, int flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400349 struct page **ppage;
350 unsigned int remainder;
Jason Baronf279cd02014-09-24 18:08:00 +0000351 int err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400352
Trond Myklebust24c56842006-10-17 15:06:22 -0400353 remainder = xdr->page_len - base;
354 base += xdr->page_base;
355 ppage = xdr->pages + (base >> PAGE_SHIFT);
356 base &= ~PAGE_MASK;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500357 do_sendpage = sock->ops->sendpage;
358 if (!zerocopy)
359 do_sendpage = sock_no_sendpage;
Trond Myklebust24c56842006-10-17 15:06:22 -0400360 for(;;) {
361 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
362 int flags = XS_SENDMSG_FLAGS;
363
364 remainder -= len;
Trond Myklebust31303d62015-10-06 15:59:20 -0400365 if (more)
Trond Myklebust24c56842006-10-17 15:06:22 -0400366 flags |= MSG_MORE;
Trond Myklebust31303d62015-10-06 15:59:20 -0400367 if (remainder != 0)
368 flags |= MSG_SENDPAGE_NOTLAST | MSG_MORE;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500369 err = do_sendpage(sock, *ppage, base, len, flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400370 if (remainder == 0 || err != len)
371 break;
Jason Baronf279cd02014-09-24 18:08:00 +0000372 *sent_p += err;
Trond Myklebust24c56842006-10-17 15:06:22 -0400373 ppage++;
374 base = 0;
375 }
Jason Baronf279cd02014-09-24 18:08:00 +0000376 if (err > 0) {
377 *sent_p += err;
378 err = 0;
379 }
380 return err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400381}
382
Chuck Lever9903cd12005-08-11 16:25:26 -0400383/**
384 * xs_sendpages - write pages directly to a socket
385 * @sock: socket to send on
386 * @addr: UDP only -- address of destination
387 * @addrlen: UDP only -- length of destination address
388 * @xdr: buffer containing this request
389 * @base: starting position in the buffer
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500390 * @zerocopy: true if it is safe to use sendpage()
Jason Baronf279cd02014-09-24 18:08:00 +0000391 * @sent_p: return the total number of bytes successfully queued for sending
Chuck Lever9903cd12005-08-11 16:25:26 -0400392 *
Chuck Levera246b012005-08-11 16:25:23 -0400393 */
Jason Baronf279cd02014-09-24 18:08:00 +0000394static 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 -0400395{
Trond Myklebust24c56842006-10-17 15:06:22 -0400396 unsigned int remainder = xdr->len - base;
Jason Baronf279cd02014-09-24 18:08:00 +0000397 int err = 0;
398 int sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400399
Chuck Lever262965f2005-08-11 16:25:56 -0400400 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400401 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400402
Trond Myklebust24c56842006-10-17 15:06:22 -0400403 if (base != 0) {
404 addr = NULL;
405 addrlen = 0;
406 }
Chuck Lever262965f2005-08-11 16:25:56 -0400407
Trond Myklebust24c56842006-10-17 15:06:22 -0400408 if (base < xdr->head[0].iov_len || addr != NULL) {
409 unsigned int len = xdr->head[0].iov_len - base;
410 remainder -= len;
411 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
412 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400413 goto out;
Jason Baronf279cd02014-09-24 18:08:00 +0000414 *sent_p += err;
Chuck Levera246b012005-08-11 16:25:23 -0400415 base = 0;
416 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400417 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400418
Trond Myklebust24c56842006-10-17 15:06:22 -0400419 if (base < xdr->page_len) {
420 unsigned int len = xdr->page_len - base;
421 remainder -= len;
Jason Baronf279cd02014-09-24 18:08:00 +0000422 err = xs_send_pagedata(sock, xdr, base, remainder != 0, zerocopy, &sent);
423 *sent_p += sent;
424 if (remainder == 0 || sent != len)
Chuck Levera246b012005-08-11 16:25:23 -0400425 goto out;
426 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400427 } else
428 base -= xdr->page_len;
429
430 if (base >= xdr->tail[0].iov_len)
Jason Baronf279cd02014-09-24 18:08:00 +0000431 return 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400432 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400433out:
Jason Baronf279cd02014-09-24 18:08:00 +0000434 if (err > 0) {
435 *sent_p += err;
436 err = 0;
437 }
438 return err;
Chuck Levera246b012005-08-11 16:25:23 -0400439}
440
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400441static void xs_nospace_callback(struct rpc_task *task)
442{
443 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
444
445 transport->inet->sk_write_pending--;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400446}
447
Chuck Lever9903cd12005-08-11 16:25:26 -0400448/**
Chuck Lever262965f2005-08-11 16:25:56 -0400449 * xs_nospace - place task on wait queue if transmit was incomplete
450 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400451 *
Chuck Levera246b012005-08-11 16:25:23 -0400452 */
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400453static int xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400454{
Chuck Lever262965f2005-08-11 16:25:56 -0400455 struct rpc_rqst *req = task->tk_rqstp;
456 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500457 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500458 struct sock *sk = transport->inet;
Trond Myklebust24ca9a82011-11-22 14:44:28 +0200459 int ret = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400460
Chuck Lever46121cf2007-01-31 12:14:08 -0500461 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400462 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
463 req->rq_slen);
464
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400465 /* Protect against races with write_space */
466 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400467
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400468 /* Don't race with disconnect */
469 if (xprt_connected(xprt)) {
Trond Myklebust13331a52016-01-06 08:57:06 -0500470 /* wait for more buffer space */
471 sk->sk_write_pending++;
472 xprt_wait_for_buffer_space(task, xs_nospace_callback);
473 } else
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400474 ret = -ENOTCONN;
Chuck Lever262965f2005-08-11 16:25:56 -0400475
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400476 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500477
478 /* Race breaker in case memory is freed before above code is called */
David Vrabeld48f9ce2016-09-19 13:58:30 +0100479 if (ret == -EAGAIN) {
480 struct socket_wq *wq;
481
482 rcu_read_lock();
483 wq = rcu_dereference(sk->sk_wq);
484 set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags);
485 rcu_read_unlock();
486
487 sk->sk_write_space(sk);
488 }
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400489 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400490}
491
Chuck Lever61677ee2011-05-09 15:22:34 -0400492/*
493 * Construct a stream transport record marker in @buf.
494 */
495static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
496{
497 u32 reclen = buf->len - sizeof(rpc_fraghdr);
498 rpc_fraghdr *base = buf->head[0].iov_base;
499 *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
500}
501
Chuck Lever262965f2005-08-11 16:25:56 -0400502/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400503 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
504 * @task: RPC task that manages the state of an RPC request
505 *
506 * Return values:
507 * 0: The request has been sent
508 * EAGAIN: The socket was blocked, please call again later to
509 * complete the request
510 * ENOTCONN: Caller needs to invoke connect logic then call again
511 * other: Some other error occured, the request was not sent
512 */
513static int xs_local_send_request(struct rpc_task *task)
514{
515 struct rpc_rqst *req = task->tk_rqstp;
516 struct rpc_xprt *xprt = req->rq_xprt;
517 struct sock_xprt *transport =
518 container_of(xprt, struct sock_xprt, xprt);
519 struct xdr_buf *xdr = &req->rq_snd_buf;
520 int status;
Jason Baronf279cd02014-09-24 18:08:00 +0000521 int sent = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -0400522
523 xs_encode_stream_record_marker(&req->rq_snd_buf);
524
525 xs_pktdump("packet data:",
526 req->rq_svec->iov_base, req->rq_svec->iov_len);
527
Jason Baronf279cd02014-09-24 18:08:00 +0000528 status = xs_sendpages(transport->sock, NULL, 0, xdr, req->rq_bytes_sent,
529 true, &sent);
Chuck Lever176e21e2011-05-09 15:22:44 -0400530 dprintk("RPC: %s(%u) = %d\n",
531 __func__, xdr->len - req->rq_bytes_sent, status);
NeilBrown743c69e2015-07-27 10:55:35 +1000532
533 if (status == -EAGAIN && sock_writeable(transport->inet))
534 status = -ENOBUFS;
535
Jason Baronf279cd02014-09-24 18:08:00 +0000536 if (likely(sent > 0) || status == 0) {
537 req->rq_bytes_sent += sent;
538 req->rq_xmit_bytes_sent += sent;
Chuck Lever176e21e2011-05-09 15:22:44 -0400539 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
540 req->rq_bytes_sent = 0;
541 return 0;
542 }
543 status = -EAGAIN;
544 }
545
546 switch (status) {
Trond Myklebust3601c4a2014-06-30 13:42:19 -0400547 case -ENOBUFS:
Trond Myklebustb5872f02015-07-03 09:32:23 -0400548 break;
Chuck Lever176e21e2011-05-09 15:22:44 -0400549 case -EAGAIN:
550 status = xs_nospace(task);
551 break;
552 default:
553 dprintk("RPC: sendmsg returned unrecognized error %d\n",
554 -status);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -0500555 /* fall through */
Chuck Lever176e21e2011-05-09 15:22:44 -0400556 case -EPIPE:
557 xs_close(xprt);
558 status = -ENOTCONN;
559 }
560
561 return status;
562}
563
564/**
Chuck Lever262965f2005-08-11 16:25:56 -0400565 * xs_udp_send_request - write an RPC request to a UDP socket
566 * @task: address of RPC task that manages the state of an RPC request
567 *
568 * Return values:
569 * 0: The request has been sent
570 * EAGAIN: The socket was blocked, please call again later to
571 * complete the request
572 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300573 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -0400574 */
575static int xs_udp_send_request(struct rpc_task *task)
576{
577 struct rpc_rqst *req = task->tk_rqstp;
578 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500579 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400580 struct xdr_buf *xdr = &req->rq_snd_buf;
Jason Baronf279cd02014-09-24 18:08:00 +0000581 int sent = 0;
Chuck Lever262965f2005-08-11 16:25:56 -0400582 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400583
Chuck Lever9903cd12005-08-11 16:25:26 -0400584 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400585 req->rq_svec->iov_base,
586 req->rq_svec->iov_len);
587
Trond Myklebust01d37c42009-03-11 14:09:39 -0400588 if (!xprt_bound(xprt))
589 return -ENOTCONN;
Jason Baronf279cd02014-09-24 18:08:00 +0000590 status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen,
591 xdr, req->rq_bytes_sent, true, &sent);
Chuck Levera246b012005-08-11 16:25:23 -0400592
Chuck Lever46121cf2007-01-31 12:14:08 -0500593 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400594 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400595
Jason Baron3dedbb52014-09-24 18:08:04 +0000596 /* firewall is blocking us, don't return -EAGAIN or we end up looping */
597 if (status == -EPERM)
598 goto process_status;
599
NeilBrown743c69e2015-07-27 10:55:35 +1000600 if (status == -EAGAIN && sock_writeable(transport->inet))
601 status = -ENOBUFS;
602
Jason Baronf279cd02014-09-24 18:08:00 +0000603 if (sent > 0 || status == 0) {
604 req->rq_xmit_bytes_sent += sent;
605 if (sent >= req->rq_slen)
Trond Myklebust21997002007-10-01 11:43:37 -0400606 return 0;
607 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400608 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400609 }
Chuck Levera246b012005-08-11 16:25:23 -0400610
Jason Baron3dedbb52014-09-24 18:08:04 +0000611process_status:
Chuck Lever262965f2005-08-11 16:25:56 -0400612 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400613 case -ENOTSOCK:
614 status = -ENOTCONN;
615 /* Should we call xs_close() here? */
616 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400617 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400618 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400619 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400620 case -ENETUNREACH:
Trond Myklebust3601c4a2014-06-30 13:42:19 -0400621 case -ENOBUFS:
Chuck Lever262965f2005-08-11 16:25:56 -0400622 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400623 case -ECONNREFUSED:
Jason Baron3dedbb52014-09-24 18:08:04 +0000624 case -EPERM:
Chuck Levera246b012005-08-11 16:25:23 -0400625 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400626 * prompts ECONNREFUSED. */
Trond Myklebust13331a52016-01-06 08:57:06 -0500627 break;
628 default:
629 dprintk("RPC: sendmsg returned unrecognized error %d\n",
630 -status);
Chuck Levera246b012005-08-11 16:25:23 -0400631 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800632
Chuck Lever262965f2005-08-11 16:25:56 -0400633 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400634}
635
Trond Myklebuste06799f2007-11-05 15:44:12 -0500636/**
Chuck Lever262965f2005-08-11 16:25:56 -0400637 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400638 * @task: address of RPC task that manages the state of an RPC request
639 *
640 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400641 * 0: The request has been sent
642 * EAGAIN: The socket was blocked, please call again later to
643 * complete the request
644 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300645 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400646 *
647 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400648 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400649 */
Chuck Lever262965f2005-08-11 16:25:56 -0400650static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400651{
652 struct rpc_rqst *req = task->tk_rqstp;
653 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500654 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400655 struct xdr_buf *xdr = &req->rq_snd_buf;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500656 bool zerocopy = true;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -0400657 bool vm_wait = false;
Chuck Leverb595bb12007-08-06 11:56:42 -0400658 int status;
Jason Baronf279cd02014-09-24 18:08:00 +0000659 int sent;
Chuck Levera246b012005-08-11 16:25:23 -0400660
Chuck Lever61677ee2011-05-09 15:22:34 -0400661 xs_encode_stream_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400662
Chuck Lever262965f2005-08-11 16:25:56 -0400663 xs_pktdump("packet data:",
664 req->rq_svec->iov_base,
665 req->rq_svec->iov_len);
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500666 /* Don't use zero copy if this is a resend. If the RPC call
667 * completes while the socket holds a reference to the pages,
668 * then we may end up resending corrupted data.
669 */
670 if (task->tk_flags & RPC_TASK_SENT)
671 zerocopy = false;
Chuck Levera246b012005-08-11 16:25:23 -0400672
Trond Myklebust7196dbb2017-02-08 11:17:54 -0500673 if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
674 xs_tcp_set_socket_timeouts(xprt, transport->sock);
675
Chuck Levera246b012005-08-11 16:25:23 -0400676 /* Continue transmitting the packet/record. We must be careful
677 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400678 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400679 while (1) {
Jason Baronf279cd02014-09-24 18:08:00 +0000680 sent = 0;
681 status = xs_sendpages(transport->sock, NULL, 0, xdr,
682 req->rq_bytes_sent, zerocopy, &sent);
Chuck Levera246b012005-08-11 16:25:23 -0400683
Chuck Lever46121cf2007-01-31 12:14:08 -0500684 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400685 xdr->len - req->rq_bytes_sent, status);
686
Chuck Lever262965f2005-08-11 16:25:56 -0400687 /* If we've sent the entire packet, immediately
688 * reset the count of bytes sent. */
Jason Baronf279cd02014-09-24 18:08:00 +0000689 req->rq_bytes_sent += sent;
690 req->rq_xmit_bytes_sent += sent;
Chuck Lever262965f2005-08-11 16:25:56 -0400691 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
692 req->rq_bytes_sent = 0;
693 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400694 }
695
Trond Myklebust9ffadfb2016-05-29 00:42:03 -0400696 WARN_ON_ONCE(sent == 0 && status == 0);
697
698 if (status == -EAGAIN ) {
699 /*
700 * Return EAGAIN if we're sure we're hitting the
701 * socket send buffer limits.
702 */
703 if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
704 break;
705 /*
706 * Did we hit a memory allocation failure?
707 */
708 if (sent == 0) {
709 status = -ENOBUFS;
710 if (vm_wait)
711 break;
712 /* Retry, knowing now that we're below the
713 * socket send buffer limit
714 */
715 vm_wait = true;
716 }
717 continue;
718 }
Trond Myklebustf580dd02015-07-11 17:48:52 +0200719 if (status < 0)
720 break;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -0400721 vm_wait = false;
Chuck Levera246b012005-08-11 16:25:23 -0400722 }
723
Chuck Lever262965f2005-08-11 16:25:56 -0400724 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400725 case -ENOTSOCK:
726 status = -ENOTCONN;
727 /* Should we call xs_close() here? */
728 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400729 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400730 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400731 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400732 case -ECONNRESET:
Chuck Lever262965f2005-08-11 16:25:56 -0400733 case -ECONNREFUSED:
734 case -ENOTCONN:
Trond Myklebust3913c782015-02-08 21:44:04 -0500735 case -EADDRINUSE:
Trond Myklebustb5872f02015-07-03 09:32:23 -0400736 case -ENOBUFS:
Trond Myklebustb9d2bb22012-10-23 11:40:02 -0400737 case -EPIPE:
Trond Myklebust13331a52016-01-06 08:57:06 -0500738 break;
739 default:
740 dprintk("RPC: sendmsg returned unrecognized error %d\n",
741 -status);
Chuck Levera246b012005-08-11 16:25:23 -0400742 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800743
Chuck Levera246b012005-08-11 16:25:23 -0400744 return status;
745}
746
Chuck Lever9903cd12005-08-11 16:25:26 -0400747/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400748 * xs_tcp_release_xprt - clean up after a tcp transmission
749 * @xprt: transport
750 * @task: rpc task
751 *
752 * This cleans up if an error causes us to abort the transmission of a request.
753 * In this case, the socket may need to be reset in order to avoid confusing
754 * the server.
755 */
756static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
757{
758 struct rpc_rqst *req;
759
760 if (task != xprt->snd_task)
761 return;
762 if (task == NULL)
763 goto out_release;
764 req = task->tk_rqstp;
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400765 if (req == NULL)
766 goto out_release;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400767 if (req->rq_bytes_sent == 0)
768 goto out_release;
769 if (req->rq_bytes_sent == req->rq_snd_buf.len)
770 goto out_release;
Trond Myklebusta4f08352013-01-08 09:10:21 -0500771 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400772out_release:
773 xprt_release_xprt(xprt, task);
774}
775
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400776static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
777{
778 transport->old_data_ready = sk->sk_data_ready;
779 transport->old_state_change = sk->sk_state_change;
780 transport->old_write_space = sk->sk_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -0500781 transport->old_error_report = sk->sk_error_report;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400782}
783
784static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
785{
786 sk->sk_data_ready = transport->old_data_ready;
787 sk->sk_state_change = transport->old_state_change;
788 sk->sk_write_space = transport->old_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -0500789 sk->sk_error_report = transport->old_error_report;
790}
791
Trond Myklebust42d42a52016-05-23 09:24:55 -0400792static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
793{
794 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
795
796 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
797}
798
Trond Myklebustb70ae912015-02-09 09:41:32 -0500799static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
800{
801 smp_mb__before_atomic();
802 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
803 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust42d42a52016-05-23 09:24:55 -0400804 xs_sock_reset_state_flags(xprt);
Trond Myklebustb70ae912015-02-09 09:41:32 -0500805 smp_mb__after_atomic();
806}
807
808static void xs_sock_mark_closed(struct rpc_xprt *xprt)
809{
810 xs_sock_reset_connection_flags(xprt);
811 /* Mark transport as closed and wake up all pending tasks */
812 xprt_disconnect_done(xprt);
813}
814
Trond Myklebust21180712013-12-31 13:22:59 -0500815/**
816 * xs_error_report - callback to handle TCP socket state errors
817 * @sk: socket
818 *
819 * Note: we don't call sock_error() since there may be a rpc_task
820 * using the socket, and so we don't want to clear sk->sk_err.
821 */
822static void xs_error_report(struct sock *sk)
823{
824 struct rpc_xprt *xprt;
825 int err;
826
827 read_lock_bh(&sk->sk_callback_lock);
828 if (!(xprt = xprt_from_sock(sk)))
829 goto out;
830
831 err = -sk->sk_err;
832 if (err == 0)
833 goto out;
Trond Myklebustb70ae912015-02-09 09:41:32 -0500834 /* Is this a reset event? */
835 if (sk->sk_state == TCP_CLOSE)
836 xs_sock_mark_closed(xprt);
Trond Myklebust21180712013-12-31 13:22:59 -0500837 dprintk("RPC: xs_error_report client %p, error=%d...\n",
838 xprt, -err);
Trond Myklebuste8353c762013-12-31 13:39:22 -0500839 trace_rpc_socket_error(xprt, sk->sk_socket, err);
Trond Myklebust21180712013-12-31 13:22:59 -0500840 xprt_wake_pending_tasks(xprt, err);
841 out:
842 read_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400843}
844
Chuck Leverfe315e72009-03-11 14:10:21 -0400845static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400846{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500847 struct socket *sock = transport->sock;
848 struct sock *sk = transport->inet;
Trond Myklebust6cc7e902015-02-08 18:35:25 -0500849 struct rpc_xprt *xprt = &transport->xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400850
Chuck Leverfe315e72009-03-11 14:10:21 -0400851 if (sk == NULL)
852 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400853
Jeff Layton264d1df2015-06-03 16:14:27 -0400854 if (atomic_read(&transport->xprt.swapper))
855 sk_clear_memalloc(sk);
856
Trond Myklebust09939202015-08-29 19:11:21 -0700857 kernel_sock_shutdown(sock, SHUT_RDWR);
858
Trond Myklebustedc1b012015-10-05 10:53:49 -0400859 mutex_lock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -0400860 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500861 transport->inet = NULL;
862 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400863
Chuck Lever9903cd12005-08-11 16:25:26 -0400864 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400865
866 xs_restore_old_callbacks(transport, sk);
Trond Myklebust0c787892015-08-29 13:36:30 -0700867 xprt_clear_connected(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400868 write_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust6cc7e902015-02-08 18:35:25 -0500869 xs_sock_reset_connection_flags(xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -0400870 mutex_unlock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -0400871
Trond Myklebust6cc7e902015-02-08 18:35:25 -0500872 trace_rpc_socket_close(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -0400873 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -0400874}
875
876/**
877 * xs_close - close a socket
878 * @xprt: transport
879 *
880 * This is used when all requests are complete; ie, no DRC state remains
881 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400882 *
883 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
884 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e72009-03-11 14:10:21 -0400885 */
886static void xs_close(struct rpc_xprt *xprt)
887{
888 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
889
890 dprintk("RPC: xs_close xprt %p\n", xprt);
891
892 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -0400893 xprt->reestablish_timeout = 0;
Chuck Leverfe315e72009-03-11 14:10:21 -0400894
Trond Myklebust62da3b22007-11-06 18:44:20 -0500895 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400896}
897
Chuck Lever4a068252015-05-11 14:02:25 -0400898static void xs_inject_disconnect(struct rpc_xprt *xprt)
899{
900 dprintk("RPC: injecting transport disconnect on xprt=%p\n",
901 xprt);
902 xprt_disconnect_done(xprt);
903}
904
Kinglong Mee315f3812014-03-24 11:07:22 +0800905static void xs_xprt_free(struct rpc_xprt *xprt)
906{
907 xs_free_peer_addresses(xprt);
908 xprt_free(xprt);
909}
910
Chuck Lever9903cd12005-08-11 16:25:26 -0400911/**
912 * xs_destroy - prepare to shutdown a transport
913 * @xprt: doomed transport
914 *
915 */
916static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400917{
Trond Myklebust03c788272015-09-17 10:42:27 -0400918 struct sock_xprt *transport = container_of(xprt,
919 struct sock_xprt, xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -0500920 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400921
Trond Myklebust03c788272015-09-17 10:42:27 -0400922 cancel_delayed_work_sync(&transport->connect_worker);
Trond Myklebusta1311d82013-10-31 09:18:49 -0400923 xs_close(xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -0400924 cancel_work_sync(&transport->recv_worker);
Kinglong Mee315f3812014-03-24 11:07:22 +0800925 xs_xprt_free(xprt);
Trond Myklebusta1311d82013-10-31 09:18:49 -0400926 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400927}
928
Chuck Lever176e21e2011-05-09 15:22:44 -0400929static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
930{
931 struct xdr_skb_reader desc = {
932 .skb = skb,
933 .offset = sizeof(rpc_fraghdr),
934 .count = skb->len - sizeof(rpc_fraghdr),
935 };
936
937 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
938 return -1;
939 if (desc.count)
940 return -1;
941 return 0;
942}
943
944/**
Trond Myklebusta2648092015-10-06 17:03:00 -0400945 * xs_local_data_read_skb
946 * @xprt: transport
947 * @sk: socket
948 * @skb: skbuff
Chuck Lever176e21e2011-05-09 15:22:44 -0400949 *
950 * Currently this assumes we can read the whole reply in a single gulp.
951 */
Trond Myklebusta2648092015-10-06 17:03:00 -0400952static void xs_local_data_read_skb(struct rpc_xprt *xprt,
953 struct sock *sk,
954 struct sk_buff *skb)
Chuck Lever176e21e2011-05-09 15:22:44 -0400955{
956 struct rpc_task *task;
Chuck Lever176e21e2011-05-09 15:22:44 -0400957 struct rpc_rqst *rovr;
Trond Myklebusta2648092015-10-06 17:03:00 -0400958 int repsize, copied;
Chuck Lever176e21e2011-05-09 15:22:44 -0400959 u32 _xid;
960 __be32 *xp;
961
Chuck Lever176e21e2011-05-09 15:22:44 -0400962 repsize = skb->len - sizeof(rpc_fraghdr);
963 if (repsize < 4) {
964 dprintk("RPC: impossible RPC reply size %d\n", repsize);
Trond Myklebusta2648092015-10-06 17:03:00 -0400965 return;
Chuck Lever176e21e2011-05-09 15:22:44 -0400966 }
967
968 /* Copy the XID from the skb... */
969 xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
970 if (xp == NULL)
Trond Myklebusta2648092015-10-06 17:03:00 -0400971 return;
Chuck Lever176e21e2011-05-09 15:22:44 -0400972
973 /* Look up and lock the request corresponding to the given XID */
Trond Myklebustce7c2522017-08-16 15:30:35 -0400974 spin_lock(&xprt->recv_lock);
Chuck Lever176e21e2011-05-09 15:22:44 -0400975 rovr = xprt_lookup_rqst(xprt, *xp);
976 if (!rovr)
977 goto out_unlock;
Trond Myklebust729749b2017-08-13 10:03:59 -0400978 xprt_pin_rqst(rovr);
Trond Myklebustce7c2522017-08-16 15:30:35 -0400979 spin_unlock(&xprt->recv_lock);
Chuck Lever176e21e2011-05-09 15:22:44 -0400980 task = rovr->rq_task;
981
982 copied = rovr->rq_private_buf.buflen;
983 if (copied > repsize)
984 copied = repsize;
985
986 if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
987 dprintk("RPC: sk_buff copy failed\n");
Trond Myklebustce7c2522017-08-16 15:30:35 -0400988 spin_lock(&xprt->recv_lock);
Trond Myklebust729749b2017-08-13 10:03:59 -0400989 goto out_unpin;
Chuck Lever176e21e2011-05-09 15:22:44 -0400990 }
991
Trond Myklebustce7c2522017-08-16 15:30:35 -0400992 spin_lock(&xprt->recv_lock);
Chuck Lever176e21e2011-05-09 15:22:44 -0400993 xprt_complete_rqst(task, copied);
Trond Myklebust729749b2017-08-13 10:03:59 -0400994out_unpin:
995 xprt_unpin_rqst(rovr);
Chuck Lever176e21e2011-05-09 15:22:44 -0400996 out_unlock:
Trond Myklebustce7c2522017-08-16 15:30:35 -0400997 spin_unlock(&xprt->recv_lock);
Trond Myklebusta2648092015-10-06 17:03:00 -0400998}
999
1000static void xs_local_data_receive(struct sock_xprt *transport)
1001{
1002 struct sk_buff *skb;
1003 struct sock *sk;
1004 int err;
1005
1006 mutex_lock(&transport->recv_mutex);
1007 sk = transport->inet;
1008 if (sk == NULL)
1009 goto out;
1010 for (;;) {
1011 skb = skb_recv_datagram(sk, 0, 1, &err);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001012 if (skb != NULL) {
1013 xs_local_data_read_skb(&transport->xprt, sk, skb);
1014 skb_free_datagram(sk, skb);
1015 continue;
1016 }
1017 if (!test_and_clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebusta2648092015-10-06 17:03:00 -04001018 break;
Trond Myklebusta2648092015-10-06 17:03:00 -04001019 }
1020out:
1021 mutex_unlock(&transport->recv_mutex);
1022}
1023
1024static void xs_local_data_receive_workfn(struct work_struct *work)
1025{
1026 struct sock_xprt *transport =
1027 container_of(work, struct sock_xprt, recv_worker);
1028 xs_local_data_receive(transport);
Chuck Lever176e21e2011-05-09 15:22:44 -04001029}
1030
Chuck Lever9903cd12005-08-11 16:25:26 -04001031/**
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001032 * xs_udp_data_read_skb - receive callback for UDP sockets
1033 * @xprt: transport
1034 * @sk: socket
1035 * @skb: skbuff
Chuck Lever9903cd12005-08-11 16:25:26 -04001036 *
1037 */
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001038static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1039 struct sock *sk,
1040 struct sk_buff *skb)
Chuck Lever9903cd12005-08-11 16:25:26 -04001041{
1042 struct rpc_task *task;
Chuck Levera246b012005-08-11 16:25:23 -04001043 struct rpc_rqst *rovr;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001044 int repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001045 u32 _xid;
1046 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -04001047
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001048 repsize = skb->len;
Chuck Levera246b012005-08-11 16:25:23 -04001049 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001050 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001051 return;
Chuck Levera246b012005-08-11 16:25:23 -04001052 }
1053
1054 /* Copy the XID from the skb... */
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001055 xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
Chuck Levera246b012005-08-11 16:25:23 -04001056 if (xp == NULL)
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001057 return;
Chuck Levera246b012005-08-11 16:25:23 -04001058
1059 /* Look up and lock the request corresponding to the given XID */
Trond Myklebustce7c2522017-08-16 15:30:35 -04001060 spin_lock(&xprt->recv_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001061 rovr = xprt_lookup_rqst(xprt, *xp);
1062 if (!rovr)
1063 goto out_unlock;
Trond Myklebust729749b2017-08-13 10:03:59 -04001064 xprt_pin_rqst(rovr);
Trond Myklebustce7c2522017-08-16 15:30:35 -04001065 spin_unlock(&xprt->recv_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001066 task = rovr->rq_task;
1067
Chuck Levera246b012005-08-11 16:25:23 -04001068 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1069 copied = repsize;
1070
1071 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001072 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
Eric Dumazet02c22342016-04-27 16:44:30 -07001073 __UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
Trond Myklebustce7c2522017-08-16 15:30:35 -04001074 spin_lock(&xprt->recv_lock);
Trond Myklebust729749b2017-08-13 10:03:59 -04001075 goto out_unpin;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001076 }
1077
Eric Dumazet02c22342016-04-27 16:44:30 -07001078 __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -04001079
Trond Myklebust729749b2017-08-13 10:03:59 -04001080 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001081 xprt_adjust_cwnd(xprt, task, copied);
Trond Myklebustce7c2522017-08-16 15:30:35 -04001082 spin_unlock_bh(&xprt->transport_lock);
1083 spin_lock(&xprt->recv_lock);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001084 xprt_complete_rqst(task, copied);
Trond Myklebust729749b2017-08-13 10:03:59 -04001085out_unpin:
1086 xprt_unpin_rqst(rovr);
Chuck Levera246b012005-08-11 16:25:23 -04001087 out_unlock:
Trond Myklebustce7c2522017-08-16 15:30:35 -04001088 spin_unlock(&xprt->recv_lock);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001089}
1090
1091static void xs_udp_data_receive(struct sock_xprt *transport)
1092{
1093 struct sk_buff *skb;
1094 struct sock *sk;
1095 int err;
1096
1097 mutex_lock(&transport->recv_mutex);
1098 sk = transport->inet;
1099 if (sk == NULL)
1100 goto out;
1101 for (;;) {
Paolo Abeni7c13f972016-11-04 11:28:59 +01001102 skb = skb_recv_udp(sk, 0, 1, &err);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001103 if (skb != NULL) {
1104 xs_udp_data_read_skb(&transport->xprt, sk, skb);
Paolo Abeni850cbad2016-10-21 13:55:47 +02001105 consume_skb(skb);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001106 continue;
1107 }
1108 if (!test_and_clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001109 break;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001110 }
1111out:
1112 mutex_unlock(&transport->recv_mutex);
1113}
1114
1115static void xs_udp_data_receive_workfn(struct work_struct *work)
1116{
1117 struct sock_xprt *transport =
1118 container_of(work, struct sock_xprt, recv_worker);
1119 xs_udp_data_receive(transport);
1120}
1121
1122/**
1123 * xs_data_ready - "data ready" callback for UDP sockets
1124 * @sk: socket with data to read
1125 *
1126 */
1127static void xs_data_ready(struct sock *sk)
1128{
1129 struct rpc_xprt *xprt;
1130
1131 read_lock_bh(&sk->sk_callback_lock);
1132 dprintk("RPC: xs_data_ready...\n");
1133 xprt = xprt_from_sock(sk);
1134 if (xprt != NULL) {
1135 struct sock_xprt *transport = container_of(xprt,
1136 struct sock_xprt, xprt);
Trond Myklebust5157b952016-05-29 10:13:24 -04001137 transport->old_data_ready(sk);
1138 /* Any data means we had a useful conversation, so
1139 * then we don't need to delay the next reconnect
1140 */
1141 if (xprt->reestablish_timeout)
1142 xprt->reestablish_timeout = 0;
Trond Myklebust42d42a52016-05-23 09:24:55 -04001143 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebust40a5f1b2016-05-27 10:39:50 -04001144 queue_work(xprtiod_workqueue, &transport->recv_worker);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001145 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001146 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001147}
1148
Trond Myklebusta519fc72012-09-12 16:49:15 -04001149/*
1150 * Helper function to force a TCP close if the server is sending
1151 * junk and/or it has put us in CLOSE_WAIT
1152 */
1153static void xs_tcp_force_close(struct rpc_xprt *xprt)
1154{
Trond Myklebusta519fc72012-09-12 16:49:15 -04001155 xprt_force_disconnect(xprt);
1156}
1157
Chuck Leverdd456472006-12-05 16:35:44 -05001158static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001159{
Chuck Lever51971132006-12-05 16:35:19 -05001160 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001161 size_t len, used;
1162 char *p;
1163
Chuck Lever51971132006-12-05 16:35:19 -05001164 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
1165 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001166 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001167 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001168 if (used != len)
1169 return;
Chuck Lever808012f2005-08-25 16:25:49 -07001170
Chuck Lever51971132006-12-05 16:35:19 -05001171 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
1172 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -05001173 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -04001174 else
Chuck Levere136d092006-12-05 16:35:23 -05001175 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -05001176 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -07001177
Chuck Levere136d092006-12-05 16:35:23 -05001178 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001179 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -07001180
Chuck Levera246b012005-08-11 16:25:23 -04001181 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001182 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001183 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebusta519fc72012-09-12 16:49:15 -04001184 xs_tcp_force_close(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001185 return;
Chuck Levera246b012005-08-11 16:25:23 -04001186 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001187 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -05001188 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001189}
1190
Chuck Lever51971132006-12-05 16:35:19 -05001191static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001192{
Chuck Lever51971132006-12-05 16:35:19 -05001193 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001194 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001195 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001196 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
1197 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1198 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001199 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001200 }
1201 }
1202}
1203
Chuck Leverdd456472006-12-05 16:35:44 -05001204static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001205{
1206 size_t len, used;
1207 char *p;
1208
Chuck Lever51971132006-12-05 16:35:19 -05001209 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001210 dprintk("RPC: reading XID (%zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001211 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001212 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001213 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001214 if (used != len)
1215 return;
Chuck Levere136d092006-12-05 16:35:23 -05001216 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001217 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -05001218 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001219 dprintk("RPC: reading %s XID %08x\n",
1220 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1221 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -05001222 ntohl(transport->tcp_xid));
1223 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001224}
1225
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001226static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1227 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001228{
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001229 size_t len, used;
1230 u32 offset;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001231 char *p;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001232
1233 /*
1234 * We want transport->tcp_offset to be 8 at the end of this routine
1235 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1236 * When this function is called for the first time,
1237 * transport->tcp_offset is 4 (after having already read the xid).
1238 */
1239 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001240 len = sizeof(transport->tcp_calldir) - offset;
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001241 dprintk("RPC: reading CALL/REPLY flag (%zu bytes)\n", len);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001242 p = ((char *) &transport->tcp_calldir) + offset;
1243 used = xdr_skb_read_bits(desc, p, len);
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001244 transport->tcp_offset += used;
1245 if (used != len)
1246 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001247 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001248 /*
1249 * We don't yet have the XDR buffer, so we will write the calldir
1250 * out after we get the buffer from the 'struct rpc_rqst'
1251 */
Trond Myklebustb76ce562010-06-16 13:57:32 -04001252 switch (ntohl(transport->tcp_calldir)) {
1253 case RPC_REPLY:
1254 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1255 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001256 transport->tcp_flags |= TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001257 break;
1258 case RPC_CALL:
1259 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1260 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001261 transport->tcp_flags &= ~TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001262 break;
1263 default:
1264 dprintk("RPC: invalid request message type\n");
Trond Myklebusta519fc72012-09-12 16:49:15 -04001265 xs_tcp_force_close(&transport->xprt);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001266 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001267 xs_tcp_check_fraghdr(transport);
1268}
1269
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001270static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
1271 struct xdr_skb_reader *desc,
1272 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001273{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001274 struct sock_xprt *transport =
1275 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001276 struct xdr_buf *rcvbuf;
1277 size_t len;
1278 ssize_t r;
1279
Chuck Levera246b012005-08-11 16:25:23 -04001280 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001281
1282 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1283 /*
1284 * Save the RPC direction in the XDR buffer
1285 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001286 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
Trond Myklebustb76ce562010-06-16 13:57:32 -04001287 &transport->tcp_calldir,
1288 sizeof(transport->tcp_calldir));
1289 transport->tcp_copied += sizeof(transport->tcp_calldir);
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001290 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -04001291 }
1292
Chuck Levera246b012005-08-11 16:25:23 -04001293 len = desc->count;
Trond Myklebust040249d2017-08-13 02:13:45 -04001294 if (len > transport->tcp_reclen - transport->tcp_offset)
1295 desc->count = transport->tcp_reclen - transport->tcp_offset;
1296 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001297 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001298
Trond Myklebust040249d2017-08-13 02:13:45 -04001299 if (desc->count) {
Chuck Levera246b012005-08-11 16:25:23 -04001300 /* Error when copying to the receive buffer,
1301 * usually because we weren't able to allocate
1302 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001303 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001304 * will not receive any additional updates,
1305 * and time out.
1306 * Any remaining data from this record will
1307 * be discarded.
1308 */
Chuck Levere136d092006-12-05 16:35:23 -05001309 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001310 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001311 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001312 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1313 "tcp_offset = %u, tcp_reclen = %u\n",
1314 xprt, transport->tcp_copied,
1315 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001316 return;
Chuck Levera246b012005-08-11 16:25:23 -04001317 }
1318
Trond Myklebust040249d2017-08-13 02:13:45 -04001319 transport->tcp_copied += r;
1320 transport->tcp_offset += r;
1321 desc->count = len - r;
1322
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001323 dprintk("RPC: XID %08x read %zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001324 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001325 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1326 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1327 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001328
Chuck Lever51971132006-12-05 16:35:19 -05001329 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001330 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001331 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001332 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1333 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001334 }
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001335}
1336
1337/*
1338 * Finds the request corresponding to the RPC xid and invokes the common
1339 * tcp read code to read the data.
1340 */
1341static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1342 struct xdr_skb_reader *desc)
1343{
1344 struct sock_xprt *transport =
1345 container_of(xprt, struct sock_xprt, xprt);
1346 struct rpc_rqst *req;
1347
1348 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1349
1350 /* Find and lock the request corresponding to this xid */
Trond Myklebustce7c2522017-08-16 15:30:35 -04001351 spin_lock(&xprt->recv_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001352 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1353 if (!req) {
1354 dprintk("RPC: XID %08x request not found!\n",
1355 ntohl(transport->tcp_xid));
Trond Myklebustce7c2522017-08-16 15:30:35 -04001356 spin_unlock(&xprt->recv_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001357 return -1;
1358 }
Trond Myklebust729749b2017-08-13 10:03:59 -04001359 xprt_pin_rqst(req);
Trond Myklebustce7c2522017-08-16 15:30:35 -04001360 spin_unlock(&xprt->recv_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001361
1362 xs_tcp_read_common(xprt, desc, req);
1363
Trond Myklebustce7c2522017-08-16 15:30:35 -04001364 spin_lock(&xprt->recv_lock);
Chuck Levere136d092006-12-05 16:35:23 -05001365 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001366 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Trond Myklebust729749b2017-08-13 10:03:59 -04001367 xprt_unpin_rqst(req);
Trond Myklebustce7c2522017-08-16 15:30:35 -04001368 spin_unlock(&xprt->recv_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001369 return 0;
1370}
1371
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001372#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001373/*
1374 * Obtains an rpc_rqst previously allocated and invokes the common
1375 * tcp read code to read the data. The result is placed in the callback
1376 * queue.
1377 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1378 * connection and return -1.
1379 */
Trond Myklebust2ea24492014-02-10 11:18:39 -05001380static int xs_tcp_read_callback(struct rpc_xprt *xprt,
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001381 struct xdr_skb_reader *desc)
1382{
1383 struct sock_xprt *transport =
1384 container_of(xprt, struct sock_xprt, xprt);
1385 struct rpc_rqst *req;
1386
Trond Myklebustc89091c2017-08-16 12:09:44 -04001387 /* Look up the request corresponding to the given XID */
Trond Myklebust2ea24492014-02-10 11:18:39 -05001388 req = xprt_lookup_bc_request(xprt, transport->tcp_xid);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001389 if (req == NULL) {
1390 printk(KERN_WARNING "Callback slot table overflowed\n");
1391 xprt_force_disconnect(xprt);
1392 return -1;
1393 }
1394
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001395 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1396 xs_tcp_read_common(xprt, desc, req);
1397
Trond Myklebust2ea24492014-02-10 11:18:39 -05001398 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
1399 xprt_complete_bc_request(req, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001400
1401 return 0;
1402}
1403
1404static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1405 struct xdr_skb_reader *desc)
1406{
1407 struct sock_xprt *transport =
1408 container_of(xprt, struct sock_xprt, xprt);
1409
1410 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1411 xs_tcp_read_reply(xprt, desc) :
1412 xs_tcp_read_callback(xprt, desc);
1413}
Chuck Lever76566772015-10-24 17:28:32 -04001414
1415static int xs_tcp_bc_up(struct svc_serv *serv, struct net *net)
1416{
1417 int ret;
1418
1419 ret = svc_create_xprt(serv, "tcp-bc", net, PF_INET, 0,
1420 SVC_SOCK_ANONYMOUS);
1421 if (ret < 0)
1422 return ret;
1423 return 0;
1424}
Chuck Lever6b26cc82016-05-02 14:40:40 -04001425
1426static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
1427{
1428 return PAGE_SIZE;
1429}
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001430#else
1431static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1432 struct xdr_skb_reader *desc)
1433{
1434 return xs_tcp_read_reply(xprt, desc);
1435}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001436#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001437
1438/*
1439 * Read data off the transport. This can be either an RPC_CALL or an
1440 * RPC_REPLY. Relay the processing to helper functions.
1441 */
1442static void xs_tcp_read_data(struct rpc_xprt *xprt,
1443 struct xdr_skb_reader *desc)
1444{
1445 struct sock_xprt *transport =
1446 container_of(xprt, struct sock_xprt, xprt);
1447
1448 if (_xs_tcp_read_data(xprt, desc) == 0)
1449 xs_tcp_check_fraghdr(transport);
1450 else {
1451 /*
1452 * The transport_lock protects the request handling.
1453 * There's no need to hold it to update the tcp_flags.
1454 */
1455 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1456 }
Chuck Levera246b012005-08-11 16:25:23 -04001457}
1458
Chuck Leverdd456472006-12-05 16:35:44 -05001459static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001460{
1461 size_t len;
1462
Chuck Lever51971132006-12-05 16:35:19 -05001463 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001464 if (len > desc->count)
1465 len = desc->count;
1466 desc->count -= len;
1467 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001468 transport->tcp_offset += len;
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001469 dprintk("RPC: discarded %zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001470 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001471}
1472
Chuck Lever9903cd12005-08-11 16:25:26 -04001473static 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 -04001474{
1475 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001476 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001477 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001478 .skb = skb,
1479 .offset = offset,
1480 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001481 };
Chuck Levera246b012005-08-11 16:25:23 -04001482
Chuck Lever46121cf2007-01-31 12:14:08 -05001483 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001484 do {
Jeff Layton1a867a02014-10-28 14:24:14 -04001485 trace_xs_tcp_data_recv(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001486 /* Read in a new fragment marker if necessary */
1487 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001488 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001489 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001490 continue;
1491 }
1492 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001493 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001494 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001495 continue;
1496 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001497 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001498 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001499 xs_tcp_read_calldir(transport, &desc);
1500 continue;
1501 }
Chuck Levera246b012005-08-11 16:25:23 -04001502 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001503 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001504 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001505 continue;
1506 }
1507 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001508 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001509 } while (desc.count);
Jeff Layton1a867a02014-10-28 14:24:14 -04001510 trace_xs_tcp_data_recv(transport);
Chuck Lever46121cf2007-01-31 12:14:08 -05001511 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001512 return len - desc.count;
1513}
1514
Trond Myklebust66d7a562015-10-05 11:19:20 -04001515static void xs_tcp_data_receive(struct sock_xprt *transport)
1516{
1517 struct rpc_xprt *xprt = &transport->xprt;
1518 struct sock *sk;
1519 read_descriptor_t rd_desc = {
1520 .count = 2*1024*1024,
1521 .arg.data = xprt,
1522 };
1523 unsigned long total = 0;
Trond Myklebust8d6f97d2017-08-12 15:11:49 -04001524 int loop;
Trond Myklebust66d7a562015-10-05 11:19:20 -04001525 int read = 0;
1526
Trond Myklebustedc1b012015-10-05 10:53:49 -04001527 mutex_lock(&transport->recv_mutex);
Trond Myklebust66d7a562015-10-05 11:19:20 -04001528 sk = transport->inet;
Trond Myklebustedc1b012015-10-05 10:53:49 -04001529 if (sk == NULL)
1530 goto out;
Trond Myklebust66d7a562015-10-05 11:19:20 -04001531
1532 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Trond Myklebust8d6f97d2017-08-12 15:11:49 -04001533 for (loop = 0; loop < 64; loop++) {
Trond Myklebustedc1b012015-10-05 10:53:49 -04001534 lock_sock(sk);
Trond Myklebust66d7a562015-10-05 11:19:20 -04001535 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001536 if (read <= 0) {
1537 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
1538 release_sock(sk);
Trond Myklebust8d6f97d2017-08-12 15:11:49 -04001539 break;
Trond Myklebust42d42a52016-05-23 09:24:55 -04001540 }
Trond Myklebust8d6f97d2017-08-12 15:11:49 -04001541 release_sock(sk);
1542 total += read;
Trond Myklebust66d7a562015-10-05 11:19:20 -04001543 rd_desc.count = 65536;
1544 }
Trond Myklebust8d6f97d2017-08-12 15:11:49 -04001545 if (test_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
1546 queue_work(xprtiod_workqueue, &transport->recv_worker);
Trond Myklebustedc1b012015-10-05 10:53:49 -04001547out:
1548 mutex_unlock(&transport->recv_mutex);
Trond Myklebust66d7a562015-10-05 11:19:20 -04001549 trace_xs_tcp_data_ready(xprt, read, total);
1550}
1551
Trond Myklebustedc1b012015-10-05 10:53:49 -04001552static void xs_tcp_data_receive_workfn(struct work_struct *work)
1553{
1554 struct sock_xprt *transport =
1555 container_of(work, struct sock_xprt, recv_worker);
1556 xs_tcp_data_receive(transport);
1557}
1558
Chuck Lever9903cd12005-08-11 16:25:26 -04001559/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001560 * xs_tcp_state_change - callback to handle TCP socket state changes
1561 * @sk: socket whose state has changed
1562 *
1563 */
1564static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001565{
Chuck Lever9903cd12005-08-11 16:25:26 -04001566 struct rpc_xprt *xprt;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001567 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001568
Eric Dumazetf064af12010-09-22 12:43:39 +00001569 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001570 if (!(xprt = xprt_from_sock(sk)))
1571 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001572 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001573 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001574 sk->sk_state, xprt_connected(xprt),
1575 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001576 sock_flag(sk, SOCK_ZAPPED),
1577 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001578
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001579 transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001580 trace_rpc_socket_state_change(xprt, sk->sk_socket);
Chuck Levera246b012005-08-11 16:25:23 -04001581 switch (sk->sk_state) {
1582 case TCP_ESTABLISHED:
Eric Dumazetf064af12010-09-22 12:43:39 +00001583 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001584 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001585
Chuck Levera246b012005-08-11 16:25:23 -04001586 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001587 transport->tcp_offset = 0;
1588 transport->tcp_reclen = 0;
1589 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001590 transport->tcp_flags =
1591 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Trond Myklebust8b717982013-09-26 10:18:04 -04001592 xprt->connect_cookie++;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001593 clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
1594 xprt_clear_connecting(xprt);
Chuck Lever51971132006-12-05 16:35:19 -05001595
Trond Myklebust2a491992009-03-11 14:38:00 -04001596 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001597 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001598 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001599 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001600 case TCP_FIN_WAIT1:
1601 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001602 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001603 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001604 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001605 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001606 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001607 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001608 smp_mb__after_atomic();
Chuck Levera246b012005-08-11 16:25:23 -04001609 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001610 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001611 /* The server initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001612 xprt->connect_cookie++;
Trond Myklebustd0bea452012-10-23 11:35:47 -04001613 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebusta519fc72012-09-12 16:49:15 -04001614 xs_tcp_force_close(xprt);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001615 /* fall through */
Trond Myklebust663b8852008-01-01 18:42:12 -05001616 case TCP_CLOSING:
1617 /*
1618 * If the server closed down the connection, make sure that
1619 * we back off before reconnecting
1620 */
1621 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1622 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001623 break;
1624 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001625 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001626 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001627 clear_bit(XPRT_CONNECTED, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001628 smp_mb__after_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001629 break;
1630 case TCP_CLOSE:
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001631 if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
1632 &transport->sock_state))
1633 xprt_clear_connecting(xprt);
NeilBrown3ffbc1d2017-07-19 14:05:01 +10001634 if (sk->sk_err)
1635 xprt_wake_pending_tasks(xprt, -sk->sk_err);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001636 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001637 }
1638 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001639 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001640}
1641
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001642static void xs_write_space(struct sock *sk)
1643{
Trond Myklebust13331a52016-01-06 08:57:06 -05001644 struct socket_wq *wq;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001645 struct rpc_xprt *xprt;
1646
Trond Myklebust13331a52016-01-06 08:57:06 -05001647 if (!sk->sk_socket)
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001648 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001649 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001650
1651 if (unlikely(!(xprt = xprt_from_sock(sk))))
1652 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001653 rcu_read_lock();
1654 wq = rcu_dereference(sk->sk_wq);
1655 if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0)
1656 goto out;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001657
1658 xprt_write_space(xprt);
Trond Myklebust13331a52016-01-06 08:57:06 -05001659out:
1660 rcu_read_unlock();
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001661}
1662
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001663/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001664 * xs_udp_write_space - callback invoked when socket buffer space
1665 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001666 * @sk: socket whose state has changed
1667 *
Chuck Levera246b012005-08-11 16:25:23 -04001668 * Called when more output buffer space is available for this socket.
1669 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001670 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001671 * with a bunch of small requests.
1672 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001673static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001674{
Eric Dumazetf064af12010-09-22 12:43:39 +00001675 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001676
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001677 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001678 if (sock_writeable(sk))
1679 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001680
Eric Dumazetf064af12010-09-22 12:43:39 +00001681 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001682}
Chuck Levera246b012005-08-11 16:25:23 -04001683
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001684/**
1685 * xs_tcp_write_space - callback invoked when socket buffer space
1686 * becomes available
1687 * @sk: socket whose state has changed
1688 *
1689 * Called when more output buffer space is available for this socket.
1690 * We try not to wake our writers until they can make "significant"
1691 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1692 * with a bunch of small requests.
1693 */
1694static void xs_tcp_write_space(struct sock *sk)
1695{
Eric Dumazetf064af12010-09-22 12:43:39 +00001696 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001697
1698 /* from net/core/stream.c:sk_stream_write_space */
Eric Dumazet64dc6132013-07-22 20:26:31 -07001699 if (sk_stream_is_writeable(sk))
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001700 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001701
Eric Dumazetf064af12010-09-22 12:43:39 +00001702 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001703}
1704
Chuck Lever470056c2005-08-25 16:25:56 -07001705static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001706{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001707 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1708 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001709
Chuck Lever7c6e0662006-12-05 16:35:30 -05001710 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001711 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001712 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001713 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001714 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001715 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001716 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001717 sk->sk_write_space(sk);
1718 }
1719}
1720
Chuck Lever43118c22005-08-25 16:25:49 -07001721/**
Chuck Lever470056c2005-08-25 16:25:56 -07001722 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001723 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001724 * @sndsize: requested size of send buffer, in bytes
1725 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001726 *
Chuck Lever470056c2005-08-25 16:25:56 -07001727 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001728 */
Chuck Lever470056c2005-08-25 16:25:56 -07001729static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001730{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001731 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1732
1733 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001734 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001735 transport->sndsize = sndsize + 1024;
1736 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001737 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001738 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001739
1740 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001741}
1742
Chuck Lever46c0ee82005-08-25 16:25:52 -07001743/**
1744 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1745 * @task: task that timed out
1746 *
1747 * Adjust the congestion window after a retransmit timeout has occurred.
1748 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001749static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever46c0ee82005-08-25 16:25:52 -07001750{
Chuck Leverb977b642017-02-08 17:00:51 -05001751 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001752 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
Chuck Leverb977b642017-02-08 17:00:51 -05001753 spin_unlock_bh(&xprt->transport_lock);
Chuck Lever46c0ee82005-08-25 16:25:52 -07001754}
1755
Chuck Leverb85d8802006-05-25 01:40:49 -04001756static unsigned short xs_get_random_port(void)
1757{
Frank Sorenson5d718992016-07-08 16:35:23 -05001758 unsigned short range = xprt_max_resvport - xprt_min_resvport + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001759 unsigned short rand = (unsigned short) prandom_u32() % range;
Chuck Leverb85d8802006-05-25 01:40:49 -04001760 return rand + xprt_min_resvport;
1761}
1762
Chuck Lever92200412006-01-03 09:55:51 +01001763/**
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001764 * xs_set_reuseaddr_port - set the socket's port and address reuse options
1765 * @sock: socket
1766 *
1767 * Note that this function has to be called on all sockets that share the
1768 * same port, and it must be called before binding.
1769 */
1770static void xs_sock_set_reuseport(struct socket *sock)
1771{
Trond Myklebust402e23b2015-02-09 17:20:14 -05001772 int opt = 1;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001773
Trond Myklebust402e23b2015-02-09 17:20:14 -05001774 kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
1775 (char *)&opt, sizeof(opt));
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001776}
1777
1778static unsigned short xs_sock_getport(struct socket *sock)
1779{
1780 struct sockaddr_storage buf;
1781 int buflen;
1782 unsigned short port = 0;
1783
1784 if (kernel_getsockname(sock, (struct sockaddr *)&buf, &buflen) < 0)
1785 goto out;
1786 switch (buf.ss_family) {
1787 case AF_INET6:
1788 port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
1789 break;
1790 case AF_INET:
1791 port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
1792 }
1793out:
1794 return port;
1795}
1796
1797/**
Chuck Lever92200412006-01-03 09:55:51 +01001798 * xs_set_port - reset the port number in the remote endpoint address
1799 * @xprt: generic transport
1800 * @port: new port number
1801 *
1802 */
1803static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1804{
Chuck Lever46121cf2007-01-31 12:14:08 -05001805 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001806
Chuck Lever9dc3b092009-08-09 15:09:46 -04001807 rpc_set_port(xs_addr(xprt), port);
1808 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001809}
1810
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001811static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
1812{
1813 if (transport->srcport == 0)
1814 transport->srcport = xs_sock_getport(sock);
1815}
1816
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001817static unsigned short xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001818{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001819 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001820
1821 if (port == 0 && transport->xprt.resvport)
1822 port = xs_get_random_port();
1823 return port;
1824}
1825
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001826static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001827{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001828 if (transport->srcport != 0)
1829 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001830 if (!transport->xprt.resvport)
1831 return 0;
1832 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1833 return xprt_max_resvport;
1834 return --port;
1835}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001836static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001837{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001838 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001839 int err, nloop = 0;
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001840 unsigned short port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001841 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001842
Chris Perl0f7a6222014-09-05 15:40:21 -04001843 /*
1844 * If we are asking for any ephemeral port (i.e. port == 0 &&
1845 * transport->xprt.resvport == 0), don't bind. Let the local
1846 * port selection happen implicitly when the socket is used
1847 * (for example at connect time).
1848 *
1849 * This ensures that we can continue to establish TCP
1850 * connections even when all local ephemeral ports are already
1851 * a part of some TCP connection. This makes no difference
1852 * for UDP sockets, but also doens't harm them.
1853 *
1854 * If we're asking for any reserved port (i.e. port == 0 &&
1855 * transport->xprt.resvport == 1) xs_get_srcport above will
1856 * ensure that port is non-zero and we will bind as needed.
1857 */
1858 if (port == 0)
1859 return 0;
1860
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001861 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001862 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001863 rpc_set_port((struct sockaddr *)&myaddr, port);
1864 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1865 transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001866 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001867 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001868 break;
Chuck Levera246b012005-08-11 16:25:23 -04001869 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001870 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001871 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001872 if (port > last)
1873 nloop++;
1874 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001875
Chuck Lever4232e862010-10-20 11:52:51 -04001876 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001877 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1878 &((struct sockaddr_in *)&myaddr)->sin_addr,
1879 port, err ? "failed" : "ok", err);
1880 else
1881 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1882 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1883 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001884 return err;
1885}
1886
Chuck Lever176e21e2011-05-09 15:22:44 -04001887/*
1888 * We don't support autobind on AF_LOCAL sockets
1889 */
1890static void xs_local_rpcbind(struct rpc_task *task)
1891{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001892 xprt_set_bound(task->tk_xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04001893}
1894
1895static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1896{
1897}
Chuck Levera246b012005-08-11 16:25:23 -04001898
Peter Zijlstraed075362006-12-06 20:35:24 -08001899#ifdef CONFIG_DEBUG_LOCK_ALLOC
1900static struct lock_class_key xs_key[2];
1901static struct lock_class_key xs_slock_key[2];
1902
Chuck Lever176e21e2011-05-09 15:22:44 -04001903static inline void xs_reclassify_socketu(struct socket *sock)
1904{
1905 struct sock *sk = sock->sk;
1906
Chuck Lever176e21e2011-05-09 15:22:44 -04001907 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1908 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1909}
1910
Chuck Lever8945ee52007-08-06 11:58:04 -04001911static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001912{
1913 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001914
Chuck Lever8945ee52007-08-06 11:58:04 -04001915 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1916 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1917}
Peter Zijlstraed075362006-12-06 20:35:24 -08001918
Chuck Lever8945ee52007-08-06 11:58:04 -04001919static inline void xs_reclassify_socket6(struct socket *sock)
1920{
1921 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001922
Chuck Lever8945ee52007-08-06 11:58:04 -04001923 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1924 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001925}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001926
1927static inline void xs_reclassify_socket(int family, struct socket *sock)
1928{
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02001929 if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -04001930 return;
1931
Chuck Lever4232e862010-10-20 11:52:51 -04001932 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001933 case AF_LOCAL:
1934 xs_reclassify_socketu(sock);
1935 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001936 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001937 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001938 break;
1939 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001940 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001941 break;
1942 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001943}
Peter Zijlstraed075362006-12-06 20:35:24 -08001944#else
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001945static inline void xs_reclassify_socket(int family, struct socket *sock)
1946{
1947}
Peter Zijlstraed075362006-12-06 20:35:24 -08001948#endif
1949
NeilBrown93dc41b2013-10-31 16:14:36 +11001950static void xs_dummy_setup_socket(struct work_struct *work)
1951{
1952}
1953
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001954static struct socket *xs_create_sock(struct rpc_xprt *xprt,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001955 struct sock_xprt *transport, int family, int type,
1956 int protocol, bool reuseport)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001957{
1958 struct socket *sock;
1959 int err;
1960
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001961 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001962 if (err < 0) {
1963 dprintk("RPC: can't create %d transport socket (%d).\n",
1964 protocol, -err);
1965 goto out;
1966 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001967 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001968
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001969 if (reuseport)
1970 xs_sock_set_reuseport(sock);
1971
Ben Hutchings4cea2882011-02-22 21:54:34 +00001972 err = xs_bind(transport, sock);
1973 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001974 sock_release(sock);
1975 goto out;
1976 }
1977
1978 return sock;
1979out:
1980 return ERR_PTR(err);
1981}
1982
Chuck Lever176e21e2011-05-09 15:22:44 -04001983static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1984 struct socket *sock)
1985{
1986 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1987 xprt);
1988
1989 if (!transport->inet) {
1990 struct sock *sk = sock->sk;
1991
1992 write_lock_bh(&sk->sk_callback_lock);
1993
1994 xs_save_old_callbacks(transport, sk);
1995
1996 sk->sk_user_data = xprt;
Trond Myklebusta2648092015-10-06 17:03:00 -04001997 sk->sk_data_ready = xs_data_ready;
Chuck Lever176e21e2011-05-09 15:22:44 -04001998 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07001999 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05002000 sk->sk_error_report = xs_error_report;
Trond Myklebustc2126152015-08-19 21:46:15 -05002001 sk->sk_allocation = GFP_NOIO;
Chuck Lever176e21e2011-05-09 15:22:44 -04002002
2003 xprt_clear_connected(xprt);
2004
2005 /* Reset to new socket */
2006 transport->sock = sock;
2007 transport->inet = sk;
2008
2009 write_unlock_bh(&sk->sk_callback_lock);
2010 }
2011
2012 /* Tell the socket layer to start connecting... */
2013 xprt->stat.connect_count++;
2014 xprt->stat.connect_start = jiffies;
2015 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
2016}
2017
2018/**
2019 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
Chuck Lever176e21e2011-05-09 15:22:44 -04002020 * @transport: socket transport to connect
Chuck Lever176e21e2011-05-09 15:22:44 -04002021 */
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002022static int xs_local_setup_socket(struct sock_xprt *transport)
Chuck Lever176e21e2011-05-09 15:22:44 -04002023{
Chuck Lever176e21e2011-05-09 15:22:44 -04002024 struct rpc_xprt *xprt = &transport->xprt;
2025 struct socket *sock;
2026 int status = -EIO;
2027
Chuck Lever176e21e2011-05-09 15:22:44 -04002028 status = __sock_create(xprt->xprt_net, AF_LOCAL,
2029 SOCK_STREAM, 0, &sock, 1);
2030 if (status < 0) {
2031 dprintk("RPC: can't create AF_LOCAL "
2032 "transport socket (%d).\n", -status);
2033 goto out;
2034 }
Stefan Hajnoczid1358912015-12-02 14:17:52 +08002035 xs_reclassify_socket(AF_LOCAL, sock);
Chuck Lever176e21e2011-05-09 15:22:44 -04002036
2037 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
2038 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2039
2040 status = xs_local_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002041 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever176e21e2011-05-09 15:22:44 -04002042 switch (status) {
2043 case 0:
2044 dprintk("RPC: xprt %p connected to %s\n",
2045 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2046 xprt_set_connected(xprt);
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002047 case -ENOBUFS:
Chuck Lever176e21e2011-05-09 15:22:44 -04002048 break;
2049 case -ENOENT:
2050 dprintk("RPC: xprt %p: socket %s does not exist\n",
2051 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2052 break;
Trond Myklebust4a20a982012-12-15 17:02:29 -05002053 case -ECONNREFUSED:
2054 dprintk("RPC: xprt %p: connection refused for %s\n",
2055 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2056 break;
Chuck Lever176e21e2011-05-09 15:22:44 -04002057 default:
2058 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
2059 __func__, -status,
2060 xprt->address_strings[RPC_DISPLAY_ADDR]);
2061 }
2062
2063out:
2064 xprt_clear_connecting(xprt);
2065 xprt_wake_pending_tasks(xprt, status);
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002066 return status;
2067}
2068
Linus Torvaldsb6669732013-02-28 18:02:55 -08002069static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002070{
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002071 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2072 int ret;
2073
2074 if (RPC_IS_ASYNC(task)) {
2075 /*
2076 * We want the AF_LOCAL connect to be resolved in the
2077 * filesystem namespace of the process making the rpc
2078 * call. Thus we connect synchronously.
2079 *
2080 * If we want to support asynchronous AF_LOCAL calls,
2081 * we'll need to figure out how to pass a namespace to
2082 * connect.
2083 */
2084 rpc_exit(task, -ENOTCONN);
2085 return;
2086 }
2087 ret = xs_local_setup_socket(transport);
2088 if (ret && !RPC_IS_SOFTCONN(task))
2089 msleep_interruptible(15000);
Chuck Lever176e21e2011-05-09 15:22:44 -04002090}
2091
Jeff Layton3c87ef62015-06-03 16:14:25 -04002092#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Jeff Laytond6e971d2015-06-03 16:14:28 -04002093/*
2094 * Note that this should be called with XPRT_LOCKED held (or when we otherwise
2095 * know that we have exclusive access to the socket), to guard against
2096 * races with xs_reset_transport.
2097 */
Mel Gormana564b8f2012-07-31 16:45:12 -07002098static void xs_set_memalloc(struct rpc_xprt *xprt)
2099{
2100 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2101 xprt);
2102
Jeff Laytond6e971d2015-06-03 16:14:28 -04002103 /*
2104 * If there's no sock, then we have nothing to set. The
2105 * reconnecting process will get it for us.
2106 */
2107 if (!transport->inet)
2108 return;
Jeff Layton8e228132015-06-03 16:14:26 -04002109 if (atomic_read(&xprt->swapper))
Mel Gormana564b8f2012-07-31 16:45:12 -07002110 sk_set_memalloc(transport->inet);
2111}
2112
2113/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002114 * xs_enable_swap - Tag this transport as being used for swap.
Mel Gormana564b8f2012-07-31 16:45:12 -07002115 * @xprt: transport to tag
Mel Gormana564b8f2012-07-31 16:45:12 -07002116 *
Jeff Layton8e228132015-06-03 16:14:26 -04002117 * Take a reference to this transport on behalf of the rpc_clnt, and
2118 * optionally mark it for swapping if it wasn't already.
Mel Gormana564b8f2012-07-31 16:45:12 -07002119 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002120static int
2121xs_enable_swap(struct rpc_xprt *xprt)
Mel Gormana564b8f2012-07-31 16:45:12 -07002122{
Jeff Laytond6e971d2015-06-03 16:14:28 -04002123 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Mel Gormana564b8f2012-07-31 16:45:12 -07002124
Jeff Laytond6e971d2015-06-03 16:14:28 -04002125 if (atomic_inc_return(&xprt->swapper) != 1)
2126 return 0;
2127 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2128 return -ERESTARTSYS;
2129 if (xs->inet)
2130 sk_set_memalloc(xs->inet);
2131 xprt_release_xprt(xprt, NULL);
Jeff Layton8e228132015-06-03 16:14:26 -04002132 return 0;
Mel Gormana564b8f2012-07-31 16:45:12 -07002133}
Jeff Layton8e228132015-06-03 16:14:26 -04002134
2135/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002136 * xs_disable_swap - Untag this transport as being used for swap.
Jeff Layton8e228132015-06-03 16:14:26 -04002137 * @xprt: transport to tag
2138 *
2139 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
2140 * swapper refcount goes to 0, untag the socket as a memalloc socket.
2141 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002142static void
2143xs_disable_swap(struct rpc_xprt *xprt)
Jeff Layton8e228132015-06-03 16:14:26 -04002144{
Jeff Laytond6e971d2015-06-03 16:14:28 -04002145 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Jeff Layton8e228132015-06-03 16:14:26 -04002146
Jeff Laytond6e971d2015-06-03 16:14:28 -04002147 if (!atomic_dec_and_test(&xprt->swapper))
2148 return;
2149 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2150 return;
2151 if (xs->inet)
2152 sk_clear_memalloc(xs->inet);
2153 xprt_release_xprt(xprt, NULL);
Mel Gormana564b8f2012-07-31 16:45:12 -07002154}
Mel Gormana564b8f2012-07-31 16:45:12 -07002155#else
2156static void xs_set_memalloc(struct rpc_xprt *xprt)
2157{
2158}
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002159
2160static int
2161xs_enable_swap(struct rpc_xprt *xprt)
2162{
2163 return -EINVAL;
2164}
2165
2166static void
2167xs_disable_swap(struct rpc_xprt *xprt)
2168{
2169}
Mel Gormana564b8f2012-07-31 16:45:12 -07002170#endif
2171
Chuck Lever16be2d22007-08-06 11:57:38 -04002172static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04002173{
Chuck Lever16be2d22007-08-06 11:57:38 -04002174 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04002175
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002176 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002177 struct sock *sk = sock->sk;
2178
2179 write_lock_bh(&sk->sk_callback_lock);
2180
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002181 xs_save_old_callbacks(transport, sk);
2182
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002183 sk->sk_user_data = xprt;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002184 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002185 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002186 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebustc2126152015-08-19 21:46:15 -05002187 sk->sk_allocation = GFP_NOIO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002188
2189 xprt_set_connected(xprt);
2190
2191 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002192 transport->sock = sock;
2193 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002194
Mel Gormana564b8f2012-07-31 16:45:12 -07002195 xs_set_memalloc(xprt);
2196
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002197 write_unlock_bh(&sk->sk_callback_lock);
2198 }
Chuck Lever470056c2005-08-25 16:25:56 -07002199 xs_udp_do_set_buffer_size(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002200
2201 xprt->stat.connect_start = jiffies;
Chuck Lever16be2d22007-08-06 11:57:38 -04002202}
2203
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002204static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04002205{
2206 struct sock_xprt *transport =
2207 container_of(work, struct sock_xprt, connect_worker.work);
2208 struct rpc_xprt *xprt = &transport->xprt;
Colin Ian Kingd099b8a2017-09-18 12:21:14 +01002209 struct socket *sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002210 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04002211
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002212 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002213 xs_addr(xprt)->sa_family, SOCK_DGRAM,
2214 IPPROTO_UDP, false);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002215 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04002216 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04002217
Chuck Leverc740eff2009-08-09 15:09:46 -04002218 dprintk("RPC: worker connecting xprt %p via %s to "
2219 "%s (port %s)\n", xprt,
2220 xprt->address_strings[RPC_DISPLAY_PROTO],
2221 xprt->address_strings[RPC_DISPLAY_ADDR],
2222 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04002223
2224 xs_udp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002225 trace_rpc_socket_connect(xprt, sock, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002226 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002227out:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002228 xprt_unlock_connect(xprt, transport);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002229 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002230 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002231}
2232
Trond Myklebust4876cc72015-06-19 16:17:57 -04002233/**
2234 * xs_tcp_shutdown - gracefully shut down a TCP socket
2235 * @xprt: transport
2236 *
2237 * Initiates a graceful shutdown of the TCP socket by calling the
2238 * equivalent of shutdown(SHUT_RDWR);
2239 */
2240static void xs_tcp_shutdown(struct rpc_xprt *xprt)
2241{
2242 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2243 struct socket *sock = transport->sock;
2244
2245 if (sock == NULL)
2246 return;
2247 if (xprt_connected(xprt)) {
2248 kernel_sock_shutdown(sock, SHUT_RDWR);
2249 trace_rpc_socket_shutdown(xprt, sock);
2250 } else
2251 xs_reset_transport(transport);
2252}
2253
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002254static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
2255 struct socket *sock)
2256{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002257 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2258 unsigned int keepidle;
2259 unsigned int keepcnt;
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002260 unsigned int opt_on = 1;
2261 unsigned int timeo;
2262
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002263 spin_lock_bh(&xprt->transport_lock);
2264 keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
2265 keepcnt = xprt->timeout->to_retries + 1;
2266 timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
2267 (xprt->timeout->to_retries + 1);
2268 clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2269 spin_unlock_bh(&xprt->transport_lock);
2270
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002271 /* TCP Keepalive options */
2272 kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
2273 (char *)&opt_on, sizeof(opt_on));
2274 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
2275 (char *)&keepidle, sizeof(keepidle));
2276 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
2277 (char *)&keepidle, sizeof(keepidle));
2278 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
2279 (char *)&keepcnt, sizeof(keepcnt));
2280
2281 /* TCP user timeout (see RFC5482) */
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002282 kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT,
2283 (char *)&timeo, sizeof(timeo));
2284}
2285
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002286static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
2287 unsigned long connect_timeout,
2288 unsigned long reconnect_timeout)
2289{
2290 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2291 struct rpc_timeout to;
2292 unsigned long initval;
2293
2294 spin_lock_bh(&xprt->transport_lock);
2295 if (reconnect_timeout < xprt->max_reconnect_timeout)
2296 xprt->max_reconnect_timeout = reconnect_timeout;
2297 if (connect_timeout < xprt->connect_timeout) {
2298 memcpy(&to, xprt->timeout, sizeof(to));
2299 initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
2300 /* Arbitrary lower limit */
2301 if (initval < XS_TCP_INIT_REEST_TO << 1)
2302 initval = XS_TCP_INIT_REEST_TO << 1;
2303 to.to_initval = initval;
2304 to.to_maxval = initval;
2305 memcpy(&transport->tcp_timeout, &to,
2306 sizeof(transport->tcp_timeout));
2307 xprt->timeout = &transport->tcp_timeout;
2308 xprt->connect_timeout = connect_timeout;
2309 }
2310 set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2311 spin_unlock_bh(&xprt->transport_lock);
2312}
2313
Chuck Lever16be2d22007-08-06 11:57:38 -04002314static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002315{
Chuck Lever16be2d22007-08-06 11:57:38 -04002316 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002317 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002318
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002319 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002320 struct sock *sk = sock->sk;
NeilBrownd88e4d82016-08-04 16:24:28 +10002321 unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC;
Trond Myklebust7f260e82013-09-24 11:25:22 -04002322
NeilBrownd88e4d82016-08-04 16:24:28 +10002323 /* Avoid temporary address, they are bad for long-lived
2324 * connections such as NFS mounts.
2325 * RFC4941, section 3.6 suggests that:
2326 * Individual applications, which have specific
2327 * knowledge about the normal duration of connections,
2328 * MAY override this as appropriate.
2329 */
2330 kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES,
2331 (char *)&addr_pref, sizeof(addr_pref));
2332
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002333 xs_tcp_set_socket_timeouts(xprt, sock);
Trond Myklebust775f06a2015-06-20 15:31:54 -04002334
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002335 write_lock_bh(&sk->sk_callback_lock);
2336
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002337 xs_save_old_callbacks(transport, sk);
2338
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002339 sk->sk_user_data = xprt;
Trond Myklebust5157b952016-05-29 10:13:24 -04002340 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002341 sk->sk_state_change = xs_tcp_state_change;
2342 sk->sk_write_space = xs_tcp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002343 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05002344 sk->sk_error_report = xs_error_report;
Trond Myklebustc2126152015-08-19 21:46:15 -05002345 sk->sk_allocation = GFP_NOIO;
Chuck Lever3167e122005-08-25 16:25:55 -07002346
2347 /* socket options */
Chuck Lever3167e122005-08-25 16:25:55 -07002348 sock_reset_flag(sk, SOCK_LINGER);
Chuck Lever3167e122005-08-25 16:25:55 -07002349 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002350
2351 xprt_clear_connected(xprt);
2352
2353 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002354 transport->sock = sock;
2355 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002356
2357 write_unlock_bh(&sk->sk_callback_lock);
2358 }
2359
Trond Myklebust01d37c42009-03-11 14:09:39 -04002360 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002361 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002362
Mel Gormana564b8f2012-07-31 16:45:12 -07002363 xs_set_memalloc(xprt);
2364
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002365 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05002366 xprt->stat.connect_count++;
2367 xprt->stat.connect_start = jiffies;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04002368 set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002369 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2370 switch (ret) {
2371 case 0:
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002372 xs_set_srcport(transport, sock);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002373 /* fall through */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002374 case -EINPROGRESS:
2375 /* SYN_SENT! */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002376 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2377 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust1f4c17a2016-08-01 13:36:08 -04002378 break;
2379 case -EADDRNOTAVAIL:
2380 /* Source port number is unavailable. Try a new one! */
2381 transport->srcport = 0;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002382 }
2383out:
2384 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002385}
2386
2387/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002388 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
Chuck Lever16be2d22007-08-06 11:57:38 -04002389 *
2390 * Invoked by a work queue tasklet.
2391 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002392static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002393{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002394 struct sock_xprt *transport =
2395 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002396 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002397 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002398 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002399
Chuck Lever16be2d22007-08-06 11:57:38 -04002400 if (!sock) {
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002401 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002402 xs_addr(xprt)->sa_family, SOCK_STREAM,
2403 IPPROTO_TCP, true);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002404 if (IS_ERR(sock)) {
2405 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002406 goto out;
2407 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002408 }
2409
Chuck Leverc740eff2009-08-09 15:09:46 -04002410 dprintk("RPC: worker connecting xprt %p via %s to "
2411 "%s (port %s)\n", xprt,
2412 xprt->address_strings[RPC_DISPLAY_PROTO],
2413 xprt->address_strings[RPC_DISPLAY_ADDR],
2414 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002415
2416 status = xs_tcp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002417 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever46121cf2007-01-31 12:14:08 -05002418 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2419 xprt, -status, xprt_connected(xprt),
2420 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002421 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002422 default:
2423 printk("%s: connect returned unhandled error %d\n",
2424 __func__, status);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002425 /* fall through */
Trond Myklebustf75e6742009-04-21 17:18:20 -04002426 case -EADDRNOTAVAIL:
2427 /* We're probably in TIME_WAIT. Get rid of existing socket,
2428 * and retry
2429 */
Trond Myklebusta519fc72012-09-12 16:49:15 -04002430 xs_tcp_force_close(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002431 break;
Trond Myklebust2a491992009-03-11 14:38:00 -04002432 case 0:
2433 case -EINPROGRESS:
2434 case -EALREADY:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002435 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002436 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002437 case -EINVAL:
2438 /* Happens, for instance, if the user specified a link
2439 * local IPv6 address without a scope-id.
2440 */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002441 case -ECONNREFUSED:
2442 case -ECONNRESET:
2443 case -ENETUNREACH:
Trond Myklebust3913c782015-02-08 21:44:04 -05002444 case -EADDRINUSE:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002445 case -ENOBUFS:
NeilBrown6ea44ad2017-05-25 17:00:32 +10002446 /*
2447 * xs_tcp_force_close() wakes tasks with -EIO.
2448 * We need to wake them first to ensure the
2449 * correct error code.
2450 */
2451 xprt_wake_pending_tasks(xprt, status);
Trond Myklebust4efdd922015-02-08 15:34:28 -05002452 xs_tcp_force_close(xprt);
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002453 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002454 }
Trond Myklebust2a491992009-03-11 14:38:00 -04002455 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002456out:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002457 xprt_unlock_connect(xprt, transport);
Chuck Lever2226feb2005-08-11 16:25:38 -04002458 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002459 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04002460}
2461
Trond Myklebust02910172016-08-04 00:00:33 -04002462static unsigned long xs_reconnect_delay(const struct rpc_xprt *xprt)
2463{
2464 unsigned long start, now = jiffies;
2465
2466 start = xprt->stat.connect_start + xprt->reestablish_timeout;
2467 if (time_after(start, now))
2468 return start - now;
2469 return 0;
2470}
2471
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002472static void xs_reconnect_backoff(struct rpc_xprt *xprt)
2473{
2474 xprt->reestablish_timeout <<= 1;
2475 if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
2476 xprt->reestablish_timeout = xprt->max_reconnect_timeout;
2477 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2478 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2479}
2480
Chuck Lever68e220b2007-08-06 11:57:48 -04002481/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002482 * xs_connect - connect a socket to a remote endpoint
Trond Myklebust1b092092013-01-08 09:26:49 -05002483 * @xprt: pointer to transport structure
Chuck Lever9903cd12005-08-11 16:25:26 -04002484 * @task: address of RPC task that manages state of connect request
2485 *
2486 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002487 *
2488 * UDP socket connects are synchronous, but we use a work queue anyway
2489 * to guarantee that even unprivileged user processes can set up a
2490 * socket on a privileged port.
2491 *
2492 * If a UDP socket connect fails, the delay behavior here prevents
2493 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002494 */
Trond Myklebust1b092092013-01-08 09:26:49 -05002495static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002496{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002497 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002498 unsigned long delay = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002499
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002500 WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2501
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002502 if (transport->sock != NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002503 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2504 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002505 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002506
2507 /* Start by resetting any existing state */
2508 xs_reset_transport(transport);
2509
Trond Myklebust02910172016-08-04 00:00:33 -04002510 delay = xs_reconnect_delay(xprt);
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002511 xs_reconnect_backoff(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002512
Trond Myklebust02910172016-08-04 00:00:33 -04002513 } else
Chuck Lever46121cf2007-01-31 12:14:08 -05002514 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002515
2516 queue_delayed_work(xprtiod_workqueue,
2517 &transport->connect_worker,
2518 delay);
Chuck Levera246b012005-08-11 16:25:23 -04002519}
2520
Chuck Lever262ca072006-03-20 13:44:16 -05002521/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002522 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2523 * @xprt: rpc_xprt struct containing statistics
2524 * @seq: output file
2525 *
2526 */
2527static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2528{
2529 long idle_time = 0;
2530
2531 if (xprt_connected(xprt))
2532 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2533
2534 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
Andy Adamson15a45202012-02-14 16:19:18 -05002535 "%llu %llu %lu %llu %llu\n",
Chuck Lever176e21e2011-05-09 15:22:44 -04002536 xprt->stat.bind_count,
2537 xprt->stat.connect_count,
2538 xprt->stat.connect_time,
2539 idle_time,
2540 xprt->stat.sends,
2541 xprt->stat.recvs,
2542 xprt->stat.bad_xids,
2543 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002544 xprt->stat.bklog_u,
2545 xprt->stat.max_slots,
2546 xprt->stat.sending_u,
2547 xprt->stat.pending_u);
Chuck Lever176e21e2011-05-09 15:22:44 -04002548}
2549
2550/**
Chuck Lever262ca072006-03-20 13:44:16 -05002551 * xs_udp_print_stats - display UDP socket-specifc stats
2552 * @xprt: rpc_xprt struct containing statistics
2553 * @seq: output file
2554 *
2555 */
2556static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2557{
Chuck Leverc8475462006-12-05 16:35:26 -05002558 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2559
Andy Adamson15a45202012-02-14 16:19:18 -05002560 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2561 "%lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002562 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002563 xprt->stat.bind_count,
2564 xprt->stat.sends,
2565 xprt->stat.recvs,
2566 xprt->stat.bad_xids,
2567 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002568 xprt->stat.bklog_u,
2569 xprt->stat.max_slots,
2570 xprt->stat.sending_u,
2571 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002572}
2573
2574/**
2575 * xs_tcp_print_stats - display TCP socket-specifc stats
2576 * @xprt: rpc_xprt struct containing statistics
2577 * @seq: output file
2578 *
2579 */
2580static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2581{
Chuck Leverc8475462006-12-05 16:35:26 -05002582 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002583 long idle_time = 0;
2584
2585 if (xprt_connected(xprt))
2586 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2587
Andy Adamson15a45202012-02-14 16:19:18 -05002588 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2589 "%llu %llu %lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002590 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002591 xprt->stat.bind_count,
2592 xprt->stat.connect_count,
2593 xprt->stat.connect_time,
2594 idle_time,
2595 xprt->stat.sends,
2596 xprt->stat.recvs,
2597 xprt->stat.bad_xids,
2598 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002599 xprt->stat.bklog_u,
2600 xprt->stat.max_slots,
2601 xprt->stat.sending_u,
2602 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002603}
2604
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002605/*
2606 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2607 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2608 * to use the server side send routines.
2609 */
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002610static int bc_malloc(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002611{
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002612 struct rpc_rqst *rqst = task->tk_rqstp;
2613 size_t size = rqst->rq_callsize;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002614 struct page *page;
2615 struct rpc_buffer *buf;
2616
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002617 if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
2618 WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
2619 size);
2620 return -EINVAL;
2621 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002622
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002623 page = alloc_page(GFP_KERNEL);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002624 if (!page)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002625 return -ENOMEM;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002626
2627 buf = page_address(page);
2628 buf->len = PAGE_SIZE;
2629
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002630 rqst->rq_buffer = buf->data;
Jeff Layton18e601d2016-10-24 20:33:23 -04002631 rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002632 return 0;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002633}
2634
2635/*
2636 * Free the space allocated in the bc_alloc routine
2637 */
Chuck Lever3435c742016-09-15 10:55:29 -04002638static void bc_free(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002639{
Chuck Lever3435c742016-09-15 10:55:29 -04002640 void *buffer = task->tk_rqstp->rq_buffer;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002641 struct rpc_buffer *buf;
2642
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002643 buf = container_of(buffer, struct rpc_buffer, data);
2644 free_page((unsigned long)buf);
2645}
2646
2647/*
2648 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2649 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2650 */
2651static int bc_sendto(struct rpc_rqst *req)
2652{
2653 int len;
2654 struct xdr_buf *xbufp = &req->rq_snd_buf;
2655 struct rpc_xprt *xprt = req->rq_xprt;
2656 struct sock_xprt *transport =
2657 container_of(xprt, struct sock_xprt, xprt);
2658 struct socket *sock = transport->sock;
2659 unsigned long headoff;
2660 unsigned long tailoff;
2661
Chuck Lever61677ee2011-05-09 15:22:34 -04002662 xs_encode_stream_record_marker(xbufp);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002663
2664 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2665 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2666 len = svc_send_common(sock, xbufp,
2667 virt_to_page(xbufp->head[0].iov_base), headoff,
2668 xbufp->tail[0].iov_base, tailoff);
2669
2670 if (len != xbufp->len) {
2671 printk(KERN_NOTICE "Error sending entire callback!\n");
2672 len = -EAGAIN;
2673 }
2674
2675 return len;
2676}
2677
2678/*
2679 * The send routine. Borrows from svc_send
2680 */
2681static int bc_send_request(struct rpc_task *task)
2682{
2683 struct rpc_rqst *req = task->tk_rqstp;
2684 struct svc_xprt *xprt;
Andrzej Hajda7fc56132015-09-24 16:00:09 +02002685 int len;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002686
2687 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2688 /*
2689 * Get the server socket associated with this callback xprt
2690 */
2691 xprt = req->rq_xprt->bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002692
2693 /*
2694 * Grab the mutex to serialize data as the connection is shared
2695 * with the fore channel
2696 */
2697 if (!mutex_trylock(&xprt->xpt_mutex)) {
2698 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2699 if (!mutex_trylock(&xprt->xpt_mutex))
2700 return -EAGAIN;
2701 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2702 }
2703 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2704 len = -ENOTCONN;
2705 else
2706 len = bc_sendto(req);
2707 mutex_unlock(&xprt->xpt_mutex);
2708
2709 if (len > 0)
2710 len = 0;
2711
2712 return len;
2713}
2714
2715/*
2716 * The close routine. Since this is client initiated, we do nothing
2717 */
2718
2719static void bc_close(struct rpc_xprt *xprt)
2720{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002721}
2722
2723/*
2724 * The xprt destroy routine. Again, because this connection is client
2725 * initiated, we do nothing
2726 */
2727
2728static void bc_destroy(struct rpc_xprt *xprt)
2729{
Kinglong Mee47f72ef2014-03-24 11:58:16 +08002730 dprintk("RPC: bc_destroy xprt %p\n", xprt);
2731
2732 xs_xprt_free(xprt);
2733 module_put(THIS_MODULE);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002734}
2735
Chuck Leverd31ae252017-08-01 12:00:39 -04002736static const struct rpc_xprt_ops xs_local_ops = {
Chuck Lever176e21e2011-05-09 15:22:44 -04002737 .reserve_xprt = xprt_reserve_xprt,
2738 .release_xprt = xs_tcp_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002739 .alloc_slot = xprt_alloc_slot,
Chuck Lever176e21e2011-05-09 15:22:44 -04002740 .rpcbind = xs_local_rpcbind,
2741 .set_port = xs_local_set_port,
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002742 .connect = xs_local_connect,
Chuck Lever176e21e2011-05-09 15:22:44 -04002743 .buf_alloc = rpc_malloc,
2744 .buf_free = rpc_free,
2745 .send_request = xs_local_send_request,
2746 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2747 .close = xs_close,
Trond Myklebusta1311d82013-10-31 09:18:49 -04002748 .destroy = xs_destroy,
Chuck Lever176e21e2011-05-09 15:22:44 -04002749 .print_stats = xs_local_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002750 .enable_swap = xs_enable_swap,
2751 .disable_swap = xs_disable_swap,
Chuck Lever176e21e2011-05-09 15:22:44 -04002752};
2753
Chuck Leverd31ae252017-08-01 12:00:39 -04002754static const struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002755 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002756 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002757 .release_xprt = xprt_release_xprt_cong,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002758 .alloc_slot = xprt_alloc_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002759 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002760 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002761 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002762 .buf_alloc = rpc_malloc,
2763 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002764 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002765 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002766 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002767 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002768 .close = xs_close,
2769 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002770 .print_stats = xs_udp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002771 .enable_swap = xs_enable_swap,
2772 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002773 .inject_disconnect = xs_inject_disconnect,
Chuck Lever262965f2005-08-11 16:25:56 -04002774};
2775
Chuck Leverd31ae252017-08-01 12:00:39 -04002776static const struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002777 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002778 .release_xprt = xs_tcp_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002779 .alloc_slot = xprt_lock_and_alloc_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002780 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002781 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002782 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002783 .buf_alloc = rpc_malloc,
2784 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002785 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002786 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustc627d312015-02-10 11:06:04 -05002787 .close = xs_tcp_shutdown,
Chuck Lever9903cd12005-08-11 16:25:26 -04002788 .destroy = xs_destroy,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002789 .set_connect_timeout = xs_tcp_set_connect_timeout,
Chuck Lever262ca072006-03-20 13:44:16 -05002790 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002791 .enable_swap = xs_enable_swap,
2792 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002793 .inject_disconnect = xs_inject_disconnect,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002794#ifdef CONFIG_SUNRPC_BACKCHANNEL
2795 .bc_setup = xprt_setup_bc,
Chuck Lever76566772015-10-24 17:28:32 -04002796 .bc_up = xs_tcp_bc_up,
Chuck Lever6b26cc82016-05-02 14:40:40 -04002797 .bc_maxpayload = xs_tcp_bc_maxpayload,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002798 .bc_free_rqst = xprt_free_bc_rqst,
2799 .bc_destroy = xprt_destroy_bc,
2800#endif
Chuck Levera246b012005-08-11 16:25:23 -04002801};
2802
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002803/*
2804 * The rpc_xprt_ops for the server backchannel
2805 */
2806
Chuck Leverd31ae252017-08-01 12:00:39 -04002807static const struct rpc_xprt_ops bc_tcp_ops = {
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002808 .reserve_xprt = xprt_reserve_xprt,
2809 .release_xprt = xprt_release_xprt,
Bryan Schumaker84e28a32012-09-24 13:39:01 -04002810 .alloc_slot = xprt_alloc_slot,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002811 .buf_alloc = bc_malloc,
2812 .buf_free = bc_free,
2813 .send_request = bc_send_request,
2814 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2815 .close = bc_close,
2816 .destroy = bc_destroy,
2817 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002818 .enable_swap = xs_enable_swap,
2819 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002820 .inject_disconnect = xs_inject_disconnect,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002821};
2822
Chuck Lever92476852010-10-20 11:53:01 -04002823static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2824{
2825 static const struct sockaddr_in sin = {
2826 .sin_family = AF_INET,
2827 .sin_addr.s_addr = htonl(INADDR_ANY),
2828 };
2829 static const struct sockaddr_in6 sin6 = {
2830 .sin6_family = AF_INET6,
2831 .sin6_addr = IN6ADDR_ANY_INIT,
2832 };
2833
2834 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002835 case AF_LOCAL:
2836 break;
Chuck Lever92476852010-10-20 11:53:01 -04002837 case AF_INET:
2838 memcpy(sap, &sin, sizeof(sin));
2839 break;
2840 case AF_INET6:
2841 memcpy(sap, &sin6, sizeof(sin6));
2842 break;
2843 default:
2844 dprintk("RPC: %s: Bad address family\n", __func__);
2845 return -EAFNOSUPPORT;
2846 }
2847 return 0;
2848}
2849
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002850static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002851 unsigned int slot_table_size,
2852 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002853{
2854 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002855 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002856
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002857 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002858 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002859 return ERR_PTR(-EBADF);
2860 }
2861
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002862 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2863 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002864 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002865 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2866 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002867 return ERR_PTR(-ENOMEM);
2868 }
2869
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002870 new = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002871 mutex_init(&new->recv_mutex);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002872 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2873 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002874 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002875 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002876 else {
2877 int err;
2878 err = xs_init_anyaddr(args->dstaddr->sa_family,
2879 (struct sockaddr *)&new->srcaddr);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002880 if (err != 0) {
2881 xprt_free(xprt);
Chuck Lever92476852010-10-20 11:53:01 -04002882 return ERR_PTR(err);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002883 }
Chuck Lever92476852010-10-20 11:53:01 -04002884 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002885
2886 return xprt;
2887}
2888
Chuck Lever176e21e2011-05-09 15:22:44 -04002889static const struct rpc_timeout xs_local_default_timeout = {
2890 .to_initval = 10 * HZ,
2891 .to_maxval = 10 * HZ,
2892 .to_retries = 2,
2893};
2894
2895/**
2896 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2897 * @args: rpc transport creation arguments
2898 *
2899 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2900 */
2901static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2902{
2903 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2904 struct sock_xprt *transport;
2905 struct rpc_xprt *xprt;
2906 struct rpc_xprt *ret;
2907
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002908 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2909 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002910 if (IS_ERR(xprt))
2911 return xprt;
2912 transport = container_of(xprt, struct sock_xprt, xprt);
2913
2914 xprt->prot = 0;
2915 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2916 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2917
2918 xprt->bind_timeout = XS_BIND_TO;
2919 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2920 xprt->idle_timeout = XS_IDLE_DISC_TO;
2921
2922 xprt->ops = &xs_local_ops;
2923 xprt->timeout = &xs_local_default_timeout;
2924
Trond Myklebusta2648092015-10-06 17:03:00 -04002925 INIT_WORK(&transport->recv_worker, xs_local_data_receive_workfn);
NeilBrown93dc41b2013-10-31 16:14:36 +11002926 INIT_DELAYED_WORK(&transport->connect_worker,
2927 xs_dummy_setup_socket);
2928
Chuck Lever176e21e2011-05-09 15:22:44 -04002929 switch (sun->sun_family) {
2930 case AF_LOCAL:
2931 if (sun->sun_path[0] != '/') {
2932 dprintk("RPC: bad AF_LOCAL address: %s\n",
2933 sun->sun_path);
2934 ret = ERR_PTR(-EINVAL);
2935 goto out_err;
2936 }
2937 xprt_set_bound(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002938 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
J. Bruce Fields7073ea82013-02-21 10:14:22 -05002939 ret = ERR_PTR(xs_local_setup_socket(transport));
2940 if (ret)
2941 goto out_err;
Chuck Lever176e21e2011-05-09 15:22:44 -04002942 break;
2943 default:
2944 ret = ERR_PTR(-EAFNOSUPPORT);
2945 goto out_err;
2946 }
2947
2948 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2949 xprt->address_strings[RPC_DISPLAY_ADDR]);
2950
2951 if (try_module_get(THIS_MODULE))
2952 return xprt;
2953 ret = ERR_PTR(-EINVAL);
2954out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002955 xs_xprt_free(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002956 return ret;
2957}
2958
Trond Myklebust2881ae72007-12-20 16:03:54 -05002959static const struct rpc_timeout xs_udp_default_timeout = {
2960 .to_initval = 5 * HZ,
2961 .to_maxval = 30 * HZ,
2962 .to_increment = 5 * HZ,
2963 .to_retries = 5,
2964};
2965
Chuck Lever9903cd12005-08-11 16:25:26 -04002966/**
2967 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002968 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002969 *
2970 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002971static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002972{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002973 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002974 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002975 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002976 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002977
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002978 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2979 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002980 if (IS_ERR(xprt))
2981 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002982 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002983
Chuck Leverec739ef2006-08-22 20:06:15 -04002984 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002985 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002986 /* XXX: header size can vary due to auth type, IPv6, etc. */
2987 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2988
Chuck Lever03bf4b72005-08-25 16:25:55 -07002989 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002990 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2991 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002992
Chuck Lever262965f2005-08-11 16:25:56 -04002993 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002994
Trond Myklebustba7392b2007-12-20 16:03:55 -05002995 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002996
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002997 INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002998 INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2999
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003000 switch (addr->sa_family) {
3001 case AF_INET:
3002 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
3003 xprt_set_bound(xprt);
3004
Chuck Lever9dc3b092009-08-09 15:09:46 -04003005 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003006 break;
3007 case AF_INET6:
3008 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
3009 xprt_set_bound(xprt);
3010
Chuck Lever9dc3b092009-08-09 15:09:46 -04003011 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003012 break;
3013 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003014 ret = ERR_PTR(-EAFNOSUPPORT);
3015 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003016 }
3017
Chuck Leverc740eff2009-08-09 15:09:46 -04003018 if (xprt_bound(xprt))
3019 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3020 xprt->address_strings[RPC_DISPLAY_ADDR],
3021 xprt->address_strings[RPC_DISPLAY_PORT],
3022 xprt->address_strings[RPC_DISPLAY_PROTO]);
3023 else
3024 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
3025 xprt->address_strings[RPC_DISPLAY_ADDR],
3026 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04003027
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003028 if (try_module_get(THIS_MODULE))
3029 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003030 ret = ERR_PTR(-EINVAL);
3031out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003032 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003033 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04003034}
3035
Trond Myklebust2881ae72007-12-20 16:03:54 -05003036static const struct rpc_timeout xs_tcp_default_timeout = {
3037 .to_initval = 60 * HZ,
3038 .to_maxval = 60 * HZ,
3039 .to_retries = 2,
3040};
3041
Chuck Lever9903cd12005-08-11 16:25:26 -04003042/**
3043 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02003044 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04003045 *
3046 */
Adrian Bunk483066d2007-10-24 18:24:02 +02003047static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04003048{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003049 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04003050 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05003051 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003052 struct rpc_xprt *ret;
Trond Myklebustb7993ce2013-04-14 11:42:00 -04003053 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
3054
3055 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
3056 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Levera246b012005-08-11 16:25:23 -04003057
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003058 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
Trond Myklebustb7993ce2013-04-14 11:42:00 -04003059 max_slot_table_size);
Chuck Leverc8541ec2006-10-17 14:44:27 -04003060 if (IS_ERR(xprt))
3061 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05003062 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04003063
Chuck Leverec739ef2006-08-22 20:06:15 -04003064 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07003065 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07003066 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04003067
Chuck Lever03bf4b72005-08-25 16:25:55 -07003068 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07003069 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
3070 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04003071
Chuck Lever262965f2005-08-11 16:25:56 -04003072 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05003073 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04003074
Trond Myklebust3851f1c2016-08-04 00:08:45 -04003075 xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05003076 xprt->connect_timeout = xprt->timeout->to_initval *
3077 (xprt->timeout->to_retries + 1);
Trond Myklebust3851f1c2016-08-04 00:08:45 -04003078
Trond Myklebustedc1b012015-10-05 10:53:49 -04003079 INIT_WORK(&transport->recv_worker, xs_tcp_data_receive_workfn);
3080 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
3081
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003082 switch (addr->sa_family) {
3083 case AF_INET:
3084 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
3085 xprt_set_bound(xprt);
3086
Chuck Lever9dc3b092009-08-09 15:09:46 -04003087 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003088 break;
3089 case AF_INET6:
3090 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
3091 xprt_set_bound(xprt);
3092
Chuck Lever9dc3b092009-08-09 15:09:46 -04003093 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003094 break;
3095 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003096 ret = ERR_PTR(-EAFNOSUPPORT);
3097 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003098 }
3099
Chuck Leverc740eff2009-08-09 15:09:46 -04003100 if (xprt_bound(xprt))
3101 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3102 xprt->address_strings[RPC_DISPLAY_ADDR],
3103 xprt->address_strings[RPC_DISPLAY_PORT],
3104 xprt->address_strings[RPC_DISPLAY_PROTO]);
3105 else
3106 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
3107 xprt->address_strings[RPC_DISPLAY_ADDR],
3108 xprt->address_strings[RPC_DISPLAY_PROTO]);
3109
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003110 if (try_module_get(THIS_MODULE))
3111 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003112 ret = ERR_PTR(-EINVAL);
3113out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003114 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003115 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04003116}
Chuck Lever282b32e2006-12-05 16:35:51 -05003117
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003118/**
3119 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3120 * @args: rpc transport creation arguments
3121 *
3122 */
3123static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
3124{
3125 struct sockaddr *addr = args->dstaddr;
3126 struct rpc_xprt *xprt;
3127 struct sock_xprt *transport;
3128 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003129 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003130
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003131 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3132 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003133 if (IS_ERR(xprt))
3134 return xprt;
3135 transport = container_of(xprt, struct sock_xprt, xprt);
3136
3137 xprt->prot = IPPROTO_TCP;
3138 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
3139 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3140 xprt->timeout = &xs_tcp_default_timeout;
3141
3142 /* backchannel */
3143 xprt_set_bound(xprt);
3144 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003145 xprt->reestablish_timeout = 0;
3146 xprt->idle_timeout = 0;
3147
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003148 xprt->ops = &bc_tcp_ops;
3149
3150 switch (addr->sa_family) {
3151 case AF_INET:
3152 xs_format_peer_addresses(xprt, "tcp",
3153 RPCBIND_NETID_TCP);
3154 break;
3155 case AF_INET6:
3156 xs_format_peer_addresses(xprt, "tcp",
3157 RPCBIND_NETID_TCP6);
3158 break;
3159 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003160 ret = ERR_PTR(-EAFNOSUPPORT);
3161 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003162 }
3163
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04003164 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3165 xprt->address_strings[RPC_DISPLAY_ADDR],
3166 xprt->address_strings[RPC_DISPLAY_PORT],
3167 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003168
3169 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003170 * Once we've associated a backchannel xprt with a connection,
Weng Meiling28303ca2013-11-30 17:56:44 +08003171 * we want to keep it around as long as the connection lasts,
3172 * in case we need to start using it for a backchannel again;
3173 * this reference won't be dropped until bc_xprt is destroyed.
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003174 */
3175 xprt_get(xprt);
3176 args->bc_xprt->xpt_bc_xprt = xprt;
3177 xprt->bc_xprt = args->bc_xprt;
3178 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
3179 transport->sock = bc_sock->sk_sock;
3180 transport->inet = bc_sock->sk_sk;
3181
3182 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003183 * Since we don't want connections for the backchannel, we set
3184 * the xprt status to connected
3185 */
3186 xprt_set_connected(xprt);
3187
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003188 if (try_module_get(THIS_MODULE))
3189 return xprt;
Kinglong Mee642aab52014-03-24 12:00:28 +08003190
3191 args->bc_xprt->xpt_bc_xprt = NULL;
J. Bruce Fields39a9bea2016-05-17 12:38:21 -04003192 args->bc_xprt->xpt_bc_xps = NULL;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003193 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003194 ret = ERR_PTR(-EINVAL);
3195out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003196 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003197 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003198}
3199
Chuck Lever176e21e2011-05-09 15:22:44 -04003200static struct xprt_class xs_local_transport = {
3201 .list = LIST_HEAD_INIT(xs_local_transport.list),
3202 .name = "named UNIX socket",
3203 .owner = THIS_MODULE,
3204 .ident = XPRT_TRANSPORT_LOCAL,
3205 .setup = xs_setup_local,
3206};
3207
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003208static struct xprt_class xs_udp_transport = {
3209 .list = LIST_HEAD_INIT(xs_udp_transport.list),
3210 .name = "udp",
3211 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003212 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003213 .setup = xs_setup_udp,
3214};
3215
3216static struct xprt_class xs_tcp_transport = {
3217 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
3218 .name = "tcp",
3219 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003220 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003221 .setup = xs_setup_tcp,
3222};
3223
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003224static struct xprt_class xs_bc_tcp_transport = {
3225 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3226 .name = "tcp NFSv4.1 backchannel",
3227 .owner = THIS_MODULE,
3228 .ident = XPRT_TRANSPORT_BC_TCP,
3229 .setup = xs_setup_bc_tcp,
3230};
3231
Chuck Lever282b32e2006-12-05 16:35:51 -05003232/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003233 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05003234 *
3235 */
3236int init_socket_xprt(void)
3237{
Jeff Laytonf895b252014-11-17 16:58:04 -05003238#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08003239 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08003240 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05003241#endif
3242
Chuck Lever176e21e2011-05-09 15:22:44 -04003243 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003244 xprt_register_transport(&xs_udp_transport);
3245 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003246 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003247
Chuck Lever282b32e2006-12-05 16:35:51 -05003248 return 0;
3249}
3250
3251/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003252 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05003253 *
3254 */
3255void cleanup_socket_xprt(void)
3256{
Jeff Laytonf895b252014-11-17 16:58:04 -05003257#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Leverfbf76682006-12-05 16:35:54 -05003258 if (sunrpc_table_header) {
3259 unregister_sysctl_table(sunrpc_table_header);
3260 sunrpc_table_header = NULL;
3261 }
3262#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003263
Chuck Lever176e21e2011-05-09 15:22:44 -04003264 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003265 xprt_unregister_transport(&xs_udp_transport);
3266 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003267 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05003268}
Trond Myklebustcbf11072009-08-09 15:06:19 -04003269
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003270static int param_set_uint_minmax(const char *val,
3271 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003272 unsigned int min, unsigned int max)
3273{
Daniel Walter00cfaa92014-06-21 13:06:38 +01003274 unsigned int num;
Trond Myklebustcbf11072009-08-09 15:06:19 -04003275 int ret;
3276
3277 if (!val)
3278 return -EINVAL;
Daniel Walter00cfaa92014-06-21 13:06:38 +01003279 ret = kstrtouint(val, 0, &num);
Dan Carpenter9761a242017-02-19 00:34:59 +03003280 if (ret)
3281 return ret;
3282 if (num < min || num > max)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003283 return -EINVAL;
3284 *((unsigned int *)kp->arg) = num;
3285 return 0;
3286}
3287
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003288static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003289{
Frank Sorensonffb6ca32016-07-08 16:35:25 -05003290 if (kp->arg == &xprt_min_resvport)
3291 return param_set_uint_minmax(val, kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003292 RPC_MIN_RESVPORT,
Frank Sorensonffb6ca32016-07-08 16:35:25 -05003293 xprt_max_resvport);
3294 return param_set_uint_minmax(val, kp,
3295 xprt_min_resvport,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003296 RPC_MAX_RESVPORT);
3297}
3298
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303299static const struct kernel_param_ops param_ops_portnr = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003300 .set = param_set_portnr,
3301 .get = param_get_uint,
3302};
3303
Trond Myklebustcbf11072009-08-09 15:06:19 -04003304#define param_check_portnr(name, p) \
3305 __param_check(name, p, unsigned int);
3306
3307module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3308module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3309
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003310static int param_set_slot_table_size(const char *val,
3311 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003312{
3313 return param_set_uint_minmax(val, kp,
3314 RPC_MIN_SLOT_TABLE,
3315 RPC_MAX_SLOT_TABLE);
3316}
3317
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303318static const struct kernel_param_ops param_ops_slot_table_size = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003319 .set = param_set_slot_table_size,
3320 .get = param_get_uint,
3321};
3322
Trond Myklebustcbf11072009-08-09 15:06:19 -04003323#define param_check_slot_table_size(name, p) \
3324 __param_check(name, p, unsigned int);
3325
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003326static int param_set_max_slot_table_size(const char *val,
3327 const struct kernel_param *kp)
3328{
3329 return param_set_uint_minmax(val, kp,
3330 RPC_MIN_SLOT_TABLE,
3331 RPC_MAX_SLOT_TABLE_LIMIT);
3332}
3333
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303334static const struct kernel_param_ops param_ops_max_slot_table_size = {
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003335 .set = param_set_max_slot_table_size,
3336 .get = param_get_uint,
3337};
3338
3339#define param_check_max_slot_table_size(name, p) \
3340 __param_check(name, p, unsigned int);
3341
Trond Myklebustcbf11072009-08-09 15:06:19 -04003342module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3343 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003344module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3345 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003346module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3347 slot_table_size, 0644);
3348