blob: 0bda8a73e8a86ea99ff5c04192a4624752d8a067 [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
94/*
95 * FIXME: changing the UDP slot table size should also resize the UDP
96 * socket buffers for existing UDP transports
97 */
Joe Perchesfe2c6332013-06-11 23:04:25 -070098static struct ctl_table xs_tunables_table[] = {
Chuck Leverfbf76682006-12-05 16:35:54 -050099 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500100 .procname = "udp_slot_table_entries",
101 .data = &xprt_udp_slot_table_entries,
102 .maxlen = sizeof(unsigned int),
103 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800104 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500105 .extra1 = &min_slot_table_size,
106 .extra2 = &max_slot_table_size
107 },
108 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500109 .procname = "tcp_slot_table_entries",
110 .data = &xprt_tcp_slot_table_entries,
111 .maxlen = sizeof(unsigned int),
112 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800113 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500114 .extra1 = &min_slot_table_size,
115 .extra2 = &max_slot_table_size
116 },
117 {
Trond Myklebustd9ba1312011-07-17 18:11:30 -0400118 .procname = "tcp_max_slot_table_entries",
119 .data = &xprt_max_tcp_slot_table_entries,
120 .maxlen = sizeof(unsigned int),
121 .mode = 0644,
122 .proc_handler = proc_dointvec_minmax,
123 .extra1 = &min_slot_table_size,
124 .extra2 = &max_tcp_slot_table_limit
125 },
126 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500127 .procname = "min_resvport",
128 .data = &xprt_min_resvport,
129 .maxlen = sizeof(unsigned int),
130 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800131 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500132 .extra1 = &xprt_min_resvport_limit,
J. Bruce Fields826799e2018-10-18 15:27:02 -0400133 .extra2 = &xprt_max_resvport_limit
Chuck Leverfbf76682006-12-05 16:35:54 -0500134 },
135 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500136 .procname = "max_resvport",
137 .data = &xprt_max_resvport,
138 .maxlen = sizeof(unsigned int),
139 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800140 .proc_handler = proc_dointvec_minmax,
J. Bruce Fields826799e2018-10-18 15:27:02 -0400141 .extra1 = &xprt_min_resvport_limit,
Chuck Leverfbf76682006-12-05 16:35:54 -0500142 .extra2 = &xprt_max_resvport_limit
143 },
144 {
Trond Myklebust25fe6142009-03-11 14:38:03 -0400145 .procname = "tcp_fin_timeout",
146 .data = &xs_tcp_fin_timeout,
147 .maxlen = sizeof(xs_tcp_fin_timeout),
148 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800149 .proc_handler = proc_dointvec_jiffies,
Trond Myklebust25fe6142009-03-11 14:38:03 -0400150 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800151 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500152};
153
Joe Perchesfe2c6332013-06-11 23:04:25 -0700154static struct ctl_table sunrpc_table[] = {
Chuck Leverfbf76682006-12-05 16:35:54 -0500155 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500156 .procname = "sunrpc",
157 .mode = 0555,
158 .child = xs_tunables_table
159 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800160 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500161};
162
Chuck Leverfbf76682006-12-05 16:35:54 -0500163/*
Chuck Lever03bf4b72005-08-25 16:25:55 -0700164 * Wait duration for a reply from the RPC portmapper.
165 */
166#define XS_BIND_TO (60U * HZ)
167
168/*
169 * Delay if a UDP socket connect error occurs. This is most likely some
170 * kind of resource problem on the local host.
171 */
172#define XS_UDP_REEST_TO (2U * HZ)
173
174/*
175 * The reestablish timeout allows clients to delay for a bit before attempting
176 * to reconnect to a server that just dropped our connection.
177 *
178 * We implement an exponential backoff when trying to reestablish a TCP
179 * transport connection with the server. Some servers like to drop a TCP
180 * connection when they are overworked, so we start with a short timeout and
181 * increase over time if the server is down or not responding.
182 */
183#define XS_TCP_INIT_REEST_TO (3U * HZ)
Chuck Lever03bf4b72005-08-25 16:25:55 -0700184
185/*
186 * TCP idle timeout; client drops the transport socket if it is idle
187 * for this long. Note that we also timeout UDP sockets to prevent
188 * holding port numbers when there is no RPC traffic.
189 */
190#define XS_IDLE_DISC_TO (5U * 60 * HZ)
191
Jeff Laytonf895b252014-11-17 16:58:04 -0500192#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Levera246b012005-08-11 16:25:23 -0400193# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400194# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400195#endif
196
Chuck Levera246b012005-08-11 16:25:23 -0400197#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400198static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400199{
Chuck Lever9903cd12005-08-11 16:25:26 -0400200 u8 *buf = (u8 *) packet;
201 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400202
Chuck Lever46121cf2007-01-31 12:14:08 -0500203 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400204 for (j = 0; j < count && j < 128; j += 4) {
205 if (!(j & 31)) {
206 if (j)
207 dprintk("\n");
208 dprintk("0x%04x ", j);
209 }
210 dprintk("%02x%02x%02x%02x ",
211 buf[j], buf[j+1], buf[j+2], buf[j+3]);
212 }
213 dprintk("\n");
214}
215#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400216static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400217{
218 /* NOP */
219}
220#endif
221
Trond Myklebust21180712013-12-31 13:22:59 -0500222static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
223{
224 return (struct rpc_xprt *) sk->sk_user_data;
225}
226
Chuck Lever95392c52007-08-06 11:57:58 -0400227static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400228{
Chuck Lever95392c52007-08-06 11:57:58 -0400229 return (struct sockaddr *) &xprt->addr;
230}
231
Chuck Lever176e21e2011-05-09 15:22:44 -0400232static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
233{
234 return (struct sockaddr_un *) &xprt->addr;
235}
236
Chuck Lever95392c52007-08-06 11:57:58 -0400237static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
238{
239 return (struct sockaddr_in *) &xprt->addr;
240}
241
242static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
243{
244 return (struct sockaddr_in6 *) &xprt->addr;
245}
246
Chuck Leverc877b842009-08-09 15:09:36 -0400247static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400248{
Chuck Leverc877b842009-08-09 15:09:36 -0400249 struct sockaddr *sap = xs_addr(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400250 struct sockaddr_in6 *sin6;
251 struct sockaddr_in *sin;
Chuck Lever176e21e2011-05-09 15:22:44 -0400252 struct sockaddr_un *sun;
Chuck Leverc877b842009-08-09 15:09:36 -0400253 char buf[128];
Chuck Leveredb267a2006-08-22 20:06:18 -0400254
Chuck Lever9dc3b092009-08-09 15:09:46 -0400255 switch (sap->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400256 case AF_LOCAL:
257 sun = xs_addr_un(xprt);
258 strlcpy(buf, sun->sun_path, sizeof(buf));
259 xprt->address_strings[RPC_DISPLAY_ADDR] =
260 kstrdup(buf, GFP_KERNEL);
261 break;
Chuck Lever9dc3b092009-08-09 15:09:46 -0400262 case AF_INET:
Chuck Lever176e21e2011-05-09 15:22:44 -0400263 (void)rpc_ntop(sap, buf, sizeof(buf));
264 xprt->address_strings[RPC_DISPLAY_ADDR] =
265 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400266 sin = xs_addr_in(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800267 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
Chuck Lever9dc3b092009-08-09 15:09:46 -0400268 break;
269 case AF_INET6:
Chuck Lever176e21e2011-05-09 15:22:44 -0400270 (void)rpc_ntop(sap, buf, sizeof(buf));
271 xprt->address_strings[RPC_DISPLAY_ADDR] =
272 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400273 sin6 = xs_addr_in6(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800274 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400275 break;
276 default:
277 BUG();
Chuck Leveredb267a2006-08-22 20:06:18 -0400278 }
Chuck Lever176e21e2011-05-09 15:22:44 -0400279
Chuck Lever9dc3b092009-08-09 15:09:46 -0400280 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
Chuck Leveredb267a2006-08-22 20:06:18 -0400281}
282
Chuck Lever9dc3b092009-08-09 15:09:46 -0400283static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400284{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400285 struct sockaddr *sap = xs_addr(xprt);
286 char buf[128];
Chuck Lever4b6473f2007-08-06 11:57:12 -0400287
Joe Perches81160e662010-03-08 12:15:59 -0800288 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400289 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400290
Joe Perches81160e662010-03-08 12:15:59 -0800291 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400292 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
293}
Chuck Lever4b6473f2007-08-06 11:57:12 -0400294
Chuck Lever9dc3b092009-08-09 15:09:46 -0400295static void xs_format_peer_addresses(struct rpc_xprt *xprt,
296 const char *protocol,
297 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400298{
Chuck Leverb454ae92008-01-07 18:34:48 -0500299 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leverb454ae92008-01-07 18:34:48 -0500300 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leverc877b842009-08-09 15:09:36 -0400301 xs_format_common_peer_addresses(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400302 xs_format_common_peer_ports(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400303}
304
Chuck Lever9dc3b092009-08-09 15:09:46 -0400305static void xs_update_peer_port(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400306{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400307 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
308 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400309
Chuck Lever9dc3b092009-08-09 15:09:46 -0400310 xs_format_common_peer_ports(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400311}
312
313static void xs_free_peer_addresses(struct rpc_xprt *xprt)
314{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500315 unsigned int i;
316
317 for (i = 0; i < RPC_DISPLAY_MAX; i++)
318 switch (i) {
319 case RPC_DISPLAY_PROTO:
320 case RPC_DISPLAY_NETID:
321 continue;
322 default:
323 kfree(xprt->address_strings[i]);
324 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400325}
326
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400327static size_t
328xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp)
329{
330 size_t i,n;
331
Trond Myklebust16e5e902018-12-02 15:22:48 -0500332 if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES))
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400333 return want;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400334 n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT;
335 for (i = 0; i < n; i++) {
336 if (buf->pages[i])
337 continue;
338 buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp);
339 if (!buf->pages[i]) {
Trond Myklebust16e5e902018-12-02 15:22:48 -0500340 i *= PAGE_SIZE;
341 return i > buf->page_base ? i - buf->page_base : 0;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400342 }
343 }
344 return want;
345}
346
347static ssize_t
348xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek)
349{
350 ssize_t ret;
351 if (seek != 0)
352 iov_iter_advance(&msg->msg_iter, seek);
353 ret = sock_recvmsg(sock, msg, flags);
354 return ret > 0 ? ret + seek : ret;
355}
356
357static ssize_t
358xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags,
359 struct kvec *kvec, size_t count, size_t seek)
360{
Al Viro0e9b4a82018-11-01 18:19:03 -0400361 iov_iter_kvec(&msg->msg_iter, READ, kvec, 1, count);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400362 return xs_sock_recvmsg(sock, msg, flags, seek);
363}
364
365static ssize_t
366xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags,
367 struct bio_vec *bvec, unsigned long nr, size_t count,
368 size_t seek)
369{
Al Viro0e9b4a82018-11-01 18:19:03 -0400370 iov_iter_bvec(&msg->msg_iter, READ, bvec, nr, count);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400371 return xs_sock_recvmsg(sock, msg, flags, seek);
372}
373
374static ssize_t
375xs_read_discard(struct socket *sock, struct msghdr *msg, int flags,
376 size_t count)
377{
Trond Myklebustb76a5af2018-12-03 15:58:58 -0500378 iov_iter_discard(&msg->msg_iter, READ, count);
379 return sock_recvmsg(sock, msg, flags);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400380}
381
Trond Myklebust6a829eb2019-01-03 09:04:45 -0500382#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
383static void
384xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
385{
386 struct bvec_iter bi = {
387 .bi_size = count,
388 };
389 struct bio_vec bv;
390
391 bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
392 for_each_bvec(bv, bvec, bi, bi)
393 flush_dcache_page(bv.bv_page);
394}
395#else
396static inline void
397xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
398{
399}
400#endif
401
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400402static ssize_t
403xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags,
404 struct xdr_buf *buf, size_t count, size_t seek, size_t *read)
405{
406 size_t want, seek_init = seek, offset = 0;
407 ssize_t ret;
408
Trond Myklebuste92053a2019-02-15 18:14:27 -0500409 want = min_t(size_t, count, buf->head[0].iov_len);
410 if (seek < want) {
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400411 ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek);
412 if (ret <= 0)
413 goto sock_err;
414 offset += ret;
415 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
416 goto out;
417 if (ret != want)
Trond Myklebustc4433052018-12-04 07:52:11 -0500418 goto out;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400419 seek = 0;
420 } else {
Trond Myklebuste92053a2019-02-15 18:14:27 -0500421 seek -= want;
422 offset += want;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400423 }
Trond Myklebust16e5e902018-12-02 15:22:48 -0500424
425 want = xs_alloc_sparse_pages(buf,
426 min_t(size_t, count - offset, buf->page_len),
Trond Myklebust12a3ad62019-03-02 10:14:02 -0500427 GFP_KERNEL);
Trond Myklebust16e5e902018-12-02 15:22:48 -0500428 if (seek < want) {
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400429 ret = xs_read_bvec(sock, msg, flags, buf->bvec,
430 xdr_buf_pagecount(buf),
431 want + buf->page_base,
432 seek + buf->page_base);
433 if (ret <= 0)
434 goto sock_err;
Trond Myklebust6a829eb2019-01-03 09:04:45 -0500435 xs_flush_bvec(buf->bvec, ret, seek + buf->page_base);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400436 offset += ret - buf->page_base;
437 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
438 goto out;
439 if (ret != want)
Trond Myklebustc4433052018-12-04 07:52:11 -0500440 goto out;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400441 seek = 0;
442 } else {
Trond Myklebust16e5e902018-12-02 15:22:48 -0500443 seek -= want;
444 offset += want;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400445 }
Trond Myklebust16e5e902018-12-02 15:22:48 -0500446
Trond Myklebuste92053a2019-02-15 18:14:27 -0500447 want = min_t(size_t, count - offset, buf->tail[0].iov_len);
448 if (seek < want) {
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400449 ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek);
450 if (ret <= 0)
451 goto sock_err;
452 offset += ret;
453 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
454 goto out;
455 if (ret != want)
Trond Myklebustc4433052018-12-04 07:52:11 -0500456 goto out;
Trond Myklebust9734ad52019-03-15 12:47:34 -0400457 } else if (offset < seek_init)
Trond Myklebuste92053a2019-02-15 18:14:27 -0500458 offset = seek_init;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400459 ret = -EMSGSIZE;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400460out:
461 *read = offset - seek_init;
462 return ret;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400463sock_err:
464 offset += seek;
465 goto out;
466}
467
468static void
469xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf)
470{
471 if (!transport->recv.copied) {
472 if (buf->head[0].iov_len >= transport->recv.offset)
473 memcpy(buf->head[0].iov_base,
474 &transport->recv.xid,
475 transport->recv.offset);
476 transport->recv.copied = transport->recv.offset;
477 }
478}
479
480static bool
481xs_read_stream_request_done(struct sock_xprt *transport)
482{
483 return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT);
484}
485
Trond Myklebuste92053a2019-02-15 18:14:27 -0500486static void
487xs_read_stream_check_eor(struct sock_xprt *transport,
488 struct msghdr *msg)
489{
490 if (xs_read_stream_request_done(transport))
491 msg->msg_flags |= MSG_EOR;
492}
493
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400494static ssize_t
495xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg,
496 int flags, struct rpc_rqst *req)
497{
498 struct xdr_buf *buf = &req->rq_private_buf;
Alakesh Haloi01f2f5b2019-03-26 02:00:01 +0000499 size_t want, uninitialized_var(read);
500 ssize_t uninitialized_var(ret);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400501
502 xs_read_header(transport, buf);
503
504 want = transport->recv.len - transport->recv.offset;
Trond Myklebuste92053a2019-02-15 18:14:27 -0500505 if (want != 0) {
506 ret = xs_read_xdr_buf(transport->sock, msg, flags, buf,
507 transport->recv.copied + want,
508 transport->recv.copied,
509 &read);
510 transport->recv.offset += read;
511 transport->recv.copied += read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400512 }
513
Trond Myklebust727fcc62019-02-15 16:53:04 -0500514 if (transport->recv.offset == transport->recv.len)
515 xs_read_stream_check_eor(transport, msg);
516
Trond Myklebuste92053a2019-02-15 18:14:27 -0500517 if (want == 0)
518 return 0;
519
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400520 switch (ret) {
Trond Myklebustc4433052018-12-04 07:52:11 -0500521 default:
522 break;
Trond Myklebust26781ea2018-12-03 15:41:38 -0500523 case -EFAULT:
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400524 case -EMSGSIZE:
Trond Myklebust26781ea2018-12-03 15:41:38 -0500525 msg->msg_flags |= MSG_TRUNC;
Trond Myklebustc4433052018-12-04 07:52:11 -0500526 return read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400527 case 0:
528 return -ESHUTDOWN;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400529 }
Trond Myklebustc4433052018-12-04 07:52:11 -0500530 return ret < 0 ? ret : read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400531}
532
533static size_t
534xs_read_stream_headersize(bool isfrag)
535{
536 if (isfrag)
537 return sizeof(__be32);
538 return 3 * sizeof(__be32);
539}
540
541static ssize_t
542xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg,
543 int flags, size_t want, size_t seek)
544{
545 struct kvec kvec = {
546 .iov_base = &transport->recv.fraghdr,
547 .iov_len = want,
548 };
549 return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek);
550}
551
552#if defined(CONFIG_SUNRPC_BACKCHANNEL)
553static ssize_t
554xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
555{
556 struct rpc_xprt *xprt = &transport->xprt;
557 struct rpc_rqst *req;
558 ssize_t ret;
559
560 /* Look up and lock the request corresponding to the given XID */
561 req = xprt_lookup_bc_request(xprt, transport->recv.xid);
562 if (!req) {
563 printk(KERN_WARNING "Callback slot table overflowed\n");
564 return -ESHUTDOWN;
565 }
Trond Myklebust45835a62019-09-12 08:04:25 -0400566 if (transport->recv.copied && !req->rq_private_buf.len)
567 return -ESHUTDOWN;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400568
569 ret = xs_read_stream_request(transport, msg, flags, req);
570 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
Trond Myklebustc4433052018-12-04 07:52:11 -0500571 xprt_complete_bc_request(req, transport->recv.copied);
Trond Myklebust45835a62019-09-12 08:04:25 -0400572 else
573 req->rq_private_buf.len = transport->recv.copied;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400574
575 return ret;
576}
577#else /* CONFIG_SUNRPC_BACKCHANNEL */
578static ssize_t
579xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
580{
581 return -ESHUTDOWN;
582}
583#endif /* CONFIG_SUNRPC_BACKCHANNEL */
584
585static ssize_t
586xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags)
587{
588 struct rpc_xprt *xprt = &transport->xprt;
589 struct rpc_rqst *req;
590 ssize_t ret = 0;
591
592 /* Look up and lock the request corresponding to the given XID */
593 spin_lock(&xprt->queue_lock);
594 req = xprt_lookup_rqst(xprt, transport->recv.xid);
Trond Myklebust45835a62019-09-12 08:04:25 -0400595 if (!req || (transport->recv.copied && !req->rq_private_buf.len)) {
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400596 msg->msg_flags |= MSG_TRUNC;
597 goto out;
598 }
599 xprt_pin_rqst(req);
600 spin_unlock(&xprt->queue_lock);
601
602 ret = xs_read_stream_request(transport, msg, flags, req);
603
604 spin_lock(&xprt->queue_lock);
605 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
Trond Myklebustc4433052018-12-04 07:52:11 -0500606 xprt_complete_rqst(req->rq_task, transport->recv.copied);
Trond Myklebust45835a62019-09-12 08:04:25 -0400607 else
608 req->rq_private_buf.len = transport->recv.copied;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400609 xprt_unpin_rqst(req);
610out:
611 spin_unlock(&xprt->queue_lock);
612 return ret;
613}
614
615static ssize_t
616xs_read_stream(struct sock_xprt *transport, int flags)
617{
618 struct msghdr msg = { 0 };
619 size_t want, read = 0;
620 ssize_t ret = 0;
621
622 if (transport->recv.len == 0) {
623 want = xs_read_stream_headersize(transport->recv.copied != 0);
624 ret = xs_read_stream_header(transport, &msg, flags, want,
625 transport->recv.offset);
626 if (ret <= 0)
627 goto out_err;
628 transport->recv.offset = ret;
Trond Myklebustc4433052018-12-04 07:52:11 -0500629 if (transport->recv.offset != want)
630 return transport->recv.offset;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400631 transport->recv.len = be32_to_cpu(transport->recv.fraghdr) &
632 RPC_FRAGMENT_SIZE_MASK;
633 transport->recv.offset -= sizeof(transport->recv.fraghdr);
634 read = ret;
635 }
636
637 switch (be32_to_cpu(transport->recv.calldir)) {
Trond Myklebustc4433052018-12-04 07:52:11 -0500638 default:
639 msg.msg_flags |= MSG_TRUNC;
640 break;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400641 case RPC_CALL:
642 ret = xs_read_stream_call(transport, &msg, flags);
643 break;
644 case RPC_REPLY:
645 ret = xs_read_stream_reply(transport, &msg, flags);
646 }
647 if (msg.msg_flags & MSG_TRUNC) {
648 transport->recv.calldir = cpu_to_be32(-1);
649 transport->recv.copied = -1;
650 }
651 if (ret < 0)
652 goto out_err;
653 read += ret;
654 if (transport->recv.offset < transport->recv.len) {
Trond Myklebustc4433052018-12-04 07:52:11 -0500655 if (!(msg.msg_flags & MSG_TRUNC))
656 return read;
Trond Myklebustb76a5af2018-12-03 15:58:58 -0500657 msg.msg_flags = 0;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400658 ret = xs_read_discard(transport->sock, &msg, flags,
659 transport->recv.len - transport->recv.offset);
660 if (ret <= 0)
661 goto out_err;
662 transport->recv.offset += ret;
663 read += ret;
664 if (transport->recv.offset != transport->recv.len)
Trond Myklebustc4433052018-12-04 07:52:11 -0500665 return read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400666 }
667 if (xs_read_stream_request_done(transport)) {
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400668 trace_xs_stream_read_request(transport);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400669 transport->recv.copied = 0;
670 }
671 transport->recv.offset = 0;
672 transport->recv.len = 0;
673 return read;
674out_err:
Trond Myklebust794628572018-12-03 18:49:00 -0500675 return ret != 0 ? ret : -ESHUTDOWN;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400676}
677
Trond Myklebust0ffe86f2019-01-30 14:51:26 -0500678static __poll_t xs_poll_socket(struct sock_xprt *transport)
679{
Trond Myklebusta73881c2019-02-26 06:33:02 -0500680 return transport->sock->ops->poll(transport->file, transport->sock,
681 NULL);
Trond Myklebust0ffe86f2019-01-30 14:51:26 -0500682}
683
684static bool xs_poll_socket_readable(struct sock_xprt *transport)
685{
686 __poll_t events = xs_poll_socket(transport);
687
688 return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP);
689}
690
691static void xs_poll_check_readable(struct sock_xprt *transport)
692{
693
694 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
695 if (!xs_poll_socket_readable(transport))
696 return;
697 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
698 queue_work(xprtiod_workqueue, &transport->recv_worker);
699}
700
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400701static void xs_stream_data_receive(struct sock_xprt *transport)
702{
703 size_t read = 0;
704 ssize_t ret = 0;
705
706 mutex_lock(&transport->recv_mutex);
707 if (transport->sock == NULL)
708 goto out;
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400709 for (;;) {
710 ret = xs_read_stream(transport, MSG_DONTWAIT);
Trond Myklebustc4433052018-12-04 07:52:11 -0500711 if (ret < 0)
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400712 break;
713 read += ret;
714 cond_resched();
715 }
Trond Myklebust5f52a9d2019-02-16 09:31:47 -0500716 if (ret == -ESHUTDOWN)
717 kernel_sock_shutdown(transport->sock, SHUT_RDWR);
718 else
719 xs_poll_check_readable(transport);
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400720out:
721 mutex_unlock(&transport->recv_mutex);
722 trace_xs_stream_read_data(&transport->xprt, ret, read);
723}
724
725static void xs_stream_data_receive_workfn(struct work_struct *work)
726{
727 struct sock_xprt *transport =
728 container_of(work, struct sock_xprt, recv_worker);
Trond Myklebusta1231fd2019-02-18 10:02:29 -0500729 unsigned int pflags = memalloc_nofs_save();
730
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400731 xs_stream_data_receive(transport);
Trond Myklebusta1231fd2019-02-18 10:02:29 -0500732 memalloc_nofs_restore(pflags);
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400733}
734
Trond Myklebust550aebf2018-09-14 14:32:45 -0400735static void
736xs_stream_reset_connect(struct sock_xprt *transport)
737{
738 transport->recv.offset = 0;
739 transport->recv.len = 0;
740 transport->recv.copied = 0;
741 transport->xmit.offset = 0;
Trond Myklebustae053552019-02-20 14:56:20 -0500742}
743
744static void
745xs_stream_start_connect(struct sock_xprt *transport)
746{
Trond Myklebust550aebf2018-09-14 14:32:45 -0400747 transport->xprt.stat.connect_count++;
748 transport->xprt.stat.connect_start = jiffies;
749}
750
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400751#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
752
Chuck Lever9903cd12005-08-11 16:25:26 -0400753/**
Trond Myklebustc5445772018-09-03 23:39:27 -0400754 * xs_nospace - handle transmit was incomplete
Trond Myklebust50f484e2018-08-30 13:27:29 -0400755 * @req: pointer to RPC request
Chuck Lever9903cd12005-08-11 16:25:26 -0400756 *
Chuck Levera246b012005-08-11 16:25:23 -0400757 */
Trond Myklebustc5445772018-09-03 23:39:27 -0400758static int xs_nospace(struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -0400759{
Chuck Lever262965f2005-08-11 16:25:56 -0400760 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500761 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500762 struct sock *sk = transport->inet;
Trond Myklebust24ca9a82011-11-22 14:44:28 +0200763 int ret = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400764
Chuck Lever46121cf2007-01-31 12:14:08 -0500765 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Trond Myklebustc5445772018-09-03 23:39:27 -0400766 req->rq_task->tk_pid,
767 req->rq_slen - transport->xmit.offset,
Chuck Lever262965f2005-08-11 16:25:56 -0400768 req->rq_slen);
769
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400770 /* Protect against races with write_space */
Trond Myklebustb5e92412019-05-02 11:21:08 -0400771 spin_lock(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400772
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400773 /* Don't race with disconnect */
774 if (xprt_connected(xprt)) {
Trond Myklebust13331a52016-01-06 08:57:06 -0500775 /* wait for more buffer space */
776 sk->sk_write_pending++;
Trond Myklebustc5445772018-09-03 23:39:27 -0400777 xprt_wait_for_buffer_space(xprt);
Trond Myklebust13331a52016-01-06 08:57:06 -0500778 } else
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400779 ret = -ENOTCONN;
Chuck Lever262965f2005-08-11 16:25:56 -0400780
Trond Myklebustb5e92412019-05-02 11:21:08 -0400781 spin_unlock(&xprt->transport_lock);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500782
783 /* Race breaker in case memory is freed before above code is called */
David Vrabeld48f9ce2016-09-19 13:58:30 +0100784 if (ret == -EAGAIN) {
785 struct socket_wq *wq;
786
787 rcu_read_lock();
788 wq = rcu_dereference(sk->sk_wq);
789 set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags);
790 rcu_read_unlock();
791
792 sk->sk_write_space(sk);
793 }
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400794 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400795}
796
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400797static void
798xs_stream_prepare_request(struct rpc_rqst *req)
799{
Trond Myklebust75369082019-07-17 21:22:38 -0400800 xdr_free_bvec(&req->rq_rcv_buf);
Trond Myklebust12a3ad62019-03-02 10:14:02 -0500801 req->rq_task->tk_status = xdr_alloc_bvec(&req->rq_rcv_buf, GFP_KERNEL);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400802}
803
Chuck Lever61677ee2011-05-09 15:22:34 -0400804/*
Trond Myklebust4cd34e72018-08-31 10:00:02 -0400805 * Determine if the previous message in the stream was aborted before it
806 * could complete transmission.
807 */
808static bool
809xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
810{
811 return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
812}
813
814/*
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500815 * Return the stream record marker field for a record of length < 2^31-1
Chuck Lever61677ee2011-05-09 15:22:34 -0400816 */
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500817static rpc_fraghdr
818xs_stream_record_marker(struct xdr_buf *xdr)
Chuck Lever61677ee2011-05-09 15:22:34 -0400819{
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500820 if (!xdr->len)
821 return 0;
822 return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len);
Chuck Lever61677ee2011-05-09 15:22:34 -0400823}
824
Chuck Lever262965f2005-08-11 16:25:56 -0400825/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400826 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
Trond Myklebust50f484e2018-08-30 13:27:29 -0400827 * @req: pointer to RPC request
Chuck Lever176e21e2011-05-09 15:22:44 -0400828 *
829 * Return values:
830 * 0: The request has been sent
831 * EAGAIN: The socket was blocked, please call again later to
832 * complete the request
833 * ENOTCONN: Caller needs to invoke connect logic then call again
834 * other: Some other error occured, the request was not sent
835 */
Trond Myklebustadfa7142018-09-03 23:58:59 -0400836static int xs_local_send_request(struct rpc_rqst *req)
Chuck Lever176e21e2011-05-09 15:22:44 -0400837{
Chuck Lever176e21e2011-05-09 15:22:44 -0400838 struct rpc_xprt *xprt = req->rq_xprt;
839 struct sock_xprt *transport =
840 container_of(xprt, struct sock_xprt, xprt);
841 struct xdr_buf *xdr = &req->rq_snd_buf;
Trond Myklebust7e3d3622019-06-24 19:15:44 -0400842 rpc_fraghdr rm = xs_stream_record_marker(xdr);
843 unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
Chuck Lever9e55eef42020-03-02 15:19:54 -0500844 struct msghdr msg = {
845 .msg_flags = XS_SENDMSG_FLAGS,
846 };
847 unsigned int uninitialized_var(sent);
Chuck Lever176e21e2011-05-09 15:22:44 -0400848 int status;
849
Trond Myklebust4cd34e72018-08-31 10:00:02 -0400850 /* Close the stream if the previous transmission was incomplete */
851 if (xs_send_request_was_aborted(transport, req)) {
852 xs_close(xprt);
853 return -ENOTCONN;
854 }
855
Chuck Lever176e21e2011-05-09 15:22:44 -0400856 xs_pktdump("packet data:",
857 req->rq_svec->iov_base, req->rq_svec->iov_len);
858
Chuck Lever78215752018-03-05 15:13:07 -0500859 req->rq_xtime = ktime_get();
Chuck Lever9e55eef42020-03-02 15:19:54 -0500860 status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
861 transport->xmit.offset, rm, &sent);
Chuck Lever176e21e2011-05-09 15:22:44 -0400862 dprintk("RPC: %s(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400863 __func__, xdr->len - transport->xmit.offset, status);
NeilBrown743c69e2015-07-27 10:55:35 +1000864
865 if (status == -EAGAIN && sock_writeable(transport->inet))
866 status = -ENOBUFS;
867
Jason Baronf279cd02014-09-24 18:08:00 +0000868 if (likely(sent > 0) || status == 0) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400869 transport->xmit.offset += sent;
870 req->rq_bytes_sent = transport->xmit.offset;
Trond Myklebust7e3d3622019-06-24 19:15:44 -0400871 if (likely(req->rq_bytes_sent >= msglen)) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400872 req->rq_xmit_bytes_sent += transport->xmit.offset;
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400873 transport->xmit.offset = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -0400874 return 0;
875 }
876 status = -EAGAIN;
877 }
878
879 switch (status) {
Trond Myklebust3601c4a2014-06-30 13:42:19 -0400880 case -ENOBUFS:
Trond Myklebustb5872f02015-07-03 09:32:23 -0400881 break;
Chuck Lever176e21e2011-05-09 15:22:44 -0400882 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -0400883 status = xs_nospace(req);
Chuck Lever176e21e2011-05-09 15:22:44 -0400884 break;
885 default:
886 dprintk("RPC: sendmsg returned unrecognized error %d\n",
887 -status);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -0500888 /* fall through */
Chuck Lever176e21e2011-05-09 15:22:44 -0400889 case -EPIPE:
890 xs_close(xprt);
891 status = -ENOTCONN;
892 }
893
894 return status;
895}
896
897/**
Chuck Lever262965f2005-08-11 16:25:56 -0400898 * xs_udp_send_request - write an RPC request to a UDP socket
Trond Myklebust50f484e2018-08-30 13:27:29 -0400899 * @req: pointer to RPC request
Chuck Lever262965f2005-08-11 16:25:56 -0400900 *
901 * Return values:
902 * 0: The request has been sent
903 * EAGAIN: The socket was blocked, please call again later to
904 * complete the request
905 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300906 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -0400907 */
Trond Myklebustadfa7142018-09-03 23:58:59 -0400908static int xs_udp_send_request(struct rpc_rqst *req)
Chuck Lever262965f2005-08-11 16:25:56 -0400909{
Chuck Lever262965f2005-08-11 16:25:56 -0400910 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500911 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400912 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Lever9e55eef42020-03-02 15:19:54 -0500913 struct msghdr msg = {
914 .msg_name = xs_addr(xprt),
915 .msg_namelen = xprt->addrlen,
916 .msg_flags = XS_SENDMSG_FLAGS,
917 };
918 unsigned int uninitialized_var(sent);
Chuck Lever262965f2005-08-11 16:25:56 -0400919 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400920
Chuck Lever9903cd12005-08-11 16:25:26 -0400921 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400922 req->rq_svec->iov_base,
923 req->rq_svec->iov_len);
924
Trond Myklebust01d37c42009-03-11 14:09:39 -0400925 if (!xprt_bound(xprt))
926 return -ENOTCONN;
Trond Myklebust75891f52018-09-03 17:37:36 -0400927
928 if (!xprt_request_get_cong(xprt, req))
929 return -EBADSLT;
930
Chuck Lever78215752018-03-05 15:13:07 -0500931 req->rq_xtime = ktime_get();
Chuck Lever9e55eef42020-03-02 15:19:54 -0500932 status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, 0, &sent);
Chuck Levera246b012005-08-11 16:25:23 -0400933
Chuck Lever46121cf2007-01-31 12:14:08 -0500934 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400935 xdr->len, status);
Chuck Levera246b012005-08-11 16:25:23 -0400936
Jason Baron3dedbb52014-09-24 18:08:04 +0000937 /* firewall is blocking us, don't return -EAGAIN or we end up looping */
938 if (status == -EPERM)
939 goto process_status;
940
NeilBrown743c69e2015-07-27 10:55:35 +1000941 if (status == -EAGAIN && sock_writeable(transport->inet))
942 status = -ENOBUFS;
943
Jason Baronf279cd02014-09-24 18:08:00 +0000944 if (sent > 0 || status == 0) {
945 req->rq_xmit_bytes_sent += sent;
946 if (sent >= req->rq_slen)
Trond Myklebust21997002007-10-01 11:43:37 -0400947 return 0;
948 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400949 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400950 }
Chuck Levera246b012005-08-11 16:25:23 -0400951
Jason Baron3dedbb52014-09-24 18:08:04 +0000952process_status:
Chuck Lever262965f2005-08-11 16:25:56 -0400953 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400954 case -ENOTSOCK:
955 status = -ENOTCONN;
956 /* Should we call xs_close() here? */
957 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400958 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -0400959 status = xs_nospace(req);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400960 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400961 case -ENETUNREACH:
Trond Myklebust3601c4a2014-06-30 13:42:19 -0400962 case -ENOBUFS:
Chuck Lever262965f2005-08-11 16:25:56 -0400963 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400964 case -ECONNREFUSED:
Jason Baron3dedbb52014-09-24 18:08:04 +0000965 case -EPERM:
Chuck Levera246b012005-08-11 16:25:23 -0400966 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400967 * prompts ECONNREFUSED. */
Trond Myklebust13331a52016-01-06 08:57:06 -0500968 break;
969 default:
970 dprintk("RPC: sendmsg returned unrecognized error %d\n",
971 -status);
Chuck Levera246b012005-08-11 16:25:23 -0400972 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800973
Chuck Lever262965f2005-08-11 16:25:56 -0400974 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400975}
976
Trond Myklebuste06799f2007-11-05 15:44:12 -0500977/**
Chuck Lever262965f2005-08-11 16:25:56 -0400978 * xs_tcp_send_request - write an RPC request to a TCP socket
Trond Myklebust50f484e2018-08-30 13:27:29 -0400979 * @req: pointer to RPC request
Chuck Lever9903cd12005-08-11 16:25:26 -0400980 *
981 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400982 * 0: The request has been sent
983 * EAGAIN: The socket was blocked, please call again later to
984 * complete the request
985 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300986 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400987 *
988 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400989 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400990 */
Trond Myklebustadfa7142018-09-03 23:58:59 -0400991static int xs_tcp_send_request(struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -0400992{
Chuck Levera246b012005-08-11 16:25:23 -0400993 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500994 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400995 struct xdr_buf *xdr = &req->rq_snd_buf;
Trond Myklebust7e3d3622019-06-24 19:15:44 -0400996 rpc_fraghdr rm = xs_stream_record_marker(xdr);
997 unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
Chuck Lever9e55eef42020-03-02 15:19:54 -0500998 struct msghdr msg = {
999 .msg_flags = XS_SENDMSG_FLAGS,
1000 };
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001001 bool vm_wait = false;
Chuck Lever9e55eef42020-03-02 15:19:54 -05001002 unsigned int uninitialized_var(sent);
Chuck Leverb595bb12007-08-06 11:56:42 -04001003 int status;
Chuck Levera246b012005-08-11 16:25:23 -04001004
Trond Myklebust4cd34e72018-08-31 10:00:02 -04001005 /* Close the stream if the previous transmission was incomplete */
1006 if (xs_send_request_was_aborted(transport, req)) {
1007 if (transport->sock != NULL)
1008 kernel_sock_shutdown(transport->sock, SHUT_RDWR);
1009 return -ENOTCONN;
1010 }
1011
Chuck Lever262965f2005-08-11 16:25:56 -04001012 xs_pktdump("packet data:",
1013 req->rq_svec->iov_base,
1014 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -04001015
Trond Myklebust7196dbb2017-02-08 11:17:54 -05001016 if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
1017 xs_tcp_set_socket_timeouts(xprt, transport->sock);
1018
Chuck Levera246b012005-08-11 16:25:23 -04001019 /* Continue transmitting the packet/record. We must be careful
1020 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -04001021 * called sendmsg(). */
Chuck Lever78215752018-03-05 15:13:07 -05001022 req->rq_xtime = ktime_get();
Chuck Levera246b012005-08-11 16:25:23 -04001023 while (1) {
Chuck Lever9e55eef42020-03-02 15:19:54 -05001024 status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
1025 transport->xmit.offset, rm, &sent);
Chuck Levera246b012005-08-11 16:25:23 -04001026
Chuck Lever46121cf2007-01-31 12:14:08 -05001027 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001028 xdr->len - transport->xmit.offset, status);
Chuck Lever262965f2005-08-11 16:25:56 -04001029
Chuck Lever262965f2005-08-11 16:25:56 -04001030 /* If we've sent the entire packet, immediately
1031 * reset the count of bytes sent. */
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001032 transport->xmit.offset += sent;
1033 req->rq_bytes_sent = transport->xmit.offset;
Trond Myklebust7e3d3622019-06-24 19:15:44 -04001034 if (likely(req->rq_bytes_sent >= msglen)) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001035 req->rq_xmit_bytes_sent += transport->xmit.offset;
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001036 transport->xmit.offset = 0;
Chuck Lever262965f2005-08-11 16:25:56 -04001037 return 0;
Chuck Levera246b012005-08-11 16:25:23 -04001038 }
1039
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001040 WARN_ON_ONCE(sent == 0 && status == 0);
1041
1042 if (status == -EAGAIN ) {
1043 /*
1044 * Return EAGAIN if we're sure we're hitting the
1045 * socket send buffer limits.
1046 */
1047 if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
1048 break;
1049 /*
1050 * Did we hit a memory allocation failure?
1051 */
1052 if (sent == 0) {
1053 status = -ENOBUFS;
1054 if (vm_wait)
1055 break;
1056 /* Retry, knowing now that we're below the
1057 * socket send buffer limit
1058 */
1059 vm_wait = true;
1060 }
1061 continue;
1062 }
Trond Myklebustf580dd02015-07-11 17:48:52 +02001063 if (status < 0)
1064 break;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001065 vm_wait = false;
Chuck Levera246b012005-08-11 16:25:23 -04001066 }
1067
Chuck Lever262965f2005-08-11 16:25:56 -04001068 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -04001069 case -ENOTSOCK:
1070 status = -ENOTCONN;
1071 /* Should we call xs_close() here? */
1072 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001073 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -04001074 status = xs_nospace(req);
Chuck Lever262965f2005-08-11 16:25:56 -04001075 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001076 case -ECONNRESET:
Chuck Lever262965f2005-08-11 16:25:56 -04001077 case -ECONNREFUSED:
1078 case -ENOTCONN:
Trond Myklebust3913c782015-02-08 21:44:04 -05001079 case -EADDRINUSE:
Trond Myklebustb5872f02015-07-03 09:32:23 -04001080 case -ENOBUFS:
Trond Myklebustb9d2bb22012-10-23 11:40:02 -04001081 case -EPIPE:
Trond Myklebust13331a52016-01-06 08:57:06 -05001082 break;
1083 default:
1084 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1085 -status);
Chuck Levera246b012005-08-11 16:25:23 -04001086 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +08001087
Chuck Levera246b012005-08-11 16:25:23 -04001088 return status;
1089}
1090
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001091static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1092{
1093 transport->old_data_ready = sk->sk_data_ready;
1094 transport->old_state_change = sk->sk_state_change;
1095 transport->old_write_space = sk->sk_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -05001096 transport->old_error_report = sk->sk_error_report;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001097}
1098
1099static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1100{
1101 sk->sk_data_ready = transport->old_data_ready;
1102 sk->sk_state_change = transport->old_state_change;
1103 sk->sk_write_space = transport->old_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -05001104 sk->sk_error_report = transport->old_error_report;
1105}
1106
Trond Myklebust42d42a52016-05-23 09:24:55 -04001107static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
1108{
1109 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1110
1111 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001112 clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state);
1113 clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state);
1114 clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state);
1115}
1116
1117static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr)
1118{
1119 set_bit(nr, &transport->sock_state);
1120 queue_work(xprtiod_workqueue, &transport->error_worker);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001121}
1122
Trond Myklebustb70ae912015-02-09 09:41:32 -05001123static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1124{
1125 smp_mb__before_atomic();
1126 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1127 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001128 xs_sock_reset_state_flags(xprt);
Trond Myklebustb70ae912015-02-09 09:41:32 -05001129 smp_mb__after_atomic();
1130}
1131
Trond Myklebust21180712013-12-31 13:22:59 -05001132/**
1133 * xs_error_report - callback to handle TCP socket state errors
1134 * @sk: socket
1135 *
1136 * Note: we don't call sock_error() since there may be a rpc_task
1137 * using the socket, and so we don't want to clear sk->sk_err.
1138 */
1139static void xs_error_report(struct sock *sk)
1140{
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001141 struct sock_xprt *transport;
Trond Myklebust21180712013-12-31 13:22:59 -05001142 struct rpc_xprt *xprt;
Trond Myklebust21180712013-12-31 13:22:59 -05001143
1144 read_lock_bh(&sk->sk_callback_lock);
1145 if (!(xprt = xprt_from_sock(sk)))
1146 goto out;
1147
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001148 transport = container_of(xprt, struct sock_xprt, xprt);
Benjamin Coddingtonaf845372019-10-02 10:40:55 -04001149 transport->xprt_err = -sk->sk_err;
1150 if (transport->xprt_err == 0)
Trond Myklebust21180712013-12-31 13:22:59 -05001151 goto out;
1152 dprintk("RPC: xs_error_report client %p, error=%d...\n",
Benjamin Coddingtonaf845372019-10-02 10:40:55 -04001153 xprt, -transport->xprt_err);
1154 trace_rpc_socket_error(xprt, sk->sk_socket, transport->xprt_err);
1155
1156 /* barrier ensures xprt_err is set before XPRT_SOCK_WAKE_ERROR */
1157 smp_mb__before_atomic();
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001158 xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR);
Trond Myklebust21180712013-12-31 13:22:59 -05001159 out:
1160 read_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001161}
1162
Chuck Leverfe315e762009-03-11 14:10:21 -04001163static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001164{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001165 struct socket *sock = transport->sock;
1166 struct sock *sk = transport->inet;
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001167 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebusta73881c2019-02-26 06:33:02 -05001168 struct file *filp = transport->file;
Chuck Levera246b012005-08-11 16:25:23 -04001169
Chuck Leverfe315e762009-03-11 14:10:21 -04001170 if (sk == NULL)
1171 return;
Chuck Lever9903cd12005-08-11 16:25:26 -04001172
Jeff Layton264d1df2015-06-03 16:14:27 -04001173 if (atomic_read(&transport->xprt.swapper))
1174 sk_clear_memalloc(sk);
1175
Trond Myklebust09939202015-08-29 19:11:21 -07001176 kernel_sock_shutdown(sock, SHUT_RDWR);
1177
Trond Myklebustedc1b012015-10-05 10:53:49 -04001178 mutex_lock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -04001179 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001180 transport->inet = NULL;
1181 transport->sock = NULL;
Trond Myklebusta73881c2019-02-26 06:33:02 -05001182 transport->file = NULL;
Chuck Levera246b012005-08-11 16:25:23 -04001183
Chuck Lever9903cd12005-08-11 16:25:26 -04001184 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001185
1186 xs_restore_old_callbacks(transport, sk);
Trond Myklebust0c787892015-08-29 13:36:30 -07001187 xprt_clear_connected(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001188 write_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001189 xs_sock_reset_connection_flags(xprt);
Trond Myklebustae053552019-02-20 14:56:20 -05001190 /* Reset stream record info */
1191 xs_stream_reset_connect(transport);
Trond Myklebustedc1b012015-10-05 10:53:49 -04001192 mutex_unlock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -04001193
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001194 trace_rpc_socket_close(xprt, sock);
Trond Myklebusta73881c2019-02-26 06:33:02 -05001195 fput(filp);
Trond Myklebust0445f922018-12-17 13:34:59 -05001196
1197 xprt_disconnect_done(xprt);
Chuck Leverfe315e762009-03-11 14:10:21 -04001198}
1199
1200/**
1201 * xs_close - close a socket
1202 * @xprt: transport
1203 *
1204 * This is used when all requests are complete; ie, no DRC state remains
1205 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -04001206 *
1207 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1208 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e762009-03-11 14:10:21 -04001209 */
1210static void xs_close(struct rpc_xprt *xprt)
1211{
1212 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1213
1214 dprintk("RPC: xs_close xprt %p\n", xprt);
1215
1216 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -04001217 xprt->reestablish_timeout = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001218}
1219
Chuck Lever4a068252015-05-11 14:02:25 -04001220static void xs_inject_disconnect(struct rpc_xprt *xprt)
1221{
1222 dprintk("RPC: injecting transport disconnect on xprt=%p\n",
1223 xprt);
1224 xprt_disconnect_done(xprt);
1225}
1226
Kinglong Mee315f3812014-03-24 11:07:22 +08001227static void xs_xprt_free(struct rpc_xprt *xprt)
1228{
1229 xs_free_peer_addresses(xprt);
1230 xprt_free(xprt);
1231}
1232
Chuck Lever9903cd12005-08-11 16:25:26 -04001233/**
1234 * xs_destroy - prepare to shutdown a transport
1235 * @xprt: doomed transport
1236 *
1237 */
1238static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001239{
Trond Myklebust03c788272015-09-17 10:42:27 -04001240 struct sock_xprt *transport = container_of(xprt,
1241 struct sock_xprt, xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05001242 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001243
Trond Myklebust03c788272015-09-17 10:42:27 -04001244 cancel_delayed_work_sync(&transport->connect_worker);
Trond Myklebusta1311d82013-10-31 09:18:49 -04001245 xs_close(xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04001246 cancel_work_sync(&transport->recv_worker);
Trond Myklebustb5e92412019-05-02 11:21:08 -04001247 cancel_work_sync(&transport->error_worker);
Kinglong Mee315f3812014-03-24 11:07:22 +08001248 xs_xprt_free(xprt);
Trond Myklebusta1311d82013-10-31 09:18:49 -04001249 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -04001250}
1251
Chuck Lever9903cd12005-08-11 16:25:26 -04001252/**
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001253 * xs_udp_data_read_skb - receive callback for UDP sockets
1254 * @xprt: transport
1255 * @sk: socket
1256 * @skb: skbuff
Chuck Lever9903cd12005-08-11 16:25:26 -04001257 *
1258 */
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001259static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1260 struct sock *sk,
1261 struct sk_buff *skb)
Chuck Lever9903cd12005-08-11 16:25:26 -04001262{
1263 struct rpc_task *task;
Chuck Levera246b012005-08-11 16:25:23 -04001264 struct rpc_rqst *rovr;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001265 int repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001266 u32 _xid;
1267 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -04001268
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001269 repsize = skb->len;
Chuck Levera246b012005-08-11 16:25:23 -04001270 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001271 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001272 return;
Chuck Levera246b012005-08-11 16:25:23 -04001273 }
1274
1275 /* Copy the XID from the skb... */
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001276 xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
Chuck Levera246b012005-08-11 16:25:23 -04001277 if (xp == NULL)
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001278 return;
Chuck Levera246b012005-08-11 16:25:23 -04001279
1280 /* Look up and lock the request corresponding to the given XID */
Trond Myklebust75c84152018-08-31 10:21:00 -04001281 spin_lock(&xprt->queue_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001282 rovr = xprt_lookup_rqst(xprt, *xp);
1283 if (!rovr)
1284 goto out_unlock;
Trond Myklebust729749b2017-08-13 10:03:59 -04001285 xprt_pin_rqst(rovr);
Chuck Leverecd465e2018-03-05 15:12:57 -05001286 xprt_update_rtt(rovr->rq_task);
Trond Myklebust75c84152018-08-31 10:21:00 -04001287 spin_unlock(&xprt->queue_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001288 task = rovr->rq_task;
1289
Chuck Levera246b012005-08-11 16:25:23 -04001290 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1291 copied = repsize;
1292
1293 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001294 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
Trond Myklebust75c84152018-08-31 10:21:00 -04001295 spin_lock(&xprt->queue_lock);
Trond Myklebust0afa6b42018-02-09 09:39:42 -05001296 __UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
Trond Myklebust729749b2017-08-13 10:03:59 -04001297 goto out_unpin;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001298 }
1299
Chuck Levera246b012005-08-11 16:25:23 -04001300
Trond Myklebustb5e92412019-05-02 11:21:08 -04001301 spin_lock(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001302 xprt_adjust_cwnd(xprt, task, copied);
Trond Myklebustb5e92412019-05-02 11:21:08 -04001303 spin_unlock(&xprt->transport_lock);
Trond Myklebust75c84152018-08-31 10:21:00 -04001304 spin_lock(&xprt->queue_lock);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001305 xprt_complete_rqst(task, copied);
Trond Myklebust0afa6b42018-02-09 09:39:42 -05001306 __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
Trond Myklebust729749b2017-08-13 10:03:59 -04001307out_unpin:
1308 xprt_unpin_rqst(rovr);
Chuck Levera246b012005-08-11 16:25:23 -04001309 out_unlock:
Trond Myklebust75c84152018-08-31 10:21:00 -04001310 spin_unlock(&xprt->queue_lock);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001311}
1312
1313static void xs_udp_data_receive(struct sock_xprt *transport)
1314{
1315 struct sk_buff *skb;
1316 struct sock *sk;
1317 int err;
1318
1319 mutex_lock(&transport->recv_mutex);
1320 sk = transport->inet;
1321 if (sk == NULL)
1322 goto out;
1323 for (;;) {
Paolo Abeni7c13f972016-11-04 11:28:59 +01001324 skb = skb_recv_udp(sk, 0, 1, &err);
Trond Myklebust4f546142018-09-14 17:45:23 -04001325 if (skb == NULL)
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001326 break;
Trond Myklebust4f546142018-09-14 17:45:23 -04001327 xs_udp_data_read_skb(&transport->xprt, sk, skb);
1328 consume_skb(skb);
1329 cond_resched();
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001330 }
Trond Myklebust0ffe86f2019-01-30 14:51:26 -05001331 xs_poll_check_readable(transport);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001332out:
1333 mutex_unlock(&transport->recv_mutex);
1334}
1335
1336static void xs_udp_data_receive_workfn(struct work_struct *work)
1337{
1338 struct sock_xprt *transport =
1339 container_of(work, struct sock_xprt, recv_worker);
Trond Myklebusta1231fd2019-02-18 10:02:29 -05001340 unsigned int pflags = memalloc_nofs_save();
1341
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001342 xs_udp_data_receive(transport);
Trond Myklebusta1231fd2019-02-18 10:02:29 -05001343 memalloc_nofs_restore(pflags);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001344}
1345
1346/**
1347 * xs_data_ready - "data ready" callback for UDP sockets
1348 * @sk: socket with data to read
1349 *
1350 */
1351static void xs_data_ready(struct sock *sk)
1352{
1353 struct rpc_xprt *xprt;
1354
1355 read_lock_bh(&sk->sk_callback_lock);
1356 dprintk("RPC: xs_data_ready...\n");
1357 xprt = xprt_from_sock(sk);
1358 if (xprt != NULL) {
1359 struct sock_xprt *transport = container_of(xprt,
1360 struct sock_xprt, xprt);
Trond Myklebust5157b952016-05-29 10:13:24 -04001361 transport->old_data_ready(sk);
1362 /* Any data means we had a useful conversation, so
1363 * then we don't need to delay the next reconnect
1364 */
1365 if (xprt->reestablish_timeout)
1366 xprt->reestablish_timeout = 0;
Trond Myklebust42d42a52016-05-23 09:24:55 -04001367 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebust40a5f1b2016-05-27 10:39:50 -04001368 queue_work(xprtiod_workqueue, &transport->recv_worker);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001369 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001370 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001371}
1372
Trond Myklebusta519fc72012-09-12 16:49:15 -04001373/*
1374 * Helper function to force a TCP close if the server is sending
1375 * junk and/or it has put us in CLOSE_WAIT
1376 */
1377static void xs_tcp_force_close(struct rpc_xprt *xprt)
1378{
Trond Myklebusta519fc72012-09-12 16:49:15 -04001379 xprt_force_disconnect(xprt);
1380}
1381
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001382#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Chuck Lever6b26cc82016-05-02 14:40:40 -04001383static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
1384{
1385 return PAGE_SIZE;
1386}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001387#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001388
Chuck Lever9903cd12005-08-11 16:25:26 -04001389/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001390 * xs_tcp_state_change - callback to handle TCP socket state changes
1391 * @sk: socket whose state has changed
1392 *
1393 */
1394static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001395{
Chuck Lever9903cd12005-08-11 16:25:26 -04001396 struct rpc_xprt *xprt;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001397 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001398
Eric Dumazetf064af12010-09-22 12:43:39 +00001399 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001400 if (!(xprt = xprt_from_sock(sk)))
1401 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001402 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001403 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001404 sk->sk_state, xprt_connected(xprt),
1405 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001406 sock_flag(sk, SOCK_ZAPPED),
1407 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001408
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001409 transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001410 trace_rpc_socket_state_change(xprt, sk->sk_socket);
Chuck Levera246b012005-08-11 16:25:23 -04001411 switch (sk->sk_state) {
1412 case TCP_ESTABLISHED:
Chuck Levera246b012005-08-11 16:25:23 -04001413 if (!xprt_test_and_set_connected(xprt)) {
Trond Myklebust8b717982013-09-26 10:18:04 -04001414 xprt->connect_cookie++;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001415 clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
1416 xprt_clear_connecting(xprt);
Chuck Lever51971132006-12-05 16:35:19 -05001417
Chuck Lever3968a8a2018-10-01 14:25:36 -04001418 xprt->stat.connect_count++;
1419 xprt->stat.connect_time += (long)jiffies -
1420 xprt->stat.connect_start;
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001421 xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING);
Chuck Levera246b012005-08-11 16:25:23 -04001422 }
Chuck Levera246b012005-08-11 16:25:23 -04001423 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001424 case TCP_FIN_WAIT1:
1425 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001426 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001427 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001428 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001429 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001430 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001431 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001432 smp_mb__after_atomic();
Chuck Levera246b012005-08-11 16:25:23 -04001433 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001434 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001435 /* The server initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001436 xprt->connect_cookie++;
Trond Myklebustd0bea452012-10-23 11:35:47 -04001437 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001438 xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001439 /* fall through */
Trond Myklebust663b8852008-01-01 18:42:12 -05001440 case TCP_CLOSING:
1441 /*
1442 * If the server closed down the connection, make sure that
1443 * we back off before reconnecting
1444 */
1445 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1446 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001447 break;
1448 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001449 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001450 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001451 clear_bit(XPRT_CONNECTED, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001452 smp_mb__after_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001453 break;
1454 case TCP_CLOSE:
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001455 if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
1456 &transport->sock_state))
1457 xprt_clear_connecting(xprt);
Trond Myklebust9b308892018-02-05 10:20:06 -05001458 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust9b308892018-02-05 10:20:06 -05001459 /* Trigger the socket release */
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001460 xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
Chuck Levera246b012005-08-11 16:25:23 -04001461 }
1462 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001463 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001464}
1465
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001466static void xs_write_space(struct sock *sk)
1467{
Trond Myklebust13331a52016-01-06 08:57:06 -05001468 struct socket_wq *wq;
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001469 struct sock_xprt *transport;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001470 struct rpc_xprt *xprt;
1471
Trond Myklebust13331a52016-01-06 08:57:06 -05001472 if (!sk->sk_socket)
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001473 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001474 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001475
1476 if (unlikely(!(xprt = xprt_from_sock(sk))))
1477 return;
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001478 transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust13331a52016-01-06 08:57:06 -05001479 rcu_read_lock();
1480 wq = rcu_dereference(sk->sk_wq);
1481 if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0)
1482 goto out;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001483
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001484 xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE);
1485 sk->sk_write_pending--;
Trond Myklebust13331a52016-01-06 08:57:06 -05001486out:
1487 rcu_read_unlock();
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001488}
1489
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001490/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001491 * xs_udp_write_space - callback invoked when socket buffer space
1492 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001493 * @sk: socket whose state has changed
1494 *
Chuck Levera246b012005-08-11 16:25:23 -04001495 * Called when more output buffer space is available for this socket.
1496 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001497 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001498 * with a bunch of small requests.
1499 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001500static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001501{
Eric Dumazetf064af12010-09-22 12:43:39 +00001502 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001503
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001504 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001505 if (sock_writeable(sk))
1506 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001507
Eric Dumazetf064af12010-09-22 12:43:39 +00001508 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001509}
Chuck Levera246b012005-08-11 16:25:23 -04001510
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001511/**
1512 * xs_tcp_write_space - callback invoked when socket buffer space
1513 * becomes available
1514 * @sk: socket whose state has changed
1515 *
1516 * Called when more output buffer space is available for this socket.
1517 * We try not to wake our writers until they can make "significant"
1518 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1519 * with a bunch of small requests.
1520 */
1521static void xs_tcp_write_space(struct sock *sk)
1522{
Eric Dumazetf064af12010-09-22 12:43:39 +00001523 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001524
1525 /* from net/core/stream.c:sk_stream_write_space */
Eric Dumazet64dc6132013-07-22 20:26:31 -07001526 if (sk_stream_is_writeable(sk))
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001527 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001528
Eric Dumazetf064af12010-09-22 12:43:39 +00001529 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001530}
1531
Chuck Lever470056c2005-08-25 16:25:56 -07001532static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001533{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001534 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1535 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001536
Chuck Lever7c6e0662006-12-05 16:35:30 -05001537 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001538 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001539 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001540 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001541 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001542 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001543 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001544 sk->sk_write_space(sk);
1545 }
1546}
1547
Chuck Lever43118c22005-08-25 16:25:49 -07001548/**
Chuck Lever470056c2005-08-25 16:25:56 -07001549 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001550 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001551 * @sndsize: requested size of send buffer, in bytes
1552 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001553 *
Chuck Lever470056c2005-08-25 16:25:56 -07001554 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001555 */
Chuck Lever470056c2005-08-25 16:25:56 -07001556static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001557{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001558 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1559
1560 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001561 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001562 transport->sndsize = sndsize + 1024;
1563 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001564 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001565 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001566
1567 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001568}
1569
Chuck Lever46c0ee82005-08-25 16:25:52 -07001570/**
1571 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
Chuck Leveracf0a392018-12-19 11:00:22 -05001572 * @xprt: controlling transport
Chuck Lever46c0ee82005-08-25 16:25:52 -07001573 * @task: task that timed out
1574 *
1575 * Adjust the congestion window after a retransmit timeout has occurred.
1576 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001577static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever46c0ee82005-08-25 16:25:52 -07001578{
Trond Myklebustb5e92412019-05-02 11:21:08 -04001579 spin_lock(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001580 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
Trond Myklebustb5e92412019-05-02 11:21:08 -04001581 spin_unlock(&xprt->transport_lock);
Chuck Lever46c0ee82005-08-25 16:25:52 -07001582}
1583
J. Bruce Fields826799e2018-10-18 15:27:02 -04001584static int xs_get_random_port(void)
Chuck Leverb85d8802006-05-25 01:40:49 -04001585{
J. Bruce Fields826799e2018-10-18 15:27:02 -04001586 unsigned short min = xprt_min_resvport, max = xprt_max_resvport;
1587 unsigned short range;
1588 unsigned short rand;
1589
1590 if (max < min)
1591 return -EADDRINUSE;
1592 range = max - min + 1;
1593 rand = (unsigned short) prandom_u32() % range;
1594 return rand + min;
Chuck Leverb85d8802006-05-25 01:40:49 -04001595}
1596
Chuck Lever92200412006-01-03 09:55:51 +01001597/**
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001598 * xs_set_reuseaddr_port - set the socket's port and address reuse options
1599 * @sock: socket
1600 *
1601 * Note that this function has to be called on all sockets that share the
1602 * same port, and it must be called before binding.
1603 */
1604static void xs_sock_set_reuseport(struct socket *sock)
1605{
Trond Myklebust402e23b2015-02-09 17:20:14 -05001606 int opt = 1;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001607
Trond Myklebust402e23b2015-02-09 17:20:14 -05001608 kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
1609 (char *)&opt, sizeof(opt));
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001610}
1611
1612static unsigned short xs_sock_getport(struct socket *sock)
1613{
1614 struct sockaddr_storage buf;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001615 unsigned short port = 0;
1616
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001617 if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001618 goto out;
1619 switch (buf.ss_family) {
1620 case AF_INET6:
1621 port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
1622 break;
1623 case AF_INET:
1624 port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
1625 }
1626out:
1627 return port;
1628}
1629
1630/**
Chuck Lever92200412006-01-03 09:55:51 +01001631 * xs_set_port - reset the port number in the remote endpoint address
1632 * @xprt: generic transport
1633 * @port: new port number
1634 *
1635 */
1636static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1637{
Chuck Lever46121cf2007-01-31 12:14:08 -05001638 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001639
Chuck Lever9dc3b092009-08-09 15:09:46 -04001640 rpc_set_port(xs_addr(xprt), port);
1641 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001642}
1643
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001644static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
1645{
Trond Myklebuste6237b62019-10-17 11:13:54 -04001646 if (transport->srcport == 0 && transport->xprt.reuseport)
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001647 transport->srcport = xs_sock_getport(sock);
1648}
1649
J. Bruce Fields826799e2018-10-18 15:27:02 -04001650static int xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001651{
J. Bruce Fields826799e2018-10-18 15:27:02 -04001652 int port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001653
1654 if (port == 0 && transport->xprt.resvport)
1655 port = xs_get_random_port();
1656 return port;
1657}
1658
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001659static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001660{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001661 if (transport->srcport != 0)
1662 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001663 if (!transport->xprt.resvport)
1664 return 0;
1665 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1666 return xprt_max_resvport;
1667 return --port;
1668}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001669static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001670{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001671 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001672 int err, nloop = 0;
J. Bruce Fields826799e2018-10-18 15:27:02 -04001673 int port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001674 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001675
Chris Perl0f7a6222014-09-05 15:40:21 -04001676 /*
1677 * If we are asking for any ephemeral port (i.e. port == 0 &&
1678 * transport->xprt.resvport == 0), don't bind. Let the local
1679 * port selection happen implicitly when the socket is used
1680 * (for example at connect time).
1681 *
1682 * This ensures that we can continue to establish TCP
1683 * connections even when all local ephemeral ports are already
1684 * a part of some TCP connection. This makes no difference
1685 * for UDP sockets, but also doens't harm them.
1686 *
1687 * If we're asking for any reserved port (i.e. port == 0 &&
1688 * transport->xprt.resvport == 1) xs_get_srcport above will
1689 * ensure that port is non-zero and we will bind as needed.
1690 */
J. Bruce Fields826799e2018-10-18 15:27:02 -04001691 if (port <= 0)
1692 return port;
Chris Perl0f7a6222014-09-05 15:40:21 -04001693
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001694 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001695 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001696 rpc_set_port((struct sockaddr *)&myaddr, port);
1697 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1698 transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001699 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001700 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001701 break;
Chuck Levera246b012005-08-11 16:25:23 -04001702 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001703 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001704 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001705 if (port > last)
1706 nloop++;
1707 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001708
Chuck Lever4232e862010-10-20 11:52:51 -04001709 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001710 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1711 &((struct sockaddr_in *)&myaddr)->sin_addr,
1712 port, err ? "failed" : "ok", err);
1713 else
1714 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1715 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1716 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001717 return err;
1718}
1719
Chuck Lever176e21e2011-05-09 15:22:44 -04001720/*
1721 * We don't support autobind on AF_LOCAL sockets
1722 */
1723static void xs_local_rpcbind(struct rpc_task *task)
1724{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001725 xprt_set_bound(task->tk_xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04001726}
1727
1728static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1729{
1730}
Chuck Levera246b012005-08-11 16:25:23 -04001731
Peter Zijlstraed075362006-12-06 20:35:24 -08001732#ifdef CONFIG_DEBUG_LOCK_ALLOC
1733static struct lock_class_key xs_key[2];
1734static struct lock_class_key xs_slock_key[2];
1735
Chuck Lever176e21e2011-05-09 15:22:44 -04001736static inline void xs_reclassify_socketu(struct socket *sock)
1737{
1738 struct sock *sk = sock->sk;
1739
Chuck Lever176e21e2011-05-09 15:22:44 -04001740 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1741 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1742}
1743
Chuck Lever8945ee52007-08-06 11:58:04 -04001744static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001745{
1746 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001747
Chuck Lever8945ee52007-08-06 11:58:04 -04001748 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1749 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1750}
Peter Zijlstraed075362006-12-06 20:35:24 -08001751
Chuck Lever8945ee52007-08-06 11:58:04 -04001752static inline void xs_reclassify_socket6(struct socket *sock)
1753{
1754 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001755
Chuck Lever8945ee52007-08-06 11:58:04 -04001756 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1757 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001758}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001759
1760static inline void xs_reclassify_socket(int family, struct socket *sock)
1761{
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02001762 if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -04001763 return;
1764
Chuck Lever4232e862010-10-20 11:52:51 -04001765 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001766 case AF_LOCAL:
1767 xs_reclassify_socketu(sock);
1768 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001769 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001770 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001771 break;
1772 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001773 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001774 break;
1775 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001776}
Peter Zijlstraed075362006-12-06 20:35:24 -08001777#else
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001778static inline void xs_reclassify_socket(int family, struct socket *sock)
1779{
1780}
Peter Zijlstraed075362006-12-06 20:35:24 -08001781#endif
1782
NeilBrown93dc41b2013-10-31 16:14:36 +11001783static void xs_dummy_setup_socket(struct work_struct *work)
1784{
1785}
1786
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001787static struct socket *xs_create_sock(struct rpc_xprt *xprt,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001788 struct sock_xprt *transport, int family, int type,
1789 int protocol, bool reuseport)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001790{
Trond Myklebusta73881c2019-02-26 06:33:02 -05001791 struct file *filp;
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001792 struct socket *sock;
1793 int err;
1794
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001795 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001796 if (err < 0) {
1797 dprintk("RPC: can't create %d transport socket (%d).\n",
1798 protocol, -err);
1799 goto out;
1800 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001801 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001802
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001803 if (reuseport)
1804 xs_sock_set_reuseport(sock);
1805
Ben Hutchings4cea2882011-02-22 21:54:34 +00001806 err = xs_bind(transport, sock);
1807 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001808 sock_release(sock);
1809 goto out;
1810 }
1811
Trond Myklebusta73881c2019-02-26 06:33:02 -05001812 filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1813 if (IS_ERR(filp))
1814 return ERR_CAST(filp);
1815 transport->file = filp;
1816
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001817 return sock;
1818out:
1819 return ERR_PTR(err);
1820}
1821
Chuck Lever176e21e2011-05-09 15:22:44 -04001822static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1823 struct socket *sock)
1824{
1825 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1826 xprt);
1827
1828 if (!transport->inet) {
1829 struct sock *sk = sock->sk;
1830
1831 write_lock_bh(&sk->sk_callback_lock);
1832
1833 xs_save_old_callbacks(transport, sk);
1834
1835 sk->sk_user_data = xprt;
Trond Myklebusta2648092015-10-06 17:03:00 -04001836 sk->sk_data_ready = xs_data_ready;
Chuck Lever176e21e2011-05-09 15:22:44 -04001837 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07001838 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05001839 sk->sk_error_report = xs_error_report;
Chuck Lever176e21e2011-05-09 15:22:44 -04001840
1841 xprt_clear_connected(xprt);
1842
1843 /* Reset to new socket */
1844 transport->sock = sock;
1845 transport->inet = sk;
1846
1847 write_unlock_bh(&sk->sk_callback_lock);
1848 }
1849
Trond Myklebustae053552019-02-20 14:56:20 -05001850 xs_stream_start_connect(transport);
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001851
Chuck Lever176e21e2011-05-09 15:22:44 -04001852 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1853}
1854
1855/**
1856 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
Chuck Lever176e21e2011-05-09 15:22:44 -04001857 * @transport: socket transport to connect
Chuck Lever176e21e2011-05-09 15:22:44 -04001858 */
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001859static int xs_local_setup_socket(struct sock_xprt *transport)
Chuck Lever176e21e2011-05-09 15:22:44 -04001860{
Chuck Lever176e21e2011-05-09 15:22:44 -04001861 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebusta73881c2019-02-26 06:33:02 -05001862 struct file *filp;
Chuck Lever176e21e2011-05-09 15:22:44 -04001863 struct socket *sock;
Colin Ian King68e9a242020-02-28 23:44:14 +00001864 int status;
Chuck Lever176e21e2011-05-09 15:22:44 -04001865
Chuck Lever176e21e2011-05-09 15:22:44 -04001866 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1867 SOCK_STREAM, 0, &sock, 1);
1868 if (status < 0) {
1869 dprintk("RPC: can't create AF_LOCAL "
1870 "transport socket (%d).\n", -status);
1871 goto out;
1872 }
Stefan Hajnoczid1358912015-12-02 14:17:52 +08001873 xs_reclassify_socket(AF_LOCAL, sock);
Chuck Lever176e21e2011-05-09 15:22:44 -04001874
Trond Myklebusta73881c2019-02-26 06:33:02 -05001875 filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1876 if (IS_ERR(filp)) {
1877 status = PTR_ERR(filp);
1878 goto out;
1879 }
1880 transport->file = filp;
1881
Chuck Lever176e21e2011-05-09 15:22:44 -04001882 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1883 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1884
1885 status = xs_local_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001886 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever176e21e2011-05-09 15:22:44 -04001887 switch (status) {
1888 case 0:
1889 dprintk("RPC: xprt %p connected to %s\n",
1890 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
Chuck Lever3968a8a2018-10-01 14:25:36 -04001891 xprt->stat.connect_count++;
1892 xprt->stat.connect_time += (long)jiffies -
1893 xprt->stat.connect_start;
Chuck Lever176e21e2011-05-09 15:22:44 -04001894 xprt_set_connected(xprt);
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001895 case -ENOBUFS:
Chuck Lever176e21e2011-05-09 15:22:44 -04001896 break;
1897 case -ENOENT:
1898 dprintk("RPC: xprt %p: socket %s does not exist\n",
1899 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1900 break;
Trond Myklebust4a20a982012-12-15 17:02:29 -05001901 case -ECONNREFUSED:
1902 dprintk("RPC: xprt %p: connection refused for %s\n",
1903 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1904 break;
Chuck Lever176e21e2011-05-09 15:22:44 -04001905 default:
1906 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1907 __func__, -status,
1908 xprt->address_strings[RPC_DISPLAY_ADDR]);
1909 }
1910
1911out:
1912 xprt_clear_connecting(xprt);
1913 xprt_wake_pending_tasks(xprt, status);
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001914 return status;
1915}
1916
Linus Torvaldsb6669732013-02-28 18:02:55 -08001917static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001918{
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001919 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1920 int ret;
1921
1922 if (RPC_IS_ASYNC(task)) {
1923 /*
1924 * We want the AF_LOCAL connect to be resolved in the
1925 * filesystem namespace of the process making the rpc
1926 * call. Thus we connect synchronously.
1927 *
1928 * If we want to support asynchronous AF_LOCAL calls,
1929 * we'll need to figure out how to pass a namespace to
1930 * connect.
1931 */
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001932 task->tk_rpc_status = -ENOTCONN;
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001933 rpc_exit(task, -ENOTCONN);
1934 return;
1935 }
1936 ret = xs_local_setup_socket(transport);
1937 if (ret && !RPC_IS_SOFTCONN(task))
1938 msleep_interruptible(15000);
Chuck Lever176e21e2011-05-09 15:22:44 -04001939}
1940
Jeff Layton3c87ef62015-06-03 16:14:25 -04001941#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Jeff Laytond6e971d2015-06-03 16:14:28 -04001942/*
1943 * Note that this should be called with XPRT_LOCKED held (or when we otherwise
1944 * know that we have exclusive access to the socket), to guard against
1945 * races with xs_reset_transport.
1946 */
Mel Gormana564b8f2012-07-31 16:45:12 -07001947static void xs_set_memalloc(struct rpc_xprt *xprt)
1948{
1949 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1950 xprt);
1951
Jeff Laytond6e971d2015-06-03 16:14:28 -04001952 /*
1953 * If there's no sock, then we have nothing to set. The
1954 * reconnecting process will get it for us.
1955 */
1956 if (!transport->inet)
1957 return;
Jeff Layton8e228132015-06-03 16:14:26 -04001958 if (atomic_read(&xprt->swapper))
Mel Gormana564b8f2012-07-31 16:45:12 -07001959 sk_set_memalloc(transport->inet);
1960}
1961
1962/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04001963 * xs_enable_swap - Tag this transport as being used for swap.
Mel Gormana564b8f2012-07-31 16:45:12 -07001964 * @xprt: transport to tag
Mel Gormana564b8f2012-07-31 16:45:12 -07001965 *
Jeff Layton8e228132015-06-03 16:14:26 -04001966 * Take a reference to this transport on behalf of the rpc_clnt, and
1967 * optionally mark it for swapping if it wasn't already.
Mel Gormana564b8f2012-07-31 16:45:12 -07001968 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04001969static int
1970xs_enable_swap(struct rpc_xprt *xprt)
Mel Gormana564b8f2012-07-31 16:45:12 -07001971{
Jeff Laytond6e971d2015-06-03 16:14:28 -04001972 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Mel Gormana564b8f2012-07-31 16:45:12 -07001973
Jeff Laytond6e971d2015-06-03 16:14:28 -04001974 if (atomic_inc_return(&xprt->swapper) != 1)
1975 return 0;
1976 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
1977 return -ERESTARTSYS;
1978 if (xs->inet)
1979 sk_set_memalloc(xs->inet);
1980 xprt_release_xprt(xprt, NULL);
Jeff Layton8e228132015-06-03 16:14:26 -04001981 return 0;
Mel Gormana564b8f2012-07-31 16:45:12 -07001982}
Jeff Layton8e228132015-06-03 16:14:26 -04001983
1984/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04001985 * xs_disable_swap - Untag this transport as being used for swap.
Jeff Layton8e228132015-06-03 16:14:26 -04001986 * @xprt: transport to tag
1987 *
1988 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
1989 * swapper refcount goes to 0, untag the socket as a memalloc socket.
1990 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04001991static void
1992xs_disable_swap(struct rpc_xprt *xprt)
Jeff Layton8e228132015-06-03 16:14:26 -04001993{
Jeff Laytond6e971d2015-06-03 16:14:28 -04001994 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Jeff Layton8e228132015-06-03 16:14:26 -04001995
Jeff Laytond6e971d2015-06-03 16:14:28 -04001996 if (!atomic_dec_and_test(&xprt->swapper))
1997 return;
1998 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
1999 return;
2000 if (xs->inet)
2001 sk_clear_memalloc(xs->inet);
2002 xprt_release_xprt(xprt, NULL);
Mel Gormana564b8f2012-07-31 16:45:12 -07002003}
Mel Gormana564b8f2012-07-31 16:45:12 -07002004#else
2005static void xs_set_memalloc(struct rpc_xprt *xprt)
2006{
2007}
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002008
2009static int
2010xs_enable_swap(struct rpc_xprt *xprt)
2011{
2012 return -EINVAL;
2013}
2014
2015static void
2016xs_disable_swap(struct rpc_xprt *xprt)
2017{
2018}
Mel Gormana564b8f2012-07-31 16:45:12 -07002019#endif
2020
Chuck Lever16be2d22007-08-06 11:57:38 -04002021static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04002022{
Chuck Lever16be2d22007-08-06 11:57:38 -04002023 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04002024
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002025 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002026 struct sock *sk = sock->sk;
2027
2028 write_lock_bh(&sk->sk_callback_lock);
2029
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002030 xs_save_old_callbacks(transport, sk);
2031
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002032 sk->sk_user_data = xprt;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002033 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002034 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002035 sock_set_flag(sk, SOCK_FASYNC);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002036
2037 xprt_set_connected(xprt);
2038
2039 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002040 transport->sock = sock;
2041 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002042
Mel Gormana564b8f2012-07-31 16:45:12 -07002043 xs_set_memalloc(xprt);
2044
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002045 write_unlock_bh(&sk->sk_callback_lock);
2046 }
Chuck Lever470056c2005-08-25 16:25:56 -07002047 xs_udp_do_set_buffer_size(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002048
2049 xprt->stat.connect_start = jiffies;
Chuck Lever16be2d22007-08-06 11:57:38 -04002050}
2051
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002052static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04002053{
2054 struct sock_xprt *transport =
2055 container_of(work, struct sock_xprt, connect_worker.work);
2056 struct rpc_xprt *xprt = &transport->xprt;
Colin Ian Kingd099b8a2017-09-18 12:21:14 +01002057 struct socket *sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002058 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04002059
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002060 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002061 xs_addr(xprt)->sa_family, SOCK_DGRAM,
2062 IPPROTO_UDP, false);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002063 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04002064 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04002065
Chuck Leverc740eff2009-08-09 15:09:46 -04002066 dprintk("RPC: worker connecting xprt %p via %s to "
2067 "%s (port %s)\n", xprt,
2068 xprt->address_strings[RPC_DISPLAY_PROTO],
2069 xprt->address_strings[RPC_DISPLAY_ADDR],
2070 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04002071
2072 xs_udp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002073 trace_rpc_socket_connect(xprt, sock, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002074 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002075out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002076 xprt_clear_connecting(xprt);
Trond Myklebustcf767852018-12-17 17:38:51 -05002077 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002078 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002079}
2080
Trond Myklebust4876cc72015-06-19 16:17:57 -04002081/**
2082 * xs_tcp_shutdown - gracefully shut down a TCP socket
2083 * @xprt: transport
2084 *
2085 * Initiates a graceful shutdown of the TCP socket by calling the
2086 * equivalent of shutdown(SHUT_RDWR);
2087 */
2088static void xs_tcp_shutdown(struct rpc_xprt *xprt)
2089{
2090 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2091 struct socket *sock = transport->sock;
Trond Myklebust9b308892018-02-05 10:20:06 -05002092 int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
Trond Myklebust4876cc72015-06-19 16:17:57 -04002093
2094 if (sock == NULL)
2095 return;
Trond Myklebust9b308892018-02-05 10:20:06 -05002096 switch (skst) {
2097 default:
Trond Myklebust4876cc72015-06-19 16:17:57 -04002098 kernel_sock_shutdown(sock, SHUT_RDWR);
2099 trace_rpc_socket_shutdown(xprt, sock);
Trond Myklebust9b308892018-02-05 10:20:06 -05002100 break;
2101 case TCP_CLOSE:
2102 case TCP_TIME_WAIT:
Trond Myklebust4876cc72015-06-19 16:17:57 -04002103 xs_reset_transport(transport);
Trond Myklebust9b308892018-02-05 10:20:06 -05002104 }
Trond Myklebust4876cc72015-06-19 16:17:57 -04002105}
2106
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002107static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
2108 struct socket *sock)
2109{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002110 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2111 unsigned int keepidle;
2112 unsigned int keepcnt;
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002113 unsigned int opt_on = 1;
2114 unsigned int timeo;
2115
Trond Myklebustb5e92412019-05-02 11:21:08 -04002116 spin_lock(&xprt->transport_lock);
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002117 keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
2118 keepcnt = xprt->timeout->to_retries + 1;
2119 timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
2120 (xprt->timeout->to_retries + 1);
2121 clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
Trond Myklebustb5e92412019-05-02 11:21:08 -04002122 spin_unlock(&xprt->transport_lock);
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002123
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002124 /* TCP Keepalive options */
2125 kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
2126 (char *)&opt_on, sizeof(opt_on));
2127 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
2128 (char *)&keepidle, sizeof(keepidle));
2129 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
2130 (char *)&keepidle, sizeof(keepidle));
2131 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
2132 (char *)&keepcnt, sizeof(keepcnt));
2133
2134 /* TCP user timeout (see RFC5482) */
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002135 kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT,
2136 (char *)&timeo, sizeof(timeo));
2137}
2138
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002139static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
2140 unsigned long connect_timeout,
2141 unsigned long reconnect_timeout)
2142{
2143 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2144 struct rpc_timeout to;
2145 unsigned long initval;
2146
Trond Myklebustb5e92412019-05-02 11:21:08 -04002147 spin_lock(&xprt->transport_lock);
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002148 if (reconnect_timeout < xprt->max_reconnect_timeout)
2149 xprt->max_reconnect_timeout = reconnect_timeout;
2150 if (connect_timeout < xprt->connect_timeout) {
2151 memcpy(&to, xprt->timeout, sizeof(to));
2152 initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
2153 /* Arbitrary lower limit */
2154 if (initval < XS_TCP_INIT_REEST_TO << 1)
2155 initval = XS_TCP_INIT_REEST_TO << 1;
2156 to.to_initval = initval;
2157 to.to_maxval = initval;
2158 memcpy(&transport->tcp_timeout, &to,
2159 sizeof(transport->tcp_timeout));
2160 xprt->timeout = &transport->tcp_timeout;
2161 xprt->connect_timeout = connect_timeout;
2162 }
2163 set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
Trond Myklebustb5e92412019-05-02 11:21:08 -04002164 spin_unlock(&xprt->transport_lock);
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002165}
2166
Chuck Lever16be2d22007-08-06 11:57:38 -04002167static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002168{
Chuck Lever16be2d22007-08-06 11:57:38 -04002169 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002170 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002171
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002172 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002173 struct sock *sk = sock->sk;
NeilBrownd88e4d82016-08-04 16:24:28 +10002174 unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC;
Trond Myklebust7f260e82013-09-24 11:25:22 -04002175
NeilBrownd88e4d82016-08-04 16:24:28 +10002176 /* Avoid temporary address, they are bad for long-lived
2177 * connections such as NFS mounts.
2178 * RFC4941, section 3.6 suggests that:
2179 * Individual applications, which have specific
2180 * knowledge about the normal duration of connections,
2181 * MAY override this as appropriate.
2182 */
2183 kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES,
2184 (char *)&addr_pref, sizeof(addr_pref));
2185
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002186 xs_tcp_set_socket_timeouts(xprt, sock);
Trond Myklebust775f06a2015-06-20 15:31:54 -04002187
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002188 write_lock_bh(&sk->sk_callback_lock);
2189
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002190 xs_save_old_callbacks(transport, sk);
2191
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002192 sk->sk_user_data = xprt;
Trond Myklebust5157b952016-05-29 10:13:24 -04002193 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002194 sk->sk_state_change = xs_tcp_state_change;
2195 sk->sk_write_space = xs_tcp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002196 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05002197 sk->sk_error_report = xs_error_report;
Chuck Lever3167e122005-08-25 16:25:55 -07002198
2199 /* socket options */
Chuck Lever3167e122005-08-25 16:25:55 -07002200 sock_reset_flag(sk, SOCK_LINGER);
Chuck Lever3167e122005-08-25 16:25:55 -07002201 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002202
2203 xprt_clear_connected(xprt);
2204
2205 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002206 transport->sock = sock;
2207 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002208
2209 write_unlock_bh(&sk->sk_callback_lock);
2210 }
2211
Trond Myklebust01d37c42009-03-11 14:09:39 -04002212 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002213 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002214
Mel Gormana564b8f2012-07-31 16:45:12 -07002215 xs_set_memalloc(xprt);
2216
Trond Myklebustae053552019-02-20 14:56:20 -05002217 xs_stream_start_connect(transport);
Trond Myklebuste1806c72018-08-13 16:50:49 -04002218
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002219 /* Tell the socket layer to start connecting... */
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04002220 set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002221 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2222 switch (ret) {
2223 case 0:
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002224 xs_set_srcport(transport, sock);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002225 /* fall through */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002226 case -EINPROGRESS:
2227 /* SYN_SENT! */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002228 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2229 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust1f4c17a2016-08-01 13:36:08 -04002230 break;
2231 case -EADDRNOTAVAIL:
2232 /* Source port number is unavailable. Try a new one! */
2233 transport->srcport = 0;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002234 }
2235out:
2236 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002237}
2238
2239/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002240 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
Chuck Leveracf0a392018-12-19 11:00:22 -05002241 * @work: queued work item
Chuck Lever16be2d22007-08-06 11:57:38 -04002242 *
2243 * Invoked by a work queue tasklet.
2244 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002245static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002246{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002247 struct sock_xprt *transport =
2248 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002249 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002250 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002251 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002252
Chuck Lever16be2d22007-08-06 11:57:38 -04002253 if (!sock) {
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002254 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002255 xs_addr(xprt)->sa_family, SOCK_STREAM,
2256 IPPROTO_TCP, true);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002257 if (IS_ERR(sock)) {
2258 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002259 goto out;
2260 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002261 }
2262
Chuck Leverc740eff2009-08-09 15:09:46 -04002263 dprintk("RPC: worker connecting xprt %p via %s to "
2264 "%s (port %s)\n", xprt,
2265 xprt->address_strings[RPC_DISPLAY_PROTO],
2266 xprt->address_strings[RPC_DISPLAY_ADDR],
2267 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002268
2269 status = xs_tcp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002270 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever46121cf2007-01-31 12:14:08 -05002271 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2272 xprt, -status, xprt_connected(xprt),
2273 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002274 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002275 default:
2276 printk("%s: connect returned unhandled error %d\n",
2277 __func__, status);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002278 /* fall through */
Trond Myklebustf75e6742009-04-21 17:18:20 -04002279 case -EADDRNOTAVAIL:
2280 /* We're probably in TIME_WAIT. Get rid of existing socket,
2281 * and retry
2282 */
Trond Myklebusta519fc72012-09-12 16:49:15 -04002283 xs_tcp_force_close(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002284 break;
Trond Myklebust2a491992009-03-11 14:38:00 -04002285 case 0:
2286 case -EINPROGRESS:
2287 case -EALREADY:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002288 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002289 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002290 case -EINVAL:
2291 /* Happens, for instance, if the user specified a link
2292 * local IPv6 address without a scope-id.
2293 */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002294 case -ECONNREFUSED:
2295 case -ECONNRESET:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002296 case -ENETDOWN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002297 case -ENETUNREACH:
Trond Myklebust4ba161a2017-11-24 12:00:24 -05002298 case -EHOSTUNREACH:
Trond Myklebust3913c782015-02-08 21:44:04 -05002299 case -EADDRINUSE:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002300 case -ENOBUFS:
NeilBrown6ea44ad2017-05-25 17:00:32 +10002301 /*
2302 * xs_tcp_force_close() wakes tasks with -EIO.
2303 * We need to wake them first to ensure the
2304 * correct error code.
2305 */
2306 xprt_wake_pending_tasks(xprt, status);
Trond Myklebust4efdd922015-02-08 15:34:28 -05002307 xs_tcp_force_close(xprt);
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002308 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002309 }
Trond Myklebust2a491992009-03-11 14:38:00 -04002310 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002311out:
Chuck Lever2226feb2005-08-11 16:25:38 -04002312 xprt_clear_connecting(xprt);
Trond Myklebustcf767852018-12-17 17:38:51 -05002313 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002314 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04002315}
2316
Chuck Lever68e220b2007-08-06 11:57:48 -04002317/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002318 * xs_connect - connect a socket to a remote endpoint
Trond Myklebust1b092092013-01-08 09:26:49 -05002319 * @xprt: pointer to transport structure
Chuck Lever9903cd12005-08-11 16:25:26 -04002320 * @task: address of RPC task that manages state of connect request
2321 *
2322 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002323 *
2324 * UDP socket connects are synchronous, but we use a work queue anyway
2325 * to guarantee that even unprivileged user processes can set up a
2326 * socket on a privileged port.
2327 *
2328 * If a UDP socket connect fails, the delay behavior here prevents
2329 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002330 */
Trond Myklebust1b092092013-01-08 09:26:49 -05002331static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002332{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002333 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002334 unsigned long delay = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002335
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002336 WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2337
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002338 if (transport->sock != NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002339 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2340 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002341 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002342
2343 /* Start by resetting any existing state */
2344 xs_reset_transport(transport);
2345
Chuck Lever675dd902019-06-19 10:33:42 -04002346 delay = xprt_reconnect_delay(xprt);
2347 xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO);
Trond Myklebust02910172016-08-04 00:00:33 -04002348
Trond Myklebust02910172016-08-04 00:00:33 -04002349 } else
Chuck Lever46121cf2007-01-31 12:14:08 -05002350 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002351
2352 queue_delayed_work(xprtiod_workqueue,
2353 &transport->connect_worker,
2354 delay);
Chuck Levera246b012005-08-11 16:25:23 -04002355}
2356
Trond Myklebust4f8943f2019-05-01 16:28:29 -04002357static void xs_wake_disconnect(struct sock_xprt *transport)
2358{
2359 if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state))
2360 xs_tcp_force_close(&transport->xprt);
2361}
2362
2363static void xs_wake_write(struct sock_xprt *transport)
2364{
2365 if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state))
2366 xprt_write_space(&transport->xprt);
2367}
2368
2369static void xs_wake_error(struct sock_xprt *transport)
2370{
2371 int sockerr;
Trond Myklebust4f8943f2019-05-01 16:28:29 -04002372
2373 if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
2374 return;
2375 mutex_lock(&transport->recv_mutex);
2376 if (transport->sock == NULL)
2377 goto out;
2378 if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
2379 goto out;
Benjamin Coddingtonaf845372019-10-02 10:40:55 -04002380 sockerr = xchg(&transport->xprt_err, 0);
Trond Myklebust4f8943f2019-05-01 16:28:29 -04002381 if (sockerr < 0)
2382 xprt_wake_pending_tasks(&transport->xprt, sockerr);
2383out:
2384 mutex_unlock(&transport->recv_mutex);
2385}
2386
2387static void xs_wake_pending(struct sock_xprt *transport)
2388{
2389 if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state))
2390 xprt_wake_pending_tasks(&transport->xprt, -EAGAIN);
2391}
2392
2393static void xs_error_handle(struct work_struct *work)
2394{
2395 struct sock_xprt *transport = container_of(work,
2396 struct sock_xprt, error_worker);
2397
2398 xs_wake_disconnect(transport);
2399 xs_wake_write(transport);
2400 xs_wake_error(transport);
2401 xs_wake_pending(transport);
2402}
2403
Chuck Lever262ca072006-03-20 13:44:16 -05002404/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002405 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2406 * @xprt: rpc_xprt struct containing statistics
2407 * @seq: output file
2408 *
2409 */
2410static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2411{
2412 long idle_time = 0;
2413
2414 if (xprt_connected(xprt))
2415 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2416
2417 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
Andy Adamson15a45202012-02-14 16:19:18 -05002418 "%llu %llu %lu %llu %llu\n",
Chuck Lever176e21e2011-05-09 15:22:44 -04002419 xprt->stat.bind_count,
2420 xprt->stat.connect_count,
Chuck Lever8440a882018-10-01 14:25:41 -04002421 xprt->stat.connect_time / HZ,
Chuck Lever176e21e2011-05-09 15:22:44 -04002422 idle_time,
2423 xprt->stat.sends,
2424 xprt->stat.recvs,
2425 xprt->stat.bad_xids,
2426 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002427 xprt->stat.bklog_u,
2428 xprt->stat.max_slots,
2429 xprt->stat.sending_u,
2430 xprt->stat.pending_u);
Chuck Lever176e21e2011-05-09 15:22:44 -04002431}
2432
2433/**
Chuck Lever262ca072006-03-20 13:44:16 -05002434 * xs_udp_print_stats - display UDP socket-specifc stats
2435 * @xprt: rpc_xprt struct containing statistics
2436 * @seq: output file
2437 *
2438 */
2439static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2440{
Chuck Leverc8475462006-12-05 16:35:26 -05002441 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2442
Andy Adamson15a45202012-02-14 16:19:18 -05002443 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2444 "%lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002445 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002446 xprt->stat.bind_count,
2447 xprt->stat.sends,
2448 xprt->stat.recvs,
2449 xprt->stat.bad_xids,
2450 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002451 xprt->stat.bklog_u,
2452 xprt->stat.max_slots,
2453 xprt->stat.sending_u,
2454 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002455}
2456
2457/**
2458 * xs_tcp_print_stats - display TCP socket-specifc stats
2459 * @xprt: rpc_xprt struct containing statistics
2460 * @seq: output file
2461 *
2462 */
2463static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2464{
Chuck Leverc8475462006-12-05 16:35:26 -05002465 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002466 long idle_time = 0;
2467
2468 if (xprt_connected(xprt))
2469 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2470
Andy Adamson15a45202012-02-14 16:19:18 -05002471 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2472 "%llu %llu %lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002473 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002474 xprt->stat.bind_count,
2475 xprt->stat.connect_count,
Chuck Lever8440a882018-10-01 14:25:41 -04002476 xprt->stat.connect_time / HZ,
Chuck Lever262ca072006-03-20 13:44:16 -05002477 idle_time,
2478 xprt->stat.sends,
2479 xprt->stat.recvs,
2480 xprt->stat.bad_xids,
2481 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002482 xprt->stat.bklog_u,
2483 xprt->stat.max_slots,
2484 xprt->stat.sending_u,
2485 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002486}
2487
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002488/*
2489 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2490 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2491 * to use the server side send routines.
2492 */
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002493static int bc_malloc(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002494{
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002495 struct rpc_rqst *rqst = task->tk_rqstp;
2496 size_t size = rqst->rq_callsize;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002497 struct page *page;
2498 struct rpc_buffer *buf;
2499
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002500 if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
2501 WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
2502 size);
2503 return -EINVAL;
2504 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002505
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002506 page = alloc_page(GFP_KERNEL);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002507 if (!page)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002508 return -ENOMEM;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002509
2510 buf = page_address(page);
2511 buf->len = PAGE_SIZE;
2512
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002513 rqst->rq_buffer = buf->data;
Jeff Layton18e601d2016-10-24 20:33:23 -04002514 rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002515 return 0;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002516}
2517
2518/*
2519 * Free the space allocated in the bc_alloc routine
2520 */
Chuck Lever3435c742016-09-15 10:55:29 -04002521static void bc_free(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002522{
Chuck Lever3435c742016-09-15 10:55:29 -04002523 void *buffer = task->tk_rqstp->rq_buffer;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002524 struct rpc_buffer *buf;
2525
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002526 buf = container_of(buffer, struct rpc_buffer, data);
2527 free_page((unsigned long)buf);
2528}
2529
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002530static int bc_sendto(struct rpc_rqst *req)
2531{
Chuck Leverda1661b2020-03-02 15:20:33 -05002532 struct xdr_buf *xdr = &req->rq_snd_buf;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002533 struct sock_xprt *transport =
Chuck Lever067fb112019-02-11 11:24:37 -05002534 container_of(req->rq_xprt, struct sock_xprt, xprt);
Chuck Lever067fb112019-02-11 11:24:37 -05002535 struct msghdr msg = {
Chuck Leverda1661b2020-03-02 15:20:33 -05002536 .msg_flags = 0,
Chuck Lever067fb112019-02-11 11:24:37 -05002537 };
2538 rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
Chuck Leverda1661b2020-03-02 15:20:33 -05002539 (u32)xdr->len);
2540 unsigned int sent = 0;
2541 int err;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002542
Chuck Lever8729aab2019-11-20 16:25:46 -05002543 req->rq_xtime = ktime_get();
Chuck Leverda1661b2020-03-02 15:20:33 -05002544 err = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, marker, &sent);
2545 xdr_free_bvec(xdr);
2546 if (err < 0 || sent != (xdr->len + sizeof(marker)))
Chuck Lever067fb112019-02-11 11:24:37 -05002547 return -EAGAIN;
Chuck Leverda1661b2020-03-02 15:20:33 -05002548 return sent;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002549}
2550
2551/*
2552 * The send routine. Borrows from svc_send
2553 */
Trond Myklebustadfa7142018-09-03 23:58:59 -04002554static int bc_send_request(struct rpc_rqst *req)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002555{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002556 struct svc_xprt *xprt;
Andrzej Hajda7fc56132015-09-24 16:00:09 +02002557 int len;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002558
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002559 /*
2560 * Get the server socket associated with this callback xprt
2561 */
2562 xprt = req->rq_xprt->bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002563
2564 /*
2565 * Grab the mutex to serialize data as the connection is shared
2566 * with the fore channel
2567 */
Trond Myklebustc5445772018-09-03 23:39:27 -04002568 mutex_lock(&xprt->xpt_mutex);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002569 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2570 len = -ENOTCONN;
2571 else
2572 len = bc_sendto(req);
2573 mutex_unlock(&xprt->xpt_mutex);
2574
2575 if (len > 0)
2576 len = 0;
2577
2578 return len;
2579}
2580
2581/*
2582 * The close routine. Since this is client initiated, we do nothing
2583 */
2584
2585static void bc_close(struct rpc_xprt *xprt)
2586{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002587}
2588
2589/*
2590 * The xprt destroy routine. Again, because this connection is client
2591 * initiated, we do nothing
2592 */
2593
2594static void bc_destroy(struct rpc_xprt *xprt)
2595{
Kinglong Mee47f72ef2014-03-24 11:58:16 +08002596 dprintk("RPC: bc_destroy xprt %p\n", xprt);
2597
2598 xs_xprt_free(xprt);
2599 module_put(THIS_MODULE);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002600}
2601
Chuck Leverd31ae252017-08-01 12:00:39 -04002602static const struct rpc_xprt_ops xs_local_ops = {
Chuck Lever176e21e2011-05-09 15:22:44 -04002603 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebust4cd34e72018-08-31 10:00:02 -04002604 .release_xprt = xprt_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002605 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002606 .free_slot = xprt_free_slot,
Chuck Lever176e21e2011-05-09 15:22:44 -04002607 .rpcbind = xs_local_rpcbind,
2608 .set_port = xs_local_set_port,
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002609 .connect = xs_local_connect,
Chuck Lever176e21e2011-05-09 15:22:44 -04002610 .buf_alloc = rpc_malloc,
2611 .buf_free = rpc_free,
Trond Myklebust550aebf2018-09-14 14:32:45 -04002612 .prepare_request = xs_stream_prepare_request,
Chuck Lever176e21e2011-05-09 15:22:44 -04002613 .send_request = xs_local_send_request,
Trond Myklebust8ba6a922019-04-07 13:58:46 -04002614 .wait_for_reply_request = xprt_wait_for_reply_request_def,
Chuck Lever176e21e2011-05-09 15:22:44 -04002615 .close = xs_close,
Trond Myklebusta1311d82013-10-31 09:18:49 -04002616 .destroy = xs_destroy,
Chuck Lever176e21e2011-05-09 15:22:44 -04002617 .print_stats = xs_local_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002618 .enable_swap = xs_enable_swap,
2619 .disable_swap = xs_disable_swap,
Chuck Lever176e21e2011-05-09 15:22:44 -04002620};
2621
Chuck Leverd31ae252017-08-01 12:00:39 -04002622static const struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002623 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002624 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002625 .release_xprt = xprt_release_xprt_cong,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002626 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002627 .free_slot = xprt_free_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002628 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002629 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002630 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002631 .buf_alloc = rpc_malloc,
2632 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002633 .send_request = xs_udp_send_request,
Trond Myklebust8ba6a922019-04-07 13:58:46 -04002634 .wait_for_reply_request = xprt_wait_for_reply_request_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002635 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002636 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002637 .close = xs_close,
2638 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002639 .print_stats = xs_udp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002640 .enable_swap = xs_enable_swap,
2641 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002642 .inject_disconnect = xs_inject_disconnect,
Chuck Lever262965f2005-08-11 16:25:56 -04002643};
2644
Chuck Leverd31ae252017-08-01 12:00:39 -04002645static const struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002646 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebust4cd34e72018-08-31 10:00:02 -04002647 .release_xprt = xprt_release_xprt,
Trond Myklebust36bd7de2018-09-03 18:41:32 -04002648 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002649 .free_slot = xprt_free_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002650 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002651 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002652 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002653 .buf_alloc = rpc_malloc,
2654 .buf_free = rpc_free,
Trond Myklebust277e4ab2018-09-14 09:49:06 -04002655 .prepare_request = xs_stream_prepare_request,
Chuck Lever262965f2005-08-11 16:25:56 -04002656 .send_request = xs_tcp_send_request,
Trond Myklebust8ba6a922019-04-07 13:58:46 -04002657 .wait_for_reply_request = xprt_wait_for_reply_request_def,
Trond Myklebustc627d312015-02-10 11:06:04 -05002658 .close = xs_tcp_shutdown,
Chuck Lever9903cd12005-08-11 16:25:26 -04002659 .destroy = xs_destroy,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002660 .set_connect_timeout = xs_tcp_set_connect_timeout,
Chuck Lever262ca072006-03-20 13:44:16 -05002661 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002662 .enable_swap = xs_enable_swap,
2663 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002664 .inject_disconnect = xs_inject_disconnect,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002665#ifdef CONFIG_SUNRPC_BACKCHANNEL
2666 .bc_setup = xprt_setup_bc,
Chuck Lever6b26cc82016-05-02 14:40:40 -04002667 .bc_maxpayload = xs_tcp_bc_maxpayload,
Trond Myklebust7402a4f2019-07-16 13:51:29 -04002668 .bc_num_slots = xprt_bc_max_slots,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002669 .bc_free_rqst = xprt_free_bc_rqst,
2670 .bc_destroy = xprt_destroy_bc,
2671#endif
Chuck Levera246b012005-08-11 16:25:23 -04002672};
2673
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002674/*
2675 * The rpc_xprt_ops for the server backchannel
2676 */
2677
Chuck Leverd31ae252017-08-01 12:00:39 -04002678static const struct rpc_xprt_ops bc_tcp_ops = {
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002679 .reserve_xprt = xprt_reserve_xprt,
2680 .release_xprt = xprt_release_xprt,
Bryan Schumaker84e28a32012-09-24 13:39:01 -04002681 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002682 .free_slot = xprt_free_slot,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002683 .buf_alloc = bc_malloc,
2684 .buf_free = bc_free,
2685 .send_request = bc_send_request,
Trond Myklebust8ba6a922019-04-07 13:58:46 -04002686 .wait_for_reply_request = xprt_wait_for_reply_request_def,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002687 .close = bc_close,
2688 .destroy = bc_destroy,
2689 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002690 .enable_swap = xs_enable_swap,
2691 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002692 .inject_disconnect = xs_inject_disconnect,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002693};
2694
Chuck Lever92476852010-10-20 11:53:01 -04002695static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2696{
2697 static const struct sockaddr_in sin = {
2698 .sin_family = AF_INET,
2699 .sin_addr.s_addr = htonl(INADDR_ANY),
2700 };
2701 static const struct sockaddr_in6 sin6 = {
2702 .sin6_family = AF_INET6,
2703 .sin6_addr = IN6ADDR_ANY_INIT,
2704 };
2705
2706 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002707 case AF_LOCAL:
2708 break;
Chuck Lever92476852010-10-20 11:53:01 -04002709 case AF_INET:
2710 memcpy(sap, &sin, sizeof(sin));
2711 break;
2712 case AF_INET6:
2713 memcpy(sap, &sin6, sizeof(sin6));
2714 break;
2715 default:
2716 dprintk("RPC: %s: Bad address family\n", __func__);
2717 return -EAFNOSUPPORT;
2718 }
2719 return 0;
2720}
2721
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002722static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002723 unsigned int slot_table_size,
2724 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002725{
2726 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002727 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002728
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002729 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002730 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002731 return ERR_PTR(-EBADF);
2732 }
2733
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002734 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2735 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002736 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002737 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2738 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002739 return ERR_PTR(-ENOMEM);
2740 }
2741
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002742 new = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002743 mutex_init(&new->recv_mutex);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002744 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2745 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002746 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002747 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002748 else {
2749 int err;
2750 err = xs_init_anyaddr(args->dstaddr->sa_family,
2751 (struct sockaddr *)&new->srcaddr);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002752 if (err != 0) {
2753 xprt_free(xprt);
Chuck Lever92476852010-10-20 11:53:01 -04002754 return ERR_PTR(err);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002755 }
Chuck Lever92476852010-10-20 11:53:01 -04002756 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002757
2758 return xprt;
2759}
2760
Chuck Lever176e21e2011-05-09 15:22:44 -04002761static const struct rpc_timeout xs_local_default_timeout = {
2762 .to_initval = 10 * HZ,
2763 .to_maxval = 10 * HZ,
2764 .to_retries = 2,
2765};
2766
2767/**
2768 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2769 * @args: rpc transport creation arguments
2770 *
2771 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2772 */
2773static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2774{
2775 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2776 struct sock_xprt *transport;
2777 struct rpc_xprt *xprt;
2778 struct rpc_xprt *ret;
2779
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002780 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2781 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002782 if (IS_ERR(xprt))
2783 return xprt;
2784 transport = container_of(xprt, struct sock_xprt, xprt);
2785
2786 xprt->prot = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -04002787 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2788
2789 xprt->bind_timeout = XS_BIND_TO;
2790 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2791 xprt->idle_timeout = XS_IDLE_DISC_TO;
2792
2793 xprt->ops = &xs_local_ops;
2794 xprt->timeout = &xs_local_default_timeout;
2795
Trond Myklebust550aebf2018-09-14 14:32:45 -04002796 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
Trond Myklebust4f8943f2019-05-01 16:28:29 -04002797 INIT_WORK(&transport->error_worker, xs_error_handle);
Trond Myklebust550aebf2018-09-14 14:32:45 -04002798 INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket);
NeilBrown93dc41b2013-10-31 16:14:36 +11002799
Chuck Lever176e21e2011-05-09 15:22:44 -04002800 switch (sun->sun_family) {
2801 case AF_LOCAL:
2802 if (sun->sun_path[0] != '/') {
2803 dprintk("RPC: bad AF_LOCAL address: %s\n",
2804 sun->sun_path);
2805 ret = ERR_PTR(-EINVAL);
2806 goto out_err;
2807 }
2808 xprt_set_bound(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002809 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
J. Bruce Fields7073ea82013-02-21 10:14:22 -05002810 ret = ERR_PTR(xs_local_setup_socket(transport));
2811 if (ret)
2812 goto out_err;
Chuck Lever176e21e2011-05-09 15:22:44 -04002813 break;
2814 default:
2815 ret = ERR_PTR(-EAFNOSUPPORT);
2816 goto out_err;
2817 }
2818
2819 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2820 xprt->address_strings[RPC_DISPLAY_ADDR]);
2821
2822 if (try_module_get(THIS_MODULE))
2823 return xprt;
2824 ret = ERR_PTR(-EINVAL);
2825out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002826 xs_xprt_free(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002827 return ret;
2828}
2829
Trond Myklebust2881ae72007-12-20 16:03:54 -05002830static const struct rpc_timeout xs_udp_default_timeout = {
2831 .to_initval = 5 * HZ,
2832 .to_maxval = 30 * HZ,
2833 .to_increment = 5 * HZ,
2834 .to_retries = 5,
2835};
2836
Chuck Lever9903cd12005-08-11 16:25:26 -04002837/**
2838 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002839 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002840 *
2841 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002842static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002843{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002844 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002845 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002846 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002847 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002848
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002849 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2850 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002851 if (IS_ERR(xprt))
2852 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002853 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002854
Chuck Leverec739ef2006-08-22 20:06:15 -04002855 xprt->prot = IPPROTO_UDP;
Chuck Levera246b012005-08-11 16:25:23 -04002856 /* XXX: header size can vary due to auth type, IPv6, etc. */
2857 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2858
Chuck Lever03bf4b72005-08-25 16:25:55 -07002859 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002860 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2861 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002862
Chuck Lever262965f2005-08-11 16:25:56 -04002863 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002864
Trond Myklebustba7392b2007-12-20 16:03:55 -05002865 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002866
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002867 INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
Trond Myklebust4f8943f2019-05-01 16:28:29 -04002868 INIT_WORK(&transport->error_worker, xs_error_handle);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002869 INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2870
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002871 switch (addr->sa_family) {
2872 case AF_INET:
2873 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2874 xprt_set_bound(xprt);
2875
Chuck Lever9dc3b092009-08-09 15:09:46 -04002876 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002877 break;
2878 case AF_INET6:
2879 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2880 xprt_set_bound(xprt);
2881
Chuck Lever9dc3b092009-08-09 15:09:46 -04002882 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002883 break;
2884 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002885 ret = ERR_PTR(-EAFNOSUPPORT);
2886 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002887 }
2888
Chuck Leverc740eff2009-08-09 15:09:46 -04002889 if (xprt_bound(xprt))
2890 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2891 xprt->address_strings[RPC_DISPLAY_ADDR],
2892 xprt->address_strings[RPC_DISPLAY_PORT],
2893 xprt->address_strings[RPC_DISPLAY_PROTO]);
2894 else
2895 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2896 xprt->address_strings[RPC_DISPLAY_ADDR],
2897 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002898
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002899 if (try_module_get(THIS_MODULE))
2900 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002901 ret = ERR_PTR(-EINVAL);
2902out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002903 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002904 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002905}
2906
Trond Myklebust2881ae72007-12-20 16:03:54 -05002907static const struct rpc_timeout xs_tcp_default_timeout = {
2908 .to_initval = 60 * HZ,
2909 .to_maxval = 60 * HZ,
2910 .to_retries = 2,
2911};
2912
Chuck Lever9903cd12005-08-11 16:25:26 -04002913/**
2914 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002915 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002916 *
2917 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002918static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002919{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002920 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002921 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002922 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002923 struct rpc_xprt *ret;
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002924 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2925
2926 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2927 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Levera246b012005-08-11 16:25:23 -04002928
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002929 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002930 max_slot_table_size);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002931 if (IS_ERR(xprt))
2932 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002933 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002934
Chuck Leverec739ef2006-08-22 20:06:15 -04002935 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002936 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002937
Chuck Lever03bf4b72005-08-25 16:25:55 -07002938 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002939 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2940 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002941
Chuck Lever262965f2005-08-11 16:25:56 -04002942 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002943 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002944
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002945 xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002946 xprt->connect_timeout = xprt->timeout->to_initval *
2947 (xprt->timeout->to_retries + 1);
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002948
Trond Myklebustc50b8ee2018-09-14 14:26:28 -04002949 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
Trond Myklebust4f8943f2019-05-01 16:28:29 -04002950 INIT_WORK(&transport->error_worker, xs_error_handle);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002951 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
2952
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002953 switch (addr->sa_family) {
2954 case AF_INET:
2955 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2956 xprt_set_bound(xprt);
2957
Chuck Lever9dc3b092009-08-09 15:09:46 -04002958 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002959 break;
2960 case AF_INET6:
2961 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2962 xprt_set_bound(xprt);
2963
Chuck Lever9dc3b092009-08-09 15:09:46 -04002964 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002965 break;
2966 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002967 ret = ERR_PTR(-EAFNOSUPPORT);
2968 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002969 }
2970
Chuck Leverc740eff2009-08-09 15:09:46 -04002971 if (xprt_bound(xprt))
2972 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2973 xprt->address_strings[RPC_DISPLAY_ADDR],
2974 xprt->address_strings[RPC_DISPLAY_PORT],
2975 xprt->address_strings[RPC_DISPLAY_PROTO]);
2976 else
2977 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2978 xprt->address_strings[RPC_DISPLAY_ADDR],
2979 xprt->address_strings[RPC_DISPLAY_PROTO]);
2980
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002981 if (try_module_get(THIS_MODULE))
2982 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002983 ret = ERR_PTR(-EINVAL);
2984out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002985 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002986 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002987}
Chuck Lever282b32e2006-12-05 16:35:51 -05002988
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002989/**
2990 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2991 * @args: rpc transport creation arguments
2992 *
2993 */
2994static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2995{
2996 struct sockaddr *addr = args->dstaddr;
2997 struct rpc_xprt *xprt;
2998 struct sock_xprt *transport;
2999 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003000 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003001
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003002 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3003 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003004 if (IS_ERR(xprt))
3005 return xprt;
3006 transport = container_of(xprt, struct sock_xprt, xprt);
3007
3008 xprt->prot = IPPROTO_TCP;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003009 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3010 xprt->timeout = &xs_tcp_default_timeout;
3011
3012 /* backchannel */
3013 xprt_set_bound(xprt);
3014 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003015 xprt->reestablish_timeout = 0;
3016 xprt->idle_timeout = 0;
3017
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003018 xprt->ops = &bc_tcp_ops;
3019
3020 switch (addr->sa_family) {
3021 case AF_INET:
3022 xs_format_peer_addresses(xprt, "tcp",
3023 RPCBIND_NETID_TCP);
3024 break;
3025 case AF_INET6:
3026 xs_format_peer_addresses(xprt, "tcp",
3027 RPCBIND_NETID_TCP6);
3028 break;
3029 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003030 ret = ERR_PTR(-EAFNOSUPPORT);
3031 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003032 }
3033
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04003034 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3035 xprt->address_strings[RPC_DISPLAY_ADDR],
3036 xprt->address_strings[RPC_DISPLAY_PORT],
3037 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003038
3039 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003040 * Once we've associated a backchannel xprt with a connection,
Weng Meiling28303ca2013-11-30 17:56:44 +08003041 * we want to keep it around as long as the connection lasts,
3042 * in case we need to start using it for a backchannel again;
3043 * this reference won't be dropped until bc_xprt is destroyed.
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003044 */
3045 xprt_get(xprt);
3046 args->bc_xprt->xpt_bc_xprt = xprt;
3047 xprt->bc_xprt = args->bc_xprt;
3048 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
3049 transport->sock = bc_sock->sk_sock;
3050 transport->inet = bc_sock->sk_sk;
3051
3052 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003053 * Since we don't want connections for the backchannel, we set
3054 * the xprt status to connected
3055 */
3056 xprt_set_connected(xprt);
3057
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003058 if (try_module_get(THIS_MODULE))
3059 return xprt;
Kinglong Mee642aab52014-03-24 12:00:28 +08003060
3061 args->bc_xprt->xpt_bc_xprt = NULL;
J. Bruce Fields39a9bea2016-05-17 12:38:21 -04003062 args->bc_xprt->xpt_bc_xps = NULL;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003063 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003064 ret = ERR_PTR(-EINVAL);
3065out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003066 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003067 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003068}
3069
Chuck Lever176e21e2011-05-09 15:22:44 -04003070static struct xprt_class xs_local_transport = {
3071 .list = LIST_HEAD_INIT(xs_local_transport.list),
3072 .name = "named UNIX socket",
3073 .owner = THIS_MODULE,
3074 .ident = XPRT_TRANSPORT_LOCAL,
3075 .setup = xs_setup_local,
3076};
3077
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003078static struct xprt_class xs_udp_transport = {
3079 .list = LIST_HEAD_INIT(xs_udp_transport.list),
3080 .name = "udp",
3081 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003082 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003083 .setup = xs_setup_udp,
3084};
3085
3086static struct xprt_class xs_tcp_transport = {
3087 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
3088 .name = "tcp",
3089 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003090 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003091 .setup = xs_setup_tcp,
3092};
3093
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003094static struct xprt_class xs_bc_tcp_transport = {
3095 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3096 .name = "tcp NFSv4.1 backchannel",
3097 .owner = THIS_MODULE,
3098 .ident = XPRT_TRANSPORT_BC_TCP,
3099 .setup = xs_setup_bc_tcp,
3100};
3101
Chuck Lever282b32e2006-12-05 16:35:51 -05003102/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003103 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05003104 *
3105 */
3106int init_socket_xprt(void)
3107{
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08003108 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08003109 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05003110
Chuck Lever176e21e2011-05-09 15:22:44 -04003111 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003112 xprt_register_transport(&xs_udp_transport);
3113 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003114 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003115
Chuck Lever282b32e2006-12-05 16:35:51 -05003116 return 0;
3117}
3118
3119/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003120 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05003121 *
3122 */
3123void cleanup_socket_xprt(void)
3124{
Chuck Leverfbf76682006-12-05 16:35:54 -05003125 if (sunrpc_table_header) {
3126 unregister_sysctl_table(sunrpc_table_header);
3127 sunrpc_table_header = NULL;
3128 }
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003129
Chuck Lever176e21e2011-05-09 15:22:44 -04003130 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003131 xprt_unregister_transport(&xs_udp_transport);
3132 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003133 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05003134}
Trond Myklebustcbf11072009-08-09 15:06:19 -04003135
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003136static int param_set_uint_minmax(const char *val,
3137 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003138 unsigned int min, unsigned int max)
3139{
Daniel Walter00cfaa92014-06-21 13:06:38 +01003140 unsigned int num;
Trond Myklebustcbf11072009-08-09 15:06:19 -04003141 int ret;
3142
3143 if (!val)
3144 return -EINVAL;
Daniel Walter00cfaa92014-06-21 13:06:38 +01003145 ret = kstrtouint(val, 0, &num);
Dan Carpenter9761a242017-02-19 00:34:59 +03003146 if (ret)
3147 return ret;
3148 if (num < min || num > max)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003149 return -EINVAL;
3150 *((unsigned int *)kp->arg) = num;
3151 return 0;
3152}
3153
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003154static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003155{
Frank Sorensonffb6ca32016-07-08 16:35:25 -05003156 return param_set_uint_minmax(val, kp,
J. Bruce Fields826799e2018-10-18 15:27:02 -04003157 RPC_MIN_RESVPORT,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003158 RPC_MAX_RESVPORT);
3159}
3160
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303161static const struct kernel_param_ops param_ops_portnr = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003162 .set = param_set_portnr,
3163 .get = param_get_uint,
3164};
3165
Trond Myklebustcbf11072009-08-09 15:06:19 -04003166#define param_check_portnr(name, p) \
3167 __param_check(name, p, unsigned int);
3168
3169module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3170module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3171
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003172static int param_set_slot_table_size(const char *val,
3173 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003174{
3175 return param_set_uint_minmax(val, kp,
3176 RPC_MIN_SLOT_TABLE,
3177 RPC_MAX_SLOT_TABLE);
3178}
3179
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303180static const struct kernel_param_ops param_ops_slot_table_size = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003181 .set = param_set_slot_table_size,
3182 .get = param_get_uint,
3183};
3184
Trond Myklebustcbf11072009-08-09 15:06:19 -04003185#define param_check_slot_table_size(name, p) \
3186 __param_check(name, p, unsigned int);
3187
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003188static int param_set_max_slot_table_size(const char *val,
3189 const struct kernel_param *kp)
3190{
3191 return param_set_uint_minmax(val, kp,
3192 RPC_MIN_SLOT_TABLE,
3193 RPC_MAX_SLOT_TABLE_LIMIT);
3194}
3195
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303196static const struct kernel_param_ops param_ops_max_slot_table_size = {
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003197 .set = param_set_max_slot_table_size,
3198 .get = param_get_uint,
3199};
3200
3201#define param_check_max_slot_table_size(name, p) \
3202 __param_check(name, p, unsigned int);
3203
Trond Myklebustcbf11072009-08-09 15:06:19 -04003204module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3205 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003206module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3207 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003208module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3209 slot_table_size, 0644);