blob: a3aadc04808fb0c842754d5648b598a6df15e67e [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>
Trond Myklebust277e4ab2018-09-14 09:49:06 -040050#include <linux/bvec.h>
Trond Myklebust6a829eb2019-01-03 09:04:45 -050051#include <linux/highmem.h>
Trond Myklebust277e4ab2018-09-14 09:49:06 -040052#include <linux/uio.h>
Trond Myklebusta1231fd2019-02-18 10:02:29 -050053#include <linux/sched/mm.h>
Chuck Levera246b012005-08-11 16:25:23 -040054
Trond Myklebust40b5ea02013-09-04 12:16:23 -040055#include <trace/events/sunrpc.h>
56
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030057#include "sunrpc.h"
Chuck Lever176e21e2011-05-09 15:22:44 -040058
59static void xs_close(struct rpc_xprt *xprt);
Trond Myklebust7196dbb2017-02-08 11:17:54 -050060static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
61 struct socket *sock);
Chuck Lever176e21e2011-05-09 15:22:44 -040062
Chuck Lever9903cd12005-08-11 16:25:26 -040063/*
Chuck Leverc556b752005-11-01 12:24:48 -050064 * xprtsock tunables
65 */
Trond Myklebust09acfea2012-03-11 15:22:54 -040066static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
67static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
68static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
Chuck Leverc556b752005-11-01 12:24:48 -050069
Trond Myklebust09acfea2012-03-11 15:22:54 -040070static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
71static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
Chuck Leverc556b752005-11-01 12:24:48 -050072
Trond Myklebust7d1e8252009-03-11 14:38:03 -040073#define XS_TCP_LINGER_TO (15U * HZ)
Trond Myklebust25fe6142009-03-11 14:38:03 -040074static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
Trond Myklebust7d1e8252009-03-11 14:38:03 -040075
Chuck Leverc556b752005-11-01 12:24:48 -050076/*
Chuck Leverfbf76682006-12-05 16:35:54 -050077 * We can register our own files under /proc/sys/sunrpc by
78 * calling register_sysctl_table() again. The files in that
79 * directory become the union of all files registered there.
80 *
81 * We simply need to make sure that we don't collide with
82 * someone else's file names!
83 */
84
Chuck Leverfbf76682006-12-05 16:35:54 -050085static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
86static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
Trond Myklebustd9ba1312011-07-17 18:11:30 -040087static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Leverfbf76682006-12-05 16:35:54 -050088static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
89static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
90
91static struct ctl_table_header *sunrpc_table_header;
92
93/*
94 * FIXME: changing the UDP slot table size should also resize the UDP
95 * socket buffers for existing UDP transports
96 */
Joe Perchesfe2c6332013-06-11 23:04:25 -070097static struct ctl_table xs_tunables_table[] = {
Chuck Leverfbf76682006-12-05 16:35:54 -050098 {
Chuck Leverfbf76682006-12-05 16:35:54 -050099 .procname = "udp_slot_table_entries",
100 .data = &xprt_udp_slot_table_entries,
101 .maxlen = sizeof(unsigned int),
102 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800103 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500104 .extra1 = &min_slot_table_size,
105 .extra2 = &max_slot_table_size
106 },
107 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500108 .procname = "tcp_slot_table_entries",
109 .data = &xprt_tcp_slot_table_entries,
110 .maxlen = sizeof(unsigned int),
111 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800112 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500113 .extra1 = &min_slot_table_size,
114 .extra2 = &max_slot_table_size
115 },
116 {
Trond Myklebustd9ba1312011-07-17 18:11:30 -0400117 .procname = "tcp_max_slot_table_entries",
118 .data = &xprt_max_tcp_slot_table_entries,
119 .maxlen = sizeof(unsigned int),
120 .mode = 0644,
121 .proc_handler = proc_dointvec_minmax,
122 .extra1 = &min_slot_table_size,
123 .extra2 = &max_tcp_slot_table_limit
124 },
125 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500126 .procname = "min_resvport",
127 .data = &xprt_min_resvport,
128 .maxlen = sizeof(unsigned int),
129 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800130 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500131 .extra1 = &xprt_min_resvport_limit,
J. Bruce Fields826799e2018-10-18 15:27:02 -0400132 .extra2 = &xprt_max_resvport_limit
Chuck Leverfbf76682006-12-05 16:35:54 -0500133 },
134 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500135 .procname = "max_resvport",
136 .data = &xprt_max_resvport,
137 .maxlen = sizeof(unsigned int),
138 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800139 .proc_handler = proc_dointvec_minmax,
J. Bruce Fields826799e2018-10-18 15:27:02 -0400140 .extra1 = &xprt_min_resvport_limit,
Chuck Leverfbf76682006-12-05 16:35:54 -0500141 .extra2 = &xprt_max_resvport_limit
142 },
143 {
Trond Myklebust25fe6142009-03-11 14:38:03 -0400144 .procname = "tcp_fin_timeout",
145 .data = &xs_tcp_fin_timeout,
146 .maxlen = sizeof(xs_tcp_fin_timeout),
147 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800148 .proc_handler = proc_dointvec_jiffies,
Trond Myklebust25fe6142009-03-11 14:38:03 -0400149 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800150 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500151};
152
Joe Perchesfe2c6332013-06-11 23:04:25 -0700153static struct ctl_table sunrpc_table[] = {
Chuck Leverfbf76682006-12-05 16:35:54 -0500154 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500155 .procname = "sunrpc",
156 .mode = 0555,
157 .child = xs_tunables_table
158 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800159 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500160};
161
Chuck Leverfbf76682006-12-05 16:35:54 -0500162/*
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
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400326static size_t
327xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp)
328{
329 size_t i,n;
330
Trond Myklebust16e5e902018-12-02 15:22:48 -0500331 if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES))
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400332 return want;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400333 n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT;
334 for (i = 0; i < n; i++) {
335 if (buf->pages[i])
336 continue;
337 buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp);
338 if (!buf->pages[i]) {
Trond Myklebust16e5e902018-12-02 15:22:48 -0500339 i *= PAGE_SIZE;
340 return i > buf->page_base ? i - buf->page_base : 0;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400341 }
342 }
343 return want;
344}
345
346static ssize_t
347xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek)
348{
349 ssize_t ret;
350 if (seek != 0)
351 iov_iter_advance(&msg->msg_iter, seek);
352 ret = sock_recvmsg(sock, msg, flags);
353 return ret > 0 ? ret + seek : ret;
354}
355
356static ssize_t
357xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags,
358 struct kvec *kvec, size_t count, size_t seek)
359{
Al Viro0e9b4a82018-11-01 18:19:03 -0400360 iov_iter_kvec(&msg->msg_iter, READ, kvec, 1, count);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400361 return xs_sock_recvmsg(sock, msg, flags, seek);
362}
363
364static ssize_t
365xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags,
366 struct bio_vec *bvec, unsigned long nr, size_t count,
367 size_t seek)
368{
Al Viro0e9b4a82018-11-01 18:19:03 -0400369 iov_iter_bvec(&msg->msg_iter, READ, bvec, nr, count);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400370 return xs_sock_recvmsg(sock, msg, flags, seek);
371}
372
373static ssize_t
374xs_read_discard(struct socket *sock, struct msghdr *msg, int flags,
375 size_t count)
376{
Trond Myklebustb76a5af2018-12-03 15:58:58 -0500377 iov_iter_discard(&msg->msg_iter, READ, count);
378 return sock_recvmsg(sock, msg, flags);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400379}
380
Trond Myklebust6a829eb2019-01-03 09:04:45 -0500381#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
382static void
383xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
384{
385 struct bvec_iter bi = {
386 .bi_size = count,
387 };
388 struct bio_vec bv;
389
390 bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
391 for_each_bvec(bv, bvec, bi, bi)
392 flush_dcache_page(bv.bv_page);
393}
394#else
395static inline void
396xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
397{
398}
399#endif
400
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400401static ssize_t
402xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags,
403 struct xdr_buf *buf, size_t count, size_t seek, size_t *read)
404{
405 size_t want, seek_init = seek, offset = 0;
406 ssize_t ret;
407
Trond Myklebuste92053a2019-02-15 18:14:27 -0500408 want = min_t(size_t, count, buf->head[0].iov_len);
409 if (seek < want) {
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400410 ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek);
411 if (ret <= 0)
412 goto sock_err;
413 offset += ret;
414 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
415 goto out;
416 if (ret != want)
Trond Myklebustc4433052018-12-04 07:52:11 -0500417 goto out;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400418 seek = 0;
419 } else {
Trond Myklebuste92053a2019-02-15 18:14:27 -0500420 seek -= want;
421 offset += want;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400422 }
Trond Myklebust16e5e902018-12-02 15:22:48 -0500423
424 want = xs_alloc_sparse_pages(buf,
425 min_t(size_t, count - offset, buf->page_len),
426 GFP_NOWAIT);
427 if (seek < want) {
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400428 ret = xs_read_bvec(sock, msg, flags, buf->bvec,
429 xdr_buf_pagecount(buf),
430 want + buf->page_base,
431 seek + buf->page_base);
432 if (ret <= 0)
433 goto sock_err;
Trond Myklebust6a829eb2019-01-03 09:04:45 -0500434 xs_flush_bvec(buf->bvec, ret, seek + buf->page_base);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400435 offset += ret - buf->page_base;
436 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
437 goto out;
438 if (ret != want)
Trond Myklebustc4433052018-12-04 07:52:11 -0500439 goto out;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400440 seek = 0;
441 } else {
Trond Myklebust16e5e902018-12-02 15:22:48 -0500442 seek -= want;
443 offset += want;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400444 }
Trond Myklebust16e5e902018-12-02 15:22:48 -0500445
Trond Myklebuste92053a2019-02-15 18:14:27 -0500446 want = min_t(size_t, count - offset, buf->tail[0].iov_len);
447 if (seek < want) {
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400448 ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek);
449 if (ret <= 0)
450 goto sock_err;
451 offset += ret;
452 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
453 goto out;
454 if (ret != want)
Trond Myklebustc4433052018-12-04 07:52:11 -0500455 goto out;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400456 } else
Trond Myklebuste92053a2019-02-15 18:14:27 -0500457 offset = seek_init;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400458 ret = -EMSGSIZE;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400459out:
460 *read = offset - seek_init;
461 return ret;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400462sock_err:
463 offset += seek;
464 goto out;
465}
466
467static void
468xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf)
469{
470 if (!transport->recv.copied) {
471 if (buf->head[0].iov_len >= transport->recv.offset)
472 memcpy(buf->head[0].iov_base,
473 &transport->recv.xid,
474 transport->recv.offset);
475 transport->recv.copied = transport->recv.offset;
476 }
477}
478
479static bool
480xs_read_stream_request_done(struct sock_xprt *transport)
481{
482 return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT);
483}
484
Trond Myklebuste92053a2019-02-15 18:14:27 -0500485static void
486xs_read_stream_check_eor(struct sock_xprt *transport,
487 struct msghdr *msg)
488{
489 if (xs_read_stream_request_done(transport))
490 msg->msg_flags |= MSG_EOR;
491}
492
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400493static ssize_t
494xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg,
495 int flags, struct rpc_rqst *req)
496{
497 struct xdr_buf *buf = &req->rq_private_buf;
498 size_t want, read;
499 ssize_t ret;
500
501 xs_read_header(transport, buf);
502
503 want = transport->recv.len - transport->recv.offset;
Trond Myklebuste92053a2019-02-15 18:14:27 -0500504 if (want != 0) {
505 ret = xs_read_xdr_buf(transport->sock, msg, flags, buf,
506 transport->recv.copied + want,
507 transport->recv.copied,
508 &read);
509 transport->recv.offset += read;
510 transport->recv.copied += read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400511 }
512
Trond Myklebust727fcc62019-02-15 16:53:04 -0500513 if (transport->recv.offset == transport->recv.len)
514 xs_read_stream_check_eor(transport, msg);
515
Trond Myklebuste92053a2019-02-15 18:14:27 -0500516 if (want == 0)
517 return 0;
518
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400519 switch (ret) {
Trond Myklebustc4433052018-12-04 07:52:11 -0500520 default:
521 break;
Trond Myklebust26781ea2018-12-03 15:41:38 -0500522 case -EFAULT:
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400523 case -EMSGSIZE:
Trond Myklebust26781ea2018-12-03 15:41:38 -0500524 msg->msg_flags |= MSG_TRUNC;
Trond Myklebustc4433052018-12-04 07:52:11 -0500525 return read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400526 case 0:
527 return -ESHUTDOWN;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400528 }
Trond Myklebustc4433052018-12-04 07:52:11 -0500529 return ret < 0 ? ret : read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400530}
531
532static size_t
533xs_read_stream_headersize(bool isfrag)
534{
535 if (isfrag)
536 return sizeof(__be32);
537 return 3 * sizeof(__be32);
538}
539
540static ssize_t
541xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg,
542 int flags, size_t want, size_t seek)
543{
544 struct kvec kvec = {
545 .iov_base = &transport->recv.fraghdr,
546 .iov_len = want,
547 };
548 return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek);
549}
550
551#if defined(CONFIG_SUNRPC_BACKCHANNEL)
552static ssize_t
553xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
554{
555 struct rpc_xprt *xprt = &transport->xprt;
556 struct rpc_rqst *req;
557 ssize_t ret;
558
559 /* Look up and lock the request corresponding to the given XID */
560 req = xprt_lookup_bc_request(xprt, transport->recv.xid);
561 if (!req) {
562 printk(KERN_WARNING "Callback slot table overflowed\n");
563 return -ESHUTDOWN;
564 }
565
566 ret = xs_read_stream_request(transport, msg, flags, req);
567 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
Trond Myklebustc4433052018-12-04 07:52:11 -0500568 xprt_complete_bc_request(req, transport->recv.copied);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400569
570 return ret;
571}
572#else /* CONFIG_SUNRPC_BACKCHANNEL */
573static ssize_t
574xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
575{
576 return -ESHUTDOWN;
577}
578#endif /* CONFIG_SUNRPC_BACKCHANNEL */
579
580static ssize_t
581xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags)
582{
583 struct rpc_xprt *xprt = &transport->xprt;
584 struct rpc_rqst *req;
585 ssize_t ret = 0;
586
587 /* Look up and lock the request corresponding to the given XID */
588 spin_lock(&xprt->queue_lock);
589 req = xprt_lookup_rqst(xprt, transport->recv.xid);
590 if (!req) {
591 msg->msg_flags |= MSG_TRUNC;
592 goto out;
593 }
594 xprt_pin_rqst(req);
595 spin_unlock(&xprt->queue_lock);
596
597 ret = xs_read_stream_request(transport, msg, flags, req);
598
599 spin_lock(&xprt->queue_lock);
600 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
Trond Myklebustc4433052018-12-04 07:52:11 -0500601 xprt_complete_rqst(req->rq_task, transport->recv.copied);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400602 xprt_unpin_rqst(req);
603out:
604 spin_unlock(&xprt->queue_lock);
605 return ret;
606}
607
608static ssize_t
609xs_read_stream(struct sock_xprt *transport, int flags)
610{
611 struct msghdr msg = { 0 };
612 size_t want, read = 0;
613 ssize_t ret = 0;
614
615 if (transport->recv.len == 0) {
616 want = xs_read_stream_headersize(transport->recv.copied != 0);
617 ret = xs_read_stream_header(transport, &msg, flags, want,
618 transport->recv.offset);
619 if (ret <= 0)
620 goto out_err;
621 transport->recv.offset = ret;
Trond Myklebustc4433052018-12-04 07:52:11 -0500622 if (transport->recv.offset != want)
623 return transport->recv.offset;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400624 transport->recv.len = be32_to_cpu(transport->recv.fraghdr) &
625 RPC_FRAGMENT_SIZE_MASK;
626 transport->recv.offset -= sizeof(transport->recv.fraghdr);
627 read = ret;
628 }
629
630 switch (be32_to_cpu(transport->recv.calldir)) {
Trond Myklebustc4433052018-12-04 07:52:11 -0500631 default:
632 msg.msg_flags |= MSG_TRUNC;
633 break;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400634 case RPC_CALL:
635 ret = xs_read_stream_call(transport, &msg, flags);
636 break;
637 case RPC_REPLY:
638 ret = xs_read_stream_reply(transport, &msg, flags);
639 }
640 if (msg.msg_flags & MSG_TRUNC) {
641 transport->recv.calldir = cpu_to_be32(-1);
642 transport->recv.copied = -1;
643 }
644 if (ret < 0)
645 goto out_err;
646 read += ret;
647 if (transport->recv.offset < transport->recv.len) {
Trond Myklebustc4433052018-12-04 07:52:11 -0500648 if (!(msg.msg_flags & MSG_TRUNC))
649 return read;
Trond Myklebustb76a5af2018-12-03 15:58:58 -0500650 msg.msg_flags = 0;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400651 ret = xs_read_discard(transport->sock, &msg, flags,
652 transport->recv.len - transport->recv.offset);
653 if (ret <= 0)
654 goto out_err;
655 transport->recv.offset += ret;
656 read += ret;
657 if (transport->recv.offset != transport->recv.len)
Trond Myklebustc4433052018-12-04 07:52:11 -0500658 return read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400659 }
660 if (xs_read_stream_request_done(transport)) {
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400661 trace_xs_stream_read_request(transport);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400662 transport->recv.copied = 0;
663 }
664 transport->recv.offset = 0;
665 transport->recv.len = 0;
666 return read;
667out_err:
Trond Myklebust794628572018-12-03 18:49:00 -0500668 return ret != 0 ? ret : -ESHUTDOWN;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400669}
670
Trond Myklebust0ffe86f2019-01-30 14:51:26 -0500671static __poll_t xs_poll_socket(struct sock_xprt *transport)
672{
673 return transport->sock->ops->poll(NULL, transport->sock, NULL);
674}
675
676static bool xs_poll_socket_readable(struct sock_xprt *transport)
677{
678 __poll_t events = xs_poll_socket(transport);
679
680 return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP);
681}
682
683static void xs_poll_check_readable(struct sock_xprt *transport)
684{
685
686 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
687 if (!xs_poll_socket_readable(transport))
688 return;
689 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
690 queue_work(xprtiod_workqueue, &transport->recv_worker);
691}
692
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400693static void xs_stream_data_receive(struct sock_xprt *transport)
694{
695 size_t read = 0;
696 ssize_t ret = 0;
697
698 mutex_lock(&transport->recv_mutex);
699 if (transport->sock == NULL)
700 goto out;
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400701 for (;;) {
702 ret = xs_read_stream(transport, MSG_DONTWAIT);
Trond Myklebustc4433052018-12-04 07:52:11 -0500703 if (ret < 0)
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400704 break;
705 read += ret;
706 cond_resched();
707 }
Trond Myklebust5f52a9d2019-02-16 09:31:47 -0500708 if (ret == -ESHUTDOWN)
709 kernel_sock_shutdown(transport->sock, SHUT_RDWR);
710 else
711 xs_poll_check_readable(transport);
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400712out:
713 mutex_unlock(&transport->recv_mutex);
714 trace_xs_stream_read_data(&transport->xprt, ret, read);
715}
716
717static void xs_stream_data_receive_workfn(struct work_struct *work)
718{
719 struct sock_xprt *transport =
720 container_of(work, struct sock_xprt, recv_worker);
Trond Myklebusta1231fd2019-02-18 10:02:29 -0500721 unsigned int pflags = memalloc_nofs_save();
722
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400723 xs_stream_data_receive(transport);
Trond Myklebusta1231fd2019-02-18 10:02:29 -0500724 memalloc_nofs_restore(pflags);
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400725}
726
Trond Myklebust550aebf2018-09-14 14:32:45 -0400727static void
728xs_stream_reset_connect(struct sock_xprt *transport)
729{
730 transport->recv.offset = 0;
731 transport->recv.len = 0;
732 transport->recv.copied = 0;
733 transport->xmit.offset = 0;
Trond Myklebustae053552019-02-20 14:56:20 -0500734}
735
736static void
737xs_stream_start_connect(struct sock_xprt *transport)
738{
Trond Myklebust550aebf2018-09-14 14:32:45 -0400739 transport->xprt.stat.connect_count++;
740 transport->xprt.stat.connect_start = jiffies;
741}
742
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400743#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
744
Trond Myklebust24c56842006-10-17 15:06:22 -0400745static 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 -0400746{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400747 struct msghdr msg = {
748 .msg_name = addr,
749 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400750 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
751 };
752 struct kvec iov = {
753 .iov_base = vec->iov_base + base,
754 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400755 };
756
Trond Myklebust24c56842006-10-17 15:06:22 -0400757 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400758 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
759 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
760}
761
Jason Baronf279cd02014-09-24 18:08:00 +0000762static 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 -0400763{
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500764 ssize_t (*do_sendpage)(struct socket *sock, struct page *page,
765 int offset, size_t size, int flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400766 struct page **ppage;
767 unsigned int remainder;
Jason Baronf279cd02014-09-24 18:08:00 +0000768 int err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400769
Trond Myklebust24c56842006-10-17 15:06:22 -0400770 remainder = xdr->page_len - base;
771 base += xdr->page_base;
772 ppage = xdr->pages + (base >> PAGE_SHIFT);
773 base &= ~PAGE_MASK;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500774 do_sendpage = sock->ops->sendpage;
775 if (!zerocopy)
776 do_sendpage = sock_no_sendpage;
Trond Myklebust24c56842006-10-17 15:06:22 -0400777 for(;;) {
778 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
779 int flags = XS_SENDMSG_FLAGS;
780
781 remainder -= len;
Trond Myklebust31303d62015-10-06 15:59:20 -0400782 if (more)
Trond Myklebust24c56842006-10-17 15:06:22 -0400783 flags |= MSG_MORE;
Trond Myklebust31303d62015-10-06 15:59:20 -0400784 if (remainder != 0)
785 flags |= MSG_SENDPAGE_NOTLAST | MSG_MORE;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500786 err = do_sendpage(sock, *ppage, base, len, flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400787 if (remainder == 0 || err != len)
788 break;
Jason Baronf279cd02014-09-24 18:08:00 +0000789 *sent_p += err;
Trond Myklebust24c56842006-10-17 15:06:22 -0400790 ppage++;
791 base = 0;
792 }
Jason Baronf279cd02014-09-24 18:08:00 +0000793 if (err > 0) {
794 *sent_p += err;
795 err = 0;
796 }
797 return err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400798}
799
Chuck Lever9903cd12005-08-11 16:25:26 -0400800/**
801 * xs_sendpages - write pages directly to a socket
802 * @sock: socket to send on
803 * @addr: UDP only -- address of destination
804 * @addrlen: UDP only -- length of destination address
805 * @xdr: buffer containing this request
806 * @base: starting position in the buffer
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500807 * @zerocopy: true if it is safe to use sendpage()
Jason Baronf279cd02014-09-24 18:08:00 +0000808 * @sent_p: return the total number of bytes successfully queued for sending
Chuck Lever9903cd12005-08-11 16:25:26 -0400809 *
Chuck Levera246b012005-08-11 16:25:23 -0400810 */
Jason Baronf279cd02014-09-24 18:08:00 +0000811static 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 -0400812{
Trond Myklebust24c56842006-10-17 15:06:22 -0400813 unsigned int remainder = xdr->len - base;
Jason Baronf279cd02014-09-24 18:08:00 +0000814 int err = 0;
815 int sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400816
Chuck Lever262965f2005-08-11 16:25:56 -0400817 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400818 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400819
Trond Myklebust24c56842006-10-17 15:06:22 -0400820 if (base != 0) {
821 addr = NULL;
822 addrlen = 0;
823 }
Chuck Lever262965f2005-08-11 16:25:56 -0400824
Trond Myklebust24c56842006-10-17 15:06:22 -0400825 if (base < xdr->head[0].iov_len || addr != NULL) {
826 unsigned int len = xdr->head[0].iov_len - base;
827 remainder -= len;
828 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
829 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400830 goto out;
Jason Baronf279cd02014-09-24 18:08:00 +0000831 *sent_p += err;
Chuck Levera246b012005-08-11 16:25:23 -0400832 base = 0;
833 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400834 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400835
Trond Myklebust24c56842006-10-17 15:06:22 -0400836 if (base < xdr->page_len) {
837 unsigned int len = xdr->page_len - base;
838 remainder -= len;
Jason Baronf279cd02014-09-24 18:08:00 +0000839 err = xs_send_pagedata(sock, xdr, base, remainder != 0, zerocopy, &sent);
840 *sent_p += sent;
841 if (remainder == 0 || sent != len)
Chuck Levera246b012005-08-11 16:25:23 -0400842 goto out;
843 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400844 } else
845 base -= xdr->page_len;
846
847 if (base >= xdr->tail[0].iov_len)
Jason Baronf279cd02014-09-24 18:08:00 +0000848 return 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400849 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400850out:
Jason Baronf279cd02014-09-24 18:08:00 +0000851 if (err > 0) {
852 *sent_p += err;
853 err = 0;
854 }
855 return err;
Chuck Levera246b012005-08-11 16:25:23 -0400856}
857
Chuck Lever9903cd12005-08-11 16:25:26 -0400858/**
Trond Myklebustc5445772018-09-03 23:39:27 -0400859 * xs_nospace - handle transmit was incomplete
Trond Myklebust50f484e2018-08-30 13:27:29 -0400860 * @req: pointer to RPC request
Chuck Lever9903cd12005-08-11 16:25:26 -0400861 *
Chuck Levera246b012005-08-11 16:25:23 -0400862 */
Trond Myklebustc5445772018-09-03 23:39:27 -0400863static int xs_nospace(struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -0400864{
Chuck Lever262965f2005-08-11 16:25:56 -0400865 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500866 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500867 struct sock *sk = transport->inet;
Trond Myklebust24ca9a82011-11-22 14:44:28 +0200868 int ret = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400869
Chuck Lever46121cf2007-01-31 12:14:08 -0500870 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Trond Myklebustc5445772018-09-03 23:39:27 -0400871 req->rq_task->tk_pid,
872 req->rq_slen - transport->xmit.offset,
Chuck Lever262965f2005-08-11 16:25:56 -0400873 req->rq_slen);
874
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400875 /* Protect against races with write_space */
876 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400877
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400878 /* Don't race with disconnect */
879 if (xprt_connected(xprt)) {
Trond Myklebust13331a52016-01-06 08:57:06 -0500880 /* wait for more buffer space */
881 sk->sk_write_pending++;
Trond Myklebustc5445772018-09-03 23:39:27 -0400882 xprt_wait_for_buffer_space(xprt);
Trond Myklebust13331a52016-01-06 08:57:06 -0500883 } else
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400884 ret = -ENOTCONN;
Chuck Lever262965f2005-08-11 16:25:56 -0400885
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400886 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500887
888 /* Race breaker in case memory is freed before above code is called */
David Vrabeld48f9ce2016-09-19 13:58:30 +0100889 if (ret == -EAGAIN) {
890 struct socket_wq *wq;
891
892 rcu_read_lock();
893 wq = rcu_dereference(sk->sk_wq);
894 set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags);
895 rcu_read_unlock();
896
897 sk->sk_write_space(sk);
898 }
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400899 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400900}
901
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400902static void
903xs_stream_prepare_request(struct rpc_rqst *req)
904{
905 req->rq_task->tk_status = xdr_alloc_bvec(&req->rq_rcv_buf, GFP_NOIO);
906}
907
Chuck Lever61677ee2011-05-09 15:22:34 -0400908/*
Trond Myklebust4cd34e72018-08-31 10:00:02 -0400909 * Determine if the previous message in the stream was aborted before it
910 * could complete transmission.
911 */
912static bool
913xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
914{
915 return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
916}
917
918/*
Chuck Lever61677ee2011-05-09 15:22:34 -0400919 * Construct a stream transport record marker in @buf.
920 */
921static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
922{
923 u32 reclen = buf->len - sizeof(rpc_fraghdr);
924 rpc_fraghdr *base = buf->head[0].iov_base;
925 *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
926}
927
Chuck Lever262965f2005-08-11 16:25:56 -0400928/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400929 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
Trond Myklebust50f484e2018-08-30 13:27:29 -0400930 * @req: pointer to RPC request
Chuck Lever176e21e2011-05-09 15:22:44 -0400931 *
932 * Return values:
933 * 0: The request has been sent
934 * EAGAIN: The socket was blocked, please call again later to
935 * complete the request
936 * ENOTCONN: Caller needs to invoke connect logic then call again
937 * other: Some other error occured, the request was not sent
938 */
Trond Myklebustadfa7142018-09-03 23:58:59 -0400939static int xs_local_send_request(struct rpc_rqst *req)
Chuck Lever176e21e2011-05-09 15:22:44 -0400940{
Chuck Lever176e21e2011-05-09 15:22:44 -0400941 struct rpc_xprt *xprt = req->rq_xprt;
942 struct sock_xprt *transport =
943 container_of(xprt, struct sock_xprt, xprt);
944 struct xdr_buf *xdr = &req->rq_snd_buf;
945 int status;
Jason Baronf279cd02014-09-24 18:08:00 +0000946 int sent = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -0400947
Trond Myklebust4cd34e72018-08-31 10:00:02 -0400948 /* Close the stream if the previous transmission was incomplete */
949 if (xs_send_request_was_aborted(transport, req)) {
950 xs_close(xprt);
951 return -ENOTCONN;
952 }
953
Chuck Lever176e21e2011-05-09 15:22:44 -0400954 xs_encode_stream_record_marker(&req->rq_snd_buf);
955
956 xs_pktdump("packet data:",
957 req->rq_svec->iov_base, req->rq_svec->iov_len);
958
Chuck Lever78215752018-03-05 15:13:07 -0500959 req->rq_xtime = ktime_get();
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400960 status = xs_sendpages(transport->sock, NULL, 0, xdr,
961 transport->xmit.offset,
Jason Baronf279cd02014-09-24 18:08:00 +0000962 true, &sent);
Chuck Lever176e21e2011-05-09 15:22:44 -0400963 dprintk("RPC: %s(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400964 __func__, xdr->len - transport->xmit.offset, status);
NeilBrown743c69e2015-07-27 10:55:35 +1000965
966 if (status == -EAGAIN && sock_writeable(transport->inet))
967 status = -ENOBUFS;
968
Jason Baronf279cd02014-09-24 18:08:00 +0000969 if (likely(sent > 0) || status == 0) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400970 transport->xmit.offset += sent;
971 req->rq_bytes_sent = transport->xmit.offset;
Chuck Lever176e21e2011-05-09 15:22:44 -0400972 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400973 req->rq_xmit_bytes_sent += transport->xmit.offset;
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400974 transport->xmit.offset = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -0400975 return 0;
976 }
977 status = -EAGAIN;
978 }
979
980 switch (status) {
Trond Myklebust3601c4a2014-06-30 13:42:19 -0400981 case -ENOBUFS:
Trond Myklebustb5872f02015-07-03 09:32:23 -0400982 break;
Chuck Lever176e21e2011-05-09 15:22:44 -0400983 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -0400984 status = xs_nospace(req);
Chuck Lever176e21e2011-05-09 15:22:44 -0400985 break;
986 default:
987 dprintk("RPC: sendmsg returned unrecognized error %d\n",
988 -status);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -0500989 /* fall through */
Chuck Lever176e21e2011-05-09 15:22:44 -0400990 case -EPIPE:
991 xs_close(xprt);
992 status = -ENOTCONN;
993 }
994
995 return status;
996}
997
998/**
Chuck Lever262965f2005-08-11 16:25:56 -0400999 * xs_udp_send_request - write an RPC request to a UDP socket
Trond Myklebust50f484e2018-08-30 13:27:29 -04001000 * @req: pointer to RPC request
Chuck Lever262965f2005-08-11 16:25:56 -04001001 *
1002 * Return values:
1003 * 0: The request has been sent
1004 * EAGAIN: The socket was blocked, please call again later to
1005 * complete the request
1006 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001007 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -04001008 */
Trond Myklebustadfa7142018-09-03 23:58:59 -04001009static int xs_udp_send_request(struct rpc_rqst *req)
Chuck Lever262965f2005-08-11 16:25:56 -04001010{
Chuck Lever262965f2005-08-11 16:25:56 -04001011 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001012 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -04001013 struct xdr_buf *xdr = &req->rq_snd_buf;
Jason Baronf279cd02014-09-24 18:08:00 +00001014 int sent = 0;
Chuck Lever262965f2005-08-11 16:25:56 -04001015 int status;
Chuck Levera246b012005-08-11 16:25:23 -04001016
Chuck Lever9903cd12005-08-11 16:25:26 -04001017 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -04001018 req->rq_svec->iov_base,
1019 req->rq_svec->iov_len);
1020
Trond Myklebust01d37c42009-03-11 14:09:39 -04001021 if (!xprt_bound(xprt))
1022 return -ENOTCONN;
Trond Myklebust75891f52018-09-03 17:37:36 -04001023
1024 if (!xprt_request_get_cong(xprt, req))
1025 return -EBADSLT;
1026
Chuck Lever78215752018-03-05 15:13:07 -05001027 req->rq_xtime = ktime_get();
Jason Baronf279cd02014-09-24 18:08:00 +00001028 status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen,
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001029 xdr, 0, true, &sent);
Chuck Levera246b012005-08-11 16:25:23 -04001030
Chuck Lever46121cf2007-01-31 12:14:08 -05001031 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001032 xdr->len, status);
Chuck Levera246b012005-08-11 16:25:23 -04001033
Jason Baron3dedbb52014-09-24 18:08:04 +00001034 /* firewall is blocking us, don't return -EAGAIN or we end up looping */
1035 if (status == -EPERM)
1036 goto process_status;
1037
NeilBrown743c69e2015-07-27 10:55:35 +10001038 if (status == -EAGAIN && sock_writeable(transport->inet))
1039 status = -ENOBUFS;
1040
Jason Baronf279cd02014-09-24 18:08:00 +00001041 if (sent > 0 || status == 0) {
1042 req->rq_xmit_bytes_sent += sent;
1043 if (sent >= req->rq_slen)
Trond Myklebust21997002007-10-01 11:43:37 -04001044 return 0;
1045 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -04001046 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -04001047 }
Chuck Levera246b012005-08-11 16:25:23 -04001048
Jason Baron3dedbb52014-09-24 18:08:04 +00001049process_status:
Chuck Lever262965f2005-08-11 16:25:56 -04001050 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -04001051 case -ENOTSOCK:
1052 status = -ENOTCONN;
1053 /* Should we call xs_close() here? */
1054 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -04001055 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -04001056 status = xs_nospace(req);
Trond Myklebustb6ddf642008-04-17 18:52:19 -04001057 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001058 case -ENETUNREACH:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001059 case -ENOBUFS:
Chuck Lever262965f2005-08-11 16:25:56 -04001060 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -04001061 case -ECONNREFUSED:
Jason Baron3dedbb52014-09-24 18:08:04 +00001062 case -EPERM:
Chuck Levera246b012005-08-11 16:25:23 -04001063 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -04001064 * prompts ECONNREFUSED. */
Trond Myklebust13331a52016-01-06 08:57:06 -05001065 break;
1066 default:
1067 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1068 -status);
Chuck Levera246b012005-08-11 16:25:23 -04001069 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +08001070
Chuck Lever262965f2005-08-11 16:25:56 -04001071 return status;
Chuck Levera246b012005-08-11 16:25:23 -04001072}
1073
Trond Myklebuste06799f2007-11-05 15:44:12 -05001074/**
Chuck Lever262965f2005-08-11 16:25:56 -04001075 * xs_tcp_send_request - write an RPC request to a TCP socket
Trond Myklebust50f484e2018-08-30 13:27:29 -04001076 * @req: pointer to RPC request
Chuck Lever9903cd12005-08-11 16:25:26 -04001077 *
1078 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -04001079 * 0: The request has been sent
1080 * EAGAIN: The socket was blocked, please call again later to
1081 * complete the request
1082 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001083 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -04001084 *
1085 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -04001086 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -04001087 */
Trond Myklebustadfa7142018-09-03 23:58:59 -04001088static int xs_tcp_send_request(struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001089{
Chuck Levera246b012005-08-11 16:25:23 -04001090 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001091 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -04001092 struct xdr_buf *xdr = &req->rq_snd_buf;
Trond Myklebusta6b31d12013-11-08 16:03:50 -05001093 bool zerocopy = true;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001094 bool vm_wait = false;
Chuck Leverb595bb12007-08-06 11:56:42 -04001095 int status;
Jason Baronf279cd02014-09-24 18:08:00 +00001096 int sent;
Chuck Levera246b012005-08-11 16:25:23 -04001097
Trond Myklebust4cd34e72018-08-31 10:00:02 -04001098 /* Close the stream if the previous transmission was incomplete */
1099 if (xs_send_request_was_aborted(transport, req)) {
1100 if (transport->sock != NULL)
1101 kernel_sock_shutdown(transport->sock, SHUT_RDWR);
1102 return -ENOTCONN;
1103 }
1104
Chuck Lever61677ee2011-05-09 15:22:34 -04001105 xs_encode_stream_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -04001106
Chuck Lever262965f2005-08-11 16:25:56 -04001107 xs_pktdump("packet data:",
1108 req->rq_svec->iov_base,
1109 req->rq_svec->iov_len);
Trond Myklebusta6b31d12013-11-08 16:03:50 -05001110 /* Don't use zero copy if this is a resend. If the RPC call
1111 * completes while the socket holds a reference to the pages,
1112 * then we may end up resending corrupted data.
1113 */
Trond Myklebust50f484e2018-08-30 13:27:29 -04001114 if (req->rq_task->tk_flags & RPC_TASK_SENT)
Trond Myklebusta6b31d12013-11-08 16:03:50 -05001115 zerocopy = false;
Chuck Levera246b012005-08-11 16:25:23 -04001116
Trond Myklebust7196dbb2017-02-08 11:17:54 -05001117 if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
1118 xs_tcp_set_socket_timeouts(xprt, transport->sock);
1119
Chuck Levera246b012005-08-11 16:25:23 -04001120 /* Continue transmitting the packet/record. We must be careful
1121 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -04001122 * called sendmsg(). */
Chuck Lever78215752018-03-05 15:13:07 -05001123 req->rq_xtime = ktime_get();
Chuck Levera246b012005-08-11 16:25:23 -04001124 while (1) {
Jason Baronf279cd02014-09-24 18:08:00 +00001125 sent = 0;
1126 status = xs_sendpages(transport->sock, NULL, 0, xdr,
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001127 transport->xmit.offset,
1128 zerocopy, &sent);
Chuck Levera246b012005-08-11 16:25:23 -04001129
Chuck Lever46121cf2007-01-31 12:14:08 -05001130 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001131 xdr->len - transport->xmit.offset, status);
Chuck Lever262965f2005-08-11 16:25:56 -04001132
Chuck Lever262965f2005-08-11 16:25:56 -04001133 /* If we've sent the entire packet, immediately
1134 * reset the count of bytes sent. */
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001135 transport->xmit.offset += sent;
1136 req->rq_bytes_sent = transport->xmit.offset;
Chuck Lever262965f2005-08-11 16:25:56 -04001137 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001138 req->rq_xmit_bytes_sent += transport->xmit.offset;
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001139 transport->xmit.offset = 0;
Chuck Lever262965f2005-08-11 16:25:56 -04001140 return 0;
Chuck Levera246b012005-08-11 16:25:23 -04001141 }
1142
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001143 WARN_ON_ONCE(sent == 0 && status == 0);
1144
1145 if (status == -EAGAIN ) {
1146 /*
1147 * Return EAGAIN if we're sure we're hitting the
1148 * socket send buffer limits.
1149 */
1150 if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
1151 break;
1152 /*
1153 * Did we hit a memory allocation failure?
1154 */
1155 if (sent == 0) {
1156 status = -ENOBUFS;
1157 if (vm_wait)
1158 break;
1159 /* Retry, knowing now that we're below the
1160 * socket send buffer limit
1161 */
1162 vm_wait = true;
1163 }
1164 continue;
1165 }
Trond Myklebustf580dd02015-07-11 17:48:52 +02001166 if (status < 0)
1167 break;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001168 vm_wait = false;
Chuck Levera246b012005-08-11 16:25:23 -04001169 }
1170
Chuck Lever262965f2005-08-11 16:25:56 -04001171 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -04001172 case -ENOTSOCK:
1173 status = -ENOTCONN;
1174 /* Should we call xs_close() here? */
1175 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001176 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -04001177 status = xs_nospace(req);
Chuck Lever262965f2005-08-11 16:25:56 -04001178 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001179 case -ECONNRESET:
Chuck Lever262965f2005-08-11 16:25:56 -04001180 case -ECONNREFUSED:
1181 case -ENOTCONN:
Trond Myklebust3913c782015-02-08 21:44:04 -05001182 case -EADDRINUSE:
Trond Myklebustb5872f02015-07-03 09:32:23 -04001183 case -ENOBUFS:
Trond Myklebustb9d2bb22012-10-23 11:40:02 -04001184 case -EPIPE:
Trond Myklebust13331a52016-01-06 08:57:06 -05001185 break;
1186 default:
1187 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1188 -status);
Chuck Levera246b012005-08-11 16:25:23 -04001189 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +08001190
Chuck Levera246b012005-08-11 16:25:23 -04001191 return status;
1192}
1193
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001194static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1195{
1196 transport->old_data_ready = sk->sk_data_ready;
1197 transport->old_state_change = sk->sk_state_change;
1198 transport->old_write_space = sk->sk_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -05001199 transport->old_error_report = sk->sk_error_report;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001200}
1201
1202static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1203{
1204 sk->sk_data_ready = transport->old_data_ready;
1205 sk->sk_state_change = transport->old_state_change;
1206 sk->sk_write_space = transport->old_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -05001207 sk->sk_error_report = transport->old_error_report;
1208}
1209
Trond Myklebust42d42a52016-05-23 09:24:55 -04001210static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
1211{
1212 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1213
1214 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
1215}
1216
Trond Myklebustb70ae912015-02-09 09:41:32 -05001217static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1218{
1219 smp_mb__before_atomic();
1220 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1221 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001222 xs_sock_reset_state_flags(xprt);
Trond Myklebustb70ae912015-02-09 09:41:32 -05001223 smp_mb__after_atomic();
1224}
1225
Trond Myklebust21180712013-12-31 13:22:59 -05001226/**
1227 * xs_error_report - callback to handle TCP socket state errors
1228 * @sk: socket
1229 *
1230 * Note: we don't call sock_error() since there may be a rpc_task
1231 * using the socket, and so we don't want to clear sk->sk_err.
1232 */
1233static void xs_error_report(struct sock *sk)
1234{
1235 struct rpc_xprt *xprt;
1236 int err;
1237
1238 read_lock_bh(&sk->sk_callback_lock);
1239 if (!(xprt = xprt_from_sock(sk)))
1240 goto out;
1241
1242 err = -sk->sk_err;
1243 if (err == 0)
1244 goto out;
1245 dprintk("RPC: xs_error_report client %p, error=%d...\n",
1246 xprt, -err);
Trond Myklebuste8353c762013-12-31 13:39:22 -05001247 trace_rpc_socket_error(xprt, sk->sk_socket, err);
Trond Myklebust21180712013-12-31 13:22:59 -05001248 xprt_wake_pending_tasks(xprt, err);
1249 out:
1250 read_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001251}
1252
Chuck Leverfe315e762009-03-11 14:10:21 -04001253static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001254{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001255 struct socket *sock = transport->sock;
1256 struct sock *sk = transport->inet;
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001257 struct rpc_xprt *xprt = &transport->xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001258
Chuck Leverfe315e762009-03-11 14:10:21 -04001259 if (sk == NULL)
1260 return;
Chuck Lever9903cd12005-08-11 16:25:26 -04001261
Jeff Layton264d1df2015-06-03 16:14:27 -04001262 if (atomic_read(&transport->xprt.swapper))
1263 sk_clear_memalloc(sk);
1264
Trond Myklebust09939202015-08-29 19:11:21 -07001265 kernel_sock_shutdown(sock, SHUT_RDWR);
1266
Trond Myklebustedc1b012015-10-05 10:53:49 -04001267 mutex_lock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -04001268 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001269 transport->inet = NULL;
1270 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -04001271
Chuck Lever9903cd12005-08-11 16:25:26 -04001272 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001273
1274 xs_restore_old_callbacks(transport, sk);
Trond Myklebust0c787892015-08-29 13:36:30 -07001275 xprt_clear_connected(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001276 write_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001277 xs_sock_reset_connection_flags(xprt);
Trond Myklebustae053552019-02-20 14:56:20 -05001278 /* Reset stream record info */
1279 xs_stream_reset_connect(transport);
Trond Myklebustedc1b012015-10-05 10:53:49 -04001280 mutex_unlock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -04001281
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001282 trace_rpc_socket_close(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001283 sock_release(sock);
Trond Myklebust0445f922018-12-17 13:34:59 -05001284
1285 xprt_disconnect_done(xprt);
Chuck Leverfe315e762009-03-11 14:10:21 -04001286}
1287
1288/**
1289 * xs_close - close a socket
1290 * @xprt: transport
1291 *
1292 * This is used when all requests are complete; ie, no DRC state remains
1293 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -04001294 *
1295 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1296 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e762009-03-11 14:10:21 -04001297 */
1298static void xs_close(struct rpc_xprt *xprt)
1299{
1300 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1301
1302 dprintk("RPC: xs_close xprt %p\n", xprt);
1303
1304 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -04001305 xprt->reestablish_timeout = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001306}
1307
Chuck Lever4a068252015-05-11 14:02:25 -04001308static void xs_inject_disconnect(struct rpc_xprt *xprt)
1309{
1310 dprintk("RPC: injecting transport disconnect on xprt=%p\n",
1311 xprt);
1312 xprt_disconnect_done(xprt);
1313}
1314
Kinglong Mee315f3812014-03-24 11:07:22 +08001315static void xs_xprt_free(struct rpc_xprt *xprt)
1316{
1317 xs_free_peer_addresses(xprt);
1318 xprt_free(xprt);
1319}
1320
Chuck Lever9903cd12005-08-11 16:25:26 -04001321/**
1322 * xs_destroy - prepare to shutdown a transport
1323 * @xprt: doomed transport
1324 *
1325 */
1326static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001327{
Trond Myklebust03c788272015-09-17 10:42:27 -04001328 struct sock_xprt *transport = container_of(xprt,
1329 struct sock_xprt, xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05001330 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001331
Trond Myklebust03c788272015-09-17 10:42:27 -04001332 cancel_delayed_work_sync(&transport->connect_worker);
Trond Myklebusta1311d82013-10-31 09:18:49 -04001333 xs_close(xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04001334 cancel_work_sync(&transport->recv_worker);
Kinglong Mee315f3812014-03-24 11:07:22 +08001335 xs_xprt_free(xprt);
Trond Myklebusta1311d82013-10-31 09:18:49 -04001336 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -04001337}
1338
Chuck Lever9903cd12005-08-11 16:25:26 -04001339/**
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001340 * xs_udp_data_read_skb - receive callback for UDP sockets
1341 * @xprt: transport
1342 * @sk: socket
1343 * @skb: skbuff
Chuck Lever9903cd12005-08-11 16:25:26 -04001344 *
1345 */
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001346static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1347 struct sock *sk,
1348 struct sk_buff *skb)
Chuck Lever9903cd12005-08-11 16:25:26 -04001349{
1350 struct rpc_task *task;
Chuck Levera246b012005-08-11 16:25:23 -04001351 struct rpc_rqst *rovr;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001352 int repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001353 u32 _xid;
1354 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -04001355
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001356 repsize = skb->len;
Chuck Levera246b012005-08-11 16:25:23 -04001357 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001358 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001359 return;
Chuck Levera246b012005-08-11 16:25:23 -04001360 }
1361
1362 /* Copy the XID from the skb... */
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001363 xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
Chuck Levera246b012005-08-11 16:25:23 -04001364 if (xp == NULL)
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001365 return;
Chuck Levera246b012005-08-11 16:25:23 -04001366
1367 /* Look up and lock the request corresponding to the given XID */
Trond Myklebust75c84152018-08-31 10:21:00 -04001368 spin_lock(&xprt->queue_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001369 rovr = xprt_lookup_rqst(xprt, *xp);
1370 if (!rovr)
1371 goto out_unlock;
Trond Myklebust729749b2017-08-13 10:03:59 -04001372 xprt_pin_rqst(rovr);
Chuck Leverecd465e2018-03-05 15:12:57 -05001373 xprt_update_rtt(rovr->rq_task);
Trond Myklebust75c84152018-08-31 10:21:00 -04001374 spin_unlock(&xprt->queue_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001375 task = rovr->rq_task;
1376
Chuck Levera246b012005-08-11 16:25:23 -04001377 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1378 copied = repsize;
1379
1380 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001381 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
Trond Myklebust75c84152018-08-31 10:21:00 -04001382 spin_lock(&xprt->queue_lock);
Trond Myklebust0afa6b42018-02-09 09:39:42 -05001383 __UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
Trond Myklebust729749b2017-08-13 10:03:59 -04001384 goto out_unpin;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001385 }
1386
Chuck Levera246b012005-08-11 16:25:23 -04001387
Trond Myklebust729749b2017-08-13 10:03:59 -04001388 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001389 xprt_adjust_cwnd(xprt, task, copied);
Trond Myklebustce7c2522017-08-16 15:30:35 -04001390 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust75c84152018-08-31 10:21:00 -04001391 spin_lock(&xprt->queue_lock);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001392 xprt_complete_rqst(task, copied);
Trond Myklebust0afa6b42018-02-09 09:39:42 -05001393 __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
Trond Myklebust729749b2017-08-13 10:03:59 -04001394out_unpin:
1395 xprt_unpin_rqst(rovr);
Chuck Levera246b012005-08-11 16:25:23 -04001396 out_unlock:
Trond Myklebust75c84152018-08-31 10:21:00 -04001397 spin_unlock(&xprt->queue_lock);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001398}
1399
1400static void xs_udp_data_receive(struct sock_xprt *transport)
1401{
1402 struct sk_buff *skb;
1403 struct sock *sk;
1404 int err;
1405
1406 mutex_lock(&transport->recv_mutex);
1407 sk = transport->inet;
1408 if (sk == NULL)
1409 goto out;
1410 for (;;) {
Paolo Abeni7c13f972016-11-04 11:28:59 +01001411 skb = skb_recv_udp(sk, 0, 1, &err);
Trond Myklebust4f546142018-09-14 17:45:23 -04001412 if (skb == NULL)
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001413 break;
Trond Myklebust4f546142018-09-14 17:45:23 -04001414 xs_udp_data_read_skb(&transport->xprt, sk, skb);
1415 consume_skb(skb);
1416 cond_resched();
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001417 }
Trond Myklebust0ffe86f2019-01-30 14:51:26 -05001418 xs_poll_check_readable(transport);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001419out:
1420 mutex_unlock(&transport->recv_mutex);
1421}
1422
1423static void xs_udp_data_receive_workfn(struct work_struct *work)
1424{
1425 struct sock_xprt *transport =
1426 container_of(work, struct sock_xprt, recv_worker);
Trond Myklebusta1231fd2019-02-18 10:02:29 -05001427 unsigned int pflags = memalloc_nofs_save();
1428
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001429 xs_udp_data_receive(transport);
Trond Myklebusta1231fd2019-02-18 10:02:29 -05001430 memalloc_nofs_restore(pflags);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001431}
1432
1433/**
1434 * xs_data_ready - "data ready" callback for UDP sockets
1435 * @sk: socket with data to read
1436 *
1437 */
1438static void xs_data_ready(struct sock *sk)
1439{
1440 struct rpc_xprt *xprt;
1441
1442 read_lock_bh(&sk->sk_callback_lock);
1443 dprintk("RPC: xs_data_ready...\n");
1444 xprt = xprt_from_sock(sk);
1445 if (xprt != NULL) {
1446 struct sock_xprt *transport = container_of(xprt,
1447 struct sock_xprt, xprt);
Trond Myklebust5157b952016-05-29 10:13:24 -04001448 transport->old_data_ready(sk);
1449 /* Any data means we had a useful conversation, so
1450 * then we don't need to delay the next reconnect
1451 */
1452 if (xprt->reestablish_timeout)
1453 xprt->reestablish_timeout = 0;
Trond Myklebust42d42a52016-05-23 09:24:55 -04001454 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebust40a5f1b2016-05-27 10:39:50 -04001455 queue_work(xprtiod_workqueue, &transport->recv_worker);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001456 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001457 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001458}
1459
Trond Myklebusta519fc72012-09-12 16:49:15 -04001460/*
1461 * Helper function to force a TCP close if the server is sending
1462 * junk and/or it has put us in CLOSE_WAIT
1463 */
1464static void xs_tcp_force_close(struct rpc_xprt *xprt)
1465{
Trond Myklebusta519fc72012-09-12 16:49:15 -04001466 xprt_force_disconnect(xprt);
1467}
1468
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001469#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Chuck Lever6b26cc82016-05-02 14:40:40 -04001470static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
1471{
1472 return PAGE_SIZE;
1473}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001474#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001475
Chuck Lever9903cd12005-08-11 16:25:26 -04001476/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001477 * xs_tcp_state_change - callback to handle TCP socket state changes
1478 * @sk: socket whose state has changed
1479 *
1480 */
1481static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001482{
Chuck Lever9903cd12005-08-11 16:25:26 -04001483 struct rpc_xprt *xprt;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001484 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001485
Eric Dumazetf064af12010-09-22 12:43:39 +00001486 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001487 if (!(xprt = xprt_from_sock(sk)))
1488 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001489 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001490 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001491 sk->sk_state, xprt_connected(xprt),
1492 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001493 sock_flag(sk, SOCK_ZAPPED),
1494 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001495
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001496 transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001497 trace_rpc_socket_state_change(xprt, sk->sk_socket);
Chuck Levera246b012005-08-11 16:25:23 -04001498 switch (sk->sk_state) {
1499 case TCP_ESTABLISHED:
Eric Dumazetf064af12010-09-22 12:43:39 +00001500 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001501 if (!xprt_test_and_set_connected(xprt)) {
Trond Myklebust8b717982013-09-26 10:18:04 -04001502 xprt->connect_cookie++;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001503 clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
1504 xprt_clear_connecting(xprt);
Chuck Lever51971132006-12-05 16:35:19 -05001505
Chuck Lever3968a8a2018-10-01 14:25:36 -04001506 xprt->stat.connect_count++;
1507 xprt->stat.connect_time += (long)jiffies -
1508 xprt->stat.connect_start;
Trond Myklebust2a491992009-03-11 14:38:00 -04001509 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001510 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001511 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001512 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001513 case TCP_FIN_WAIT1:
1514 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001515 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001516 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001517 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001518 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001519 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001520 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001521 smp_mb__after_atomic();
Chuck Levera246b012005-08-11 16:25:23 -04001522 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001523 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001524 /* The server initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001525 xprt->connect_cookie++;
Trond Myklebustd0bea452012-10-23 11:35:47 -04001526 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebusta519fc72012-09-12 16:49:15 -04001527 xs_tcp_force_close(xprt);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001528 /* fall through */
Trond Myklebust663b8852008-01-01 18:42:12 -05001529 case TCP_CLOSING:
1530 /*
1531 * If the server closed down the connection, make sure that
1532 * we back off before reconnecting
1533 */
1534 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1535 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001536 break;
1537 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001538 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001539 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001540 clear_bit(XPRT_CONNECTED, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001541 smp_mb__after_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001542 break;
1543 case TCP_CLOSE:
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001544 if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
1545 &transport->sock_state))
1546 xprt_clear_connecting(xprt);
Trond Myklebust9b308892018-02-05 10:20:06 -05001547 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust9b308892018-02-05 10:20:06 -05001548 /* Trigger the socket release */
1549 xs_tcp_force_close(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001550 }
1551 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001552 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001553}
1554
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001555static void xs_write_space(struct sock *sk)
1556{
Trond Myklebust13331a52016-01-06 08:57:06 -05001557 struct socket_wq *wq;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001558 struct rpc_xprt *xprt;
1559
Trond Myklebust13331a52016-01-06 08:57:06 -05001560 if (!sk->sk_socket)
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001561 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001562 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001563
1564 if (unlikely(!(xprt = xprt_from_sock(sk))))
1565 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001566 rcu_read_lock();
1567 wq = rcu_dereference(sk->sk_wq);
1568 if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0)
1569 goto out;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001570
Trond Myklebustc5445772018-09-03 23:39:27 -04001571 if (xprt_write_space(xprt))
1572 sk->sk_write_pending--;
Trond Myklebust13331a52016-01-06 08:57:06 -05001573out:
1574 rcu_read_unlock();
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001575}
1576
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001577/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001578 * xs_udp_write_space - callback invoked when socket buffer space
1579 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001580 * @sk: socket whose state has changed
1581 *
Chuck Levera246b012005-08-11 16:25:23 -04001582 * Called when more output buffer space is available for this socket.
1583 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001584 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001585 * with a bunch of small requests.
1586 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001587static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001588{
Eric Dumazetf064af12010-09-22 12:43:39 +00001589 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001590
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001591 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001592 if (sock_writeable(sk))
1593 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001594
Eric Dumazetf064af12010-09-22 12:43:39 +00001595 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001596}
Chuck Levera246b012005-08-11 16:25:23 -04001597
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001598/**
1599 * xs_tcp_write_space - callback invoked when socket buffer space
1600 * becomes available
1601 * @sk: socket whose state has changed
1602 *
1603 * Called when more output buffer space is available for this socket.
1604 * We try not to wake our writers until they can make "significant"
1605 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1606 * with a bunch of small requests.
1607 */
1608static void xs_tcp_write_space(struct sock *sk)
1609{
Eric Dumazetf064af12010-09-22 12:43:39 +00001610 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001611
1612 /* from net/core/stream.c:sk_stream_write_space */
Eric Dumazet64dc6132013-07-22 20:26:31 -07001613 if (sk_stream_is_writeable(sk))
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001614 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001615
Eric Dumazetf064af12010-09-22 12:43:39 +00001616 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001617}
1618
Chuck Lever470056c2005-08-25 16:25:56 -07001619static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001620{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001621 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1622 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001623
Chuck Lever7c6e0662006-12-05 16:35:30 -05001624 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001625 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001626 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001627 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001628 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001629 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001630 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001631 sk->sk_write_space(sk);
1632 }
1633}
1634
Chuck Lever43118c22005-08-25 16:25:49 -07001635/**
Chuck Lever470056c2005-08-25 16:25:56 -07001636 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001637 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001638 * @sndsize: requested size of send buffer, in bytes
1639 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001640 *
Chuck Lever470056c2005-08-25 16:25:56 -07001641 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001642 */
Chuck Lever470056c2005-08-25 16:25:56 -07001643static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001644{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001645 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1646
1647 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001648 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001649 transport->sndsize = sndsize + 1024;
1650 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001651 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001652 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001653
1654 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001655}
1656
Chuck Lever46c0ee82005-08-25 16:25:52 -07001657/**
1658 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
Chuck Leveracf0a392018-12-19 11:00:22 -05001659 * @xprt: controlling transport
Chuck Lever46c0ee82005-08-25 16:25:52 -07001660 * @task: task that timed out
1661 *
1662 * Adjust the congestion window after a retransmit timeout has occurred.
1663 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001664static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever46c0ee82005-08-25 16:25:52 -07001665{
Chuck Leverb977b642017-02-08 17:00:51 -05001666 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001667 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
Chuck Leverb977b642017-02-08 17:00:51 -05001668 spin_unlock_bh(&xprt->transport_lock);
Chuck Lever46c0ee82005-08-25 16:25:52 -07001669}
1670
J. Bruce Fields826799e2018-10-18 15:27:02 -04001671static int xs_get_random_port(void)
Chuck Leverb85d8802006-05-25 01:40:49 -04001672{
J. Bruce Fields826799e2018-10-18 15:27:02 -04001673 unsigned short min = xprt_min_resvport, max = xprt_max_resvport;
1674 unsigned short range;
1675 unsigned short rand;
1676
1677 if (max < min)
1678 return -EADDRINUSE;
1679 range = max - min + 1;
1680 rand = (unsigned short) prandom_u32() % range;
1681 return rand + min;
Chuck Leverb85d8802006-05-25 01:40:49 -04001682}
1683
Chuck Lever92200412006-01-03 09:55:51 +01001684/**
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001685 * xs_set_reuseaddr_port - set the socket's port and address reuse options
1686 * @sock: socket
1687 *
1688 * Note that this function has to be called on all sockets that share the
1689 * same port, and it must be called before binding.
1690 */
1691static void xs_sock_set_reuseport(struct socket *sock)
1692{
Trond Myklebust402e23b2015-02-09 17:20:14 -05001693 int opt = 1;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001694
Trond Myklebust402e23b2015-02-09 17:20:14 -05001695 kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
1696 (char *)&opt, sizeof(opt));
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001697}
1698
1699static unsigned short xs_sock_getport(struct socket *sock)
1700{
1701 struct sockaddr_storage buf;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001702 unsigned short port = 0;
1703
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001704 if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001705 goto out;
1706 switch (buf.ss_family) {
1707 case AF_INET6:
1708 port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
1709 break;
1710 case AF_INET:
1711 port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
1712 }
1713out:
1714 return port;
1715}
1716
1717/**
Chuck Lever92200412006-01-03 09:55:51 +01001718 * xs_set_port - reset the port number in the remote endpoint address
1719 * @xprt: generic transport
1720 * @port: new port number
1721 *
1722 */
1723static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1724{
Chuck Lever46121cf2007-01-31 12:14:08 -05001725 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001726
Chuck Lever9dc3b092009-08-09 15:09:46 -04001727 rpc_set_port(xs_addr(xprt), port);
1728 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001729}
1730
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001731static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
1732{
1733 if (transport->srcport == 0)
1734 transport->srcport = xs_sock_getport(sock);
1735}
1736
J. Bruce Fields826799e2018-10-18 15:27:02 -04001737static int xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001738{
J. Bruce Fields826799e2018-10-18 15:27:02 -04001739 int port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001740
1741 if (port == 0 && transport->xprt.resvport)
1742 port = xs_get_random_port();
1743 return port;
1744}
1745
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001746static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001747{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001748 if (transport->srcport != 0)
1749 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001750 if (!transport->xprt.resvport)
1751 return 0;
1752 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1753 return xprt_max_resvport;
1754 return --port;
1755}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001756static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001757{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001758 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001759 int err, nloop = 0;
J. Bruce Fields826799e2018-10-18 15:27:02 -04001760 int port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001761 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001762
Chris Perl0f7a6222014-09-05 15:40:21 -04001763 /*
1764 * If we are asking for any ephemeral port (i.e. port == 0 &&
1765 * transport->xprt.resvport == 0), don't bind. Let the local
1766 * port selection happen implicitly when the socket is used
1767 * (for example at connect time).
1768 *
1769 * This ensures that we can continue to establish TCP
1770 * connections even when all local ephemeral ports are already
1771 * a part of some TCP connection. This makes no difference
1772 * for UDP sockets, but also doens't harm them.
1773 *
1774 * If we're asking for any reserved port (i.e. port == 0 &&
1775 * transport->xprt.resvport == 1) xs_get_srcport above will
1776 * ensure that port is non-zero and we will bind as needed.
1777 */
J. Bruce Fields826799e2018-10-18 15:27:02 -04001778 if (port <= 0)
1779 return port;
Chris Perl0f7a6222014-09-05 15:40:21 -04001780
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001781 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001782 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001783 rpc_set_port((struct sockaddr *)&myaddr, port);
1784 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1785 transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001786 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001787 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001788 break;
Chuck Levera246b012005-08-11 16:25:23 -04001789 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001790 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001791 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001792 if (port > last)
1793 nloop++;
1794 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001795
Chuck Lever4232e862010-10-20 11:52:51 -04001796 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001797 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1798 &((struct sockaddr_in *)&myaddr)->sin_addr,
1799 port, err ? "failed" : "ok", err);
1800 else
1801 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1802 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1803 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001804 return err;
1805}
1806
Chuck Lever176e21e2011-05-09 15:22:44 -04001807/*
1808 * We don't support autobind on AF_LOCAL sockets
1809 */
1810static void xs_local_rpcbind(struct rpc_task *task)
1811{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001812 xprt_set_bound(task->tk_xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04001813}
1814
1815static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1816{
1817}
Chuck Levera246b012005-08-11 16:25:23 -04001818
Peter Zijlstraed075362006-12-06 20:35:24 -08001819#ifdef CONFIG_DEBUG_LOCK_ALLOC
1820static struct lock_class_key xs_key[2];
1821static struct lock_class_key xs_slock_key[2];
1822
Chuck Lever176e21e2011-05-09 15:22:44 -04001823static inline void xs_reclassify_socketu(struct socket *sock)
1824{
1825 struct sock *sk = sock->sk;
1826
Chuck Lever176e21e2011-05-09 15:22:44 -04001827 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1828 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1829}
1830
Chuck Lever8945ee52007-08-06 11:58:04 -04001831static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001832{
1833 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001834
Chuck Lever8945ee52007-08-06 11:58:04 -04001835 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1836 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1837}
Peter Zijlstraed075362006-12-06 20:35:24 -08001838
Chuck Lever8945ee52007-08-06 11:58:04 -04001839static inline void xs_reclassify_socket6(struct socket *sock)
1840{
1841 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001842
Chuck Lever8945ee52007-08-06 11:58:04 -04001843 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1844 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001845}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001846
1847static inline void xs_reclassify_socket(int family, struct socket *sock)
1848{
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02001849 if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -04001850 return;
1851
Chuck Lever4232e862010-10-20 11:52:51 -04001852 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001853 case AF_LOCAL:
1854 xs_reclassify_socketu(sock);
1855 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001856 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001857 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001858 break;
1859 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001860 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001861 break;
1862 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001863}
Peter Zijlstraed075362006-12-06 20:35:24 -08001864#else
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001865static inline void xs_reclassify_socket(int family, struct socket *sock)
1866{
1867}
Peter Zijlstraed075362006-12-06 20:35:24 -08001868#endif
1869
NeilBrown93dc41b2013-10-31 16:14:36 +11001870static void xs_dummy_setup_socket(struct work_struct *work)
1871{
1872}
1873
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001874static struct socket *xs_create_sock(struct rpc_xprt *xprt,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001875 struct sock_xprt *transport, int family, int type,
1876 int protocol, bool reuseport)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001877{
1878 struct socket *sock;
1879 int err;
1880
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001881 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001882 if (err < 0) {
1883 dprintk("RPC: can't create %d transport socket (%d).\n",
1884 protocol, -err);
1885 goto out;
1886 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001887 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001888
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001889 if (reuseport)
1890 xs_sock_set_reuseport(sock);
1891
Ben Hutchings4cea2882011-02-22 21:54:34 +00001892 err = xs_bind(transport, sock);
1893 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001894 sock_release(sock);
1895 goto out;
1896 }
1897
1898 return sock;
1899out:
1900 return ERR_PTR(err);
1901}
1902
Chuck Lever176e21e2011-05-09 15:22:44 -04001903static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1904 struct socket *sock)
1905{
1906 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1907 xprt);
1908
1909 if (!transport->inet) {
1910 struct sock *sk = sock->sk;
1911
1912 write_lock_bh(&sk->sk_callback_lock);
1913
1914 xs_save_old_callbacks(transport, sk);
1915
1916 sk->sk_user_data = xprt;
Trond Myklebusta2648092015-10-06 17:03:00 -04001917 sk->sk_data_ready = xs_data_ready;
Chuck Lever176e21e2011-05-09 15:22:44 -04001918 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07001919 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05001920 sk->sk_error_report = xs_error_report;
Chuck Lever176e21e2011-05-09 15:22:44 -04001921
1922 xprt_clear_connected(xprt);
1923
1924 /* Reset to new socket */
1925 transport->sock = sock;
1926 transport->inet = sk;
1927
1928 write_unlock_bh(&sk->sk_callback_lock);
1929 }
1930
Trond Myklebustae053552019-02-20 14:56:20 -05001931 xs_stream_start_connect(transport);
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001932
Chuck Lever176e21e2011-05-09 15:22:44 -04001933 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1934}
1935
1936/**
1937 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
Chuck Lever176e21e2011-05-09 15:22:44 -04001938 * @transport: socket transport to connect
Chuck Lever176e21e2011-05-09 15:22:44 -04001939 */
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001940static int xs_local_setup_socket(struct sock_xprt *transport)
Chuck Lever176e21e2011-05-09 15:22:44 -04001941{
Chuck Lever176e21e2011-05-09 15:22:44 -04001942 struct rpc_xprt *xprt = &transport->xprt;
1943 struct socket *sock;
1944 int status = -EIO;
1945
Chuck Lever176e21e2011-05-09 15:22:44 -04001946 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1947 SOCK_STREAM, 0, &sock, 1);
1948 if (status < 0) {
1949 dprintk("RPC: can't create AF_LOCAL "
1950 "transport socket (%d).\n", -status);
1951 goto out;
1952 }
Stefan Hajnoczid1358912015-12-02 14:17:52 +08001953 xs_reclassify_socket(AF_LOCAL, sock);
Chuck Lever176e21e2011-05-09 15:22:44 -04001954
1955 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1956 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1957
1958 status = xs_local_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001959 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever176e21e2011-05-09 15:22:44 -04001960 switch (status) {
1961 case 0:
1962 dprintk("RPC: xprt %p connected to %s\n",
1963 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
Chuck Lever3968a8a2018-10-01 14:25:36 -04001964 xprt->stat.connect_count++;
1965 xprt->stat.connect_time += (long)jiffies -
1966 xprt->stat.connect_start;
Chuck Lever176e21e2011-05-09 15:22:44 -04001967 xprt_set_connected(xprt);
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001968 case -ENOBUFS:
Chuck Lever176e21e2011-05-09 15:22:44 -04001969 break;
1970 case -ENOENT:
1971 dprintk("RPC: xprt %p: socket %s does not exist\n",
1972 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1973 break;
Trond Myklebust4a20a982012-12-15 17:02:29 -05001974 case -ECONNREFUSED:
1975 dprintk("RPC: xprt %p: connection refused for %s\n",
1976 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1977 break;
Chuck Lever176e21e2011-05-09 15:22:44 -04001978 default:
1979 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1980 __func__, -status,
1981 xprt->address_strings[RPC_DISPLAY_ADDR]);
1982 }
1983
1984out:
1985 xprt_clear_connecting(xprt);
1986 xprt_wake_pending_tasks(xprt, status);
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001987 return status;
1988}
1989
Linus Torvaldsb6669732013-02-28 18:02:55 -08001990static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001991{
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001992 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1993 int ret;
1994
1995 if (RPC_IS_ASYNC(task)) {
1996 /*
1997 * We want the AF_LOCAL connect to be resolved in the
1998 * filesystem namespace of the process making the rpc
1999 * call. Thus we connect synchronously.
2000 *
2001 * If we want to support asynchronous AF_LOCAL calls,
2002 * we'll need to figure out how to pass a namespace to
2003 * connect.
2004 */
2005 rpc_exit(task, -ENOTCONN);
2006 return;
2007 }
2008 ret = xs_local_setup_socket(transport);
2009 if (ret && !RPC_IS_SOFTCONN(task))
2010 msleep_interruptible(15000);
Chuck Lever176e21e2011-05-09 15:22:44 -04002011}
2012
Jeff Layton3c87ef62015-06-03 16:14:25 -04002013#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Jeff Laytond6e971d2015-06-03 16:14:28 -04002014/*
2015 * Note that this should be called with XPRT_LOCKED held (or when we otherwise
2016 * know that we have exclusive access to the socket), to guard against
2017 * races with xs_reset_transport.
2018 */
Mel Gormana564b8f2012-07-31 16:45:12 -07002019static void xs_set_memalloc(struct rpc_xprt *xprt)
2020{
2021 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2022 xprt);
2023
Jeff Laytond6e971d2015-06-03 16:14:28 -04002024 /*
2025 * If there's no sock, then we have nothing to set. The
2026 * reconnecting process will get it for us.
2027 */
2028 if (!transport->inet)
2029 return;
Jeff Layton8e228132015-06-03 16:14:26 -04002030 if (atomic_read(&xprt->swapper))
Mel Gormana564b8f2012-07-31 16:45:12 -07002031 sk_set_memalloc(transport->inet);
2032}
2033
2034/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002035 * xs_enable_swap - Tag this transport as being used for swap.
Mel Gormana564b8f2012-07-31 16:45:12 -07002036 * @xprt: transport to tag
Mel Gormana564b8f2012-07-31 16:45:12 -07002037 *
Jeff Layton8e228132015-06-03 16:14:26 -04002038 * Take a reference to this transport on behalf of the rpc_clnt, and
2039 * optionally mark it for swapping if it wasn't already.
Mel Gormana564b8f2012-07-31 16:45:12 -07002040 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002041static int
2042xs_enable_swap(struct rpc_xprt *xprt)
Mel Gormana564b8f2012-07-31 16:45:12 -07002043{
Jeff Laytond6e971d2015-06-03 16:14:28 -04002044 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Mel Gormana564b8f2012-07-31 16:45:12 -07002045
Jeff Laytond6e971d2015-06-03 16:14:28 -04002046 if (atomic_inc_return(&xprt->swapper) != 1)
2047 return 0;
2048 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2049 return -ERESTARTSYS;
2050 if (xs->inet)
2051 sk_set_memalloc(xs->inet);
2052 xprt_release_xprt(xprt, NULL);
Jeff Layton8e228132015-06-03 16:14:26 -04002053 return 0;
Mel Gormana564b8f2012-07-31 16:45:12 -07002054}
Jeff Layton8e228132015-06-03 16:14:26 -04002055
2056/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002057 * xs_disable_swap - Untag this transport as being used for swap.
Jeff Layton8e228132015-06-03 16:14:26 -04002058 * @xprt: transport to tag
2059 *
2060 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
2061 * swapper refcount goes to 0, untag the socket as a memalloc socket.
2062 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002063static void
2064xs_disable_swap(struct rpc_xprt *xprt)
Jeff Layton8e228132015-06-03 16:14:26 -04002065{
Jeff Laytond6e971d2015-06-03 16:14:28 -04002066 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Jeff Layton8e228132015-06-03 16:14:26 -04002067
Jeff Laytond6e971d2015-06-03 16:14:28 -04002068 if (!atomic_dec_and_test(&xprt->swapper))
2069 return;
2070 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2071 return;
2072 if (xs->inet)
2073 sk_clear_memalloc(xs->inet);
2074 xprt_release_xprt(xprt, NULL);
Mel Gormana564b8f2012-07-31 16:45:12 -07002075}
Mel Gormana564b8f2012-07-31 16:45:12 -07002076#else
2077static void xs_set_memalloc(struct rpc_xprt *xprt)
2078{
2079}
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002080
2081static int
2082xs_enable_swap(struct rpc_xprt *xprt)
2083{
2084 return -EINVAL;
2085}
2086
2087static void
2088xs_disable_swap(struct rpc_xprt *xprt)
2089{
2090}
Mel Gormana564b8f2012-07-31 16:45:12 -07002091#endif
2092
Chuck Lever16be2d22007-08-06 11:57:38 -04002093static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04002094{
Chuck Lever16be2d22007-08-06 11:57:38 -04002095 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04002096
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002097 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002098 struct sock *sk = sock->sk;
2099
2100 write_lock_bh(&sk->sk_callback_lock);
2101
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002102 xs_save_old_callbacks(transport, sk);
2103
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002104 sk->sk_user_data = xprt;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002105 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002106 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002107 sock_set_flag(sk, SOCK_FASYNC);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002108
2109 xprt_set_connected(xprt);
2110
2111 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002112 transport->sock = sock;
2113 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002114
Mel Gormana564b8f2012-07-31 16:45:12 -07002115 xs_set_memalloc(xprt);
2116
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002117 write_unlock_bh(&sk->sk_callback_lock);
2118 }
Chuck Lever470056c2005-08-25 16:25:56 -07002119 xs_udp_do_set_buffer_size(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002120
2121 xprt->stat.connect_start = jiffies;
Chuck Lever16be2d22007-08-06 11:57:38 -04002122}
2123
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002124static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04002125{
2126 struct sock_xprt *transport =
2127 container_of(work, struct sock_xprt, connect_worker.work);
2128 struct rpc_xprt *xprt = &transport->xprt;
Colin Ian Kingd099b8a2017-09-18 12:21:14 +01002129 struct socket *sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002130 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04002131
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002132 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002133 xs_addr(xprt)->sa_family, SOCK_DGRAM,
2134 IPPROTO_UDP, false);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002135 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04002136 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04002137
Chuck Leverc740eff2009-08-09 15:09:46 -04002138 dprintk("RPC: worker connecting xprt %p via %s to "
2139 "%s (port %s)\n", xprt,
2140 xprt->address_strings[RPC_DISPLAY_PROTO],
2141 xprt->address_strings[RPC_DISPLAY_ADDR],
2142 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04002143
2144 xs_udp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002145 trace_rpc_socket_connect(xprt, sock, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002146 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002147out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002148 xprt_clear_connecting(xprt);
Trond Myklebustcf767852018-12-17 17:38:51 -05002149 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002150 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002151}
2152
Trond Myklebust4876cc72015-06-19 16:17:57 -04002153/**
2154 * xs_tcp_shutdown - gracefully shut down a TCP socket
2155 * @xprt: transport
2156 *
2157 * Initiates a graceful shutdown of the TCP socket by calling the
2158 * equivalent of shutdown(SHUT_RDWR);
2159 */
2160static void xs_tcp_shutdown(struct rpc_xprt *xprt)
2161{
2162 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2163 struct socket *sock = transport->sock;
Trond Myklebust9b308892018-02-05 10:20:06 -05002164 int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
Trond Myklebust4876cc72015-06-19 16:17:57 -04002165
2166 if (sock == NULL)
2167 return;
Trond Myklebust9b308892018-02-05 10:20:06 -05002168 switch (skst) {
2169 default:
Trond Myklebust4876cc72015-06-19 16:17:57 -04002170 kernel_sock_shutdown(sock, SHUT_RDWR);
2171 trace_rpc_socket_shutdown(xprt, sock);
Trond Myklebust9b308892018-02-05 10:20:06 -05002172 break;
2173 case TCP_CLOSE:
2174 case TCP_TIME_WAIT:
Trond Myklebust4876cc72015-06-19 16:17:57 -04002175 xs_reset_transport(transport);
Trond Myklebust9b308892018-02-05 10:20:06 -05002176 }
Trond Myklebust4876cc72015-06-19 16:17:57 -04002177}
2178
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002179static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
2180 struct socket *sock)
2181{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002182 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2183 unsigned int keepidle;
2184 unsigned int keepcnt;
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002185 unsigned int opt_on = 1;
2186 unsigned int timeo;
2187
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002188 spin_lock_bh(&xprt->transport_lock);
2189 keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
2190 keepcnt = xprt->timeout->to_retries + 1;
2191 timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
2192 (xprt->timeout->to_retries + 1);
2193 clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2194 spin_unlock_bh(&xprt->transport_lock);
2195
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002196 /* TCP Keepalive options */
2197 kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
2198 (char *)&opt_on, sizeof(opt_on));
2199 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
2200 (char *)&keepidle, sizeof(keepidle));
2201 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
2202 (char *)&keepidle, sizeof(keepidle));
2203 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
2204 (char *)&keepcnt, sizeof(keepcnt));
2205
2206 /* TCP user timeout (see RFC5482) */
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002207 kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT,
2208 (char *)&timeo, sizeof(timeo));
2209}
2210
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002211static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
2212 unsigned long connect_timeout,
2213 unsigned long reconnect_timeout)
2214{
2215 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2216 struct rpc_timeout to;
2217 unsigned long initval;
2218
2219 spin_lock_bh(&xprt->transport_lock);
2220 if (reconnect_timeout < xprt->max_reconnect_timeout)
2221 xprt->max_reconnect_timeout = reconnect_timeout;
2222 if (connect_timeout < xprt->connect_timeout) {
2223 memcpy(&to, xprt->timeout, sizeof(to));
2224 initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
2225 /* Arbitrary lower limit */
2226 if (initval < XS_TCP_INIT_REEST_TO << 1)
2227 initval = XS_TCP_INIT_REEST_TO << 1;
2228 to.to_initval = initval;
2229 to.to_maxval = initval;
2230 memcpy(&transport->tcp_timeout, &to,
2231 sizeof(transport->tcp_timeout));
2232 xprt->timeout = &transport->tcp_timeout;
2233 xprt->connect_timeout = connect_timeout;
2234 }
2235 set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2236 spin_unlock_bh(&xprt->transport_lock);
2237}
2238
Chuck Lever16be2d22007-08-06 11:57:38 -04002239static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002240{
Chuck Lever16be2d22007-08-06 11:57:38 -04002241 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002242 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002243
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002244 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002245 struct sock *sk = sock->sk;
NeilBrownd88e4d82016-08-04 16:24:28 +10002246 unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC;
Trond Myklebust7f260e82013-09-24 11:25:22 -04002247
NeilBrownd88e4d82016-08-04 16:24:28 +10002248 /* Avoid temporary address, they are bad for long-lived
2249 * connections such as NFS mounts.
2250 * RFC4941, section 3.6 suggests that:
2251 * Individual applications, which have specific
2252 * knowledge about the normal duration of connections,
2253 * MAY override this as appropriate.
2254 */
2255 kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES,
2256 (char *)&addr_pref, sizeof(addr_pref));
2257
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002258 xs_tcp_set_socket_timeouts(xprt, sock);
Trond Myklebust775f06a2015-06-20 15:31:54 -04002259
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002260 write_lock_bh(&sk->sk_callback_lock);
2261
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002262 xs_save_old_callbacks(transport, sk);
2263
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002264 sk->sk_user_data = xprt;
Trond Myklebust5157b952016-05-29 10:13:24 -04002265 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002266 sk->sk_state_change = xs_tcp_state_change;
2267 sk->sk_write_space = xs_tcp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002268 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05002269 sk->sk_error_report = xs_error_report;
Chuck Lever3167e122005-08-25 16:25:55 -07002270
2271 /* socket options */
Chuck Lever3167e122005-08-25 16:25:55 -07002272 sock_reset_flag(sk, SOCK_LINGER);
Chuck Lever3167e122005-08-25 16:25:55 -07002273 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002274
2275 xprt_clear_connected(xprt);
2276
2277 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002278 transport->sock = sock;
2279 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002280
2281 write_unlock_bh(&sk->sk_callback_lock);
2282 }
2283
Trond Myklebust01d37c42009-03-11 14:09:39 -04002284 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002285 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002286
Mel Gormana564b8f2012-07-31 16:45:12 -07002287 xs_set_memalloc(xprt);
2288
Trond Myklebustae053552019-02-20 14:56:20 -05002289 xs_stream_start_connect(transport);
Trond Myklebuste1806c72018-08-13 16:50:49 -04002290
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002291 /* Tell the socket layer to start connecting... */
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04002292 set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002293 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2294 switch (ret) {
2295 case 0:
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002296 xs_set_srcport(transport, sock);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002297 /* fall through */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002298 case -EINPROGRESS:
2299 /* SYN_SENT! */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002300 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2301 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust1f4c17a2016-08-01 13:36:08 -04002302 break;
2303 case -EADDRNOTAVAIL:
2304 /* Source port number is unavailable. Try a new one! */
2305 transport->srcport = 0;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002306 }
2307out:
2308 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002309}
2310
2311/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002312 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
Chuck Leveracf0a392018-12-19 11:00:22 -05002313 * @work: queued work item
Chuck Lever16be2d22007-08-06 11:57:38 -04002314 *
2315 * Invoked by a work queue tasklet.
2316 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002317static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002318{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002319 struct sock_xprt *transport =
2320 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002321 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002322 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002323 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002324
Chuck Lever16be2d22007-08-06 11:57:38 -04002325 if (!sock) {
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002326 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002327 xs_addr(xprt)->sa_family, SOCK_STREAM,
2328 IPPROTO_TCP, true);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002329 if (IS_ERR(sock)) {
2330 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002331 goto out;
2332 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002333 }
2334
Chuck Leverc740eff2009-08-09 15:09:46 -04002335 dprintk("RPC: worker connecting xprt %p via %s to "
2336 "%s (port %s)\n", xprt,
2337 xprt->address_strings[RPC_DISPLAY_PROTO],
2338 xprt->address_strings[RPC_DISPLAY_ADDR],
2339 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002340
2341 status = xs_tcp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002342 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever46121cf2007-01-31 12:14:08 -05002343 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2344 xprt, -status, xprt_connected(xprt),
2345 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002346 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002347 default:
2348 printk("%s: connect returned unhandled error %d\n",
2349 __func__, status);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002350 /* fall through */
Trond Myklebustf75e6742009-04-21 17:18:20 -04002351 case -EADDRNOTAVAIL:
2352 /* We're probably in TIME_WAIT. Get rid of existing socket,
2353 * and retry
2354 */
Trond Myklebusta519fc72012-09-12 16:49:15 -04002355 xs_tcp_force_close(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002356 break;
Trond Myklebust2a491992009-03-11 14:38:00 -04002357 case 0:
2358 case -EINPROGRESS:
2359 case -EALREADY:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002360 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002361 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002362 case -EINVAL:
2363 /* Happens, for instance, if the user specified a link
2364 * local IPv6 address without a scope-id.
2365 */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002366 case -ECONNREFUSED:
2367 case -ECONNRESET:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002368 case -ENETDOWN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002369 case -ENETUNREACH:
Trond Myklebust4ba161a2017-11-24 12:00:24 -05002370 case -EHOSTUNREACH:
Trond Myklebust3913c782015-02-08 21:44:04 -05002371 case -EADDRINUSE:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002372 case -ENOBUFS:
NeilBrown6ea44ad2017-05-25 17:00:32 +10002373 /*
2374 * xs_tcp_force_close() wakes tasks with -EIO.
2375 * We need to wake them first to ensure the
2376 * correct error code.
2377 */
2378 xprt_wake_pending_tasks(xprt, status);
Trond Myklebust4efdd922015-02-08 15:34:28 -05002379 xs_tcp_force_close(xprt);
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002380 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002381 }
Trond Myklebust2a491992009-03-11 14:38:00 -04002382 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002383out:
Chuck Lever2226feb2005-08-11 16:25:38 -04002384 xprt_clear_connecting(xprt);
Trond Myklebustcf767852018-12-17 17:38:51 -05002385 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002386 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04002387}
2388
Trond Myklebust02910172016-08-04 00:00:33 -04002389static unsigned long xs_reconnect_delay(const struct rpc_xprt *xprt)
2390{
2391 unsigned long start, now = jiffies;
2392
2393 start = xprt->stat.connect_start + xprt->reestablish_timeout;
2394 if (time_after(start, now))
2395 return start - now;
2396 return 0;
2397}
2398
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002399static void xs_reconnect_backoff(struct rpc_xprt *xprt)
2400{
2401 xprt->reestablish_timeout <<= 1;
2402 if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
2403 xprt->reestablish_timeout = xprt->max_reconnect_timeout;
2404 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2405 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2406}
2407
Chuck Lever68e220b2007-08-06 11:57:48 -04002408/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002409 * xs_connect - connect a socket to a remote endpoint
Trond Myklebust1b092092013-01-08 09:26:49 -05002410 * @xprt: pointer to transport structure
Chuck Lever9903cd12005-08-11 16:25:26 -04002411 * @task: address of RPC task that manages state of connect request
2412 *
2413 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002414 *
2415 * UDP socket connects are synchronous, but we use a work queue anyway
2416 * to guarantee that even unprivileged user processes can set up a
2417 * socket on a privileged port.
2418 *
2419 * If a UDP socket connect fails, the delay behavior here prevents
2420 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002421 */
Trond Myklebust1b092092013-01-08 09:26:49 -05002422static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002423{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002424 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002425 unsigned long delay = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002426
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002427 WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2428
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002429 if (transport->sock != NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002430 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2431 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002432 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002433
2434 /* Start by resetting any existing state */
2435 xs_reset_transport(transport);
2436
Trond Myklebust02910172016-08-04 00:00:33 -04002437 delay = xs_reconnect_delay(xprt);
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002438 xs_reconnect_backoff(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002439
Trond Myklebust02910172016-08-04 00:00:33 -04002440 } else
Chuck Lever46121cf2007-01-31 12:14:08 -05002441 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002442
2443 queue_delayed_work(xprtiod_workqueue,
2444 &transport->connect_worker,
2445 delay);
Chuck Levera246b012005-08-11 16:25:23 -04002446}
2447
Chuck Lever262ca072006-03-20 13:44:16 -05002448/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002449 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2450 * @xprt: rpc_xprt struct containing statistics
2451 * @seq: output file
2452 *
2453 */
2454static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2455{
2456 long idle_time = 0;
2457
2458 if (xprt_connected(xprt))
2459 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2460
2461 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
Andy Adamson15a45202012-02-14 16:19:18 -05002462 "%llu %llu %lu %llu %llu\n",
Chuck Lever176e21e2011-05-09 15:22:44 -04002463 xprt->stat.bind_count,
2464 xprt->stat.connect_count,
Chuck Lever8440a882018-10-01 14:25:41 -04002465 xprt->stat.connect_time / HZ,
Chuck Lever176e21e2011-05-09 15:22:44 -04002466 idle_time,
2467 xprt->stat.sends,
2468 xprt->stat.recvs,
2469 xprt->stat.bad_xids,
2470 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002471 xprt->stat.bklog_u,
2472 xprt->stat.max_slots,
2473 xprt->stat.sending_u,
2474 xprt->stat.pending_u);
Chuck Lever176e21e2011-05-09 15:22:44 -04002475}
2476
2477/**
Chuck Lever262ca072006-03-20 13:44:16 -05002478 * xs_udp_print_stats - display UDP socket-specifc stats
2479 * @xprt: rpc_xprt struct containing statistics
2480 * @seq: output file
2481 *
2482 */
2483static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2484{
Chuck Leverc8475462006-12-05 16:35:26 -05002485 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2486
Andy Adamson15a45202012-02-14 16:19:18 -05002487 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2488 "%lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002489 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002490 xprt->stat.bind_count,
2491 xprt->stat.sends,
2492 xprt->stat.recvs,
2493 xprt->stat.bad_xids,
2494 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002495 xprt->stat.bklog_u,
2496 xprt->stat.max_slots,
2497 xprt->stat.sending_u,
2498 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002499}
2500
2501/**
2502 * xs_tcp_print_stats - display TCP socket-specifc stats
2503 * @xprt: rpc_xprt struct containing statistics
2504 * @seq: output file
2505 *
2506 */
2507static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2508{
Chuck Leverc8475462006-12-05 16:35:26 -05002509 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002510 long idle_time = 0;
2511
2512 if (xprt_connected(xprt))
2513 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2514
Andy Adamson15a45202012-02-14 16:19:18 -05002515 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2516 "%llu %llu %lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002517 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002518 xprt->stat.bind_count,
2519 xprt->stat.connect_count,
Chuck Lever8440a882018-10-01 14:25:41 -04002520 xprt->stat.connect_time / HZ,
Chuck Lever262ca072006-03-20 13:44:16 -05002521 idle_time,
2522 xprt->stat.sends,
2523 xprt->stat.recvs,
2524 xprt->stat.bad_xids,
2525 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002526 xprt->stat.bklog_u,
2527 xprt->stat.max_slots,
2528 xprt->stat.sending_u,
2529 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002530}
2531
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002532/*
2533 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2534 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2535 * to use the server side send routines.
2536 */
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002537static int bc_malloc(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002538{
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002539 struct rpc_rqst *rqst = task->tk_rqstp;
2540 size_t size = rqst->rq_callsize;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002541 struct page *page;
2542 struct rpc_buffer *buf;
2543
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002544 if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
2545 WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
2546 size);
2547 return -EINVAL;
2548 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002549
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002550 page = alloc_page(GFP_KERNEL);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002551 if (!page)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002552 return -ENOMEM;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002553
2554 buf = page_address(page);
2555 buf->len = PAGE_SIZE;
2556
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002557 rqst->rq_buffer = buf->data;
Jeff Layton18e601d2016-10-24 20:33:23 -04002558 rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002559 return 0;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002560}
2561
2562/*
2563 * Free the space allocated in the bc_alloc routine
2564 */
Chuck Lever3435c742016-09-15 10:55:29 -04002565static void bc_free(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002566{
Chuck Lever3435c742016-09-15 10:55:29 -04002567 void *buffer = task->tk_rqstp->rq_buffer;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002568 struct rpc_buffer *buf;
2569
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002570 buf = container_of(buffer, struct rpc_buffer, data);
2571 free_page((unsigned long)buf);
2572}
2573
2574/*
2575 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2576 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2577 */
2578static int bc_sendto(struct rpc_rqst *req)
2579{
2580 int len;
2581 struct xdr_buf *xbufp = &req->rq_snd_buf;
2582 struct rpc_xprt *xprt = req->rq_xprt;
2583 struct sock_xprt *transport =
2584 container_of(xprt, struct sock_xprt, xprt);
2585 struct socket *sock = transport->sock;
2586 unsigned long headoff;
2587 unsigned long tailoff;
2588
Chuck Lever61677ee2011-05-09 15:22:34 -04002589 xs_encode_stream_record_marker(xbufp);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002590
2591 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2592 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2593 len = svc_send_common(sock, xbufp,
2594 virt_to_page(xbufp->head[0].iov_base), headoff,
2595 xbufp->tail[0].iov_base, tailoff);
2596
2597 if (len != xbufp->len) {
2598 printk(KERN_NOTICE "Error sending entire callback!\n");
2599 len = -EAGAIN;
2600 }
2601
2602 return len;
2603}
2604
2605/*
2606 * The send routine. Borrows from svc_send
2607 */
Trond Myklebustadfa7142018-09-03 23:58:59 -04002608static int bc_send_request(struct rpc_rqst *req)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002609{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002610 struct svc_xprt *xprt;
Andrzej Hajda7fc56132015-09-24 16:00:09 +02002611 int len;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002612
2613 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2614 /*
2615 * Get the server socket associated with this callback xprt
2616 */
2617 xprt = req->rq_xprt->bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002618
2619 /*
2620 * Grab the mutex to serialize data as the connection is shared
2621 * with the fore channel
2622 */
Trond Myklebustc5445772018-09-03 23:39:27 -04002623 mutex_lock(&xprt->xpt_mutex);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002624 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2625 len = -ENOTCONN;
2626 else
2627 len = bc_sendto(req);
2628 mutex_unlock(&xprt->xpt_mutex);
2629
2630 if (len > 0)
2631 len = 0;
2632
2633 return len;
2634}
2635
2636/*
2637 * The close routine. Since this is client initiated, we do nothing
2638 */
2639
2640static void bc_close(struct rpc_xprt *xprt)
2641{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002642}
2643
2644/*
2645 * The xprt destroy routine. Again, because this connection is client
2646 * initiated, we do nothing
2647 */
2648
2649static void bc_destroy(struct rpc_xprt *xprt)
2650{
Kinglong Mee47f72ef2014-03-24 11:58:16 +08002651 dprintk("RPC: bc_destroy xprt %p\n", xprt);
2652
2653 xs_xprt_free(xprt);
2654 module_put(THIS_MODULE);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002655}
2656
Chuck Leverd31ae252017-08-01 12:00:39 -04002657static const struct rpc_xprt_ops xs_local_ops = {
Chuck Lever176e21e2011-05-09 15:22:44 -04002658 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebust4cd34e72018-08-31 10:00:02 -04002659 .release_xprt = xprt_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002660 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002661 .free_slot = xprt_free_slot,
Chuck Lever176e21e2011-05-09 15:22:44 -04002662 .rpcbind = xs_local_rpcbind,
2663 .set_port = xs_local_set_port,
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002664 .connect = xs_local_connect,
Chuck Lever176e21e2011-05-09 15:22:44 -04002665 .buf_alloc = rpc_malloc,
2666 .buf_free = rpc_free,
Trond Myklebust550aebf2018-09-14 14:32:45 -04002667 .prepare_request = xs_stream_prepare_request,
Chuck Lever176e21e2011-05-09 15:22:44 -04002668 .send_request = xs_local_send_request,
2669 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2670 .close = xs_close,
Trond Myklebusta1311d82013-10-31 09:18:49 -04002671 .destroy = xs_destroy,
Chuck Lever176e21e2011-05-09 15:22:44 -04002672 .print_stats = xs_local_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002673 .enable_swap = xs_enable_swap,
2674 .disable_swap = xs_disable_swap,
Chuck Lever176e21e2011-05-09 15:22:44 -04002675};
2676
Chuck Leverd31ae252017-08-01 12:00:39 -04002677static const struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002678 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002679 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002680 .release_xprt = xprt_release_xprt_cong,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002681 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002682 .free_slot = xprt_free_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002683 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002684 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002685 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002686 .buf_alloc = rpc_malloc,
2687 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002688 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002689 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002690 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002691 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002692 .close = xs_close,
2693 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002694 .print_stats = xs_udp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002695 .enable_swap = xs_enable_swap,
2696 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002697 .inject_disconnect = xs_inject_disconnect,
Chuck Lever262965f2005-08-11 16:25:56 -04002698};
2699
Chuck Leverd31ae252017-08-01 12:00:39 -04002700static const struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002701 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebust4cd34e72018-08-31 10:00:02 -04002702 .release_xprt = xprt_release_xprt,
Trond Myklebust36bd7de2018-09-03 18:41:32 -04002703 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002704 .free_slot = xprt_free_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002705 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002706 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002707 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002708 .buf_alloc = rpc_malloc,
2709 .buf_free = rpc_free,
Trond Myklebust277e4ab2018-09-14 09:49:06 -04002710 .prepare_request = xs_stream_prepare_request,
Chuck Lever262965f2005-08-11 16:25:56 -04002711 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002712 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustc627d312015-02-10 11:06:04 -05002713 .close = xs_tcp_shutdown,
Chuck Lever9903cd12005-08-11 16:25:26 -04002714 .destroy = xs_destroy,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002715 .set_connect_timeout = xs_tcp_set_connect_timeout,
Chuck Lever262ca072006-03-20 13:44:16 -05002716 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002717 .enable_swap = xs_enable_swap,
2718 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002719 .inject_disconnect = xs_inject_disconnect,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002720#ifdef CONFIG_SUNRPC_BACKCHANNEL
2721 .bc_setup = xprt_setup_bc,
Chuck Lever6b26cc82016-05-02 14:40:40 -04002722 .bc_maxpayload = xs_tcp_bc_maxpayload,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002723 .bc_free_rqst = xprt_free_bc_rqst,
2724 .bc_destroy = xprt_destroy_bc,
2725#endif
Chuck Levera246b012005-08-11 16:25:23 -04002726};
2727
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002728/*
2729 * The rpc_xprt_ops for the server backchannel
2730 */
2731
Chuck Leverd31ae252017-08-01 12:00:39 -04002732static const struct rpc_xprt_ops bc_tcp_ops = {
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002733 .reserve_xprt = xprt_reserve_xprt,
2734 .release_xprt = xprt_release_xprt,
Bryan Schumaker84e28a32012-09-24 13:39:01 -04002735 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002736 .free_slot = xprt_free_slot,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002737 .buf_alloc = bc_malloc,
2738 .buf_free = bc_free,
2739 .send_request = bc_send_request,
2740 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2741 .close = bc_close,
2742 .destroy = bc_destroy,
2743 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002744 .enable_swap = xs_enable_swap,
2745 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002746 .inject_disconnect = xs_inject_disconnect,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002747};
2748
Chuck Lever92476852010-10-20 11:53:01 -04002749static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2750{
2751 static const struct sockaddr_in sin = {
2752 .sin_family = AF_INET,
2753 .sin_addr.s_addr = htonl(INADDR_ANY),
2754 };
2755 static const struct sockaddr_in6 sin6 = {
2756 .sin6_family = AF_INET6,
2757 .sin6_addr = IN6ADDR_ANY_INIT,
2758 };
2759
2760 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002761 case AF_LOCAL:
2762 break;
Chuck Lever92476852010-10-20 11:53:01 -04002763 case AF_INET:
2764 memcpy(sap, &sin, sizeof(sin));
2765 break;
2766 case AF_INET6:
2767 memcpy(sap, &sin6, sizeof(sin6));
2768 break;
2769 default:
2770 dprintk("RPC: %s: Bad address family\n", __func__);
2771 return -EAFNOSUPPORT;
2772 }
2773 return 0;
2774}
2775
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002776static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002777 unsigned int slot_table_size,
2778 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002779{
2780 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002781 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002782
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002783 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002784 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002785 return ERR_PTR(-EBADF);
2786 }
2787
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002788 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2789 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002790 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002791 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2792 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002793 return ERR_PTR(-ENOMEM);
2794 }
2795
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002796 new = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002797 mutex_init(&new->recv_mutex);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002798 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2799 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002800 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002801 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002802 else {
2803 int err;
2804 err = xs_init_anyaddr(args->dstaddr->sa_family,
2805 (struct sockaddr *)&new->srcaddr);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002806 if (err != 0) {
2807 xprt_free(xprt);
Chuck Lever92476852010-10-20 11:53:01 -04002808 return ERR_PTR(err);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002809 }
Chuck Lever92476852010-10-20 11:53:01 -04002810 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002811
2812 return xprt;
2813}
2814
Chuck Lever176e21e2011-05-09 15:22:44 -04002815static const struct rpc_timeout xs_local_default_timeout = {
2816 .to_initval = 10 * HZ,
2817 .to_maxval = 10 * HZ,
2818 .to_retries = 2,
2819};
2820
2821/**
2822 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2823 * @args: rpc transport creation arguments
2824 *
2825 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2826 */
2827static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2828{
2829 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2830 struct sock_xprt *transport;
2831 struct rpc_xprt *xprt;
2832 struct rpc_xprt *ret;
2833
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002834 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2835 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002836 if (IS_ERR(xprt))
2837 return xprt;
2838 transport = container_of(xprt, struct sock_xprt, xprt);
2839
2840 xprt->prot = 0;
2841 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2842 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2843
2844 xprt->bind_timeout = XS_BIND_TO;
2845 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2846 xprt->idle_timeout = XS_IDLE_DISC_TO;
2847
2848 xprt->ops = &xs_local_ops;
2849 xprt->timeout = &xs_local_default_timeout;
2850
Trond Myklebust550aebf2018-09-14 14:32:45 -04002851 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
2852 INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket);
NeilBrown93dc41b2013-10-31 16:14:36 +11002853
Chuck Lever176e21e2011-05-09 15:22:44 -04002854 switch (sun->sun_family) {
2855 case AF_LOCAL:
2856 if (sun->sun_path[0] != '/') {
2857 dprintk("RPC: bad AF_LOCAL address: %s\n",
2858 sun->sun_path);
2859 ret = ERR_PTR(-EINVAL);
2860 goto out_err;
2861 }
2862 xprt_set_bound(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002863 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
J. Bruce Fields7073ea82013-02-21 10:14:22 -05002864 ret = ERR_PTR(xs_local_setup_socket(transport));
2865 if (ret)
2866 goto out_err;
Chuck Lever176e21e2011-05-09 15:22:44 -04002867 break;
2868 default:
2869 ret = ERR_PTR(-EAFNOSUPPORT);
2870 goto out_err;
2871 }
2872
2873 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2874 xprt->address_strings[RPC_DISPLAY_ADDR]);
2875
2876 if (try_module_get(THIS_MODULE))
2877 return xprt;
2878 ret = ERR_PTR(-EINVAL);
2879out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002880 xs_xprt_free(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002881 return ret;
2882}
2883
Trond Myklebust2881ae72007-12-20 16:03:54 -05002884static const struct rpc_timeout xs_udp_default_timeout = {
2885 .to_initval = 5 * HZ,
2886 .to_maxval = 30 * HZ,
2887 .to_increment = 5 * HZ,
2888 .to_retries = 5,
2889};
2890
Chuck Lever9903cd12005-08-11 16:25:26 -04002891/**
2892 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002893 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002894 *
2895 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002896static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002897{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002898 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002899 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002900 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002901 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002902
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002903 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2904 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002905 if (IS_ERR(xprt))
2906 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002907 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002908
Chuck Leverec739ef2006-08-22 20:06:15 -04002909 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002910 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002911 /* XXX: header size can vary due to auth type, IPv6, etc. */
2912 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2913
Chuck Lever03bf4b72005-08-25 16:25:55 -07002914 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002915 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2916 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002917
Chuck Lever262965f2005-08-11 16:25:56 -04002918 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002919
Trond Myklebustba7392b2007-12-20 16:03:55 -05002920 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002921
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002922 INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002923 INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2924
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002925 switch (addr->sa_family) {
2926 case AF_INET:
2927 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2928 xprt_set_bound(xprt);
2929
Chuck Lever9dc3b092009-08-09 15:09:46 -04002930 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002931 break;
2932 case AF_INET6:
2933 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2934 xprt_set_bound(xprt);
2935
Chuck Lever9dc3b092009-08-09 15:09:46 -04002936 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002937 break;
2938 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002939 ret = ERR_PTR(-EAFNOSUPPORT);
2940 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002941 }
2942
Chuck Leverc740eff2009-08-09 15:09:46 -04002943 if (xprt_bound(xprt))
2944 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2945 xprt->address_strings[RPC_DISPLAY_ADDR],
2946 xprt->address_strings[RPC_DISPLAY_PORT],
2947 xprt->address_strings[RPC_DISPLAY_PROTO]);
2948 else
2949 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2950 xprt->address_strings[RPC_DISPLAY_ADDR],
2951 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002952
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002953 if (try_module_get(THIS_MODULE))
2954 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002955 ret = ERR_PTR(-EINVAL);
2956out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002957 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002958 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002959}
2960
Trond Myklebust2881ae72007-12-20 16:03:54 -05002961static const struct rpc_timeout xs_tcp_default_timeout = {
2962 .to_initval = 60 * HZ,
2963 .to_maxval = 60 * HZ,
2964 .to_retries = 2,
2965};
2966
Chuck Lever9903cd12005-08-11 16:25:26 -04002967/**
2968 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002969 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002970 *
2971 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002972static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002973{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002974 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002975 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002976 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002977 struct rpc_xprt *ret;
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002978 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2979
2980 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2981 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Levera246b012005-08-11 16:25:23 -04002982
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002983 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002984 max_slot_table_size);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002985 if (IS_ERR(xprt))
2986 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002987 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002988
Chuck Leverec739ef2006-08-22 20:06:15 -04002989 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002990 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002991 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002992
Chuck Lever03bf4b72005-08-25 16:25:55 -07002993 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002994 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2995 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002996
Chuck Lever262965f2005-08-11 16:25:56 -04002997 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002998 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002999
Trond Myklebust3851f1c2016-08-04 00:08:45 -04003000 xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05003001 xprt->connect_timeout = xprt->timeout->to_initval *
3002 (xprt->timeout->to_retries + 1);
Trond Myklebust3851f1c2016-08-04 00:08:45 -04003003
Trond Myklebustc50b8ee2018-09-14 14:26:28 -04003004 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
Trond Myklebustedc1b012015-10-05 10:53:49 -04003005 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
3006
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003007 switch (addr->sa_family) {
3008 case AF_INET:
3009 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
3010 xprt_set_bound(xprt);
3011
Chuck Lever9dc3b092009-08-09 15:09:46 -04003012 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003013 break;
3014 case AF_INET6:
3015 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
3016 xprt_set_bound(xprt);
3017
Chuck Lever9dc3b092009-08-09 15:09:46 -04003018 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003019 break;
3020 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003021 ret = ERR_PTR(-EAFNOSUPPORT);
3022 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003023 }
3024
Chuck Leverc740eff2009-08-09 15:09:46 -04003025 if (xprt_bound(xprt))
3026 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3027 xprt->address_strings[RPC_DISPLAY_ADDR],
3028 xprt->address_strings[RPC_DISPLAY_PORT],
3029 xprt->address_strings[RPC_DISPLAY_PROTO]);
3030 else
3031 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
3032 xprt->address_strings[RPC_DISPLAY_ADDR],
3033 xprt->address_strings[RPC_DISPLAY_PROTO]);
3034
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003035 if (try_module_get(THIS_MODULE))
3036 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003037 ret = ERR_PTR(-EINVAL);
3038out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003039 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003040 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04003041}
Chuck Lever282b32e2006-12-05 16:35:51 -05003042
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003043/**
3044 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3045 * @args: rpc transport creation arguments
3046 *
3047 */
3048static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
3049{
3050 struct sockaddr *addr = args->dstaddr;
3051 struct rpc_xprt *xprt;
3052 struct sock_xprt *transport;
3053 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003054 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003055
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003056 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3057 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003058 if (IS_ERR(xprt))
3059 return xprt;
3060 transport = container_of(xprt, struct sock_xprt, xprt);
3061
3062 xprt->prot = IPPROTO_TCP;
3063 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
3064 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3065 xprt->timeout = &xs_tcp_default_timeout;
3066
3067 /* backchannel */
3068 xprt_set_bound(xprt);
3069 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003070 xprt->reestablish_timeout = 0;
3071 xprt->idle_timeout = 0;
3072
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003073 xprt->ops = &bc_tcp_ops;
3074
3075 switch (addr->sa_family) {
3076 case AF_INET:
3077 xs_format_peer_addresses(xprt, "tcp",
3078 RPCBIND_NETID_TCP);
3079 break;
3080 case AF_INET6:
3081 xs_format_peer_addresses(xprt, "tcp",
3082 RPCBIND_NETID_TCP6);
3083 break;
3084 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003085 ret = ERR_PTR(-EAFNOSUPPORT);
3086 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003087 }
3088
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04003089 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3090 xprt->address_strings[RPC_DISPLAY_ADDR],
3091 xprt->address_strings[RPC_DISPLAY_PORT],
3092 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003093
3094 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003095 * Once we've associated a backchannel xprt with a connection,
Weng Meiling28303ca2013-11-30 17:56:44 +08003096 * we want to keep it around as long as the connection lasts,
3097 * in case we need to start using it for a backchannel again;
3098 * this reference won't be dropped until bc_xprt is destroyed.
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003099 */
3100 xprt_get(xprt);
3101 args->bc_xprt->xpt_bc_xprt = xprt;
3102 xprt->bc_xprt = args->bc_xprt;
3103 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
3104 transport->sock = bc_sock->sk_sock;
3105 transport->inet = bc_sock->sk_sk;
3106
3107 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003108 * Since we don't want connections for the backchannel, we set
3109 * the xprt status to connected
3110 */
3111 xprt_set_connected(xprt);
3112
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003113 if (try_module_get(THIS_MODULE))
3114 return xprt;
Kinglong Mee642aab52014-03-24 12:00:28 +08003115
3116 args->bc_xprt->xpt_bc_xprt = NULL;
J. Bruce Fields39a9bea2016-05-17 12:38:21 -04003117 args->bc_xprt->xpt_bc_xps = NULL;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003118 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003119 ret = ERR_PTR(-EINVAL);
3120out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003121 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003122 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003123}
3124
Chuck Lever176e21e2011-05-09 15:22:44 -04003125static struct xprt_class xs_local_transport = {
3126 .list = LIST_HEAD_INIT(xs_local_transport.list),
3127 .name = "named UNIX socket",
3128 .owner = THIS_MODULE,
3129 .ident = XPRT_TRANSPORT_LOCAL,
3130 .setup = xs_setup_local,
3131};
3132
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003133static struct xprt_class xs_udp_transport = {
3134 .list = LIST_HEAD_INIT(xs_udp_transport.list),
3135 .name = "udp",
3136 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003137 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003138 .setup = xs_setup_udp,
3139};
3140
3141static struct xprt_class xs_tcp_transport = {
3142 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
3143 .name = "tcp",
3144 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003145 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003146 .setup = xs_setup_tcp,
3147};
3148
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003149static struct xprt_class xs_bc_tcp_transport = {
3150 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3151 .name = "tcp NFSv4.1 backchannel",
3152 .owner = THIS_MODULE,
3153 .ident = XPRT_TRANSPORT_BC_TCP,
3154 .setup = xs_setup_bc_tcp,
3155};
3156
Chuck Lever282b32e2006-12-05 16:35:51 -05003157/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003158 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05003159 *
3160 */
3161int init_socket_xprt(void)
3162{
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08003163 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08003164 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05003165
Chuck Lever176e21e2011-05-09 15:22:44 -04003166 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003167 xprt_register_transport(&xs_udp_transport);
3168 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003169 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003170
Chuck Lever282b32e2006-12-05 16:35:51 -05003171 return 0;
3172}
3173
3174/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003175 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05003176 *
3177 */
3178void cleanup_socket_xprt(void)
3179{
Chuck Leverfbf76682006-12-05 16:35:54 -05003180 if (sunrpc_table_header) {
3181 unregister_sysctl_table(sunrpc_table_header);
3182 sunrpc_table_header = NULL;
3183 }
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003184
Chuck Lever176e21e2011-05-09 15:22:44 -04003185 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003186 xprt_unregister_transport(&xs_udp_transport);
3187 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003188 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05003189}
Trond Myklebustcbf11072009-08-09 15:06:19 -04003190
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003191static int param_set_uint_minmax(const char *val,
3192 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003193 unsigned int min, unsigned int max)
3194{
Daniel Walter00cfaa92014-06-21 13:06:38 +01003195 unsigned int num;
Trond Myklebustcbf11072009-08-09 15:06:19 -04003196 int ret;
3197
3198 if (!val)
3199 return -EINVAL;
Daniel Walter00cfaa92014-06-21 13:06:38 +01003200 ret = kstrtouint(val, 0, &num);
Dan Carpenter9761a242017-02-19 00:34:59 +03003201 if (ret)
3202 return ret;
3203 if (num < min || num > max)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003204 return -EINVAL;
3205 *((unsigned int *)kp->arg) = num;
3206 return 0;
3207}
3208
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003209static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003210{
Frank Sorensonffb6ca32016-07-08 16:35:25 -05003211 return param_set_uint_minmax(val, kp,
J. Bruce Fields826799e2018-10-18 15:27:02 -04003212 RPC_MIN_RESVPORT,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003213 RPC_MAX_RESVPORT);
3214}
3215
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303216static const struct kernel_param_ops param_ops_portnr = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003217 .set = param_set_portnr,
3218 .get = param_get_uint,
3219};
3220
Trond Myklebustcbf11072009-08-09 15:06:19 -04003221#define param_check_portnr(name, p) \
3222 __param_check(name, p, unsigned int);
3223
3224module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3225module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3226
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003227static int param_set_slot_table_size(const char *val,
3228 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003229{
3230 return param_set_uint_minmax(val, kp,
3231 RPC_MIN_SLOT_TABLE,
3232 RPC_MAX_SLOT_TABLE);
3233}
3234
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303235static const struct kernel_param_ops param_ops_slot_table_size = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003236 .set = param_set_slot_table_size,
3237 .get = param_get_uint,
3238};
3239
Trond Myklebustcbf11072009-08-09 15:06:19 -04003240#define param_check_slot_table_size(name, p) \
3241 __param_check(name, p, unsigned int);
3242
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003243static int param_set_max_slot_table_size(const char *val,
3244 const struct kernel_param *kp)
3245{
3246 return param_set_uint_minmax(val, kp,
3247 RPC_MIN_SLOT_TABLE,
3248 RPC_MAX_SLOT_TABLE_LIMIT);
3249}
3250
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303251static const struct kernel_param_ops param_ops_max_slot_table_size = {
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003252 .set = param_set_max_slot_table_size,
3253 .get = param_get_uint,
3254};
3255
3256#define param_check_max_slot_table_size(name, p) \
3257 __param_check(name, p, unsigned int);
3258
Trond Myklebustcbf11072009-08-09 15:06:19 -04003259module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3260 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003261module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3262 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003263module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3264 slot_table_size, 0644);