blob: 04f1b78bcbca3d3e0f7203c59c46774b1b35fe3e [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
Chuck Lever9e55eef42020-03-02 15:19:54 -050057#include "socklib.h"
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030058#include "sunrpc.h"
Chuck Lever176e21e2011-05-09 15:22:44 -040059
60static void xs_close(struct rpc_xprt *xprt);
Trond Myklebust7196dbb2017-02-08 11:17:54 -050061static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
62 struct socket *sock);
Chuck Lever176e21e2011-05-09 15:22:44 -040063
Chuck Lever9903cd12005-08-11 16:25:26 -040064/*
Chuck Leverc556b752005-11-01 12:24:48 -050065 * xprtsock tunables
66 */
Trond Myklebust09acfea2012-03-11 15:22:54 -040067static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
68static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
69static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
Chuck Leverc556b752005-11-01 12:24:48 -050070
Trond Myklebust09acfea2012-03-11 15:22:54 -040071static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
72static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
Chuck Leverc556b752005-11-01 12:24:48 -050073
Trond Myklebust7d1e8252009-03-11 14:38:03 -040074#define XS_TCP_LINGER_TO (15U * HZ)
Trond Myklebust25fe6142009-03-11 14:38:03 -040075static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
Trond Myklebust7d1e8252009-03-11 14:38:03 -040076
Chuck Leverc556b752005-11-01 12:24:48 -050077/*
Chuck Leverfbf76682006-12-05 16:35:54 -050078 * We can register our own files under /proc/sys/sunrpc by
79 * calling register_sysctl_table() again. The files in that
80 * directory become the union of all files registered there.
81 *
82 * We simply need to make sure that we don't collide with
83 * someone else's file names!
84 */
85
Chuck Leverfbf76682006-12-05 16:35:54 -050086static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
87static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
Trond Myklebustd9ba1312011-07-17 18:11:30 -040088static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Leverfbf76682006-12-05 16:35:54 -050089static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
90static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
91
92static struct ctl_table_header *sunrpc_table_header;
93
Olga Kornievskaiad3abc732021-06-08 15:59:15 -040094static struct xprt_class xs_local_transport;
95static struct xprt_class xs_udp_transport;
96static struct xprt_class xs_tcp_transport;
97static struct xprt_class xs_bc_tcp_transport;
98
Chuck Leverfbf76682006-12-05 16:35:54 -050099/*
100 * FIXME: changing the UDP slot table size should also resize the UDP
101 * socket buffers for existing UDP transports
102 */
Joe Perchesfe2c6332013-06-11 23:04:25 -0700103static struct ctl_table xs_tunables_table[] = {
Chuck Leverfbf76682006-12-05 16:35:54 -0500104 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500105 .procname = "udp_slot_table_entries",
106 .data = &xprt_udp_slot_table_entries,
107 .maxlen = sizeof(unsigned int),
108 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800109 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500110 .extra1 = &min_slot_table_size,
111 .extra2 = &max_slot_table_size
112 },
113 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500114 .procname = "tcp_slot_table_entries",
115 .data = &xprt_tcp_slot_table_entries,
116 .maxlen = sizeof(unsigned int),
117 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800118 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500119 .extra1 = &min_slot_table_size,
120 .extra2 = &max_slot_table_size
121 },
122 {
Trond Myklebustd9ba1312011-07-17 18:11:30 -0400123 .procname = "tcp_max_slot_table_entries",
124 .data = &xprt_max_tcp_slot_table_entries,
125 .maxlen = sizeof(unsigned int),
126 .mode = 0644,
127 .proc_handler = proc_dointvec_minmax,
128 .extra1 = &min_slot_table_size,
129 .extra2 = &max_tcp_slot_table_limit
130 },
131 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500132 .procname = "min_resvport",
133 .data = &xprt_min_resvport,
134 .maxlen = sizeof(unsigned int),
135 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800136 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500137 .extra1 = &xprt_min_resvport_limit,
J. Bruce Fields826799e2018-10-18 15:27:02 -0400138 .extra2 = &xprt_max_resvport_limit
Chuck Leverfbf76682006-12-05 16:35:54 -0500139 },
140 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500141 .procname = "max_resvport",
142 .data = &xprt_max_resvport,
143 .maxlen = sizeof(unsigned int),
144 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800145 .proc_handler = proc_dointvec_minmax,
J. Bruce Fields826799e2018-10-18 15:27:02 -0400146 .extra1 = &xprt_min_resvport_limit,
Chuck Leverfbf76682006-12-05 16:35:54 -0500147 .extra2 = &xprt_max_resvport_limit
148 },
149 {
Trond Myklebust25fe6142009-03-11 14:38:03 -0400150 .procname = "tcp_fin_timeout",
151 .data = &xs_tcp_fin_timeout,
152 .maxlen = sizeof(xs_tcp_fin_timeout),
153 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800154 .proc_handler = proc_dointvec_jiffies,
Trond Myklebust25fe6142009-03-11 14:38:03 -0400155 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800156 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500157};
158
Joe Perchesfe2c6332013-06-11 23:04:25 -0700159static struct ctl_table sunrpc_table[] = {
Chuck Leverfbf76682006-12-05 16:35:54 -0500160 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500161 .procname = "sunrpc",
162 .mode = 0555,
163 .child = xs_tunables_table
164 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800165 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500166};
167
Chuck Leverfbf76682006-12-05 16:35:54 -0500168/*
Chuck Lever03bf4b72005-08-25 16:25:55 -0700169 * Wait duration for a reply from the RPC portmapper.
170 */
171#define XS_BIND_TO (60U * HZ)
172
173/*
174 * Delay if a UDP socket connect error occurs. This is most likely some
175 * kind of resource problem on the local host.
176 */
177#define XS_UDP_REEST_TO (2U * HZ)
178
179/*
180 * The reestablish timeout allows clients to delay for a bit before attempting
181 * to reconnect to a server that just dropped our connection.
182 *
183 * We implement an exponential backoff when trying to reestablish a TCP
184 * transport connection with the server. Some servers like to drop a TCP
185 * connection when they are overworked, so we start with a short timeout and
186 * increase over time if the server is down or not responding.
187 */
188#define XS_TCP_INIT_REEST_TO (3U * HZ)
Chuck Lever03bf4b72005-08-25 16:25:55 -0700189
190/*
191 * TCP idle timeout; client drops the transport socket if it is idle
192 * for this long. Note that we also timeout UDP sockets to prevent
193 * holding port numbers when there is no RPC traffic.
194 */
195#define XS_IDLE_DISC_TO (5U * 60 * HZ)
196
Jeff Laytonf895b252014-11-17 16:58:04 -0500197#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Levera246b012005-08-11 16:25:23 -0400198# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400199# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400200#endif
201
Chuck Levera246b012005-08-11 16:25:23 -0400202#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400203static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400204{
Chuck Lever9903cd12005-08-11 16:25:26 -0400205 u8 *buf = (u8 *) packet;
206 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400207
Chuck Lever46121cf2007-01-31 12:14:08 -0500208 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400209 for (j = 0; j < count && j < 128; j += 4) {
210 if (!(j & 31)) {
211 if (j)
212 dprintk("\n");
213 dprintk("0x%04x ", j);
214 }
215 dprintk("%02x%02x%02x%02x ",
216 buf[j], buf[j+1], buf[j+2], buf[j+3]);
217 }
218 dprintk("\n");
219}
220#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400221static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400222{
223 /* NOP */
224}
225#endif
226
Trond Myklebust21180712013-12-31 13:22:59 -0500227static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
228{
229 return (struct rpc_xprt *) sk->sk_user_data;
230}
231
Chuck Lever95392c52007-08-06 11:57:58 -0400232static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400233{
Chuck Lever95392c52007-08-06 11:57:58 -0400234 return (struct sockaddr *) &xprt->addr;
235}
236
Chuck Lever176e21e2011-05-09 15:22:44 -0400237static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
238{
239 return (struct sockaddr_un *) &xprt->addr;
240}
241
Chuck Lever95392c52007-08-06 11:57:58 -0400242static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
243{
244 return (struct sockaddr_in *) &xprt->addr;
245}
246
247static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
248{
249 return (struct sockaddr_in6 *) &xprt->addr;
250}
251
Chuck Leverc877b842009-08-09 15:09:36 -0400252static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400253{
Chuck Leverc877b842009-08-09 15:09:36 -0400254 struct sockaddr *sap = xs_addr(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400255 struct sockaddr_in6 *sin6;
256 struct sockaddr_in *sin;
Chuck Lever176e21e2011-05-09 15:22:44 -0400257 struct sockaddr_un *sun;
Chuck Leverc877b842009-08-09 15:09:36 -0400258 char buf[128];
Chuck Leveredb267a2006-08-22 20:06:18 -0400259
Chuck Lever9dc3b092009-08-09 15:09:46 -0400260 switch (sap->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400261 case AF_LOCAL:
262 sun = xs_addr_un(xprt);
263 strlcpy(buf, sun->sun_path, sizeof(buf));
264 xprt->address_strings[RPC_DISPLAY_ADDR] =
265 kstrdup(buf, GFP_KERNEL);
266 break;
Chuck Lever9dc3b092009-08-09 15:09:46 -0400267 case AF_INET:
Chuck Lever176e21e2011-05-09 15:22:44 -0400268 (void)rpc_ntop(sap, buf, sizeof(buf));
269 xprt->address_strings[RPC_DISPLAY_ADDR] =
270 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400271 sin = xs_addr_in(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800272 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
Chuck Lever9dc3b092009-08-09 15:09:46 -0400273 break;
274 case AF_INET6:
Chuck Lever176e21e2011-05-09 15:22:44 -0400275 (void)rpc_ntop(sap, buf, sizeof(buf));
276 xprt->address_strings[RPC_DISPLAY_ADDR] =
277 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400278 sin6 = xs_addr_in6(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800279 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400280 break;
281 default:
282 BUG();
Chuck Leveredb267a2006-08-22 20:06:18 -0400283 }
Chuck Lever176e21e2011-05-09 15:22:44 -0400284
Chuck Lever9dc3b092009-08-09 15:09:46 -0400285 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
Chuck Leveredb267a2006-08-22 20:06:18 -0400286}
287
Chuck Lever9dc3b092009-08-09 15:09:46 -0400288static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400289{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400290 struct sockaddr *sap = xs_addr(xprt);
291 char buf[128];
Chuck Lever4b6473f2007-08-06 11:57:12 -0400292
Joe Perches81160e662010-03-08 12:15:59 -0800293 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400294 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400295
Joe Perches81160e662010-03-08 12:15:59 -0800296 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400297 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
298}
Chuck Lever4b6473f2007-08-06 11:57:12 -0400299
Chuck Lever9dc3b092009-08-09 15:09:46 -0400300static void xs_format_peer_addresses(struct rpc_xprt *xprt,
301 const char *protocol,
302 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400303{
Chuck Leverb454ae92008-01-07 18:34:48 -0500304 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leverb454ae92008-01-07 18:34:48 -0500305 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leverc877b842009-08-09 15:09:36 -0400306 xs_format_common_peer_addresses(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400307 xs_format_common_peer_ports(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400308}
309
Chuck Lever9dc3b092009-08-09 15:09:46 -0400310static void xs_update_peer_port(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400311{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400312 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
313 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400314
Chuck Lever9dc3b092009-08-09 15:09:46 -0400315 xs_format_common_peer_ports(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400316}
317
318static void xs_free_peer_addresses(struct rpc_xprt *xprt)
319{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500320 unsigned int i;
321
322 for (i = 0; i < RPC_DISPLAY_MAX; i++)
323 switch (i) {
324 case RPC_DISPLAY_PROTO:
325 case RPC_DISPLAY_NETID:
326 continue;
327 default:
328 kfree(xprt->address_strings[i]);
329 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400330}
331
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400332static size_t
333xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp)
334{
335 size_t i,n;
336
Trond Myklebust16e5e902018-12-02 15:22:48 -0500337 if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES))
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400338 return want;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400339 n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT;
340 for (i = 0; i < n; i++) {
341 if (buf->pages[i])
342 continue;
343 buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp);
344 if (!buf->pages[i]) {
Trond Myklebust16e5e902018-12-02 15:22:48 -0500345 i *= PAGE_SIZE;
346 return i > buf->page_base ? i - buf->page_base : 0;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400347 }
348 }
349 return want;
350}
351
352static ssize_t
353xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek)
354{
355 ssize_t ret;
356 if (seek != 0)
357 iov_iter_advance(&msg->msg_iter, seek);
358 ret = sock_recvmsg(sock, msg, flags);
359 return ret > 0 ? ret + seek : ret;
360}
361
362static ssize_t
363xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags,
364 struct kvec *kvec, size_t count, size_t seek)
365{
Al Viro0e9b4a82018-11-01 18:19:03 -0400366 iov_iter_kvec(&msg->msg_iter, READ, kvec, 1, count);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400367 return xs_sock_recvmsg(sock, msg, flags, seek);
368}
369
370static ssize_t
371xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags,
372 struct bio_vec *bvec, unsigned long nr, size_t count,
373 size_t seek)
374{
Al Viro0e9b4a82018-11-01 18:19:03 -0400375 iov_iter_bvec(&msg->msg_iter, READ, bvec, nr, count);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400376 return xs_sock_recvmsg(sock, msg, flags, seek);
377}
378
379static ssize_t
380xs_read_discard(struct socket *sock, struct msghdr *msg, int flags,
381 size_t count)
382{
Trond Myklebustb76a5af2018-12-03 15:58:58 -0500383 iov_iter_discard(&msg->msg_iter, READ, count);
384 return sock_recvmsg(sock, msg, flags);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400385}
386
Trond Myklebust6a829eb2019-01-03 09:04:45 -0500387#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
388static void
389xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
390{
391 struct bvec_iter bi = {
392 .bi_size = count,
393 };
394 struct bio_vec bv;
395
396 bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
397 for_each_bvec(bv, bvec, bi, bi)
398 flush_dcache_page(bv.bv_page);
399}
400#else
401static inline void
402xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
403{
404}
405#endif
406
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400407static ssize_t
408xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags,
409 struct xdr_buf *buf, size_t count, size_t seek, size_t *read)
410{
411 size_t want, seek_init = seek, offset = 0;
412 ssize_t ret;
413
Trond Myklebuste92053a2019-02-15 18:14:27 -0500414 want = min_t(size_t, count, buf->head[0].iov_len);
415 if (seek < want) {
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400416 ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek);
417 if (ret <= 0)
418 goto sock_err;
419 offset += ret;
420 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
421 goto out;
422 if (ret != want)
Trond Myklebustc4433052018-12-04 07:52:11 -0500423 goto out;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400424 seek = 0;
425 } else {
Trond Myklebuste92053a2019-02-15 18:14:27 -0500426 seek -= want;
427 offset += want;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400428 }
Trond Myklebust16e5e902018-12-02 15:22:48 -0500429
430 want = xs_alloc_sparse_pages(buf,
431 min_t(size_t, count - offset, buf->page_len),
Trond Myklebust12a3ad62019-03-02 10:14:02 -0500432 GFP_KERNEL);
Trond Myklebust16e5e902018-12-02 15:22:48 -0500433 if (seek < want) {
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400434 ret = xs_read_bvec(sock, msg, flags, buf->bvec,
435 xdr_buf_pagecount(buf),
436 want + buf->page_base,
437 seek + buf->page_base);
438 if (ret <= 0)
439 goto sock_err;
Trond Myklebust6a829eb2019-01-03 09:04:45 -0500440 xs_flush_bvec(buf->bvec, ret, seek + buf->page_base);
Dan Aloniac9645c2020-12-05 11:28:35 +0200441 ret -= buf->page_base;
442 offset += ret;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400443 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
444 goto out;
445 if (ret != want)
Trond Myklebustc4433052018-12-04 07:52:11 -0500446 goto out;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400447 seek = 0;
448 } else {
Trond Myklebust16e5e902018-12-02 15:22:48 -0500449 seek -= want;
450 offset += want;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400451 }
Trond Myklebust16e5e902018-12-02 15:22:48 -0500452
Trond Myklebuste92053a2019-02-15 18:14:27 -0500453 want = min_t(size_t, count - offset, buf->tail[0].iov_len);
454 if (seek < want) {
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400455 ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek);
456 if (ret <= 0)
457 goto sock_err;
458 offset += ret;
459 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
460 goto out;
461 if (ret != want)
Trond Myklebustc4433052018-12-04 07:52:11 -0500462 goto out;
Trond Myklebust9734ad52019-03-15 12:47:34 -0400463 } else if (offset < seek_init)
Trond Myklebuste92053a2019-02-15 18:14:27 -0500464 offset = seek_init;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400465 ret = -EMSGSIZE;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400466out:
467 *read = offset - seek_init;
468 return ret;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400469sock_err:
470 offset += seek;
471 goto out;
472}
473
474static void
475xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf)
476{
477 if (!transport->recv.copied) {
478 if (buf->head[0].iov_len >= transport->recv.offset)
479 memcpy(buf->head[0].iov_base,
480 &transport->recv.xid,
481 transport->recv.offset);
482 transport->recv.copied = transport->recv.offset;
483 }
484}
485
486static bool
487xs_read_stream_request_done(struct sock_xprt *transport)
488{
489 return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT);
490}
491
Trond Myklebuste92053a2019-02-15 18:14:27 -0500492static void
493xs_read_stream_check_eor(struct sock_xprt *transport,
494 struct msghdr *msg)
495{
496 if (xs_read_stream_request_done(transport))
497 msg->msg_flags |= MSG_EOR;
498}
499
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400500static ssize_t
501xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg,
502 int flags, struct rpc_rqst *req)
503{
504 struct xdr_buf *buf = &req->rq_private_buf;
Kees Cook3f649ab2020-06-03 13:09:38 -0700505 size_t want, read;
506 ssize_t ret;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400507
508 xs_read_header(transport, buf);
509
510 want = transport->recv.len - transport->recv.offset;
Trond Myklebuste92053a2019-02-15 18:14:27 -0500511 if (want != 0) {
512 ret = xs_read_xdr_buf(transport->sock, msg, flags, buf,
513 transport->recv.copied + want,
514 transport->recv.copied,
515 &read);
516 transport->recv.offset += read;
517 transport->recv.copied += read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400518 }
519
Trond Myklebust727fcc62019-02-15 16:53:04 -0500520 if (transport->recv.offset == transport->recv.len)
521 xs_read_stream_check_eor(transport, msg);
522
Trond Myklebuste92053a2019-02-15 18:14:27 -0500523 if (want == 0)
524 return 0;
525
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400526 switch (ret) {
Trond Myklebustc4433052018-12-04 07:52:11 -0500527 default:
528 break;
Trond Myklebust26781ea2018-12-03 15:41:38 -0500529 case -EFAULT:
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400530 case -EMSGSIZE:
Trond Myklebust26781ea2018-12-03 15:41:38 -0500531 msg->msg_flags |= MSG_TRUNC;
Trond Myklebustc4433052018-12-04 07:52:11 -0500532 return read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400533 case 0:
534 return -ESHUTDOWN;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400535 }
Trond Myklebustc4433052018-12-04 07:52:11 -0500536 return ret < 0 ? ret : read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400537}
538
539static size_t
540xs_read_stream_headersize(bool isfrag)
541{
542 if (isfrag)
543 return sizeof(__be32);
544 return 3 * sizeof(__be32);
545}
546
547static ssize_t
548xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg,
549 int flags, size_t want, size_t seek)
550{
551 struct kvec kvec = {
552 .iov_base = &transport->recv.fraghdr,
553 .iov_len = want,
554 };
555 return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek);
556}
557
558#if defined(CONFIG_SUNRPC_BACKCHANNEL)
559static ssize_t
560xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
561{
562 struct rpc_xprt *xprt = &transport->xprt;
563 struct rpc_rqst *req;
564 ssize_t ret;
565
Benjamin Coddington98b5cee2021-03-22 14:37:01 -0400566 /* Is this transport associated with the backchannel? */
567 if (!xprt->bc_serv)
568 return -ESHUTDOWN;
569
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400570 /* Look up and lock the request corresponding to the given XID */
571 req = xprt_lookup_bc_request(xprt, transport->recv.xid);
572 if (!req) {
573 printk(KERN_WARNING "Callback slot table overflowed\n");
574 return -ESHUTDOWN;
575 }
Trond Myklebust45835a62019-09-12 08:04:25 -0400576 if (transport->recv.copied && !req->rq_private_buf.len)
577 return -ESHUTDOWN;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400578
579 ret = xs_read_stream_request(transport, msg, flags, req);
580 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
Trond Myklebustc4433052018-12-04 07:52:11 -0500581 xprt_complete_bc_request(req, transport->recv.copied);
Trond Myklebust45835a62019-09-12 08:04:25 -0400582 else
583 req->rq_private_buf.len = transport->recv.copied;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400584
585 return ret;
586}
587#else /* CONFIG_SUNRPC_BACKCHANNEL */
588static ssize_t
589xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
590{
591 return -ESHUTDOWN;
592}
593#endif /* CONFIG_SUNRPC_BACKCHANNEL */
594
595static ssize_t
596xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags)
597{
598 struct rpc_xprt *xprt = &transport->xprt;
599 struct rpc_rqst *req;
600 ssize_t ret = 0;
601
602 /* Look up and lock the request corresponding to the given XID */
603 spin_lock(&xprt->queue_lock);
604 req = xprt_lookup_rqst(xprt, transport->recv.xid);
Trond Myklebust45835a62019-09-12 08:04:25 -0400605 if (!req || (transport->recv.copied && !req->rq_private_buf.len)) {
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400606 msg->msg_flags |= MSG_TRUNC;
607 goto out;
608 }
609 xprt_pin_rqst(req);
610 spin_unlock(&xprt->queue_lock);
611
612 ret = xs_read_stream_request(transport, msg, flags, req);
613
614 spin_lock(&xprt->queue_lock);
615 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
Trond Myklebustc4433052018-12-04 07:52:11 -0500616 xprt_complete_rqst(req->rq_task, transport->recv.copied);
Trond Myklebust45835a62019-09-12 08:04:25 -0400617 else
618 req->rq_private_buf.len = transport->recv.copied;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400619 xprt_unpin_rqst(req);
620out:
621 spin_unlock(&xprt->queue_lock);
622 return ret;
623}
624
625static ssize_t
626xs_read_stream(struct sock_xprt *transport, int flags)
627{
628 struct msghdr msg = { 0 };
629 size_t want, read = 0;
630 ssize_t ret = 0;
631
632 if (transport->recv.len == 0) {
633 want = xs_read_stream_headersize(transport->recv.copied != 0);
634 ret = xs_read_stream_header(transport, &msg, flags, want,
635 transport->recv.offset);
636 if (ret <= 0)
637 goto out_err;
638 transport->recv.offset = ret;
Trond Myklebustc4433052018-12-04 07:52:11 -0500639 if (transport->recv.offset != want)
640 return transport->recv.offset;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400641 transport->recv.len = be32_to_cpu(transport->recv.fraghdr) &
642 RPC_FRAGMENT_SIZE_MASK;
643 transport->recv.offset -= sizeof(transport->recv.fraghdr);
644 read = ret;
645 }
646
647 switch (be32_to_cpu(transport->recv.calldir)) {
Trond Myklebustc4433052018-12-04 07:52:11 -0500648 default:
649 msg.msg_flags |= MSG_TRUNC;
650 break;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400651 case RPC_CALL:
652 ret = xs_read_stream_call(transport, &msg, flags);
653 break;
654 case RPC_REPLY:
655 ret = xs_read_stream_reply(transport, &msg, flags);
656 }
657 if (msg.msg_flags & MSG_TRUNC) {
658 transport->recv.calldir = cpu_to_be32(-1);
659 transport->recv.copied = -1;
660 }
661 if (ret < 0)
662 goto out_err;
663 read += ret;
664 if (transport->recv.offset < transport->recv.len) {
Trond Myklebustc4433052018-12-04 07:52:11 -0500665 if (!(msg.msg_flags & MSG_TRUNC))
666 return read;
Trond Myklebustb76a5af2018-12-03 15:58:58 -0500667 msg.msg_flags = 0;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400668 ret = xs_read_discard(transport->sock, &msg, flags,
669 transport->recv.len - transport->recv.offset);
670 if (ret <= 0)
671 goto out_err;
672 transport->recv.offset += ret;
673 read += ret;
674 if (transport->recv.offset != transport->recv.len)
Trond Myklebustc4433052018-12-04 07:52:11 -0500675 return read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400676 }
677 if (xs_read_stream_request_done(transport)) {
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400678 trace_xs_stream_read_request(transport);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400679 transport->recv.copied = 0;
680 }
681 transport->recv.offset = 0;
682 transport->recv.len = 0;
683 return read;
684out_err:
Trond Myklebust794628572018-12-03 18:49:00 -0500685 return ret != 0 ? ret : -ESHUTDOWN;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400686}
687
Trond Myklebust0ffe86f2019-01-30 14:51:26 -0500688static __poll_t xs_poll_socket(struct sock_xprt *transport)
689{
Trond Myklebusta73881c2019-02-26 06:33:02 -0500690 return transport->sock->ops->poll(transport->file, transport->sock,
691 NULL);
Trond Myklebust0ffe86f2019-01-30 14:51:26 -0500692}
693
694static bool xs_poll_socket_readable(struct sock_xprt *transport)
695{
696 __poll_t events = xs_poll_socket(transport);
697
698 return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP);
699}
700
701static void xs_poll_check_readable(struct sock_xprt *transport)
702{
703
704 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
705 if (!xs_poll_socket_readable(transport))
706 return;
707 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
708 queue_work(xprtiod_workqueue, &transport->recv_worker);
709}
710
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400711static void xs_stream_data_receive(struct sock_xprt *transport)
712{
713 size_t read = 0;
714 ssize_t ret = 0;
715
716 mutex_lock(&transport->recv_mutex);
717 if (transport->sock == NULL)
718 goto out;
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400719 for (;;) {
720 ret = xs_read_stream(transport, MSG_DONTWAIT);
Trond Myklebustc4433052018-12-04 07:52:11 -0500721 if (ret < 0)
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400722 break;
723 read += ret;
724 cond_resched();
725 }
Trond Myklebust5f52a9d2019-02-16 09:31:47 -0500726 if (ret == -ESHUTDOWN)
727 kernel_sock_shutdown(transport->sock, SHUT_RDWR);
728 else
729 xs_poll_check_readable(transport);
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400730out:
731 mutex_unlock(&transport->recv_mutex);
732 trace_xs_stream_read_data(&transport->xprt, ret, read);
733}
734
735static void xs_stream_data_receive_workfn(struct work_struct *work)
736{
737 struct sock_xprt *transport =
738 container_of(work, struct sock_xprt, recv_worker);
Trond Myklebusta1231fd2019-02-18 10:02:29 -0500739 unsigned int pflags = memalloc_nofs_save();
740
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400741 xs_stream_data_receive(transport);
Trond Myklebusta1231fd2019-02-18 10:02:29 -0500742 memalloc_nofs_restore(pflags);
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400743}
744
Trond Myklebust550aebf2018-09-14 14:32:45 -0400745static void
746xs_stream_reset_connect(struct sock_xprt *transport)
747{
748 transport->recv.offset = 0;
749 transport->recv.len = 0;
750 transport->recv.copied = 0;
751 transport->xmit.offset = 0;
Trond Myklebustae053552019-02-20 14:56:20 -0500752}
753
754static void
755xs_stream_start_connect(struct sock_xprt *transport)
756{
Trond Myklebust550aebf2018-09-14 14:32:45 -0400757 transport->xprt.stat.connect_count++;
758 transport->xprt.stat.connect_start = jiffies;
759}
760
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400761#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
762
Chuck Lever9903cd12005-08-11 16:25:26 -0400763/**
Trond Myklebustc5445772018-09-03 23:39:27 -0400764 * xs_nospace - handle transmit was incomplete
Trond Myklebust50f484e2018-08-30 13:27:29 -0400765 * @req: pointer to RPC request
Chuck Lever9903cd12005-08-11 16:25:26 -0400766 *
Chuck Levera246b012005-08-11 16:25:23 -0400767 */
Trond Myklebustc5445772018-09-03 23:39:27 -0400768static int xs_nospace(struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -0400769{
Chuck Lever262965f2005-08-11 16:25:56 -0400770 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500771 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500772 struct sock *sk = transport->inet;
Trond Myklebust24ca9a82011-11-22 14:44:28 +0200773 int ret = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400774
Chuck Lever015747d2020-07-08 16:09:32 -0400775 trace_rpc_socket_nospace(req, transport);
Chuck Lever262965f2005-08-11 16:25:56 -0400776
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400777 /* Protect against races with write_space */
Trond Myklebustb5e92412019-05-02 11:21:08 -0400778 spin_lock(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400779
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400780 /* Don't race with disconnect */
781 if (xprt_connected(xprt)) {
Trond Myklebust13331a52016-01-06 08:57:06 -0500782 /* wait for more buffer space */
783 sk->sk_write_pending++;
Trond Myklebustc5445772018-09-03 23:39:27 -0400784 xprt_wait_for_buffer_space(xprt);
Trond Myklebust13331a52016-01-06 08:57:06 -0500785 } else
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400786 ret = -ENOTCONN;
Chuck Lever262965f2005-08-11 16:25:56 -0400787
Trond Myklebustb5e92412019-05-02 11:21:08 -0400788 spin_unlock(&xprt->transport_lock);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500789
790 /* Race breaker in case memory is freed before above code is called */
David Vrabeld48f9ce2016-09-19 13:58:30 +0100791 if (ret == -EAGAIN) {
792 struct socket_wq *wq;
793
794 rcu_read_lock();
795 wq = rcu_dereference(sk->sk_wq);
796 set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags);
797 rcu_read_unlock();
798
799 sk->sk_write_space(sk);
800 }
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400801 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400802}
803
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400804static void
805xs_stream_prepare_request(struct rpc_rqst *req)
806{
Trond Myklebust75369082019-07-17 21:22:38 -0400807 xdr_free_bvec(&req->rq_rcv_buf);
Trond Myklebust12a3ad62019-03-02 10:14:02 -0500808 req->rq_task->tk_status = xdr_alloc_bvec(&req->rq_rcv_buf, GFP_KERNEL);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400809}
810
Chuck Lever61677ee2011-05-09 15:22:34 -0400811/*
Trond Myklebust4cd34e72018-08-31 10:00:02 -0400812 * Determine if the previous message in the stream was aborted before it
813 * could complete transmission.
814 */
815static bool
816xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
817{
818 return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
819}
820
821/*
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500822 * Return the stream record marker field for a record of length < 2^31-1
Chuck Lever61677ee2011-05-09 15:22:34 -0400823 */
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500824static rpc_fraghdr
825xs_stream_record_marker(struct xdr_buf *xdr)
Chuck Lever61677ee2011-05-09 15:22:34 -0400826{
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500827 if (!xdr->len)
828 return 0;
829 return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len);
Chuck Lever61677ee2011-05-09 15:22:34 -0400830}
831
Chuck Lever262965f2005-08-11 16:25:56 -0400832/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400833 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
Trond Myklebust50f484e2018-08-30 13:27:29 -0400834 * @req: pointer to RPC request
Chuck Lever176e21e2011-05-09 15:22:44 -0400835 *
836 * Return values:
837 * 0: The request has been sent
838 * EAGAIN: The socket was blocked, please call again later to
839 * complete the request
840 * ENOTCONN: Caller needs to invoke connect logic then call again
Bhaskar Chowdhury12b20ce2020-11-09 15:24:05 +0530841 * other: Some other error occurred, the request was not sent
Chuck Lever176e21e2011-05-09 15:22:44 -0400842 */
Trond Myklebustadfa7142018-09-03 23:58:59 -0400843static int xs_local_send_request(struct rpc_rqst *req)
Chuck Lever176e21e2011-05-09 15:22:44 -0400844{
Chuck Lever176e21e2011-05-09 15:22:44 -0400845 struct rpc_xprt *xprt = req->rq_xprt;
846 struct sock_xprt *transport =
847 container_of(xprt, struct sock_xprt, xprt);
848 struct xdr_buf *xdr = &req->rq_snd_buf;
Trond Myklebust7e3d3622019-06-24 19:15:44 -0400849 rpc_fraghdr rm = xs_stream_record_marker(xdr);
850 unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
Chuck Lever9e55eef42020-03-02 15:19:54 -0500851 struct msghdr msg = {
852 .msg_flags = XS_SENDMSG_FLAGS,
853 };
Kees Cook3f649ab2020-06-03 13:09:38 -0700854 unsigned int sent;
Chuck Lever176e21e2011-05-09 15:22:44 -0400855 int status;
856
Trond Myklebust4cd34e72018-08-31 10:00:02 -0400857 /* Close the stream if the previous transmission was incomplete */
858 if (xs_send_request_was_aborted(transport, req)) {
859 xs_close(xprt);
860 return -ENOTCONN;
861 }
862
Chuck Lever176e21e2011-05-09 15:22:44 -0400863 xs_pktdump("packet data:",
864 req->rq_svec->iov_base, req->rq_svec->iov_len);
865
Chuck Lever78215752018-03-05 15:13:07 -0500866 req->rq_xtime = ktime_get();
Chuck Lever9e55eef42020-03-02 15:19:54 -0500867 status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
868 transport->xmit.offset, rm, &sent);
Chuck Lever176e21e2011-05-09 15:22:44 -0400869 dprintk("RPC: %s(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400870 __func__, xdr->len - transport->xmit.offset, status);
NeilBrown743c69e2015-07-27 10:55:35 +1000871
872 if (status == -EAGAIN && sock_writeable(transport->inet))
873 status = -ENOBUFS;
874
Jason Baronf279cd02014-09-24 18:08:00 +0000875 if (likely(sent > 0) || status == 0) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400876 transport->xmit.offset += sent;
877 req->rq_bytes_sent = transport->xmit.offset;
Trond Myklebust7e3d3622019-06-24 19:15:44 -0400878 if (likely(req->rq_bytes_sent >= msglen)) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400879 req->rq_xmit_bytes_sent += transport->xmit.offset;
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400880 transport->xmit.offset = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -0400881 return 0;
882 }
883 status = -EAGAIN;
884 }
885
886 switch (status) {
Trond Myklebust3601c4a2014-06-30 13:42:19 -0400887 case -ENOBUFS:
Trond Myklebustb5872f02015-07-03 09:32:23 -0400888 break;
Chuck Lever176e21e2011-05-09 15:22:44 -0400889 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -0400890 status = xs_nospace(req);
Chuck Lever176e21e2011-05-09 15:22:44 -0400891 break;
892 default:
893 dprintk("RPC: sendmsg returned unrecognized error %d\n",
894 -status);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500895 fallthrough;
Chuck Lever176e21e2011-05-09 15:22:44 -0400896 case -EPIPE:
897 xs_close(xprt);
898 status = -ENOTCONN;
899 }
900
901 return status;
902}
903
904/**
Chuck Lever262965f2005-08-11 16:25:56 -0400905 * xs_udp_send_request - write an RPC request to a UDP socket
Trond Myklebust50f484e2018-08-30 13:27:29 -0400906 * @req: pointer to RPC request
Chuck Lever262965f2005-08-11 16:25:56 -0400907 *
908 * Return values:
909 * 0: The request has been sent
910 * EAGAIN: The socket was blocked, please call again later to
911 * complete the request
912 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300913 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -0400914 */
Trond Myklebustadfa7142018-09-03 23:58:59 -0400915static int xs_udp_send_request(struct rpc_rqst *req)
Chuck Lever262965f2005-08-11 16:25:56 -0400916{
Chuck Lever262965f2005-08-11 16:25:56 -0400917 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500918 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400919 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Lever9e55eef42020-03-02 15:19:54 -0500920 struct msghdr msg = {
921 .msg_name = xs_addr(xprt),
922 .msg_namelen = xprt->addrlen,
923 .msg_flags = XS_SENDMSG_FLAGS,
924 };
Kees Cook3f649ab2020-06-03 13:09:38 -0700925 unsigned int sent;
Chuck Lever262965f2005-08-11 16:25:56 -0400926 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400927
Chuck Lever9903cd12005-08-11 16:25:26 -0400928 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400929 req->rq_svec->iov_base,
930 req->rq_svec->iov_len);
931
Trond Myklebust01d37c42009-03-11 14:09:39 -0400932 if (!xprt_bound(xprt))
933 return -ENOTCONN;
Trond Myklebust75891f52018-09-03 17:37:36 -0400934
935 if (!xprt_request_get_cong(xprt, req))
936 return -EBADSLT;
937
Chuck Lever78215752018-03-05 15:13:07 -0500938 req->rq_xtime = ktime_get();
Chuck Lever9e55eef42020-03-02 15:19:54 -0500939 status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, 0, &sent);
Chuck Levera246b012005-08-11 16:25:23 -0400940
Chuck Lever46121cf2007-01-31 12:14:08 -0500941 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400942 xdr->len, status);
Chuck Levera246b012005-08-11 16:25:23 -0400943
Jason Baron3dedbb52014-09-24 18:08:04 +0000944 /* firewall is blocking us, don't return -EAGAIN or we end up looping */
945 if (status == -EPERM)
946 goto process_status;
947
NeilBrown743c69e2015-07-27 10:55:35 +1000948 if (status == -EAGAIN && sock_writeable(transport->inet))
949 status = -ENOBUFS;
950
Jason Baronf279cd02014-09-24 18:08:00 +0000951 if (sent > 0 || status == 0) {
952 req->rq_xmit_bytes_sent += sent;
953 if (sent >= req->rq_slen)
Trond Myklebust21997002007-10-01 11:43:37 -0400954 return 0;
955 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400956 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400957 }
Chuck Levera246b012005-08-11 16:25:23 -0400958
Jason Baron3dedbb52014-09-24 18:08:04 +0000959process_status:
Chuck Lever262965f2005-08-11 16:25:56 -0400960 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400961 case -ENOTSOCK:
962 status = -ENOTCONN;
963 /* Should we call xs_close() here? */
964 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400965 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -0400966 status = xs_nospace(req);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400967 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400968 case -ENETUNREACH:
Trond Myklebust3601c4a2014-06-30 13:42:19 -0400969 case -ENOBUFS:
Chuck Lever262965f2005-08-11 16:25:56 -0400970 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400971 case -ECONNREFUSED:
Jason Baron3dedbb52014-09-24 18:08:04 +0000972 case -EPERM:
Chuck Levera246b012005-08-11 16:25:23 -0400973 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400974 * prompts ECONNREFUSED. */
Trond Myklebust13331a52016-01-06 08:57:06 -0500975 break;
976 default:
977 dprintk("RPC: sendmsg returned unrecognized error %d\n",
978 -status);
Chuck Levera246b012005-08-11 16:25:23 -0400979 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800980
Chuck Lever262965f2005-08-11 16:25:56 -0400981 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400982}
983
Trond Myklebuste06799f2007-11-05 15:44:12 -0500984/**
Chuck Lever262965f2005-08-11 16:25:56 -0400985 * xs_tcp_send_request - write an RPC request to a TCP socket
Trond Myklebust50f484e2018-08-30 13:27:29 -0400986 * @req: pointer to RPC request
Chuck Lever9903cd12005-08-11 16:25:26 -0400987 *
988 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400989 * 0: The request has been sent
990 * EAGAIN: The socket was blocked, please call again later to
991 * complete the request
992 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300993 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400994 *
995 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400996 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400997 */
Trond Myklebustadfa7142018-09-03 23:58:59 -0400998static int xs_tcp_send_request(struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -0400999{
Chuck Levera246b012005-08-11 16:25:23 -04001000 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001001 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -04001002 struct xdr_buf *xdr = &req->rq_snd_buf;
Trond Myklebust7e3d3622019-06-24 19:15:44 -04001003 rpc_fraghdr rm = xs_stream_record_marker(xdr);
1004 unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
Chuck Lever9e55eef42020-03-02 15:19:54 -05001005 struct msghdr msg = {
1006 .msg_flags = XS_SENDMSG_FLAGS,
1007 };
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001008 bool vm_wait = false;
Kees Cook3f649ab2020-06-03 13:09:38 -07001009 unsigned int sent;
Chuck Leverb595bb12007-08-06 11:56:42 -04001010 int status;
Chuck Levera246b012005-08-11 16:25:23 -04001011
Trond Myklebust4cd34e72018-08-31 10:00:02 -04001012 /* Close the stream if the previous transmission was incomplete */
1013 if (xs_send_request_was_aborted(transport, req)) {
1014 if (transport->sock != NULL)
1015 kernel_sock_shutdown(transport->sock, SHUT_RDWR);
1016 return -ENOTCONN;
1017 }
Trond Myklebustd2758802021-05-17 08:50:11 -04001018 if (!transport->inet)
1019 return -ENOTCONN;
Trond Myklebust4cd34e72018-08-31 10:00:02 -04001020
Chuck Lever262965f2005-08-11 16:25:56 -04001021 xs_pktdump("packet data:",
1022 req->rq_svec->iov_base,
1023 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -04001024
Trond Myklebust7196dbb2017-02-08 11:17:54 -05001025 if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
1026 xs_tcp_set_socket_timeouts(xprt, transport->sock);
1027
Chuck Levera246b012005-08-11 16:25:23 -04001028 /* Continue transmitting the packet/record. We must be careful
1029 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -04001030 * called sendmsg(). */
Chuck Lever78215752018-03-05 15:13:07 -05001031 req->rq_xtime = ktime_get();
Trond Myklebustd737e5d2021-02-09 16:04:15 -05001032 tcp_sock_set_cork(transport->inet, true);
Chuck Levera246b012005-08-11 16:25:23 -04001033 while (1) {
Chuck Lever9e55eef42020-03-02 15:19:54 -05001034 status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
1035 transport->xmit.offset, rm, &sent);
Chuck Levera246b012005-08-11 16:25:23 -04001036
Chuck Lever46121cf2007-01-31 12:14:08 -05001037 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001038 xdr->len - transport->xmit.offset, status);
Chuck Lever262965f2005-08-11 16:25:56 -04001039
Chuck Lever262965f2005-08-11 16:25:56 -04001040 /* If we've sent the entire packet, immediately
1041 * reset the count of bytes sent. */
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001042 transport->xmit.offset += sent;
1043 req->rq_bytes_sent = transport->xmit.offset;
Trond Myklebust7e3d3622019-06-24 19:15:44 -04001044 if (likely(req->rq_bytes_sent >= msglen)) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001045 req->rq_xmit_bytes_sent += transport->xmit.offset;
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001046 transport->xmit.offset = 0;
Trond Myklebustd737e5d2021-02-09 16:04:15 -05001047 if (atomic_long_read(&xprt->xmit_queuelen) == 1)
1048 tcp_sock_set_cork(transport->inet, false);
Chuck Lever262965f2005-08-11 16:25:56 -04001049 return 0;
Chuck Levera246b012005-08-11 16:25:23 -04001050 }
1051
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001052 WARN_ON_ONCE(sent == 0 && status == 0);
1053
1054 if (status == -EAGAIN ) {
1055 /*
1056 * Return EAGAIN if we're sure we're hitting the
1057 * socket send buffer limits.
1058 */
1059 if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
1060 break;
1061 /*
1062 * Did we hit a memory allocation failure?
1063 */
1064 if (sent == 0) {
1065 status = -ENOBUFS;
1066 if (vm_wait)
1067 break;
1068 /* Retry, knowing now that we're below the
1069 * socket send buffer limit
1070 */
1071 vm_wait = true;
1072 }
1073 continue;
1074 }
Trond Myklebustf580dd02015-07-11 17:48:52 +02001075 if (status < 0)
1076 break;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001077 vm_wait = false;
Chuck Levera246b012005-08-11 16:25:23 -04001078 }
1079
Chuck Lever262965f2005-08-11 16:25:56 -04001080 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -04001081 case -ENOTSOCK:
1082 status = -ENOTCONN;
1083 /* Should we call xs_close() here? */
1084 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001085 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -04001086 status = xs_nospace(req);
Chuck Lever262965f2005-08-11 16:25:56 -04001087 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001088 case -ECONNRESET:
Chuck Lever262965f2005-08-11 16:25:56 -04001089 case -ECONNREFUSED:
1090 case -ENOTCONN:
Trond Myklebust3913c782015-02-08 21:44:04 -05001091 case -EADDRINUSE:
Trond Myklebustb5872f02015-07-03 09:32:23 -04001092 case -ENOBUFS:
Trond Myklebustb9d2bb22012-10-23 11:40:02 -04001093 case -EPIPE:
Trond Myklebust13331a52016-01-06 08:57:06 -05001094 break;
1095 default:
1096 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1097 -status);
Chuck Levera246b012005-08-11 16:25:23 -04001098 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +08001099
Chuck Levera246b012005-08-11 16:25:23 -04001100 return status;
1101}
1102
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001103static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1104{
1105 transport->old_data_ready = sk->sk_data_ready;
1106 transport->old_state_change = sk->sk_state_change;
1107 transport->old_write_space = sk->sk_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -05001108 transport->old_error_report = sk->sk_error_report;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001109}
1110
1111static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1112{
1113 sk->sk_data_ready = transport->old_data_ready;
1114 sk->sk_state_change = transport->old_state_change;
1115 sk->sk_write_space = transport->old_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -05001116 sk->sk_error_report = transport->old_error_report;
1117}
1118
Trond Myklebust42d42a52016-05-23 09:24:55 -04001119static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
1120{
1121 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1122
1123 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001124 clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state);
1125 clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state);
1126 clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state);
1127}
1128
1129static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr)
1130{
1131 set_bit(nr, &transport->sock_state);
1132 queue_work(xprtiod_workqueue, &transport->error_worker);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001133}
1134
Trond Myklebustb70ae912015-02-09 09:41:32 -05001135static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1136{
1137 smp_mb__before_atomic();
1138 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1139 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001140 xs_sock_reset_state_flags(xprt);
Trond Myklebustb70ae912015-02-09 09:41:32 -05001141 smp_mb__after_atomic();
1142}
1143
Trond Myklebust21180712013-12-31 13:22:59 -05001144/**
1145 * xs_error_report - callback to handle TCP socket state errors
1146 * @sk: socket
1147 *
1148 * Note: we don't call sock_error() since there may be a rpc_task
1149 * using the socket, and so we don't want to clear sk->sk_err.
1150 */
1151static void xs_error_report(struct sock *sk)
1152{
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001153 struct sock_xprt *transport;
Trond Myklebust21180712013-12-31 13:22:59 -05001154 struct rpc_xprt *xprt;
Trond Myklebust21180712013-12-31 13:22:59 -05001155
1156 read_lock_bh(&sk->sk_callback_lock);
1157 if (!(xprt = xprt_from_sock(sk)))
1158 goto out;
1159
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001160 transport = container_of(xprt, struct sock_xprt, xprt);
Benjamin Coddingtonaf845372019-10-02 10:40:55 -04001161 transport->xprt_err = -sk->sk_err;
1162 if (transport->xprt_err == 0)
Trond Myklebust21180712013-12-31 13:22:59 -05001163 goto out;
1164 dprintk("RPC: xs_error_report client %p, error=%d...\n",
Benjamin Coddingtonaf845372019-10-02 10:40:55 -04001165 xprt, -transport->xprt_err);
1166 trace_rpc_socket_error(xprt, sk->sk_socket, transport->xprt_err);
1167
1168 /* barrier ensures xprt_err is set before XPRT_SOCK_WAKE_ERROR */
1169 smp_mb__before_atomic();
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001170 xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR);
Trond Myklebust21180712013-12-31 13:22:59 -05001171 out:
1172 read_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001173}
1174
Chuck Leverfe315e762009-03-11 14:10:21 -04001175static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001176{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001177 struct socket *sock = transport->sock;
1178 struct sock *sk = transport->inet;
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001179 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebusta73881c2019-02-26 06:33:02 -05001180 struct file *filp = transport->file;
Chuck Levera246b012005-08-11 16:25:23 -04001181
Chuck Leverfe315e762009-03-11 14:10:21 -04001182 if (sk == NULL)
1183 return;
Chuck Lever9903cd12005-08-11 16:25:26 -04001184
Jeff Layton264d1df2015-06-03 16:14:27 -04001185 if (atomic_read(&transport->xprt.swapper))
1186 sk_clear_memalloc(sk);
1187
Trond Myklebust09939202015-08-29 19:11:21 -07001188 kernel_sock_shutdown(sock, SHUT_RDWR);
1189
Trond Myklebustedc1b012015-10-05 10:53:49 -04001190 mutex_lock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -04001191 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001192 transport->inet = NULL;
1193 transport->sock = NULL;
Trond Myklebusta73881c2019-02-26 06:33:02 -05001194 transport->file = NULL;
Chuck Levera246b012005-08-11 16:25:23 -04001195
Chuck Lever9903cd12005-08-11 16:25:26 -04001196 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001197
1198 xs_restore_old_callbacks(transport, sk);
Trond Myklebust0c787892015-08-29 13:36:30 -07001199 xprt_clear_connected(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001200 write_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001201 xs_sock_reset_connection_flags(xprt);
Trond Myklebustae053552019-02-20 14:56:20 -05001202 /* Reset stream record info */
1203 xs_stream_reset_connect(transport);
Trond Myklebustedc1b012015-10-05 10:53:49 -04001204 mutex_unlock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -04001205
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001206 trace_rpc_socket_close(xprt, sock);
Trond Myklebusta73881c2019-02-26 06:33:02 -05001207 fput(filp);
Trond Myklebust0445f922018-12-17 13:34:59 -05001208
1209 xprt_disconnect_done(xprt);
Chuck Leverfe315e762009-03-11 14:10:21 -04001210}
1211
1212/**
1213 * xs_close - close a socket
1214 * @xprt: transport
1215 *
1216 * This is used when all requests are complete; ie, no DRC state remains
1217 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -04001218 *
1219 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1220 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e762009-03-11 14:10:21 -04001221 */
1222static void xs_close(struct rpc_xprt *xprt)
1223{
1224 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1225
1226 dprintk("RPC: xs_close xprt %p\n", xprt);
1227
1228 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -04001229 xprt->reestablish_timeout = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001230}
1231
Chuck Lever4a068252015-05-11 14:02:25 -04001232static void xs_inject_disconnect(struct rpc_xprt *xprt)
1233{
1234 dprintk("RPC: injecting transport disconnect on xprt=%p\n",
1235 xprt);
1236 xprt_disconnect_done(xprt);
1237}
1238
Kinglong Mee315f3812014-03-24 11:07:22 +08001239static void xs_xprt_free(struct rpc_xprt *xprt)
1240{
1241 xs_free_peer_addresses(xprt);
1242 xprt_free(xprt);
1243}
1244
Chuck Lever9903cd12005-08-11 16:25:26 -04001245/**
1246 * xs_destroy - prepare to shutdown a transport
1247 * @xprt: doomed transport
1248 *
1249 */
1250static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001251{
Trond Myklebust03c788272015-09-17 10:42:27 -04001252 struct sock_xprt *transport = container_of(xprt,
1253 struct sock_xprt, xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05001254 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001255
Trond Myklebust03c788272015-09-17 10:42:27 -04001256 cancel_delayed_work_sync(&transport->connect_worker);
Trond Myklebusta1311d82013-10-31 09:18:49 -04001257 xs_close(xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04001258 cancel_work_sync(&transport->recv_worker);
Trond Myklebustb5e92412019-05-02 11:21:08 -04001259 cancel_work_sync(&transport->error_worker);
Kinglong Mee315f3812014-03-24 11:07:22 +08001260 xs_xprt_free(xprt);
Trond Myklebusta1311d82013-10-31 09:18:49 -04001261 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -04001262}
1263
Chuck Lever9903cd12005-08-11 16:25:26 -04001264/**
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001265 * xs_udp_data_read_skb - receive callback for UDP sockets
1266 * @xprt: transport
1267 * @sk: socket
1268 * @skb: skbuff
Chuck Lever9903cd12005-08-11 16:25:26 -04001269 *
1270 */
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001271static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1272 struct sock *sk,
1273 struct sk_buff *skb)
Chuck Lever9903cd12005-08-11 16:25:26 -04001274{
1275 struct rpc_task *task;
Chuck Levera246b012005-08-11 16:25:23 -04001276 struct rpc_rqst *rovr;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001277 int repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001278 u32 _xid;
1279 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -04001280
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001281 repsize = skb->len;
Chuck Levera246b012005-08-11 16:25:23 -04001282 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001283 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001284 return;
Chuck Levera246b012005-08-11 16:25:23 -04001285 }
1286
1287 /* Copy the XID from the skb... */
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001288 xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
Chuck Levera246b012005-08-11 16:25:23 -04001289 if (xp == NULL)
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001290 return;
Chuck Levera246b012005-08-11 16:25:23 -04001291
1292 /* Look up and lock the request corresponding to the given XID */
Trond Myklebust75c84152018-08-31 10:21:00 -04001293 spin_lock(&xprt->queue_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001294 rovr = xprt_lookup_rqst(xprt, *xp);
1295 if (!rovr)
1296 goto out_unlock;
Trond Myklebust729749b2017-08-13 10:03:59 -04001297 xprt_pin_rqst(rovr);
Chuck Leverecd465e2018-03-05 15:12:57 -05001298 xprt_update_rtt(rovr->rq_task);
Trond Myklebust75c84152018-08-31 10:21:00 -04001299 spin_unlock(&xprt->queue_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001300 task = rovr->rq_task;
1301
Chuck Levera246b012005-08-11 16:25:23 -04001302 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1303 copied = repsize;
1304
1305 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001306 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
Trond Myklebust75c84152018-08-31 10:21:00 -04001307 spin_lock(&xprt->queue_lock);
Trond Myklebust0afa6b42018-02-09 09:39:42 -05001308 __UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
Trond Myklebust729749b2017-08-13 10:03:59 -04001309 goto out_unpin;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001310 }
1311
Chuck Levera246b012005-08-11 16:25:23 -04001312
Trond Myklebustb5e92412019-05-02 11:21:08 -04001313 spin_lock(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001314 xprt_adjust_cwnd(xprt, task, copied);
Trond Myklebustb5e92412019-05-02 11:21:08 -04001315 spin_unlock(&xprt->transport_lock);
Trond Myklebust75c84152018-08-31 10:21:00 -04001316 spin_lock(&xprt->queue_lock);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001317 xprt_complete_rqst(task, copied);
Trond Myklebust0afa6b42018-02-09 09:39:42 -05001318 __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
Trond Myklebust729749b2017-08-13 10:03:59 -04001319out_unpin:
1320 xprt_unpin_rqst(rovr);
Chuck Levera246b012005-08-11 16:25:23 -04001321 out_unlock:
Trond Myklebust75c84152018-08-31 10:21:00 -04001322 spin_unlock(&xprt->queue_lock);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001323}
1324
1325static void xs_udp_data_receive(struct sock_xprt *transport)
1326{
1327 struct sk_buff *skb;
1328 struct sock *sk;
1329 int err;
1330
1331 mutex_lock(&transport->recv_mutex);
1332 sk = transport->inet;
1333 if (sk == NULL)
1334 goto out;
1335 for (;;) {
Paolo Abeni7c13f972016-11-04 11:28:59 +01001336 skb = skb_recv_udp(sk, 0, 1, &err);
Trond Myklebust4f546142018-09-14 17:45:23 -04001337 if (skb == NULL)
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001338 break;
Trond Myklebust4f546142018-09-14 17:45:23 -04001339 xs_udp_data_read_skb(&transport->xprt, sk, skb);
1340 consume_skb(skb);
1341 cond_resched();
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001342 }
Trond Myklebust0ffe86f2019-01-30 14:51:26 -05001343 xs_poll_check_readable(transport);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001344out:
1345 mutex_unlock(&transport->recv_mutex);
1346}
1347
1348static void xs_udp_data_receive_workfn(struct work_struct *work)
1349{
1350 struct sock_xprt *transport =
1351 container_of(work, struct sock_xprt, recv_worker);
Trond Myklebusta1231fd2019-02-18 10:02:29 -05001352 unsigned int pflags = memalloc_nofs_save();
1353
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001354 xs_udp_data_receive(transport);
Trond Myklebusta1231fd2019-02-18 10:02:29 -05001355 memalloc_nofs_restore(pflags);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001356}
1357
1358/**
1359 * xs_data_ready - "data ready" callback for UDP sockets
1360 * @sk: socket with data to read
1361 *
1362 */
1363static void xs_data_ready(struct sock *sk)
1364{
1365 struct rpc_xprt *xprt;
1366
1367 read_lock_bh(&sk->sk_callback_lock);
1368 dprintk("RPC: xs_data_ready...\n");
1369 xprt = xprt_from_sock(sk);
1370 if (xprt != NULL) {
1371 struct sock_xprt *transport = container_of(xprt,
1372 struct sock_xprt, xprt);
Trond Myklebust5157b952016-05-29 10:13:24 -04001373 transport->old_data_ready(sk);
1374 /* Any data means we had a useful conversation, so
1375 * then we don't need to delay the next reconnect
1376 */
1377 if (xprt->reestablish_timeout)
1378 xprt->reestablish_timeout = 0;
Trond Myklebust42d42a52016-05-23 09:24:55 -04001379 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebust40a5f1b2016-05-27 10:39:50 -04001380 queue_work(xprtiod_workqueue, &transport->recv_worker);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001381 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001382 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001383}
1384
Trond Myklebusta519fc72012-09-12 16:49:15 -04001385/*
1386 * Helper function to force a TCP close if the server is sending
1387 * junk and/or it has put us in CLOSE_WAIT
1388 */
1389static void xs_tcp_force_close(struct rpc_xprt *xprt)
1390{
Trond Myklebusta519fc72012-09-12 16:49:15 -04001391 xprt_force_disconnect(xprt);
1392}
1393
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001394#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Chuck Lever6b26cc82016-05-02 14:40:40 -04001395static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
1396{
1397 return PAGE_SIZE;
1398}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001399#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001400
Chuck Lever9903cd12005-08-11 16:25:26 -04001401/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001402 * xs_tcp_state_change - callback to handle TCP socket state changes
1403 * @sk: socket whose state has changed
1404 *
1405 */
1406static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001407{
Chuck Lever9903cd12005-08-11 16:25:26 -04001408 struct rpc_xprt *xprt;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001409 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001410
Eric Dumazetf064af12010-09-22 12:43:39 +00001411 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001412 if (!(xprt = xprt_from_sock(sk)))
1413 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001414 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001415 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001416 sk->sk_state, xprt_connected(xprt),
1417 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001418 sock_flag(sk, SOCK_ZAPPED),
1419 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001420
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001421 transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001422 trace_rpc_socket_state_change(xprt, sk->sk_socket);
Chuck Levera246b012005-08-11 16:25:23 -04001423 switch (sk->sk_state) {
1424 case TCP_ESTABLISHED:
Chuck Levera246b012005-08-11 16:25:23 -04001425 if (!xprt_test_and_set_connected(xprt)) {
Trond Myklebust8b717982013-09-26 10:18:04 -04001426 xprt->connect_cookie++;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001427 clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
1428 xprt_clear_connecting(xprt);
Chuck Lever51971132006-12-05 16:35:19 -05001429
Chuck Lever3968a8a2018-10-01 14:25:36 -04001430 xprt->stat.connect_count++;
1431 xprt->stat.connect_time += (long)jiffies -
1432 xprt->stat.connect_start;
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001433 xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING);
Chuck Levera246b012005-08-11 16:25:23 -04001434 }
Chuck Levera246b012005-08-11 16:25:23 -04001435 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001436 case TCP_FIN_WAIT1:
1437 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001438 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001439 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001440 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001441 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001442 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001443 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001444 smp_mb__after_atomic();
Chuck Levera246b012005-08-11 16:25:23 -04001445 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001446 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001447 /* The server initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001448 xprt->connect_cookie++;
Trond Myklebustd0bea452012-10-23 11:35:47 -04001449 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001450 xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001451 fallthrough;
Trond Myklebust663b8852008-01-01 18:42:12 -05001452 case TCP_CLOSING:
1453 /*
1454 * If the server closed down the connection, make sure that
1455 * we back off before reconnecting
1456 */
1457 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1458 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001459 break;
1460 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001461 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001462 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001463 clear_bit(XPRT_CONNECTED, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001464 smp_mb__after_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001465 break;
1466 case TCP_CLOSE:
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001467 if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
1468 &transport->sock_state))
1469 xprt_clear_connecting(xprt);
Trond Myklebust9b308892018-02-05 10:20:06 -05001470 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust9b308892018-02-05 10:20:06 -05001471 /* Trigger the socket release */
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001472 xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
Chuck Levera246b012005-08-11 16:25:23 -04001473 }
1474 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001475 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001476}
1477
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001478static void xs_write_space(struct sock *sk)
1479{
Trond Myklebust13331a52016-01-06 08:57:06 -05001480 struct socket_wq *wq;
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001481 struct sock_xprt *transport;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001482 struct rpc_xprt *xprt;
1483
Trond Myklebust13331a52016-01-06 08:57:06 -05001484 if (!sk->sk_socket)
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001485 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001486 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001487
1488 if (unlikely(!(xprt = xprt_from_sock(sk))))
1489 return;
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001490 transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust13331a52016-01-06 08:57:06 -05001491 rcu_read_lock();
1492 wq = rcu_dereference(sk->sk_wq);
1493 if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0)
1494 goto out;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001495
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001496 xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE);
1497 sk->sk_write_pending--;
Trond Myklebust13331a52016-01-06 08:57:06 -05001498out:
1499 rcu_read_unlock();
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001500}
1501
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001502/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001503 * xs_udp_write_space - callback invoked when socket buffer space
1504 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001505 * @sk: socket whose state has changed
1506 *
Chuck Levera246b012005-08-11 16:25:23 -04001507 * Called when more output buffer space is available for this socket.
1508 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001509 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001510 * with a bunch of small requests.
1511 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001512static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001513{
Eric Dumazetf064af12010-09-22 12:43:39 +00001514 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001515
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001516 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001517 if (sock_writeable(sk))
1518 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001519
Eric Dumazetf064af12010-09-22 12:43:39 +00001520 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001521}
Chuck Levera246b012005-08-11 16:25:23 -04001522
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001523/**
1524 * xs_tcp_write_space - callback invoked when socket buffer space
1525 * becomes available
1526 * @sk: socket whose state has changed
1527 *
1528 * Called when more output buffer space is available for this socket.
1529 * We try not to wake our writers until they can make "significant"
1530 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1531 * with a bunch of small requests.
1532 */
1533static void xs_tcp_write_space(struct sock *sk)
1534{
Eric Dumazetf064af12010-09-22 12:43:39 +00001535 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001536
1537 /* from net/core/stream.c:sk_stream_write_space */
Eric Dumazet64dc6132013-07-22 20:26:31 -07001538 if (sk_stream_is_writeable(sk))
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001539 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001540
Eric Dumazetf064af12010-09-22 12:43:39 +00001541 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001542}
1543
Chuck Lever470056c2005-08-25 16:25:56 -07001544static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001545{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001546 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1547 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001548
Chuck Lever7c6e0662006-12-05 16:35:30 -05001549 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001550 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001551 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001552 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001553 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001554 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001555 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001556 sk->sk_write_space(sk);
1557 }
1558}
1559
Chuck Lever43118c22005-08-25 16:25:49 -07001560/**
Chuck Lever470056c2005-08-25 16:25:56 -07001561 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001562 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001563 * @sndsize: requested size of send buffer, in bytes
1564 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001565 *
Chuck Lever470056c2005-08-25 16:25:56 -07001566 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001567 */
Chuck Lever470056c2005-08-25 16:25:56 -07001568static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001569{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001570 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1571
1572 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001573 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001574 transport->sndsize = sndsize + 1024;
1575 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001576 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001577 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001578
1579 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001580}
1581
Chuck Lever46c0ee82005-08-25 16:25:52 -07001582/**
1583 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
Chuck Leveracf0a392018-12-19 11:00:22 -05001584 * @xprt: controlling transport
Chuck Lever46c0ee82005-08-25 16:25:52 -07001585 * @task: task that timed out
1586 *
1587 * Adjust the congestion window after a retransmit timeout has occurred.
1588 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001589static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever46c0ee82005-08-25 16:25:52 -07001590{
Trond Myklebustb5e92412019-05-02 11:21:08 -04001591 spin_lock(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001592 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
Trond Myklebustb5e92412019-05-02 11:21:08 -04001593 spin_unlock(&xprt->transport_lock);
Chuck Lever46c0ee82005-08-25 16:25:52 -07001594}
1595
J. Bruce Fields826799e2018-10-18 15:27:02 -04001596static int xs_get_random_port(void)
Chuck Leverb85d8802006-05-25 01:40:49 -04001597{
J. Bruce Fields826799e2018-10-18 15:27:02 -04001598 unsigned short min = xprt_min_resvport, max = xprt_max_resvport;
1599 unsigned short range;
1600 unsigned short rand;
1601
1602 if (max < min)
1603 return -EADDRINUSE;
1604 range = max - min + 1;
1605 rand = (unsigned short) prandom_u32() % range;
1606 return rand + min;
Chuck Leverb85d8802006-05-25 01:40:49 -04001607}
1608
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001609static unsigned short xs_sock_getport(struct socket *sock)
1610{
1611 struct sockaddr_storage buf;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001612 unsigned short port = 0;
1613
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001614 if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001615 goto out;
1616 switch (buf.ss_family) {
1617 case AF_INET6:
1618 port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
1619 break;
1620 case AF_INET:
1621 port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
1622 }
1623out:
1624 return port;
1625}
1626
1627/**
Chuck Lever92200412006-01-03 09:55:51 +01001628 * xs_set_port - reset the port number in the remote endpoint address
1629 * @xprt: generic transport
1630 * @port: new port number
1631 *
1632 */
1633static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1634{
Chuck Lever46121cf2007-01-31 12:14:08 -05001635 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001636
Chuck Lever9dc3b092009-08-09 15:09:46 -04001637 rpc_set_port(xs_addr(xprt), port);
1638 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001639}
1640
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001641static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
1642{
Trond Myklebuste6237b62019-10-17 11:13:54 -04001643 if (transport->srcport == 0 && transport->xprt.reuseport)
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001644 transport->srcport = xs_sock_getport(sock);
1645}
1646
J. Bruce Fields826799e2018-10-18 15:27:02 -04001647static int xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001648{
J. Bruce Fields826799e2018-10-18 15:27:02 -04001649 int port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001650
1651 if (port == 0 && transport->xprt.resvport)
1652 port = xs_get_random_port();
1653 return port;
1654}
1655
Olga Kornievskaiaa8482482021-06-23 23:28:48 -04001656unsigned short get_srcport(struct rpc_xprt *xprt)
1657{
1658 struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt);
Anna Schumaker5d46dd02021-07-20 16:04:42 -04001659 return xs_sock_getport(sock->sock);
Olga Kornievskaiaa8482482021-06-23 23:28:48 -04001660}
1661EXPORT_SYMBOL(get_srcport);
1662
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001663static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001664{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001665 if (transport->srcport != 0)
1666 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001667 if (!transport->xprt.resvport)
1668 return 0;
1669 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1670 return xprt_max_resvport;
1671 return --port;
1672}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001673static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001674{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001675 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001676 int err, nloop = 0;
J. Bruce Fields826799e2018-10-18 15:27:02 -04001677 int port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001678 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001679
Chris Perl0f7a6222014-09-05 15:40:21 -04001680 /*
1681 * If we are asking for any ephemeral port (i.e. port == 0 &&
1682 * transport->xprt.resvport == 0), don't bind. Let the local
1683 * port selection happen implicitly when the socket is used
1684 * (for example at connect time).
1685 *
1686 * This ensures that we can continue to establish TCP
1687 * connections even when all local ephemeral ports are already
1688 * a part of some TCP connection. This makes no difference
Bhaskar Chowdhury12b20ce2020-11-09 15:24:05 +05301689 * for UDP sockets, but also doesn't harm them.
Chris Perl0f7a6222014-09-05 15:40:21 -04001690 *
1691 * If we're asking for any reserved port (i.e. port == 0 &&
1692 * transport->xprt.resvport == 1) xs_get_srcport above will
1693 * ensure that port is non-zero and we will bind as needed.
1694 */
J. Bruce Fields826799e2018-10-18 15:27:02 -04001695 if (port <= 0)
1696 return port;
Chris Perl0f7a6222014-09-05 15:40:21 -04001697
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001698 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001699 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001700 rpc_set_port((struct sockaddr *)&myaddr, port);
1701 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1702 transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001703 if (err == 0) {
NeilBrownbc1c56e2021-06-15 11:18:38 +10001704 if (transport->xprt.reuseport)
1705 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001706 break;
Chuck Levera246b012005-08-11 16:25:23 -04001707 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001708 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001709 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001710 if (port > last)
1711 nloop++;
1712 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001713
Chuck Lever4232e862010-10-20 11:52:51 -04001714 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001715 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1716 &((struct sockaddr_in *)&myaddr)->sin_addr,
1717 port, err ? "failed" : "ok", err);
1718 else
1719 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1720 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1721 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001722 return err;
1723}
1724
Chuck Lever176e21e2011-05-09 15:22:44 -04001725/*
1726 * We don't support autobind on AF_LOCAL sockets
1727 */
1728static void xs_local_rpcbind(struct rpc_task *task)
1729{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001730 xprt_set_bound(task->tk_xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04001731}
1732
1733static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1734{
1735}
Chuck Levera246b012005-08-11 16:25:23 -04001736
Peter Zijlstraed075362006-12-06 20:35:24 -08001737#ifdef CONFIG_DEBUG_LOCK_ALLOC
1738static struct lock_class_key xs_key[2];
1739static struct lock_class_key xs_slock_key[2];
1740
Chuck Lever176e21e2011-05-09 15:22:44 -04001741static inline void xs_reclassify_socketu(struct socket *sock)
1742{
1743 struct sock *sk = sock->sk;
1744
Chuck Lever176e21e2011-05-09 15:22:44 -04001745 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1746 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1747}
1748
Chuck Lever8945ee52007-08-06 11:58:04 -04001749static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001750{
1751 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001752
Chuck Lever8945ee52007-08-06 11:58:04 -04001753 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1754 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1755}
Peter Zijlstraed075362006-12-06 20:35:24 -08001756
Chuck Lever8945ee52007-08-06 11:58:04 -04001757static inline void xs_reclassify_socket6(struct socket *sock)
1758{
1759 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001760
Chuck Lever8945ee52007-08-06 11:58:04 -04001761 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1762 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001763}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001764
1765static inline void xs_reclassify_socket(int family, struct socket *sock)
1766{
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02001767 if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -04001768 return;
1769
Chuck Lever4232e862010-10-20 11:52:51 -04001770 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001771 case AF_LOCAL:
1772 xs_reclassify_socketu(sock);
1773 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001774 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001775 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001776 break;
1777 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001778 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001779 break;
1780 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001781}
Peter Zijlstraed075362006-12-06 20:35:24 -08001782#else
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001783static inline void xs_reclassify_socket(int family, struct socket *sock)
1784{
1785}
Peter Zijlstraed075362006-12-06 20:35:24 -08001786#endif
1787
NeilBrown93dc41b2013-10-31 16:14:36 +11001788static void xs_dummy_setup_socket(struct work_struct *work)
1789{
1790}
1791
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001792static struct socket *xs_create_sock(struct rpc_xprt *xprt,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001793 struct sock_xprt *transport, int family, int type,
1794 int protocol, bool reuseport)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001795{
Trond Myklebusta73881c2019-02-26 06:33:02 -05001796 struct file *filp;
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001797 struct socket *sock;
1798 int err;
1799
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001800 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001801 if (err < 0) {
1802 dprintk("RPC: can't create %d transport socket (%d).\n",
1803 protocol, -err);
1804 goto out;
1805 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001806 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001807
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001808 if (reuseport)
Christoph Hellwigfe31a322020-05-28 07:12:17 +02001809 sock_set_reuseport(sock->sk);
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001810
Ben Hutchings4cea2882011-02-22 21:54:34 +00001811 err = xs_bind(transport, sock);
1812 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001813 sock_release(sock);
1814 goto out;
1815 }
1816
Trond Myklebusta73881c2019-02-26 06:33:02 -05001817 filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1818 if (IS_ERR(filp))
1819 return ERR_CAST(filp);
1820 transport->file = filp;
1821
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001822 return sock;
1823out:
1824 return ERR_PTR(err);
1825}
1826
Chuck Lever176e21e2011-05-09 15:22:44 -04001827static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1828 struct socket *sock)
1829{
1830 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1831 xprt);
1832
1833 if (!transport->inet) {
1834 struct sock *sk = sock->sk;
1835
1836 write_lock_bh(&sk->sk_callback_lock);
1837
1838 xs_save_old_callbacks(transport, sk);
1839
1840 sk->sk_user_data = xprt;
Trond Myklebusta2648092015-10-06 17:03:00 -04001841 sk->sk_data_ready = xs_data_ready;
Chuck Lever176e21e2011-05-09 15:22:44 -04001842 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07001843 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05001844 sk->sk_error_report = xs_error_report;
Chuck Lever176e21e2011-05-09 15:22:44 -04001845
1846 xprt_clear_connected(xprt);
1847
1848 /* Reset to new socket */
1849 transport->sock = sock;
1850 transport->inet = sk;
1851
1852 write_unlock_bh(&sk->sk_callback_lock);
1853 }
1854
Trond Myklebustae053552019-02-20 14:56:20 -05001855 xs_stream_start_connect(transport);
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001856
Chuck Lever176e21e2011-05-09 15:22:44 -04001857 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1858}
1859
1860/**
1861 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
Chuck Lever176e21e2011-05-09 15:22:44 -04001862 * @transport: socket transport to connect
Chuck Lever176e21e2011-05-09 15:22:44 -04001863 */
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001864static int xs_local_setup_socket(struct sock_xprt *transport)
Chuck Lever176e21e2011-05-09 15:22:44 -04001865{
Chuck Lever176e21e2011-05-09 15:22:44 -04001866 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebusta73881c2019-02-26 06:33:02 -05001867 struct file *filp;
Chuck Lever176e21e2011-05-09 15:22:44 -04001868 struct socket *sock;
Colin Ian King68e9a242020-02-28 23:44:14 +00001869 int status;
Chuck Lever176e21e2011-05-09 15:22:44 -04001870
Chuck Lever176e21e2011-05-09 15:22:44 -04001871 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1872 SOCK_STREAM, 0, &sock, 1);
1873 if (status < 0) {
1874 dprintk("RPC: can't create AF_LOCAL "
1875 "transport socket (%d).\n", -status);
1876 goto out;
1877 }
Stefan Hajnoczid1358912015-12-02 14:17:52 +08001878 xs_reclassify_socket(AF_LOCAL, sock);
Chuck Lever176e21e2011-05-09 15:22:44 -04001879
Trond Myklebusta73881c2019-02-26 06:33:02 -05001880 filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1881 if (IS_ERR(filp)) {
1882 status = PTR_ERR(filp);
1883 goto out;
1884 }
1885 transport->file = filp;
1886
Chuck Lever176e21e2011-05-09 15:22:44 -04001887 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1888 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1889
1890 status = xs_local_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001891 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever176e21e2011-05-09 15:22:44 -04001892 switch (status) {
1893 case 0:
1894 dprintk("RPC: xprt %p connected to %s\n",
1895 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
Chuck Lever3968a8a2018-10-01 14:25:36 -04001896 xprt->stat.connect_count++;
1897 xprt->stat.connect_time += (long)jiffies -
1898 xprt->stat.connect_start;
Chuck Lever176e21e2011-05-09 15:22:44 -04001899 xprt_set_connected(xprt);
Gustavo A. R. Silva93f479d2020-11-20 12:40:02 -06001900 break;
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001901 case -ENOBUFS:
Chuck Lever176e21e2011-05-09 15:22:44 -04001902 break;
1903 case -ENOENT:
1904 dprintk("RPC: xprt %p: socket %s does not exist\n",
1905 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1906 break;
Trond Myklebust4a20a982012-12-15 17:02:29 -05001907 case -ECONNREFUSED:
1908 dprintk("RPC: xprt %p: connection refused for %s\n",
1909 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1910 break;
Chuck Lever176e21e2011-05-09 15:22:44 -04001911 default:
1912 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1913 __func__, -status,
1914 xprt->address_strings[RPC_DISPLAY_ADDR]);
1915 }
1916
1917out:
1918 xprt_clear_connecting(xprt);
1919 xprt_wake_pending_tasks(xprt, status);
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001920 return status;
1921}
1922
Linus Torvaldsb6669732013-02-28 18:02:55 -08001923static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001924{
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001925 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1926 int ret;
1927
1928 if (RPC_IS_ASYNC(task)) {
1929 /*
1930 * We want the AF_LOCAL connect to be resolved in the
1931 * filesystem namespace of the process making the rpc
1932 * call. Thus we connect synchronously.
1933 *
1934 * If we want to support asynchronous AF_LOCAL calls,
1935 * we'll need to figure out how to pass a namespace to
1936 * connect.
1937 */
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001938 task->tk_rpc_status = -ENOTCONN;
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001939 rpc_exit(task, -ENOTCONN);
1940 return;
1941 }
1942 ret = xs_local_setup_socket(transport);
1943 if (ret && !RPC_IS_SOFTCONN(task))
1944 msleep_interruptible(15000);
Chuck Lever176e21e2011-05-09 15:22:44 -04001945}
1946
Jeff Layton3c87ef62015-06-03 16:14:25 -04001947#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Jeff Laytond6e971d2015-06-03 16:14:28 -04001948/*
1949 * Note that this should be called with XPRT_LOCKED held (or when we otherwise
1950 * know that we have exclusive access to the socket), to guard against
1951 * races with xs_reset_transport.
1952 */
Mel Gormana564b8f2012-07-31 16:45:12 -07001953static void xs_set_memalloc(struct rpc_xprt *xprt)
1954{
1955 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1956 xprt);
1957
Jeff Laytond6e971d2015-06-03 16:14:28 -04001958 /*
1959 * If there's no sock, then we have nothing to set. The
1960 * reconnecting process will get it for us.
1961 */
1962 if (!transport->inet)
1963 return;
Jeff Layton8e228132015-06-03 16:14:26 -04001964 if (atomic_read(&xprt->swapper))
Mel Gormana564b8f2012-07-31 16:45:12 -07001965 sk_set_memalloc(transport->inet);
1966}
1967
1968/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04001969 * xs_enable_swap - Tag this transport as being used for swap.
Mel Gormana564b8f2012-07-31 16:45:12 -07001970 * @xprt: transport to tag
Mel Gormana564b8f2012-07-31 16:45:12 -07001971 *
Jeff Layton8e228132015-06-03 16:14:26 -04001972 * Take a reference to this transport on behalf of the rpc_clnt, and
1973 * optionally mark it for swapping if it wasn't already.
Mel Gormana564b8f2012-07-31 16:45:12 -07001974 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04001975static int
1976xs_enable_swap(struct rpc_xprt *xprt)
Mel Gormana564b8f2012-07-31 16:45:12 -07001977{
Jeff Laytond6e971d2015-06-03 16:14:28 -04001978 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Mel Gormana564b8f2012-07-31 16:45:12 -07001979
Jeff Laytond6e971d2015-06-03 16:14:28 -04001980 if (atomic_inc_return(&xprt->swapper) != 1)
1981 return 0;
1982 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
1983 return -ERESTARTSYS;
1984 if (xs->inet)
1985 sk_set_memalloc(xs->inet);
1986 xprt_release_xprt(xprt, NULL);
Jeff Layton8e228132015-06-03 16:14:26 -04001987 return 0;
Mel Gormana564b8f2012-07-31 16:45:12 -07001988}
Jeff Layton8e228132015-06-03 16:14:26 -04001989
1990/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04001991 * xs_disable_swap - Untag this transport as being used for swap.
Jeff Layton8e228132015-06-03 16:14:26 -04001992 * @xprt: transport to tag
1993 *
1994 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
1995 * swapper refcount goes to 0, untag the socket as a memalloc socket.
1996 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04001997static void
1998xs_disable_swap(struct rpc_xprt *xprt)
Jeff Layton8e228132015-06-03 16:14:26 -04001999{
Jeff Laytond6e971d2015-06-03 16:14:28 -04002000 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Jeff Layton8e228132015-06-03 16:14:26 -04002001
Jeff Laytond6e971d2015-06-03 16:14:28 -04002002 if (!atomic_dec_and_test(&xprt->swapper))
2003 return;
2004 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2005 return;
2006 if (xs->inet)
2007 sk_clear_memalloc(xs->inet);
2008 xprt_release_xprt(xprt, NULL);
Mel Gormana564b8f2012-07-31 16:45:12 -07002009}
Mel Gormana564b8f2012-07-31 16:45:12 -07002010#else
2011static void xs_set_memalloc(struct rpc_xprt *xprt)
2012{
2013}
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002014
2015static int
2016xs_enable_swap(struct rpc_xprt *xprt)
2017{
2018 return -EINVAL;
2019}
2020
2021static void
2022xs_disable_swap(struct rpc_xprt *xprt)
2023{
2024}
Mel Gormana564b8f2012-07-31 16:45:12 -07002025#endif
2026
Chuck Lever16be2d22007-08-06 11:57:38 -04002027static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04002028{
Chuck Lever16be2d22007-08-06 11:57:38 -04002029 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04002030
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002031 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002032 struct sock *sk = sock->sk;
2033
2034 write_lock_bh(&sk->sk_callback_lock);
2035
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002036 xs_save_old_callbacks(transport, sk);
2037
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002038 sk->sk_user_data = xprt;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002039 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002040 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002041 sock_set_flag(sk, SOCK_FASYNC);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002042
2043 xprt_set_connected(xprt);
2044
2045 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002046 transport->sock = sock;
2047 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002048
Mel Gormana564b8f2012-07-31 16:45:12 -07002049 xs_set_memalloc(xprt);
2050
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002051 write_unlock_bh(&sk->sk_callback_lock);
2052 }
Chuck Lever470056c2005-08-25 16:25:56 -07002053 xs_udp_do_set_buffer_size(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002054
2055 xprt->stat.connect_start = jiffies;
Chuck Lever16be2d22007-08-06 11:57:38 -04002056}
2057
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002058static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04002059{
2060 struct sock_xprt *transport =
2061 container_of(work, struct sock_xprt, connect_worker.work);
2062 struct rpc_xprt *xprt = &transport->xprt;
Colin Ian Kingd099b8a2017-09-18 12:21:14 +01002063 struct socket *sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002064 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04002065
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002066 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002067 xs_addr(xprt)->sa_family, SOCK_DGRAM,
2068 IPPROTO_UDP, false);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002069 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04002070 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04002071
Chuck Leverc740eff2009-08-09 15:09:46 -04002072 dprintk("RPC: worker connecting xprt %p via %s to "
2073 "%s (port %s)\n", xprt,
2074 xprt->address_strings[RPC_DISPLAY_PROTO],
2075 xprt->address_strings[RPC_DISPLAY_ADDR],
2076 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04002077
2078 xs_udp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002079 trace_rpc_socket_connect(xprt, sock, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002080 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002081out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002082 xprt_clear_connecting(xprt);
Trond Myklebustcf767852018-12-17 17:38:51 -05002083 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002084 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002085}
2086
Trond Myklebust4876cc72015-06-19 16:17:57 -04002087/**
2088 * xs_tcp_shutdown - gracefully shut down a TCP socket
2089 * @xprt: transport
2090 *
2091 * Initiates a graceful shutdown of the TCP socket by calling the
2092 * equivalent of shutdown(SHUT_RDWR);
2093 */
2094static void xs_tcp_shutdown(struct rpc_xprt *xprt)
2095{
2096 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2097 struct socket *sock = transport->sock;
Trond Myklebust9b308892018-02-05 10:20:06 -05002098 int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
Trond Myklebust4876cc72015-06-19 16:17:57 -04002099
2100 if (sock == NULL)
2101 return;
Trond Myklebust0a6ff582021-08-24 11:38:17 -04002102 if (!xprt->reuseport) {
2103 xs_close(xprt);
2104 return;
2105 }
Trond Myklebust9b308892018-02-05 10:20:06 -05002106 switch (skst) {
Trond Myklebust7c81e6a2021-08-24 11:38:18 -04002107 case TCP_FIN_WAIT1:
2108 case TCP_FIN_WAIT2:
2109 break;
2110 case TCP_ESTABLISHED:
2111 case TCP_CLOSE_WAIT:
Trond Myklebust4876cc72015-06-19 16:17:57 -04002112 kernel_sock_shutdown(sock, SHUT_RDWR);
2113 trace_rpc_socket_shutdown(xprt, sock);
Trond Myklebust9b308892018-02-05 10:20:06 -05002114 break;
Trond Myklebust7c81e6a2021-08-24 11:38:18 -04002115 default:
Trond Myklebust4876cc72015-06-19 16:17:57 -04002116 xs_reset_transport(transport);
Trond Myklebust9b308892018-02-05 10:20:06 -05002117 }
Trond Myklebust4876cc72015-06-19 16:17:57 -04002118}
2119
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002120static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
2121 struct socket *sock)
2122{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002123 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2124 unsigned int keepidle;
2125 unsigned int keepcnt;
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002126 unsigned int timeo;
2127
Trond Myklebustb5e92412019-05-02 11:21:08 -04002128 spin_lock(&xprt->transport_lock);
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002129 keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
2130 keepcnt = xprt->timeout->to_retries + 1;
2131 timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
2132 (xprt->timeout->to_retries + 1);
2133 clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
Trond Myklebustb5e92412019-05-02 11:21:08 -04002134 spin_unlock(&xprt->transport_lock);
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002135
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002136 /* TCP Keepalive options */
Christoph Hellwigce3d9542020-05-28 07:12:15 +02002137 sock_set_keepalive(sock->sk);
Christoph Hellwig71c48eb2020-05-28 07:12:23 +02002138 tcp_sock_set_keepidle(sock->sk, keepidle);
Christoph Hellwigd41ecaa2020-05-28 07:12:24 +02002139 tcp_sock_set_keepintvl(sock->sk, keepidle);
Christoph Hellwig480aeb92020-05-28 07:12:25 +02002140 tcp_sock_set_keepcnt(sock->sk, keepcnt);
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002141
2142 /* TCP user timeout (see RFC5482) */
Christoph Hellwigc488aea2020-05-28 07:12:22 +02002143 tcp_sock_set_user_timeout(sock->sk, timeo);
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002144}
2145
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002146static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
2147 unsigned long connect_timeout,
2148 unsigned long reconnect_timeout)
2149{
2150 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2151 struct rpc_timeout to;
2152 unsigned long initval;
2153
Trond Myklebustb5e92412019-05-02 11:21:08 -04002154 spin_lock(&xprt->transport_lock);
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002155 if (reconnect_timeout < xprt->max_reconnect_timeout)
2156 xprt->max_reconnect_timeout = reconnect_timeout;
2157 if (connect_timeout < xprt->connect_timeout) {
2158 memcpy(&to, xprt->timeout, sizeof(to));
2159 initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
2160 /* Arbitrary lower limit */
2161 if (initval < XS_TCP_INIT_REEST_TO << 1)
2162 initval = XS_TCP_INIT_REEST_TO << 1;
2163 to.to_initval = initval;
2164 to.to_maxval = initval;
2165 memcpy(&transport->tcp_timeout, &to,
2166 sizeof(transport->tcp_timeout));
2167 xprt->timeout = &transport->tcp_timeout;
2168 xprt->connect_timeout = connect_timeout;
2169 }
2170 set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
Trond Myklebustb5e92412019-05-02 11:21:08 -04002171 spin_unlock(&xprt->transport_lock);
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002172}
2173
Chuck Lever16be2d22007-08-06 11:57:38 -04002174static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002175{
Chuck Lever16be2d22007-08-06 11:57:38 -04002176 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002177 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002178
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002179 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002180 struct sock *sk = sock->sk;
Trond Myklebust7f260e82013-09-24 11:25:22 -04002181
NeilBrownd88e4d82016-08-04 16:24:28 +10002182 /* Avoid temporary address, they are bad for long-lived
2183 * connections such as NFS mounts.
2184 * RFC4941, section 3.6 suggests that:
2185 * Individual applications, which have specific
2186 * knowledge about the normal duration of connections,
2187 * MAY override this as appropriate.
2188 */
Christoph Hellwig18d5ad62020-05-28 07:12:33 +02002189 if (xs_addr(xprt)->sa_family == PF_INET6) {
2190 ip6_sock_set_addr_preferences(sk,
2191 IPV6_PREFER_SRC_PUBLIC);
2192 }
NeilBrownd88e4d82016-08-04 16:24:28 +10002193
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002194 xs_tcp_set_socket_timeouts(xprt, sock);
Trond Myklebustd737e5d2021-02-09 16:04:15 -05002195 tcp_sock_set_nodelay(sk);
Trond Myklebust775f06a2015-06-20 15:31:54 -04002196
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002197 write_lock_bh(&sk->sk_callback_lock);
2198
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002199 xs_save_old_callbacks(transport, sk);
2200
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002201 sk->sk_user_data = xprt;
Trond Myklebust5157b952016-05-29 10:13:24 -04002202 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002203 sk->sk_state_change = xs_tcp_state_change;
2204 sk->sk_write_space = xs_tcp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002205 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05002206 sk->sk_error_report = xs_error_report;
Chuck Lever3167e122005-08-25 16:25:55 -07002207
2208 /* socket options */
Chuck Lever3167e122005-08-25 16:25:55 -07002209 sock_reset_flag(sk, SOCK_LINGER);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002210
2211 xprt_clear_connected(xprt);
2212
2213 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002214 transport->sock = sock;
2215 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002216
2217 write_unlock_bh(&sk->sk_callback_lock);
2218 }
2219
Trond Myklebust01d37c42009-03-11 14:09:39 -04002220 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002221 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002222
Mel Gormana564b8f2012-07-31 16:45:12 -07002223 xs_set_memalloc(xprt);
2224
Trond Myklebustae053552019-02-20 14:56:20 -05002225 xs_stream_start_connect(transport);
Trond Myklebuste1806c72018-08-13 16:50:49 -04002226
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002227 /* Tell the socket layer to start connecting... */
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04002228 set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002229 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2230 switch (ret) {
2231 case 0:
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002232 xs_set_srcport(transport, sock);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002233 fallthrough;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002234 case -EINPROGRESS:
2235 /* SYN_SENT! */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002236 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2237 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust1f4c17a2016-08-01 13:36:08 -04002238 break;
2239 case -EADDRNOTAVAIL:
2240 /* Source port number is unavailable. Try a new one! */
2241 transport->srcport = 0;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002242 }
2243out:
2244 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002245}
2246
2247/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002248 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
Chuck Leveracf0a392018-12-19 11:00:22 -05002249 * @work: queued work item
Chuck Lever16be2d22007-08-06 11:57:38 -04002250 *
2251 * Invoked by a work queue tasklet.
2252 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002253static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002254{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002255 struct sock_xprt *transport =
2256 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002257 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002258 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002259 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002260
Chuck Lever16be2d22007-08-06 11:57:38 -04002261 if (!sock) {
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002262 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002263 xs_addr(xprt)->sa_family, SOCK_STREAM,
2264 IPPROTO_TCP, true);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002265 if (IS_ERR(sock)) {
2266 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002267 goto out;
2268 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002269 }
2270
Chuck Leverc740eff2009-08-09 15:09:46 -04002271 dprintk("RPC: worker connecting xprt %p via %s to "
2272 "%s (port %s)\n", xprt,
2273 xprt->address_strings[RPC_DISPLAY_PROTO],
2274 xprt->address_strings[RPC_DISPLAY_ADDR],
2275 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002276
2277 status = xs_tcp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002278 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever46121cf2007-01-31 12:14:08 -05002279 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2280 xprt, -status, xprt_connected(xprt),
2281 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002282 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002283 default:
2284 printk("%s: connect returned unhandled error %d\n",
2285 __func__, status);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002286 fallthrough;
Trond Myklebustf75e6742009-04-21 17:18:20 -04002287 case -EADDRNOTAVAIL:
2288 /* We're probably in TIME_WAIT. Get rid of existing socket,
2289 * and retry
2290 */
Trond Myklebusta519fc72012-09-12 16:49:15 -04002291 xs_tcp_force_close(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002292 break;
Trond Myklebust2a491992009-03-11 14:38:00 -04002293 case 0:
2294 case -EINPROGRESS:
2295 case -EALREADY:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002296 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002297 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002298 case -EINVAL:
2299 /* Happens, for instance, if the user specified a link
2300 * local IPv6 address without a scope-id.
2301 */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002302 case -ECONNREFUSED:
2303 case -ECONNRESET:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002304 case -ENETDOWN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002305 case -ENETUNREACH:
Trond Myklebust4ba161a2017-11-24 12:00:24 -05002306 case -EHOSTUNREACH:
Trond Myklebust3913c782015-02-08 21:44:04 -05002307 case -EADDRINUSE:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002308 case -ENOBUFS:
Calum Mackay8c711392020-10-24 22:36:38 +01002309 /* xs_tcp_force_close() wakes tasks with a fixed error code.
2310 * We need to wake them first to ensure the correct error code.
NeilBrown6ea44ad2017-05-25 17:00:32 +10002311 */
2312 xprt_wake_pending_tasks(xprt, status);
Trond Myklebust4efdd922015-02-08 15:34:28 -05002313 xs_tcp_force_close(xprt);
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002314 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002315 }
Trond Myklebust2a491992009-03-11 14:38:00 -04002316 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002317out:
Chuck Lever2226feb2005-08-11 16:25:38 -04002318 xprt_clear_connecting(xprt);
Trond Myklebustcf767852018-12-17 17:38:51 -05002319 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002320 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04002321}
2322
Chuck Lever68e220b2007-08-06 11:57:48 -04002323/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002324 * xs_connect - connect a socket to a remote endpoint
Trond Myklebust1b092092013-01-08 09:26:49 -05002325 * @xprt: pointer to transport structure
Chuck Lever9903cd12005-08-11 16:25:26 -04002326 * @task: address of RPC task that manages state of connect request
2327 *
2328 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002329 *
2330 * UDP socket connects are synchronous, but we use a work queue anyway
2331 * to guarantee that even unprivileged user processes can set up a
2332 * socket on a privileged port.
2333 *
2334 * If a UDP socket connect fails, the delay behavior here prevents
2335 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002336 */
Trond Myklebust1b092092013-01-08 09:26:49 -05002337static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002338{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002339 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002340 unsigned long delay = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002341
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002342 WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2343
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002344 if (transport->sock != NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002345 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2346 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002347 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002348
2349 /* Start by resetting any existing state */
2350 xs_reset_transport(transport);
2351
Chuck Lever675dd902019-06-19 10:33:42 -04002352 delay = xprt_reconnect_delay(xprt);
2353 xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO);
Trond Myklebust02910172016-08-04 00:00:33 -04002354
Trond Myklebust02910172016-08-04 00:00:33 -04002355 } else
Chuck Lever46121cf2007-01-31 12:14:08 -05002356 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002357
2358 queue_delayed_work(xprtiod_workqueue,
2359 &transport->connect_worker,
2360 delay);
Chuck Levera246b012005-08-11 16:25:23 -04002361}
2362
Trond Myklebust4f8943f2019-05-01 16:28:29 -04002363static void xs_wake_disconnect(struct sock_xprt *transport)
2364{
2365 if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state))
2366 xs_tcp_force_close(&transport->xprt);
2367}
2368
2369static void xs_wake_write(struct sock_xprt *transport)
2370{
2371 if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state))
2372 xprt_write_space(&transport->xprt);
2373}
2374
2375static void xs_wake_error(struct sock_xprt *transport)
2376{
2377 int sockerr;
Trond Myklebust4f8943f2019-05-01 16:28:29 -04002378
2379 if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
2380 return;
2381 mutex_lock(&transport->recv_mutex);
2382 if (transport->sock == NULL)
2383 goto out;
2384 if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
2385 goto out;
Benjamin Coddingtonaf845372019-10-02 10:40:55 -04002386 sockerr = xchg(&transport->xprt_err, 0);
Trond Myklebust4f8943f2019-05-01 16:28:29 -04002387 if (sockerr < 0)
2388 xprt_wake_pending_tasks(&transport->xprt, sockerr);
2389out:
2390 mutex_unlock(&transport->recv_mutex);
2391}
2392
2393static void xs_wake_pending(struct sock_xprt *transport)
2394{
2395 if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state))
2396 xprt_wake_pending_tasks(&transport->xprt, -EAGAIN);
2397}
2398
2399static void xs_error_handle(struct work_struct *work)
2400{
2401 struct sock_xprt *transport = container_of(work,
2402 struct sock_xprt, error_worker);
2403
2404 xs_wake_disconnect(transport);
2405 xs_wake_write(transport);
2406 xs_wake_error(transport);
2407 xs_wake_pending(transport);
2408}
2409
Chuck Lever262ca072006-03-20 13:44:16 -05002410/**
Bhaskar Chowdhury12b20ce2020-11-09 15:24:05 +05302411 * xs_local_print_stats - display AF_LOCAL socket-specific stats
Chuck Lever176e21e2011-05-09 15:22:44 -04002412 * @xprt: rpc_xprt struct containing statistics
2413 * @seq: output file
2414 *
2415 */
2416static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2417{
2418 long idle_time = 0;
2419
2420 if (xprt_connected(xprt))
2421 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2422
2423 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
Andy Adamson15a45202012-02-14 16:19:18 -05002424 "%llu %llu %lu %llu %llu\n",
Chuck Lever176e21e2011-05-09 15:22:44 -04002425 xprt->stat.bind_count,
2426 xprt->stat.connect_count,
Chuck Lever8440a882018-10-01 14:25:41 -04002427 xprt->stat.connect_time / HZ,
Chuck Lever176e21e2011-05-09 15:22:44 -04002428 idle_time,
2429 xprt->stat.sends,
2430 xprt->stat.recvs,
2431 xprt->stat.bad_xids,
2432 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002433 xprt->stat.bklog_u,
2434 xprt->stat.max_slots,
2435 xprt->stat.sending_u,
2436 xprt->stat.pending_u);
Chuck Lever176e21e2011-05-09 15:22:44 -04002437}
2438
2439/**
Bhaskar Chowdhury12b20ce2020-11-09 15:24:05 +05302440 * xs_udp_print_stats - display UDP socket-specific stats
Chuck Lever262ca072006-03-20 13:44:16 -05002441 * @xprt: rpc_xprt struct containing statistics
2442 * @seq: output file
2443 *
2444 */
2445static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2446{
Chuck Leverc8475462006-12-05 16:35:26 -05002447 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2448
Andy Adamson15a45202012-02-14 16:19:18 -05002449 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2450 "%lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002451 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002452 xprt->stat.bind_count,
2453 xprt->stat.sends,
2454 xprt->stat.recvs,
2455 xprt->stat.bad_xids,
2456 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002457 xprt->stat.bklog_u,
2458 xprt->stat.max_slots,
2459 xprt->stat.sending_u,
2460 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002461}
2462
2463/**
Bhaskar Chowdhury12b20ce2020-11-09 15:24:05 +05302464 * xs_tcp_print_stats - display TCP socket-specific stats
Chuck Lever262ca072006-03-20 13:44:16 -05002465 * @xprt: rpc_xprt struct containing statistics
2466 * @seq: output file
2467 *
2468 */
2469static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2470{
Chuck Leverc8475462006-12-05 16:35:26 -05002471 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002472 long idle_time = 0;
2473
2474 if (xprt_connected(xprt))
2475 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2476
Andy Adamson15a45202012-02-14 16:19:18 -05002477 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2478 "%llu %llu %lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002479 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002480 xprt->stat.bind_count,
2481 xprt->stat.connect_count,
Chuck Lever8440a882018-10-01 14:25:41 -04002482 xprt->stat.connect_time / HZ,
Chuck Lever262ca072006-03-20 13:44:16 -05002483 idle_time,
2484 xprt->stat.sends,
2485 xprt->stat.recvs,
2486 xprt->stat.bad_xids,
2487 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002488 xprt->stat.bklog_u,
2489 xprt->stat.max_slots,
2490 xprt->stat.sending_u,
2491 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002492}
2493
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002494/*
2495 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2496 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2497 * to use the server side send routines.
2498 */
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002499static int bc_malloc(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002500{
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002501 struct rpc_rqst *rqst = task->tk_rqstp;
2502 size_t size = rqst->rq_callsize;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002503 struct page *page;
2504 struct rpc_buffer *buf;
2505
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002506 if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
2507 WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
2508 size);
2509 return -EINVAL;
2510 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002511
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002512 page = alloc_page(GFP_KERNEL);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002513 if (!page)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002514 return -ENOMEM;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002515
2516 buf = page_address(page);
2517 buf->len = PAGE_SIZE;
2518
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002519 rqst->rq_buffer = buf->data;
Jeff Layton18e601d2016-10-24 20:33:23 -04002520 rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002521 return 0;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002522}
2523
2524/*
2525 * Free the space allocated in the bc_alloc routine
2526 */
Chuck Lever3435c742016-09-15 10:55:29 -04002527static void bc_free(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002528{
Chuck Lever3435c742016-09-15 10:55:29 -04002529 void *buffer = task->tk_rqstp->rq_buffer;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002530 struct rpc_buffer *buf;
2531
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002532 buf = container_of(buffer, struct rpc_buffer, data);
2533 free_page((unsigned long)buf);
2534}
2535
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002536static int bc_sendto(struct rpc_rqst *req)
2537{
Chuck Leverda1661b2020-03-02 15:20:33 -05002538 struct xdr_buf *xdr = &req->rq_snd_buf;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002539 struct sock_xprt *transport =
Chuck Lever067fb112019-02-11 11:24:37 -05002540 container_of(req->rq_xprt, struct sock_xprt, xprt);
Chuck Lever067fb112019-02-11 11:24:37 -05002541 struct msghdr msg = {
Chuck Leverda1661b2020-03-02 15:20:33 -05002542 .msg_flags = 0,
Chuck Lever067fb112019-02-11 11:24:37 -05002543 };
2544 rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
Chuck Leverda1661b2020-03-02 15:20:33 -05002545 (u32)xdr->len);
2546 unsigned int sent = 0;
2547 int err;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002548
Chuck Lever8729aab2019-11-20 16:25:46 -05002549 req->rq_xtime = ktime_get();
Chuck Leverda1661b2020-03-02 15:20:33 -05002550 err = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, marker, &sent);
2551 xdr_free_bvec(xdr);
2552 if (err < 0 || sent != (xdr->len + sizeof(marker)))
Chuck Lever067fb112019-02-11 11:24:37 -05002553 return -EAGAIN;
Chuck Leverda1661b2020-03-02 15:20:33 -05002554 return sent;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002555}
2556
Chuck Leverca4faf52020-05-02 10:37:44 -04002557/**
2558 * bc_send_request - Send a backchannel Call on a TCP socket
2559 * @req: rpc_rqst containing Call message to be sent
2560 *
2561 * xpt_mutex ensures @rqstp's whole message is written to the socket
2562 * without interruption.
2563 *
2564 * Return values:
2565 * %0 if the message was sent successfully
2566 * %ENOTCONN if the message was not sent
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002567 */
Trond Myklebustadfa7142018-09-03 23:58:59 -04002568static int bc_send_request(struct rpc_rqst *req)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002569{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002570 struct svc_xprt *xprt;
Andrzej Hajda7fc56132015-09-24 16:00:09 +02002571 int len;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002572
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002573 /*
2574 * Get the server socket associated with this callback xprt
2575 */
2576 xprt = req->rq_xprt->bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002577
2578 /*
2579 * Grab the mutex to serialize data as the connection is shared
2580 * with the fore channel
2581 */
Trond Myklebustc5445772018-09-03 23:39:27 -04002582 mutex_lock(&xprt->xpt_mutex);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002583 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2584 len = -ENOTCONN;
2585 else
2586 len = bc_sendto(req);
2587 mutex_unlock(&xprt->xpt_mutex);
2588
2589 if (len > 0)
2590 len = 0;
2591
2592 return len;
2593}
2594
2595/*
2596 * The close routine. Since this is client initiated, we do nothing
2597 */
2598
2599static void bc_close(struct rpc_xprt *xprt)
2600{
Chuck Lever6221f1d2020-04-17 12:40:31 -04002601 xprt_disconnect_done(xprt);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002602}
2603
2604/*
2605 * The xprt destroy routine. Again, because this connection is client
2606 * initiated, we do nothing
2607 */
2608
2609static void bc_destroy(struct rpc_xprt *xprt)
2610{
Kinglong Mee47f72ef2014-03-24 11:58:16 +08002611 dprintk("RPC: bc_destroy xprt %p\n", xprt);
2612
2613 xs_xprt_free(xprt);
2614 module_put(THIS_MODULE);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002615}
2616
Chuck Leverd31ae252017-08-01 12:00:39 -04002617static const struct rpc_xprt_ops xs_local_ops = {
Chuck Lever176e21e2011-05-09 15:22:44 -04002618 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebust4cd34e72018-08-31 10:00:02 -04002619 .release_xprt = xprt_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002620 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002621 .free_slot = xprt_free_slot,
Chuck Lever176e21e2011-05-09 15:22:44 -04002622 .rpcbind = xs_local_rpcbind,
2623 .set_port = xs_local_set_port,
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002624 .connect = xs_local_connect,
Chuck Lever176e21e2011-05-09 15:22:44 -04002625 .buf_alloc = rpc_malloc,
2626 .buf_free = rpc_free,
Trond Myklebust550aebf2018-09-14 14:32:45 -04002627 .prepare_request = xs_stream_prepare_request,
Chuck Lever176e21e2011-05-09 15:22:44 -04002628 .send_request = xs_local_send_request,
Trond Myklebust8ba6a922019-04-07 13:58:46 -04002629 .wait_for_reply_request = xprt_wait_for_reply_request_def,
Chuck Lever176e21e2011-05-09 15:22:44 -04002630 .close = xs_close,
Trond Myklebusta1311d82013-10-31 09:18:49 -04002631 .destroy = xs_destroy,
Chuck Lever176e21e2011-05-09 15:22:44 -04002632 .print_stats = xs_local_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002633 .enable_swap = xs_enable_swap,
2634 .disable_swap = xs_disable_swap,
Chuck Lever176e21e2011-05-09 15:22:44 -04002635};
2636
Chuck Leverd31ae252017-08-01 12:00:39 -04002637static const struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002638 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002639 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002640 .release_xprt = xprt_release_xprt_cong,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002641 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002642 .free_slot = xprt_free_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002643 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002644 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002645 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002646 .buf_alloc = rpc_malloc,
2647 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002648 .send_request = xs_udp_send_request,
Trond Myklebust8ba6a922019-04-07 13:58:46 -04002649 .wait_for_reply_request = xprt_wait_for_reply_request_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002650 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002651 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002652 .close = xs_close,
2653 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002654 .print_stats = xs_udp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002655 .enable_swap = xs_enable_swap,
2656 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002657 .inject_disconnect = xs_inject_disconnect,
Chuck Lever262965f2005-08-11 16:25:56 -04002658};
2659
Chuck Leverd31ae252017-08-01 12:00:39 -04002660static const struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002661 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebust4cd34e72018-08-31 10:00:02 -04002662 .release_xprt = xprt_release_xprt,
Trond Myklebust36bd7de2018-09-03 18:41:32 -04002663 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002664 .free_slot = xprt_free_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002665 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002666 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002667 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002668 .buf_alloc = rpc_malloc,
2669 .buf_free = rpc_free,
Trond Myklebust277e4ab2018-09-14 09:49:06 -04002670 .prepare_request = xs_stream_prepare_request,
Chuck Lever262965f2005-08-11 16:25:56 -04002671 .send_request = xs_tcp_send_request,
Trond Myklebust8ba6a922019-04-07 13:58:46 -04002672 .wait_for_reply_request = xprt_wait_for_reply_request_def,
Trond Myklebustc627d312015-02-10 11:06:04 -05002673 .close = xs_tcp_shutdown,
Chuck Lever9903cd12005-08-11 16:25:26 -04002674 .destroy = xs_destroy,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002675 .set_connect_timeout = xs_tcp_set_connect_timeout,
Chuck Lever262ca072006-03-20 13:44:16 -05002676 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002677 .enable_swap = xs_enable_swap,
2678 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002679 .inject_disconnect = xs_inject_disconnect,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002680#ifdef CONFIG_SUNRPC_BACKCHANNEL
2681 .bc_setup = xprt_setup_bc,
Chuck Lever6b26cc82016-05-02 14:40:40 -04002682 .bc_maxpayload = xs_tcp_bc_maxpayload,
Trond Myklebust7402a4f2019-07-16 13:51:29 -04002683 .bc_num_slots = xprt_bc_max_slots,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002684 .bc_free_rqst = xprt_free_bc_rqst,
2685 .bc_destroy = xprt_destroy_bc,
2686#endif
Chuck Levera246b012005-08-11 16:25:23 -04002687};
2688
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002689/*
2690 * The rpc_xprt_ops for the server backchannel
2691 */
2692
Chuck Leverd31ae252017-08-01 12:00:39 -04002693static const struct rpc_xprt_ops bc_tcp_ops = {
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002694 .reserve_xprt = xprt_reserve_xprt,
2695 .release_xprt = xprt_release_xprt,
Bryan Schumaker84e28a32012-09-24 13:39:01 -04002696 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002697 .free_slot = xprt_free_slot,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002698 .buf_alloc = bc_malloc,
2699 .buf_free = bc_free,
2700 .send_request = bc_send_request,
Trond Myklebust8ba6a922019-04-07 13:58:46 -04002701 .wait_for_reply_request = xprt_wait_for_reply_request_def,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002702 .close = bc_close,
2703 .destroy = bc_destroy,
2704 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002705 .enable_swap = xs_enable_swap,
2706 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002707 .inject_disconnect = xs_inject_disconnect,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002708};
2709
Chuck Lever92476852010-10-20 11:53:01 -04002710static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2711{
2712 static const struct sockaddr_in sin = {
2713 .sin_family = AF_INET,
2714 .sin_addr.s_addr = htonl(INADDR_ANY),
2715 };
2716 static const struct sockaddr_in6 sin6 = {
2717 .sin6_family = AF_INET6,
2718 .sin6_addr = IN6ADDR_ANY_INIT,
2719 };
2720
2721 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002722 case AF_LOCAL:
2723 break;
Chuck Lever92476852010-10-20 11:53:01 -04002724 case AF_INET:
2725 memcpy(sap, &sin, sizeof(sin));
2726 break;
2727 case AF_INET6:
2728 memcpy(sap, &sin6, sizeof(sin6));
2729 break;
2730 default:
2731 dprintk("RPC: %s: Bad address family\n", __func__);
2732 return -EAFNOSUPPORT;
2733 }
2734 return 0;
2735}
2736
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002737static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002738 unsigned int slot_table_size,
2739 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002740{
2741 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002742 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002743
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002744 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002745 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002746 return ERR_PTR(-EBADF);
2747 }
2748
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002749 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2750 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002751 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002752 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2753 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002754 return ERR_PTR(-ENOMEM);
2755 }
2756
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002757 new = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002758 mutex_init(&new->recv_mutex);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002759 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2760 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002761 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002762 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002763 else {
2764 int err;
2765 err = xs_init_anyaddr(args->dstaddr->sa_family,
2766 (struct sockaddr *)&new->srcaddr);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002767 if (err != 0) {
2768 xprt_free(xprt);
Chuck Lever92476852010-10-20 11:53:01 -04002769 return ERR_PTR(err);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002770 }
Chuck Lever92476852010-10-20 11:53:01 -04002771 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002772
2773 return xprt;
2774}
2775
Chuck Lever176e21e2011-05-09 15:22:44 -04002776static const struct rpc_timeout xs_local_default_timeout = {
2777 .to_initval = 10 * HZ,
2778 .to_maxval = 10 * HZ,
2779 .to_retries = 2,
2780};
2781
2782/**
2783 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2784 * @args: rpc transport creation arguments
2785 *
2786 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2787 */
2788static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2789{
2790 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2791 struct sock_xprt *transport;
2792 struct rpc_xprt *xprt;
2793 struct rpc_xprt *ret;
2794
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002795 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2796 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002797 if (IS_ERR(xprt))
2798 return xprt;
2799 transport = container_of(xprt, struct sock_xprt, xprt);
2800
2801 xprt->prot = 0;
Olga Kornievskaiad3abc732021-06-08 15:59:15 -04002802 xprt->xprt_class = &xs_local_transport;
Chuck Lever176e21e2011-05-09 15:22:44 -04002803 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2804
2805 xprt->bind_timeout = XS_BIND_TO;
2806 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2807 xprt->idle_timeout = XS_IDLE_DISC_TO;
2808
2809 xprt->ops = &xs_local_ops;
2810 xprt->timeout = &xs_local_default_timeout;
2811
Trond Myklebust550aebf2018-09-14 14:32:45 -04002812 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
Trond Myklebust4f8943f2019-05-01 16:28:29 -04002813 INIT_WORK(&transport->error_worker, xs_error_handle);
Trond Myklebust550aebf2018-09-14 14:32:45 -04002814 INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket);
NeilBrown93dc41b2013-10-31 16:14:36 +11002815
Chuck Lever176e21e2011-05-09 15:22:44 -04002816 switch (sun->sun_family) {
2817 case AF_LOCAL:
2818 if (sun->sun_path[0] != '/') {
2819 dprintk("RPC: bad AF_LOCAL address: %s\n",
2820 sun->sun_path);
2821 ret = ERR_PTR(-EINVAL);
2822 goto out_err;
2823 }
2824 xprt_set_bound(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002825 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
J. Bruce Fields7073ea82013-02-21 10:14:22 -05002826 ret = ERR_PTR(xs_local_setup_socket(transport));
2827 if (ret)
2828 goto out_err;
Chuck Lever176e21e2011-05-09 15:22:44 -04002829 break;
2830 default:
2831 ret = ERR_PTR(-EAFNOSUPPORT);
2832 goto out_err;
2833 }
2834
2835 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2836 xprt->address_strings[RPC_DISPLAY_ADDR]);
2837
2838 if (try_module_get(THIS_MODULE))
2839 return xprt;
2840 ret = ERR_PTR(-EINVAL);
2841out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002842 xs_xprt_free(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002843 return ret;
2844}
2845
Trond Myklebust2881ae72007-12-20 16:03:54 -05002846static const struct rpc_timeout xs_udp_default_timeout = {
2847 .to_initval = 5 * HZ,
2848 .to_maxval = 30 * HZ,
2849 .to_increment = 5 * HZ,
2850 .to_retries = 5,
2851};
2852
Chuck Lever9903cd12005-08-11 16:25:26 -04002853/**
2854 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002855 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002856 *
2857 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002858static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002859{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002860 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002861 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002862 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002863 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002864
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002865 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2866 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002867 if (IS_ERR(xprt))
2868 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002869 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002870
Chuck Leverec739ef2006-08-22 20:06:15 -04002871 xprt->prot = IPPROTO_UDP;
Olga Kornievskaiad3abc732021-06-08 15:59:15 -04002872 xprt->xprt_class = &xs_udp_transport;
Chuck Levera246b012005-08-11 16:25:23 -04002873 /* XXX: header size can vary due to auth type, IPv6, etc. */
2874 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2875
Chuck Lever03bf4b72005-08-25 16:25:55 -07002876 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002877 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2878 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002879
Chuck Lever262965f2005-08-11 16:25:56 -04002880 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002881
Trond Myklebustba7392b2007-12-20 16:03:55 -05002882 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002883
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002884 INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
Trond Myklebust4f8943f2019-05-01 16:28:29 -04002885 INIT_WORK(&transport->error_worker, xs_error_handle);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002886 INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2887
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002888 switch (addr->sa_family) {
2889 case AF_INET:
2890 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2891 xprt_set_bound(xprt);
2892
Chuck Lever9dc3b092009-08-09 15:09:46 -04002893 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002894 break;
2895 case AF_INET6:
2896 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2897 xprt_set_bound(xprt);
2898
Chuck Lever9dc3b092009-08-09 15:09:46 -04002899 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002900 break;
2901 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002902 ret = ERR_PTR(-EAFNOSUPPORT);
2903 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002904 }
2905
Chuck Leverc740eff2009-08-09 15:09:46 -04002906 if (xprt_bound(xprt))
2907 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2908 xprt->address_strings[RPC_DISPLAY_ADDR],
2909 xprt->address_strings[RPC_DISPLAY_PORT],
2910 xprt->address_strings[RPC_DISPLAY_PROTO]);
2911 else
2912 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2913 xprt->address_strings[RPC_DISPLAY_ADDR],
2914 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002915
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002916 if (try_module_get(THIS_MODULE))
2917 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002918 ret = ERR_PTR(-EINVAL);
2919out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002920 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002921 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002922}
2923
Trond Myklebust2881ae72007-12-20 16:03:54 -05002924static const struct rpc_timeout xs_tcp_default_timeout = {
2925 .to_initval = 60 * HZ,
2926 .to_maxval = 60 * HZ,
2927 .to_retries = 2,
2928};
2929
Chuck Lever9903cd12005-08-11 16:25:26 -04002930/**
2931 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002932 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002933 *
2934 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002935static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002936{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002937 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002938 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002939 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002940 struct rpc_xprt *ret;
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002941 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2942
2943 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2944 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Levera246b012005-08-11 16:25:23 -04002945
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002946 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002947 max_slot_table_size);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002948 if (IS_ERR(xprt))
2949 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002950 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002951
Chuck Leverec739ef2006-08-22 20:06:15 -04002952 xprt->prot = IPPROTO_TCP;
Olga Kornievskaiad3abc732021-06-08 15:59:15 -04002953 xprt->xprt_class = &xs_tcp_transport;
Chuck Lever808012f2005-08-25 16:25:49 -07002954 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002955
Chuck Lever03bf4b72005-08-25 16:25:55 -07002956 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002957 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2958 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002959
Chuck Lever262965f2005-08-11 16:25:56 -04002960 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002961 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002962
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002963 xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002964 xprt->connect_timeout = xprt->timeout->to_initval *
2965 (xprt->timeout->to_retries + 1);
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002966
Trond Myklebustc50b8ee2018-09-14 14:26:28 -04002967 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
Trond Myklebust4f8943f2019-05-01 16:28:29 -04002968 INIT_WORK(&transport->error_worker, xs_error_handle);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002969 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
2970
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002971 switch (addr->sa_family) {
2972 case AF_INET:
2973 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2974 xprt_set_bound(xprt);
2975
Chuck Lever9dc3b092009-08-09 15:09:46 -04002976 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002977 break;
2978 case AF_INET6:
2979 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2980 xprt_set_bound(xprt);
2981
Chuck Lever9dc3b092009-08-09 15:09:46 -04002982 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002983 break;
2984 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002985 ret = ERR_PTR(-EAFNOSUPPORT);
2986 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002987 }
2988
Chuck Leverc740eff2009-08-09 15:09:46 -04002989 if (xprt_bound(xprt))
2990 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2991 xprt->address_strings[RPC_DISPLAY_ADDR],
2992 xprt->address_strings[RPC_DISPLAY_PORT],
2993 xprt->address_strings[RPC_DISPLAY_PROTO]);
2994 else
2995 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2996 xprt->address_strings[RPC_DISPLAY_ADDR],
2997 xprt->address_strings[RPC_DISPLAY_PROTO]);
2998
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002999 if (try_module_get(THIS_MODULE))
3000 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003001 ret = ERR_PTR(-EINVAL);
3002out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003003 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003004 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04003005}
Chuck Lever282b32e2006-12-05 16:35:51 -05003006
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003007/**
3008 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3009 * @args: rpc transport creation arguments
3010 *
3011 */
3012static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
3013{
3014 struct sockaddr *addr = args->dstaddr;
3015 struct rpc_xprt *xprt;
3016 struct sock_xprt *transport;
3017 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003018 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003019
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003020 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3021 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003022 if (IS_ERR(xprt))
3023 return xprt;
3024 transport = container_of(xprt, struct sock_xprt, xprt);
3025
3026 xprt->prot = IPPROTO_TCP;
Olga Kornievskaiad3abc732021-06-08 15:59:15 -04003027 xprt->xprt_class = &xs_bc_tcp_transport;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003028 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3029 xprt->timeout = &xs_tcp_default_timeout;
3030
3031 /* backchannel */
3032 xprt_set_bound(xprt);
3033 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003034 xprt->reestablish_timeout = 0;
3035 xprt->idle_timeout = 0;
3036
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003037 xprt->ops = &bc_tcp_ops;
3038
3039 switch (addr->sa_family) {
3040 case AF_INET:
3041 xs_format_peer_addresses(xprt, "tcp",
3042 RPCBIND_NETID_TCP);
3043 break;
3044 case AF_INET6:
3045 xs_format_peer_addresses(xprt, "tcp",
3046 RPCBIND_NETID_TCP6);
3047 break;
3048 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003049 ret = ERR_PTR(-EAFNOSUPPORT);
3050 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003051 }
3052
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04003053 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3054 xprt->address_strings[RPC_DISPLAY_ADDR],
3055 xprt->address_strings[RPC_DISPLAY_PORT],
3056 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003057
3058 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003059 * Once we've associated a backchannel xprt with a connection,
Weng Meiling28303ca2013-11-30 17:56:44 +08003060 * we want to keep it around as long as the connection lasts,
3061 * in case we need to start using it for a backchannel again;
3062 * this reference won't be dropped until bc_xprt is destroyed.
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003063 */
3064 xprt_get(xprt);
3065 args->bc_xprt->xpt_bc_xprt = xprt;
3066 xprt->bc_xprt = args->bc_xprt;
3067 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
3068 transport->sock = bc_sock->sk_sock;
3069 transport->inet = bc_sock->sk_sk;
3070
3071 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003072 * Since we don't want connections for the backchannel, we set
3073 * the xprt status to connected
3074 */
3075 xprt_set_connected(xprt);
3076
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003077 if (try_module_get(THIS_MODULE))
3078 return xprt;
Kinglong Mee642aab52014-03-24 12:00:28 +08003079
3080 args->bc_xprt->xpt_bc_xprt = NULL;
J. Bruce Fields39a9bea2016-05-17 12:38:21 -04003081 args->bc_xprt->xpt_bc_xps = NULL;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003082 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003083 ret = ERR_PTR(-EINVAL);
3084out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003085 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003086 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003087}
3088
Chuck Lever176e21e2011-05-09 15:22:44 -04003089static struct xprt_class xs_local_transport = {
3090 .list = LIST_HEAD_INIT(xs_local_transport.list),
3091 .name = "named UNIX socket",
3092 .owner = THIS_MODULE,
3093 .ident = XPRT_TRANSPORT_LOCAL,
3094 .setup = xs_setup_local,
Trond Myklebustd5aa6b22020-11-06 16:33:38 -05003095 .netid = { "" },
Chuck Lever176e21e2011-05-09 15:22:44 -04003096};
3097
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003098static struct xprt_class xs_udp_transport = {
3099 .list = LIST_HEAD_INIT(xs_udp_transport.list),
3100 .name = "udp",
3101 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003102 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003103 .setup = xs_setup_udp,
Trond Myklebustd5aa6b22020-11-06 16:33:38 -05003104 .netid = { "udp", "udp6", "" },
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003105};
3106
3107static struct xprt_class xs_tcp_transport = {
3108 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
3109 .name = "tcp",
3110 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003111 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003112 .setup = xs_setup_tcp,
Trond Myklebustd5aa6b22020-11-06 16:33:38 -05003113 .netid = { "tcp", "tcp6", "" },
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003114};
3115
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003116static struct xprt_class xs_bc_tcp_transport = {
3117 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3118 .name = "tcp NFSv4.1 backchannel",
3119 .owner = THIS_MODULE,
3120 .ident = XPRT_TRANSPORT_BC_TCP,
3121 .setup = xs_setup_bc_tcp,
Trond Myklebustd5aa6b22020-11-06 16:33:38 -05003122 .netid = { "" },
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003123};
3124
Chuck Lever282b32e2006-12-05 16:35:51 -05003125/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003126 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05003127 *
3128 */
3129int init_socket_xprt(void)
3130{
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08003131 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08003132 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05003133
Chuck Lever176e21e2011-05-09 15:22:44 -04003134 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003135 xprt_register_transport(&xs_udp_transport);
3136 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003137 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003138
Chuck Lever282b32e2006-12-05 16:35:51 -05003139 return 0;
3140}
3141
3142/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003143 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05003144 *
3145 */
3146void cleanup_socket_xprt(void)
3147{
Chuck Leverfbf76682006-12-05 16:35:54 -05003148 if (sunrpc_table_header) {
3149 unregister_sysctl_table(sunrpc_table_header);
3150 sunrpc_table_header = NULL;
3151 }
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003152
Chuck Lever176e21e2011-05-09 15:22:44 -04003153 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003154 xprt_unregister_transport(&xs_udp_transport);
3155 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003156 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05003157}
Trond Myklebustcbf11072009-08-09 15:06:19 -04003158
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003159static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003160{
Frank Sorensonffb6ca32016-07-08 16:35:25 -05003161 return param_set_uint_minmax(val, kp,
J. Bruce Fields826799e2018-10-18 15:27:02 -04003162 RPC_MIN_RESVPORT,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003163 RPC_MAX_RESVPORT);
3164}
3165
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303166static const struct kernel_param_ops param_ops_portnr = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003167 .set = param_set_portnr,
3168 .get = param_get_uint,
3169};
3170
Trond Myklebustcbf11072009-08-09 15:06:19 -04003171#define param_check_portnr(name, p) \
3172 __param_check(name, p, unsigned int);
3173
3174module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3175module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3176
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003177static int param_set_slot_table_size(const char *val,
3178 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003179{
3180 return param_set_uint_minmax(val, kp,
3181 RPC_MIN_SLOT_TABLE,
3182 RPC_MAX_SLOT_TABLE);
3183}
3184
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303185static const struct kernel_param_ops param_ops_slot_table_size = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003186 .set = param_set_slot_table_size,
3187 .get = param_get_uint,
3188};
3189
Trond Myklebustcbf11072009-08-09 15:06:19 -04003190#define param_check_slot_table_size(name, p) \
3191 __param_check(name, p, unsigned int);
3192
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003193static int param_set_max_slot_table_size(const char *val,
3194 const struct kernel_param *kp)
3195{
3196 return param_set_uint_minmax(val, kp,
3197 RPC_MIN_SLOT_TABLE,
3198 RPC_MAX_SLOT_TABLE_LIMIT);
3199}
3200
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303201static const struct kernel_param_ops param_ops_max_slot_table_size = {
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003202 .set = param_set_max_slot_table_size,
3203 .get = param_get_uint,
3204};
3205
3206#define param_check_max_slot_table_size(name, p) \
3207 __param_check(name, p, unsigned int);
3208
Trond Myklebustcbf11072009-08-09 15:06:19 -04003209module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3210 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003211module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3212 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003213module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3214 slot_table_size, 0644);