blob: e1546dd6aacc50c4f4bf390e907ad106a86a258a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Chuck Levera246b012005-08-11 16:25:23 -04002/*
3 * linux/net/sunrpc/xprtsock.c
4 *
5 * Client-side transport implementation for sockets.
6 *
Alan Cox113aa832008-10-13 19:01:08 -07007 * TCP callback races fixes (C) 1998 Red Hat
8 * TCP send fixes (C) 1998 Red Hat
Chuck Levera246b012005-08-11 16:25:23 -04009 * TCP NFS related read + write fixes
10 * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
11 *
12 * Rewrite of larges part of the code in order to stabilize TCP stuff.
13 * Fix behaviour when socket buffer is full.
14 * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
Chuck Lever55aa4f52005-08-11 16:25:47 -040015 *
16 * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
Chuck Lever8f9d5b12007-08-06 11:57:53 -040017 *
18 * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
19 * <gilles.quillard@bull.net>
Chuck Levera246b012005-08-11 16:25:23 -040020 */
21
22#include <linux/types.h>
Chuck Lever176e21e2011-05-09 15:22:44 -040023#include <linux/string.h>
Chuck Levera246b012005-08-11 16:25:23 -040024#include <linux/slab.h>
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -040025#include <linux/module.h>
Chuck Levera246b012005-08-11 16:25:23 -040026#include <linux/capability.h>
Chuck Levera246b012005-08-11 16:25:23 -040027#include <linux/pagemap.h>
28#include <linux/errno.h>
29#include <linux/socket.h>
30#include <linux/in.h>
31#include <linux/net.h>
32#include <linux/mm.h>
Chuck Lever176e21e2011-05-09 15:22:44 -040033#include <linux/un.h>
Chuck Levera246b012005-08-11 16:25:23 -040034#include <linux/udp.h>
35#include <linux/tcp.h>
36#include <linux/sunrpc/clnt.h>
Jeff Layton59766872013-02-04 12:50:00 -050037#include <linux/sunrpc/addr.h>
Chuck Lever02107142006-01-03 09:55:49 +010038#include <linux/sunrpc/sched.h>
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030039#include <linux/sunrpc/svcsock.h>
\"Talpey, Thomas\49c36fc2007-09-10 13:47:31 -040040#include <linux/sunrpc/xprtsock.h>
Chuck Levera246b012005-08-11 16:25:23 -040041#include <linux/file.h>
Trond Myklebust9e00abc2011-07-13 19:20:49 -040042#ifdef CONFIG_SUNRPC_BACKCHANNEL
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -040043#include <linux/sunrpc/bc_xprt.h>
44#endif
Chuck Levera246b012005-08-11 16:25:23 -040045
46#include <net/sock.h>
47#include <net/checksum.h>
48#include <net/udp.h>
49#include <net/tcp.h>
Trond Myklebust277e4ab2018-09-14 09:49:06 -040050#include <linux/bvec.h>
Trond Myklebust6a829eb2019-01-03 09:04:45 -050051#include <linux/highmem.h>
Trond Myklebust277e4ab2018-09-14 09:49:06 -040052#include <linux/uio.h>
Trond Myklebusta1231fd2019-02-18 10:02:29 -050053#include <linux/sched/mm.h>
Chuck Levera246b012005-08-11 16:25:23 -040054
Trond Myklebust40b5ea02013-09-04 12:16:23 -040055#include <trace/events/sunrpc.h>
56
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030057#include "sunrpc.h"
Chuck Lever176e21e2011-05-09 15:22:44 -040058
59static void xs_close(struct rpc_xprt *xprt);
Trond Myklebust7196dbb2017-02-08 11:17:54 -050060static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
61 struct socket *sock);
Chuck Lever176e21e2011-05-09 15:22:44 -040062
Chuck Lever9903cd12005-08-11 16:25:26 -040063/*
Chuck Leverc556b752005-11-01 12:24:48 -050064 * xprtsock tunables
65 */
Trond Myklebust09acfea2012-03-11 15:22:54 -040066static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
67static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
68static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
Chuck Leverc556b752005-11-01 12:24:48 -050069
Trond Myklebust09acfea2012-03-11 15:22:54 -040070static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
71static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
Chuck Leverc556b752005-11-01 12:24:48 -050072
Trond Myklebust7d1e8252009-03-11 14:38:03 -040073#define XS_TCP_LINGER_TO (15U * HZ)
Trond Myklebust25fe6142009-03-11 14:38:03 -040074static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
Trond Myklebust7d1e8252009-03-11 14:38:03 -040075
Chuck Leverc556b752005-11-01 12:24:48 -050076/*
Chuck Leverfbf76682006-12-05 16:35:54 -050077 * We can register our own files under /proc/sys/sunrpc by
78 * calling register_sysctl_table() again. The files in that
79 * directory become the union of all files registered there.
80 *
81 * We simply need to make sure that we don't collide with
82 * someone else's file names!
83 */
84
Chuck Leverfbf76682006-12-05 16:35:54 -050085static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
86static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
Trond Myklebustd9ba1312011-07-17 18:11:30 -040087static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Leverfbf76682006-12-05 16:35:54 -050088static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
89static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
90
91static struct ctl_table_header *sunrpc_table_header;
92
93/*
94 * FIXME: changing the UDP slot table size should also resize the UDP
95 * socket buffers for existing UDP transports
96 */
Joe Perchesfe2c6332013-06-11 23:04:25 -070097static struct ctl_table xs_tunables_table[] = {
Chuck Leverfbf76682006-12-05 16:35:54 -050098 {
Chuck Leverfbf76682006-12-05 16:35:54 -050099 .procname = "udp_slot_table_entries",
100 .data = &xprt_udp_slot_table_entries,
101 .maxlen = sizeof(unsigned int),
102 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800103 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500104 .extra1 = &min_slot_table_size,
105 .extra2 = &max_slot_table_size
106 },
107 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500108 .procname = "tcp_slot_table_entries",
109 .data = &xprt_tcp_slot_table_entries,
110 .maxlen = sizeof(unsigned int),
111 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800112 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500113 .extra1 = &min_slot_table_size,
114 .extra2 = &max_slot_table_size
115 },
116 {
Trond Myklebustd9ba1312011-07-17 18:11:30 -0400117 .procname = "tcp_max_slot_table_entries",
118 .data = &xprt_max_tcp_slot_table_entries,
119 .maxlen = sizeof(unsigned int),
120 .mode = 0644,
121 .proc_handler = proc_dointvec_minmax,
122 .extra1 = &min_slot_table_size,
123 .extra2 = &max_tcp_slot_table_limit
124 },
125 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500126 .procname = "min_resvport",
127 .data = &xprt_min_resvport,
128 .maxlen = sizeof(unsigned int),
129 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800130 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500131 .extra1 = &xprt_min_resvport_limit,
J. Bruce Fields826799e2018-10-18 15:27:02 -0400132 .extra2 = &xprt_max_resvport_limit
Chuck Leverfbf76682006-12-05 16:35:54 -0500133 },
134 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500135 .procname = "max_resvport",
136 .data = &xprt_max_resvport,
137 .maxlen = sizeof(unsigned int),
138 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800139 .proc_handler = proc_dointvec_minmax,
J. Bruce Fields826799e2018-10-18 15:27:02 -0400140 .extra1 = &xprt_min_resvport_limit,
Chuck Leverfbf76682006-12-05 16:35:54 -0500141 .extra2 = &xprt_max_resvport_limit
142 },
143 {
Trond Myklebust25fe6142009-03-11 14:38:03 -0400144 .procname = "tcp_fin_timeout",
145 .data = &xs_tcp_fin_timeout,
146 .maxlen = sizeof(xs_tcp_fin_timeout),
147 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800148 .proc_handler = proc_dointvec_jiffies,
Trond Myklebust25fe6142009-03-11 14:38:03 -0400149 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800150 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500151};
152
Joe Perchesfe2c6332013-06-11 23:04:25 -0700153static struct ctl_table sunrpc_table[] = {
Chuck Leverfbf76682006-12-05 16:35:54 -0500154 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500155 .procname = "sunrpc",
156 .mode = 0555,
157 .child = xs_tunables_table
158 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800159 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500160};
161
Chuck Leverfbf76682006-12-05 16:35:54 -0500162/*
Chuck Lever03bf4b72005-08-25 16:25:55 -0700163 * Wait duration for a reply from the RPC portmapper.
164 */
165#define XS_BIND_TO (60U * HZ)
166
167/*
168 * Delay if a UDP socket connect error occurs. This is most likely some
169 * kind of resource problem on the local host.
170 */
171#define XS_UDP_REEST_TO (2U * HZ)
172
173/*
174 * The reestablish timeout allows clients to delay for a bit before attempting
175 * to reconnect to a server that just dropped our connection.
176 *
177 * We implement an exponential backoff when trying to reestablish a TCP
178 * transport connection with the server. Some servers like to drop a TCP
179 * connection when they are overworked, so we start with a short timeout and
180 * increase over time if the server is down or not responding.
181 */
182#define XS_TCP_INIT_REEST_TO (3U * HZ)
Chuck Lever03bf4b72005-08-25 16:25:55 -0700183
184/*
185 * TCP idle timeout; client drops the transport socket if it is idle
186 * for this long. Note that we also timeout UDP sockets to prevent
187 * holding port numbers when there is no RPC traffic.
188 */
189#define XS_IDLE_DISC_TO (5U * 60 * HZ)
190
Jeff Laytonf895b252014-11-17 16:58:04 -0500191#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Levera246b012005-08-11 16:25:23 -0400192# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400193# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400194#endif
195
Chuck Levera246b012005-08-11 16:25:23 -0400196#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400197static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400198{
Chuck Lever9903cd12005-08-11 16:25:26 -0400199 u8 *buf = (u8 *) packet;
200 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400201
Chuck Lever46121cf2007-01-31 12:14:08 -0500202 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400203 for (j = 0; j < count && j < 128; j += 4) {
204 if (!(j & 31)) {
205 if (j)
206 dprintk("\n");
207 dprintk("0x%04x ", j);
208 }
209 dprintk("%02x%02x%02x%02x ",
210 buf[j], buf[j+1], buf[j+2], buf[j+3]);
211 }
212 dprintk("\n");
213}
214#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400215static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400216{
217 /* NOP */
218}
219#endif
220
Trond Myklebust21180712013-12-31 13:22:59 -0500221static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
222{
223 return (struct rpc_xprt *) sk->sk_user_data;
224}
225
Chuck Lever95392c52007-08-06 11:57:58 -0400226static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400227{
Chuck Lever95392c52007-08-06 11:57:58 -0400228 return (struct sockaddr *) &xprt->addr;
229}
230
Chuck Lever176e21e2011-05-09 15:22:44 -0400231static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
232{
233 return (struct sockaddr_un *) &xprt->addr;
234}
235
Chuck Lever95392c52007-08-06 11:57:58 -0400236static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
237{
238 return (struct sockaddr_in *) &xprt->addr;
239}
240
241static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
242{
243 return (struct sockaddr_in6 *) &xprt->addr;
244}
245
Chuck Leverc877b842009-08-09 15:09:36 -0400246static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400247{
Chuck Leverc877b842009-08-09 15:09:36 -0400248 struct sockaddr *sap = xs_addr(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400249 struct sockaddr_in6 *sin6;
250 struct sockaddr_in *sin;
Chuck Lever176e21e2011-05-09 15:22:44 -0400251 struct sockaddr_un *sun;
Chuck Leverc877b842009-08-09 15:09:36 -0400252 char buf[128];
Chuck Leveredb267a2006-08-22 20:06:18 -0400253
Chuck Lever9dc3b092009-08-09 15:09:46 -0400254 switch (sap->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400255 case AF_LOCAL:
256 sun = xs_addr_un(xprt);
257 strlcpy(buf, sun->sun_path, sizeof(buf));
258 xprt->address_strings[RPC_DISPLAY_ADDR] =
259 kstrdup(buf, GFP_KERNEL);
260 break;
Chuck Lever9dc3b092009-08-09 15:09:46 -0400261 case AF_INET:
Chuck Lever176e21e2011-05-09 15:22:44 -0400262 (void)rpc_ntop(sap, buf, sizeof(buf));
263 xprt->address_strings[RPC_DISPLAY_ADDR] =
264 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400265 sin = xs_addr_in(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800266 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
Chuck Lever9dc3b092009-08-09 15:09:46 -0400267 break;
268 case AF_INET6:
Chuck Lever176e21e2011-05-09 15:22:44 -0400269 (void)rpc_ntop(sap, buf, sizeof(buf));
270 xprt->address_strings[RPC_DISPLAY_ADDR] =
271 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400272 sin6 = xs_addr_in6(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800273 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400274 break;
275 default:
276 BUG();
Chuck Leveredb267a2006-08-22 20:06:18 -0400277 }
Chuck Lever176e21e2011-05-09 15:22:44 -0400278
Chuck Lever9dc3b092009-08-09 15:09:46 -0400279 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
Chuck Leveredb267a2006-08-22 20:06:18 -0400280}
281
Chuck Lever9dc3b092009-08-09 15:09:46 -0400282static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400283{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400284 struct sockaddr *sap = xs_addr(xprt);
285 char buf[128];
Chuck Lever4b6473f2007-08-06 11:57:12 -0400286
Joe Perches81160e662010-03-08 12:15:59 -0800287 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400288 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400289
Joe Perches81160e662010-03-08 12:15:59 -0800290 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400291 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
292}
Chuck Lever4b6473f2007-08-06 11:57:12 -0400293
Chuck Lever9dc3b092009-08-09 15:09:46 -0400294static void xs_format_peer_addresses(struct rpc_xprt *xprt,
295 const char *protocol,
296 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400297{
Chuck Leverb454ae92008-01-07 18:34:48 -0500298 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leverb454ae92008-01-07 18:34:48 -0500299 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leverc877b842009-08-09 15:09:36 -0400300 xs_format_common_peer_addresses(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400301 xs_format_common_peer_ports(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400302}
303
Chuck Lever9dc3b092009-08-09 15:09:46 -0400304static void xs_update_peer_port(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400305{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400306 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
307 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400308
Chuck Lever9dc3b092009-08-09 15:09:46 -0400309 xs_format_common_peer_ports(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400310}
311
312static void xs_free_peer_addresses(struct rpc_xprt *xprt)
313{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500314 unsigned int i;
315
316 for (i = 0; i < RPC_DISPLAY_MAX; i++)
317 switch (i) {
318 case RPC_DISPLAY_PROTO:
319 case RPC_DISPLAY_NETID:
320 continue;
321 default:
322 kfree(xprt->address_strings[i]);
323 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400324}
325
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400326static size_t
327xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp)
328{
329 size_t i,n;
330
Trond Myklebust16e5e902018-12-02 15:22:48 -0500331 if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES))
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400332 return want;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400333 n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT;
334 for (i = 0; i < n; i++) {
335 if (buf->pages[i])
336 continue;
337 buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp);
338 if (!buf->pages[i]) {
Trond Myklebust16e5e902018-12-02 15:22:48 -0500339 i *= PAGE_SIZE;
340 return i > buf->page_base ? i - buf->page_base : 0;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400341 }
342 }
343 return want;
344}
345
346static ssize_t
347xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek)
348{
349 ssize_t ret;
350 if (seek != 0)
351 iov_iter_advance(&msg->msg_iter, seek);
352 ret = sock_recvmsg(sock, msg, flags);
353 return ret > 0 ? ret + seek : ret;
354}
355
356static ssize_t
357xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags,
358 struct kvec *kvec, size_t count, size_t seek)
359{
Al Viro0e9b4a82018-11-01 18:19:03 -0400360 iov_iter_kvec(&msg->msg_iter, READ, kvec, 1, count);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400361 return xs_sock_recvmsg(sock, msg, flags, seek);
362}
363
364static ssize_t
365xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags,
366 struct bio_vec *bvec, unsigned long nr, size_t count,
367 size_t seek)
368{
Al Viro0e9b4a82018-11-01 18:19:03 -0400369 iov_iter_bvec(&msg->msg_iter, READ, bvec, nr, count);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400370 return xs_sock_recvmsg(sock, msg, flags, seek);
371}
372
373static ssize_t
374xs_read_discard(struct socket *sock, struct msghdr *msg, int flags,
375 size_t count)
376{
Trond Myklebustb76a5af2018-12-03 15:58:58 -0500377 iov_iter_discard(&msg->msg_iter, READ, count);
378 return sock_recvmsg(sock, msg, flags);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400379}
380
Trond Myklebust6a829eb2019-01-03 09:04:45 -0500381#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
382static void
383xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
384{
385 struct bvec_iter bi = {
386 .bi_size = count,
387 };
388 struct bio_vec bv;
389
390 bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
391 for_each_bvec(bv, bvec, bi, bi)
392 flush_dcache_page(bv.bv_page);
393}
394#else
395static inline void
396xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
397{
398}
399#endif
400
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400401static ssize_t
402xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags,
403 struct xdr_buf *buf, size_t count, size_t seek, size_t *read)
404{
405 size_t want, seek_init = seek, offset = 0;
406 ssize_t ret;
407
Trond Myklebuste92053a2019-02-15 18:14:27 -0500408 want = min_t(size_t, count, buf->head[0].iov_len);
409 if (seek < want) {
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400410 ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek);
411 if (ret <= 0)
412 goto sock_err;
413 offset += ret;
414 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
415 goto out;
416 if (ret != want)
Trond Myklebustc4433052018-12-04 07:52:11 -0500417 goto out;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400418 seek = 0;
419 } else {
Trond Myklebuste92053a2019-02-15 18:14:27 -0500420 seek -= want;
421 offset += want;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400422 }
Trond Myklebust16e5e902018-12-02 15:22:48 -0500423
424 want = xs_alloc_sparse_pages(buf,
425 min_t(size_t, count - offset, buf->page_len),
426 GFP_NOWAIT);
427 if (seek < want) {
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400428 ret = xs_read_bvec(sock, msg, flags, buf->bvec,
429 xdr_buf_pagecount(buf),
430 want + buf->page_base,
431 seek + buf->page_base);
432 if (ret <= 0)
433 goto sock_err;
Trond Myklebust6a829eb2019-01-03 09:04:45 -0500434 xs_flush_bvec(buf->bvec, ret, seek + buf->page_base);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400435 offset += ret - buf->page_base;
436 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
437 goto out;
438 if (ret != want)
Trond Myklebustc4433052018-12-04 07:52:11 -0500439 goto out;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400440 seek = 0;
441 } else {
Trond Myklebust16e5e902018-12-02 15:22:48 -0500442 seek -= want;
443 offset += want;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400444 }
Trond Myklebust16e5e902018-12-02 15:22:48 -0500445
Trond Myklebuste92053a2019-02-15 18:14:27 -0500446 want = min_t(size_t, count - offset, buf->tail[0].iov_len);
447 if (seek < want) {
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400448 ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek);
449 if (ret <= 0)
450 goto sock_err;
451 offset += ret;
452 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
453 goto out;
454 if (ret != want)
Trond Myklebustc4433052018-12-04 07:52:11 -0500455 goto out;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400456 } else
Trond Myklebuste92053a2019-02-15 18:14:27 -0500457 offset = seek_init;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400458 ret = -EMSGSIZE;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400459out:
460 *read = offset - seek_init;
461 return ret;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400462sock_err:
463 offset += seek;
464 goto out;
465}
466
467static void
468xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf)
469{
470 if (!transport->recv.copied) {
471 if (buf->head[0].iov_len >= transport->recv.offset)
472 memcpy(buf->head[0].iov_base,
473 &transport->recv.xid,
474 transport->recv.offset);
475 transport->recv.copied = transport->recv.offset;
476 }
477}
478
479static bool
480xs_read_stream_request_done(struct sock_xprt *transport)
481{
482 return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT);
483}
484
Trond Myklebuste92053a2019-02-15 18:14:27 -0500485static void
486xs_read_stream_check_eor(struct sock_xprt *transport,
487 struct msghdr *msg)
488{
489 if (xs_read_stream_request_done(transport))
490 msg->msg_flags |= MSG_EOR;
491}
492
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400493static ssize_t
494xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg,
495 int flags, struct rpc_rqst *req)
496{
497 struct xdr_buf *buf = &req->rq_private_buf;
498 size_t want, read;
499 ssize_t ret;
500
501 xs_read_header(transport, buf);
502
503 want = transport->recv.len - transport->recv.offset;
Trond Myklebuste92053a2019-02-15 18:14:27 -0500504 if (want != 0) {
505 ret = xs_read_xdr_buf(transport->sock, msg, flags, buf,
506 transport->recv.copied + want,
507 transport->recv.copied,
508 &read);
509 transport->recv.offset += read;
510 transport->recv.copied += read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400511 }
512
Trond Myklebust727fcc62019-02-15 16:53:04 -0500513 if (transport->recv.offset == transport->recv.len)
514 xs_read_stream_check_eor(transport, msg);
515
Trond Myklebuste92053a2019-02-15 18:14:27 -0500516 if (want == 0)
517 return 0;
518
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400519 switch (ret) {
Trond Myklebustc4433052018-12-04 07:52:11 -0500520 default:
521 break;
Trond Myklebust26781ea2018-12-03 15:41:38 -0500522 case -EFAULT:
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400523 case -EMSGSIZE:
Trond Myklebust26781ea2018-12-03 15:41:38 -0500524 msg->msg_flags |= MSG_TRUNC;
Trond Myklebustc4433052018-12-04 07:52:11 -0500525 return read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400526 case 0:
527 return -ESHUTDOWN;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400528 }
Trond Myklebustc4433052018-12-04 07:52:11 -0500529 return ret < 0 ? ret : read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400530}
531
532static size_t
533xs_read_stream_headersize(bool isfrag)
534{
535 if (isfrag)
536 return sizeof(__be32);
537 return 3 * sizeof(__be32);
538}
539
540static ssize_t
541xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg,
542 int flags, size_t want, size_t seek)
543{
544 struct kvec kvec = {
545 .iov_base = &transport->recv.fraghdr,
546 .iov_len = want,
547 };
548 return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek);
549}
550
551#if defined(CONFIG_SUNRPC_BACKCHANNEL)
552static ssize_t
553xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
554{
555 struct rpc_xprt *xprt = &transport->xprt;
556 struct rpc_rqst *req;
557 ssize_t ret;
558
559 /* Look up and lock the request corresponding to the given XID */
560 req = xprt_lookup_bc_request(xprt, transport->recv.xid);
561 if (!req) {
562 printk(KERN_WARNING "Callback slot table overflowed\n");
563 return -ESHUTDOWN;
564 }
565
566 ret = xs_read_stream_request(transport, msg, flags, req);
567 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
Trond Myklebustc4433052018-12-04 07:52:11 -0500568 xprt_complete_bc_request(req, transport->recv.copied);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400569
570 return ret;
571}
572#else /* CONFIG_SUNRPC_BACKCHANNEL */
573static ssize_t
574xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
575{
576 return -ESHUTDOWN;
577}
578#endif /* CONFIG_SUNRPC_BACKCHANNEL */
579
580static ssize_t
581xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags)
582{
583 struct rpc_xprt *xprt = &transport->xprt;
584 struct rpc_rqst *req;
585 ssize_t ret = 0;
586
587 /* Look up and lock the request corresponding to the given XID */
588 spin_lock(&xprt->queue_lock);
589 req = xprt_lookup_rqst(xprt, transport->recv.xid);
590 if (!req) {
591 msg->msg_flags |= MSG_TRUNC;
592 goto out;
593 }
594 xprt_pin_rqst(req);
595 spin_unlock(&xprt->queue_lock);
596
597 ret = xs_read_stream_request(transport, msg, flags, req);
598
599 spin_lock(&xprt->queue_lock);
600 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
Trond Myklebustc4433052018-12-04 07:52:11 -0500601 xprt_complete_rqst(req->rq_task, transport->recv.copied);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400602 xprt_unpin_rqst(req);
603out:
604 spin_unlock(&xprt->queue_lock);
605 return ret;
606}
607
608static ssize_t
609xs_read_stream(struct sock_xprt *transport, int flags)
610{
611 struct msghdr msg = { 0 };
612 size_t want, read = 0;
613 ssize_t ret = 0;
614
615 if (transport->recv.len == 0) {
616 want = xs_read_stream_headersize(transport->recv.copied != 0);
617 ret = xs_read_stream_header(transport, &msg, flags, want,
618 transport->recv.offset);
619 if (ret <= 0)
620 goto out_err;
621 transport->recv.offset = ret;
Trond Myklebustc4433052018-12-04 07:52:11 -0500622 if (transport->recv.offset != want)
623 return transport->recv.offset;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400624 transport->recv.len = be32_to_cpu(transport->recv.fraghdr) &
625 RPC_FRAGMENT_SIZE_MASK;
626 transport->recv.offset -= sizeof(transport->recv.fraghdr);
627 read = ret;
628 }
629
630 switch (be32_to_cpu(transport->recv.calldir)) {
Trond Myklebustc4433052018-12-04 07:52:11 -0500631 default:
632 msg.msg_flags |= MSG_TRUNC;
633 break;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400634 case RPC_CALL:
635 ret = xs_read_stream_call(transport, &msg, flags);
636 break;
637 case RPC_REPLY:
638 ret = xs_read_stream_reply(transport, &msg, flags);
639 }
640 if (msg.msg_flags & MSG_TRUNC) {
641 transport->recv.calldir = cpu_to_be32(-1);
642 transport->recv.copied = -1;
643 }
644 if (ret < 0)
645 goto out_err;
646 read += ret;
647 if (transport->recv.offset < transport->recv.len) {
Trond Myklebustc4433052018-12-04 07:52:11 -0500648 if (!(msg.msg_flags & MSG_TRUNC))
649 return read;
Trond Myklebustb76a5af2018-12-03 15:58:58 -0500650 msg.msg_flags = 0;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400651 ret = xs_read_discard(transport->sock, &msg, flags,
652 transport->recv.len - transport->recv.offset);
653 if (ret <= 0)
654 goto out_err;
655 transport->recv.offset += ret;
656 read += ret;
657 if (transport->recv.offset != transport->recv.len)
Trond Myklebustc4433052018-12-04 07:52:11 -0500658 return read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400659 }
660 if (xs_read_stream_request_done(transport)) {
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400661 trace_xs_stream_read_request(transport);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400662 transport->recv.copied = 0;
663 }
664 transport->recv.offset = 0;
665 transport->recv.len = 0;
666 return read;
667out_err:
Trond Myklebust794628572018-12-03 18:49:00 -0500668 return ret != 0 ? ret : -ESHUTDOWN;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400669}
670
Trond Myklebust0ffe86f2019-01-30 14:51:26 -0500671static __poll_t xs_poll_socket(struct sock_xprt *transport)
672{
673 return transport->sock->ops->poll(NULL, transport->sock, NULL);
674}
675
676static bool xs_poll_socket_readable(struct sock_xprt *transport)
677{
678 __poll_t events = xs_poll_socket(transport);
679
680 return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP);
681}
682
683static void xs_poll_check_readable(struct sock_xprt *transport)
684{
685
686 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
687 if (!xs_poll_socket_readable(transport))
688 return;
689 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
690 queue_work(xprtiod_workqueue, &transport->recv_worker);
691}
692
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400693static void xs_stream_data_receive(struct sock_xprt *transport)
694{
695 size_t read = 0;
696 ssize_t ret = 0;
697
698 mutex_lock(&transport->recv_mutex);
699 if (transport->sock == NULL)
700 goto out;
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400701 for (;;) {
702 ret = xs_read_stream(transport, MSG_DONTWAIT);
Trond Myklebustc4433052018-12-04 07:52:11 -0500703 if (ret < 0)
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400704 break;
705 read += ret;
706 cond_resched();
707 }
Trond Myklebust5f52a9d2019-02-16 09:31:47 -0500708 if (ret == -ESHUTDOWN)
709 kernel_sock_shutdown(transport->sock, SHUT_RDWR);
710 else
711 xs_poll_check_readable(transport);
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400712out:
713 mutex_unlock(&transport->recv_mutex);
714 trace_xs_stream_read_data(&transport->xprt, ret, read);
715}
716
717static void xs_stream_data_receive_workfn(struct work_struct *work)
718{
719 struct sock_xprt *transport =
720 container_of(work, struct sock_xprt, recv_worker);
Trond Myklebusta1231fd2019-02-18 10:02:29 -0500721 unsigned int pflags = memalloc_nofs_save();
722
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400723 xs_stream_data_receive(transport);
Trond Myklebusta1231fd2019-02-18 10:02:29 -0500724 memalloc_nofs_restore(pflags);
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400725}
726
Trond Myklebust550aebf2018-09-14 14:32:45 -0400727static void
728xs_stream_reset_connect(struct sock_xprt *transport)
729{
730 transport->recv.offset = 0;
731 transport->recv.len = 0;
732 transport->recv.copied = 0;
733 transport->xmit.offset = 0;
Trond Myklebustae053552019-02-20 14:56:20 -0500734}
735
736static void
737xs_stream_start_connect(struct sock_xprt *transport)
738{
Trond Myklebust550aebf2018-09-14 14:32:45 -0400739 transport->xprt.stat.connect_count++;
740 transport->xprt.stat.connect_start = jiffies;
741}
742
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400743#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
744
Trond Myklebuste791f8e2019-02-19 12:39:40 -0500745static int xs_sendmsg(struct socket *sock, struct msghdr *msg, size_t seek)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400746{
Trond Myklebuste791f8e2019-02-19 12:39:40 -0500747 if (seek)
748 iov_iter_advance(&msg->msg_iter, seek);
749 return sock_sendmsg(sock, msg);
750}
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400751
Trond Myklebuste791f8e2019-02-19 12:39:40 -0500752static int xs_send_kvec(struct socket *sock, struct msghdr *msg, struct kvec *vec, size_t seek)
753{
754 if (!vec) {
755 iov_iter_kvec(&msg->msg_iter, WRITE, NULL, 0, 0);
756 return sock_sendmsg(sock, msg);
757 }
758 iov_iter_kvec(&msg->msg_iter, WRITE, vec, 1, vec->iov_len);
759 return xs_sendmsg(sock, msg, seek);
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400760}
761
Jason Baronf279cd02014-09-24 18:08:00 +0000762static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more, bool zerocopy, int *sent_p)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400763{
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500764 ssize_t (*do_sendpage)(struct socket *sock, struct page *page,
765 int offset, size_t size, int flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400766 struct page **ppage;
767 unsigned int remainder;
Jason Baronf279cd02014-09-24 18:08:00 +0000768 int err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400769
Trond Myklebust24c56842006-10-17 15:06:22 -0400770 remainder = xdr->page_len - base;
771 base += xdr->page_base;
772 ppage = xdr->pages + (base >> PAGE_SHIFT);
773 base &= ~PAGE_MASK;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500774 do_sendpage = sock->ops->sendpage;
775 if (!zerocopy)
776 do_sendpage = sock_no_sendpage;
Trond Myklebust24c56842006-10-17 15:06:22 -0400777 for(;;) {
778 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
779 int flags = XS_SENDMSG_FLAGS;
780
781 remainder -= len;
Trond Myklebust31303d62015-10-06 15:59:20 -0400782 if (more)
Trond Myklebust24c56842006-10-17 15:06:22 -0400783 flags |= MSG_MORE;
Trond Myklebust31303d62015-10-06 15:59:20 -0400784 if (remainder != 0)
785 flags |= MSG_SENDPAGE_NOTLAST | MSG_MORE;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500786 err = do_sendpage(sock, *ppage, base, len, flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400787 if (remainder == 0 || err != len)
788 break;
Jason Baronf279cd02014-09-24 18:08:00 +0000789 *sent_p += err;
Trond Myklebust24c56842006-10-17 15:06:22 -0400790 ppage++;
791 base = 0;
792 }
Jason Baronf279cd02014-09-24 18:08:00 +0000793 if (err > 0) {
794 *sent_p += err;
795 err = 0;
796 }
797 return err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400798}
799
Chuck Lever9903cd12005-08-11 16:25:26 -0400800/**
801 * xs_sendpages - write pages directly to a socket
802 * @sock: socket to send on
803 * @addr: UDP only -- address of destination
804 * @addrlen: UDP only -- length of destination address
805 * @xdr: buffer containing this request
806 * @base: starting position in the buffer
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500807 * @zerocopy: true if it is safe to use sendpage()
Jason Baronf279cd02014-09-24 18:08:00 +0000808 * @sent_p: return the total number of bytes successfully queued for sending
Chuck Lever9903cd12005-08-11 16:25:26 -0400809 *
Chuck Levera246b012005-08-11 16:25:23 -0400810 */
Jason Baronf279cd02014-09-24 18:08:00 +0000811static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, bool zerocopy, int *sent_p)
Chuck Levera246b012005-08-11 16:25:23 -0400812{
Trond Myklebuste791f8e2019-02-19 12:39:40 -0500813 struct msghdr msg = {
814 .msg_name = addr,
815 .msg_namelen = addrlen,
816 .msg_flags = XS_SENDMSG_FLAGS | MSG_MORE,
817 };
Trond Myklebust24c56842006-10-17 15:06:22 -0400818 unsigned int remainder = xdr->len - base;
Jason Baronf279cd02014-09-24 18:08:00 +0000819 int err = 0;
820 int sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400821
Chuck Lever262965f2005-08-11 16:25:56 -0400822 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400823 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400824
Trond Myklebust24c56842006-10-17 15:06:22 -0400825 if (base != 0) {
826 addr = NULL;
827 addrlen = 0;
828 }
Chuck Lever262965f2005-08-11 16:25:56 -0400829
Trond Myklebust24c56842006-10-17 15:06:22 -0400830 if (base < xdr->head[0].iov_len || addr != NULL) {
831 unsigned int len = xdr->head[0].iov_len - base;
832 remainder -= len;
Trond Myklebuste791f8e2019-02-19 12:39:40 -0500833 if (remainder == 0)
834 msg.msg_flags &= ~MSG_MORE;
835 err = xs_send_kvec(sock, &msg, &xdr->head[0], base);
Trond Myklebust24c56842006-10-17 15:06:22 -0400836 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400837 goto out;
Jason Baronf279cd02014-09-24 18:08:00 +0000838 *sent_p += err;
Chuck Levera246b012005-08-11 16:25:23 -0400839 base = 0;
840 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400841 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400842
Trond Myklebust24c56842006-10-17 15:06:22 -0400843 if (base < xdr->page_len) {
844 unsigned int len = xdr->page_len - base;
845 remainder -= len;
Jason Baronf279cd02014-09-24 18:08:00 +0000846 err = xs_send_pagedata(sock, xdr, base, remainder != 0, zerocopy, &sent);
847 *sent_p += sent;
848 if (remainder == 0 || sent != len)
Chuck Levera246b012005-08-11 16:25:23 -0400849 goto out;
850 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400851 } else
852 base -= xdr->page_len;
853
854 if (base >= xdr->tail[0].iov_len)
Jason Baronf279cd02014-09-24 18:08:00 +0000855 return 0;
Trond Myklebuste791f8e2019-02-19 12:39:40 -0500856 msg.msg_flags &= ~MSG_MORE;
857 err = xs_send_kvec(sock, &msg, &xdr->tail[0], base);
Chuck Levera246b012005-08-11 16:25:23 -0400858out:
Jason Baronf279cd02014-09-24 18:08:00 +0000859 if (err > 0) {
860 *sent_p += err;
861 err = 0;
862 }
863 return err;
Chuck Levera246b012005-08-11 16:25:23 -0400864}
865
Chuck Lever9903cd12005-08-11 16:25:26 -0400866/**
Trond Myklebustc5445772018-09-03 23:39:27 -0400867 * xs_nospace - handle transmit was incomplete
Trond Myklebust50f484e2018-08-30 13:27:29 -0400868 * @req: pointer to RPC request
Chuck Lever9903cd12005-08-11 16:25:26 -0400869 *
Chuck Levera246b012005-08-11 16:25:23 -0400870 */
Trond Myklebustc5445772018-09-03 23:39:27 -0400871static int xs_nospace(struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -0400872{
Chuck Lever262965f2005-08-11 16:25:56 -0400873 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500874 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500875 struct sock *sk = transport->inet;
Trond Myklebust24ca9a82011-11-22 14:44:28 +0200876 int ret = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400877
Chuck Lever46121cf2007-01-31 12:14:08 -0500878 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Trond Myklebustc5445772018-09-03 23:39:27 -0400879 req->rq_task->tk_pid,
880 req->rq_slen - transport->xmit.offset,
Chuck Lever262965f2005-08-11 16:25:56 -0400881 req->rq_slen);
882
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400883 /* Protect against races with write_space */
884 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400885
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400886 /* Don't race with disconnect */
887 if (xprt_connected(xprt)) {
Trond Myklebust13331a52016-01-06 08:57:06 -0500888 /* wait for more buffer space */
889 sk->sk_write_pending++;
Trond Myklebustc5445772018-09-03 23:39:27 -0400890 xprt_wait_for_buffer_space(xprt);
Trond Myklebust13331a52016-01-06 08:57:06 -0500891 } else
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400892 ret = -ENOTCONN;
Chuck Lever262965f2005-08-11 16:25:56 -0400893
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400894 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500895
896 /* Race breaker in case memory is freed before above code is called */
David Vrabeld48f9ce2016-09-19 13:58:30 +0100897 if (ret == -EAGAIN) {
898 struct socket_wq *wq;
899
900 rcu_read_lock();
901 wq = rcu_dereference(sk->sk_wq);
902 set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags);
903 rcu_read_unlock();
904
905 sk->sk_write_space(sk);
906 }
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400907 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400908}
909
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400910static void
911xs_stream_prepare_request(struct rpc_rqst *req)
912{
913 req->rq_task->tk_status = xdr_alloc_bvec(&req->rq_rcv_buf, GFP_NOIO);
914}
915
Chuck Lever61677ee2011-05-09 15:22:34 -0400916/*
Trond Myklebust4cd34e72018-08-31 10:00:02 -0400917 * Determine if the previous message in the stream was aborted before it
918 * could complete transmission.
919 */
920static bool
921xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
922{
923 return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
924}
925
926/*
Chuck Lever61677ee2011-05-09 15:22:34 -0400927 * Construct a stream transport record marker in @buf.
928 */
929static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
930{
931 u32 reclen = buf->len - sizeof(rpc_fraghdr);
932 rpc_fraghdr *base = buf->head[0].iov_base;
933 *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
934}
935
Chuck Lever262965f2005-08-11 16:25:56 -0400936/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400937 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
Trond Myklebust50f484e2018-08-30 13:27:29 -0400938 * @req: pointer to RPC request
Chuck Lever176e21e2011-05-09 15:22:44 -0400939 *
940 * Return values:
941 * 0: The request has been sent
942 * EAGAIN: The socket was blocked, please call again later to
943 * complete the request
944 * ENOTCONN: Caller needs to invoke connect logic then call again
945 * other: Some other error occured, the request was not sent
946 */
Trond Myklebustadfa7142018-09-03 23:58:59 -0400947static int xs_local_send_request(struct rpc_rqst *req)
Chuck Lever176e21e2011-05-09 15:22:44 -0400948{
Chuck Lever176e21e2011-05-09 15:22:44 -0400949 struct rpc_xprt *xprt = req->rq_xprt;
950 struct sock_xprt *transport =
951 container_of(xprt, struct sock_xprt, xprt);
952 struct xdr_buf *xdr = &req->rq_snd_buf;
953 int status;
Jason Baronf279cd02014-09-24 18:08:00 +0000954 int sent = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -0400955
Trond Myklebust4cd34e72018-08-31 10:00:02 -0400956 /* Close the stream if the previous transmission was incomplete */
957 if (xs_send_request_was_aborted(transport, req)) {
958 xs_close(xprt);
959 return -ENOTCONN;
960 }
961
Chuck Lever176e21e2011-05-09 15:22:44 -0400962 xs_encode_stream_record_marker(&req->rq_snd_buf);
963
964 xs_pktdump("packet data:",
965 req->rq_svec->iov_base, req->rq_svec->iov_len);
966
Chuck Lever78215752018-03-05 15:13:07 -0500967 req->rq_xtime = ktime_get();
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400968 status = xs_sendpages(transport->sock, NULL, 0, xdr,
969 transport->xmit.offset,
Jason Baronf279cd02014-09-24 18:08:00 +0000970 true, &sent);
Chuck Lever176e21e2011-05-09 15:22:44 -0400971 dprintk("RPC: %s(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400972 __func__, xdr->len - transport->xmit.offset, status);
NeilBrown743c69e2015-07-27 10:55:35 +1000973
974 if (status == -EAGAIN && sock_writeable(transport->inet))
975 status = -ENOBUFS;
976
Jason Baronf279cd02014-09-24 18:08:00 +0000977 if (likely(sent > 0) || status == 0) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400978 transport->xmit.offset += sent;
979 req->rq_bytes_sent = transport->xmit.offset;
Chuck Lever176e21e2011-05-09 15:22:44 -0400980 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400981 req->rq_xmit_bytes_sent += transport->xmit.offset;
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400982 transport->xmit.offset = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -0400983 return 0;
984 }
985 status = -EAGAIN;
986 }
987
988 switch (status) {
Trond Myklebust3601c4a2014-06-30 13:42:19 -0400989 case -ENOBUFS:
Trond Myklebustb5872f02015-07-03 09:32:23 -0400990 break;
Chuck Lever176e21e2011-05-09 15:22:44 -0400991 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -0400992 status = xs_nospace(req);
Chuck Lever176e21e2011-05-09 15:22:44 -0400993 break;
994 default:
995 dprintk("RPC: sendmsg returned unrecognized error %d\n",
996 -status);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -0500997 /* fall through */
Chuck Lever176e21e2011-05-09 15:22:44 -0400998 case -EPIPE:
999 xs_close(xprt);
1000 status = -ENOTCONN;
1001 }
1002
1003 return status;
1004}
1005
1006/**
Chuck Lever262965f2005-08-11 16:25:56 -04001007 * xs_udp_send_request - write an RPC request to a UDP socket
Trond Myklebust50f484e2018-08-30 13:27:29 -04001008 * @req: pointer to RPC request
Chuck Lever262965f2005-08-11 16:25:56 -04001009 *
1010 * Return values:
1011 * 0: The request has been sent
1012 * EAGAIN: The socket was blocked, please call again later to
1013 * complete the request
1014 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001015 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -04001016 */
Trond Myklebustadfa7142018-09-03 23:58:59 -04001017static int xs_udp_send_request(struct rpc_rqst *req)
Chuck Lever262965f2005-08-11 16:25:56 -04001018{
Chuck Lever262965f2005-08-11 16:25:56 -04001019 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001020 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -04001021 struct xdr_buf *xdr = &req->rq_snd_buf;
Jason Baronf279cd02014-09-24 18:08:00 +00001022 int sent = 0;
Chuck Lever262965f2005-08-11 16:25:56 -04001023 int status;
Chuck Levera246b012005-08-11 16:25:23 -04001024
Chuck Lever9903cd12005-08-11 16:25:26 -04001025 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -04001026 req->rq_svec->iov_base,
1027 req->rq_svec->iov_len);
1028
Trond Myklebust01d37c42009-03-11 14:09:39 -04001029 if (!xprt_bound(xprt))
1030 return -ENOTCONN;
Trond Myklebust75891f52018-09-03 17:37:36 -04001031
1032 if (!xprt_request_get_cong(xprt, req))
1033 return -EBADSLT;
1034
Chuck Lever78215752018-03-05 15:13:07 -05001035 req->rq_xtime = ktime_get();
Jason Baronf279cd02014-09-24 18:08:00 +00001036 status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen,
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001037 xdr, 0, true, &sent);
Chuck Levera246b012005-08-11 16:25:23 -04001038
Chuck Lever46121cf2007-01-31 12:14:08 -05001039 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001040 xdr->len, status);
Chuck Levera246b012005-08-11 16:25:23 -04001041
Jason Baron3dedbb52014-09-24 18:08:04 +00001042 /* firewall is blocking us, don't return -EAGAIN or we end up looping */
1043 if (status == -EPERM)
1044 goto process_status;
1045
NeilBrown743c69e2015-07-27 10:55:35 +10001046 if (status == -EAGAIN && sock_writeable(transport->inet))
1047 status = -ENOBUFS;
1048
Jason Baronf279cd02014-09-24 18:08:00 +00001049 if (sent > 0 || status == 0) {
1050 req->rq_xmit_bytes_sent += sent;
1051 if (sent >= req->rq_slen)
Trond Myklebust21997002007-10-01 11:43:37 -04001052 return 0;
1053 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -04001054 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -04001055 }
Chuck Levera246b012005-08-11 16:25:23 -04001056
Jason Baron3dedbb52014-09-24 18:08:04 +00001057process_status:
Chuck Lever262965f2005-08-11 16:25:56 -04001058 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -04001059 case -ENOTSOCK:
1060 status = -ENOTCONN;
1061 /* Should we call xs_close() here? */
1062 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -04001063 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -04001064 status = xs_nospace(req);
Trond Myklebustb6ddf642008-04-17 18:52:19 -04001065 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001066 case -ENETUNREACH:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001067 case -ENOBUFS:
Chuck Lever262965f2005-08-11 16:25:56 -04001068 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -04001069 case -ECONNREFUSED:
Jason Baron3dedbb52014-09-24 18:08:04 +00001070 case -EPERM:
Chuck Levera246b012005-08-11 16:25:23 -04001071 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -04001072 * prompts ECONNREFUSED. */
Trond Myklebust13331a52016-01-06 08:57:06 -05001073 break;
1074 default:
1075 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1076 -status);
Chuck Levera246b012005-08-11 16:25:23 -04001077 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +08001078
Chuck Lever262965f2005-08-11 16:25:56 -04001079 return status;
Chuck Levera246b012005-08-11 16:25:23 -04001080}
1081
Trond Myklebuste06799f2007-11-05 15:44:12 -05001082/**
Chuck Lever262965f2005-08-11 16:25:56 -04001083 * xs_tcp_send_request - write an RPC request to a TCP socket
Trond Myklebust50f484e2018-08-30 13:27:29 -04001084 * @req: pointer to RPC request
Chuck Lever9903cd12005-08-11 16:25:26 -04001085 *
1086 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -04001087 * 0: The request has been sent
1088 * EAGAIN: The socket was blocked, please call again later to
1089 * complete the request
1090 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001091 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -04001092 *
1093 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -04001094 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -04001095 */
Trond Myklebustadfa7142018-09-03 23:58:59 -04001096static int xs_tcp_send_request(struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001097{
Chuck Levera246b012005-08-11 16:25:23 -04001098 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001099 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -04001100 struct xdr_buf *xdr = &req->rq_snd_buf;
Trond Myklebusta6b31d12013-11-08 16:03:50 -05001101 bool zerocopy = true;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001102 bool vm_wait = false;
Chuck Leverb595bb12007-08-06 11:56:42 -04001103 int status;
Jason Baronf279cd02014-09-24 18:08:00 +00001104 int sent;
Chuck Levera246b012005-08-11 16:25:23 -04001105
Trond Myklebust4cd34e72018-08-31 10:00:02 -04001106 /* Close the stream if the previous transmission was incomplete */
1107 if (xs_send_request_was_aborted(transport, req)) {
1108 if (transport->sock != NULL)
1109 kernel_sock_shutdown(transport->sock, SHUT_RDWR);
1110 return -ENOTCONN;
1111 }
1112
Chuck Lever61677ee2011-05-09 15:22:34 -04001113 xs_encode_stream_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -04001114
Chuck Lever262965f2005-08-11 16:25:56 -04001115 xs_pktdump("packet data:",
1116 req->rq_svec->iov_base,
1117 req->rq_svec->iov_len);
Trond Myklebusta6b31d12013-11-08 16:03:50 -05001118 /* Don't use zero copy if this is a resend. If the RPC call
1119 * completes while the socket holds a reference to the pages,
1120 * then we may end up resending corrupted data.
1121 */
Trond Myklebust50f484e2018-08-30 13:27:29 -04001122 if (req->rq_task->tk_flags & RPC_TASK_SENT)
Trond Myklebusta6b31d12013-11-08 16:03:50 -05001123 zerocopy = false;
Chuck Levera246b012005-08-11 16:25:23 -04001124
Trond Myklebust7196dbb2017-02-08 11:17:54 -05001125 if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
1126 xs_tcp_set_socket_timeouts(xprt, transport->sock);
1127
Chuck Levera246b012005-08-11 16:25:23 -04001128 /* Continue transmitting the packet/record. We must be careful
1129 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -04001130 * called sendmsg(). */
Chuck Lever78215752018-03-05 15:13:07 -05001131 req->rq_xtime = ktime_get();
Chuck Levera246b012005-08-11 16:25:23 -04001132 while (1) {
Jason Baronf279cd02014-09-24 18:08:00 +00001133 sent = 0;
1134 status = xs_sendpages(transport->sock, NULL, 0, xdr,
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001135 transport->xmit.offset,
1136 zerocopy, &sent);
Chuck Levera246b012005-08-11 16:25:23 -04001137
Chuck Lever46121cf2007-01-31 12:14:08 -05001138 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001139 xdr->len - transport->xmit.offset, status);
Chuck Lever262965f2005-08-11 16:25:56 -04001140
Chuck Lever262965f2005-08-11 16:25:56 -04001141 /* If we've sent the entire packet, immediately
1142 * reset the count of bytes sent. */
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001143 transport->xmit.offset += sent;
1144 req->rq_bytes_sent = transport->xmit.offset;
Chuck Lever262965f2005-08-11 16:25:56 -04001145 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001146 req->rq_xmit_bytes_sent += transport->xmit.offset;
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001147 transport->xmit.offset = 0;
Chuck Lever262965f2005-08-11 16:25:56 -04001148 return 0;
Chuck Levera246b012005-08-11 16:25:23 -04001149 }
1150
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001151 WARN_ON_ONCE(sent == 0 && status == 0);
1152
1153 if (status == -EAGAIN ) {
1154 /*
1155 * Return EAGAIN if we're sure we're hitting the
1156 * socket send buffer limits.
1157 */
1158 if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
1159 break;
1160 /*
1161 * Did we hit a memory allocation failure?
1162 */
1163 if (sent == 0) {
1164 status = -ENOBUFS;
1165 if (vm_wait)
1166 break;
1167 /* Retry, knowing now that we're below the
1168 * socket send buffer limit
1169 */
1170 vm_wait = true;
1171 }
1172 continue;
1173 }
Trond Myklebustf580dd02015-07-11 17:48:52 +02001174 if (status < 0)
1175 break;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001176 vm_wait = false;
Chuck Levera246b012005-08-11 16:25:23 -04001177 }
1178
Chuck Lever262965f2005-08-11 16:25:56 -04001179 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -04001180 case -ENOTSOCK:
1181 status = -ENOTCONN;
1182 /* Should we call xs_close() here? */
1183 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001184 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -04001185 status = xs_nospace(req);
Chuck Lever262965f2005-08-11 16:25:56 -04001186 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001187 case -ECONNRESET:
Chuck Lever262965f2005-08-11 16:25:56 -04001188 case -ECONNREFUSED:
1189 case -ENOTCONN:
Trond Myklebust3913c782015-02-08 21:44:04 -05001190 case -EADDRINUSE:
Trond Myklebustb5872f02015-07-03 09:32:23 -04001191 case -ENOBUFS:
Trond Myklebustb9d2bb22012-10-23 11:40:02 -04001192 case -EPIPE:
Trond Myklebust13331a52016-01-06 08:57:06 -05001193 break;
1194 default:
1195 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1196 -status);
Chuck Levera246b012005-08-11 16:25:23 -04001197 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +08001198
Chuck Levera246b012005-08-11 16:25:23 -04001199 return status;
1200}
1201
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001202static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1203{
1204 transport->old_data_ready = sk->sk_data_ready;
1205 transport->old_state_change = sk->sk_state_change;
1206 transport->old_write_space = sk->sk_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -05001207 transport->old_error_report = sk->sk_error_report;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001208}
1209
1210static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1211{
1212 sk->sk_data_ready = transport->old_data_ready;
1213 sk->sk_state_change = transport->old_state_change;
1214 sk->sk_write_space = transport->old_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -05001215 sk->sk_error_report = transport->old_error_report;
1216}
1217
Trond Myklebust42d42a52016-05-23 09:24:55 -04001218static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
1219{
1220 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1221
1222 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
1223}
1224
Trond Myklebustb70ae912015-02-09 09:41:32 -05001225static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1226{
1227 smp_mb__before_atomic();
1228 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1229 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001230 xs_sock_reset_state_flags(xprt);
Trond Myklebustb70ae912015-02-09 09:41:32 -05001231 smp_mb__after_atomic();
1232}
1233
Trond Myklebust21180712013-12-31 13:22:59 -05001234/**
1235 * xs_error_report - callback to handle TCP socket state errors
1236 * @sk: socket
1237 *
1238 * Note: we don't call sock_error() since there may be a rpc_task
1239 * using the socket, and so we don't want to clear sk->sk_err.
1240 */
1241static void xs_error_report(struct sock *sk)
1242{
1243 struct rpc_xprt *xprt;
1244 int err;
1245
1246 read_lock_bh(&sk->sk_callback_lock);
1247 if (!(xprt = xprt_from_sock(sk)))
1248 goto out;
1249
1250 err = -sk->sk_err;
1251 if (err == 0)
1252 goto out;
1253 dprintk("RPC: xs_error_report client %p, error=%d...\n",
1254 xprt, -err);
Trond Myklebuste8353c762013-12-31 13:39:22 -05001255 trace_rpc_socket_error(xprt, sk->sk_socket, err);
Trond Myklebust21180712013-12-31 13:22:59 -05001256 xprt_wake_pending_tasks(xprt, err);
1257 out:
1258 read_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001259}
1260
Chuck Leverfe315e762009-03-11 14:10:21 -04001261static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001262{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001263 struct socket *sock = transport->sock;
1264 struct sock *sk = transport->inet;
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001265 struct rpc_xprt *xprt = &transport->xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001266
Chuck Leverfe315e762009-03-11 14:10:21 -04001267 if (sk == NULL)
1268 return;
Chuck Lever9903cd12005-08-11 16:25:26 -04001269
Jeff Layton264d1df2015-06-03 16:14:27 -04001270 if (atomic_read(&transport->xprt.swapper))
1271 sk_clear_memalloc(sk);
1272
Trond Myklebust09939202015-08-29 19:11:21 -07001273 kernel_sock_shutdown(sock, SHUT_RDWR);
1274
Trond Myklebustedc1b012015-10-05 10:53:49 -04001275 mutex_lock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -04001276 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001277 transport->inet = NULL;
1278 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -04001279
Chuck Lever9903cd12005-08-11 16:25:26 -04001280 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001281
1282 xs_restore_old_callbacks(transport, sk);
Trond Myklebust0c787892015-08-29 13:36:30 -07001283 xprt_clear_connected(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001284 write_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001285 xs_sock_reset_connection_flags(xprt);
Trond Myklebustae053552019-02-20 14:56:20 -05001286 /* Reset stream record info */
1287 xs_stream_reset_connect(transport);
Trond Myklebustedc1b012015-10-05 10:53:49 -04001288 mutex_unlock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -04001289
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001290 trace_rpc_socket_close(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001291 sock_release(sock);
Trond Myklebust0445f922018-12-17 13:34:59 -05001292
1293 xprt_disconnect_done(xprt);
Chuck Leverfe315e762009-03-11 14:10:21 -04001294}
1295
1296/**
1297 * xs_close - close a socket
1298 * @xprt: transport
1299 *
1300 * This is used when all requests are complete; ie, no DRC state remains
1301 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -04001302 *
1303 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1304 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e762009-03-11 14:10:21 -04001305 */
1306static void xs_close(struct rpc_xprt *xprt)
1307{
1308 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1309
1310 dprintk("RPC: xs_close xprt %p\n", xprt);
1311
1312 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -04001313 xprt->reestablish_timeout = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001314}
1315
Chuck Lever4a068252015-05-11 14:02:25 -04001316static void xs_inject_disconnect(struct rpc_xprt *xprt)
1317{
1318 dprintk("RPC: injecting transport disconnect on xprt=%p\n",
1319 xprt);
1320 xprt_disconnect_done(xprt);
1321}
1322
Kinglong Mee315f3812014-03-24 11:07:22 +08001323static void xs_xprt_free(struct rpc_xprt *xprt)
1324{
1325 xs_free_peer_addresses(xprt);
1326 xprt_free(xprt);
1327}
1328
Chuck Lever9903cd12005-08-11 16:25:26 -04001329/**
1330 * xs_destroy - prepare to shutdown a transport
1331 * @xprt: doomed transport
1332 *
1333 */
1334static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001335{
Trond Myklebust03c788272015-09-17 10:42:27 -04001336 struct sock_xprt *transport = container_of(xprt,
1337 struct sock_xprt, xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05001338 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001339
Trond Myklebust03c788272015-09-17 10:42:27 -04001340 cancel_delayed_work_sync(&transport->connect_worker);
Trond Myklebusta1311d82013-10-31 09:18:49 -04001341 xs_close(xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04001342 cancel_work_sync(&transport->recv_worker);
Kinglong Mee315f3812014-03-24 11:07:22 +08001343 xs_xprt_free(xprt);
Trond Myklebusta1311d82013-10-31 09:18:49 -04001344 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -04001345}
1346
Chuck Lever9903cd12005-08-11 16:25:26 -04001347/**
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001348 * xs_udp_data_read_skb - receive callback for UDP sockets
1349 * @xprt: transport
1350 * @sk: socket
1351 * @skb: skbuff
Chuck Lever9903cd12005-08-11 16:25:26 -04001352 *
1353 */
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001354static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1355 struct sock *sk,
1356 struct sk_buff *skb)
Chuck Lever9903cd12005-08-11 16:25:26 -04001357{
1358 struct rpc_task *task;
Chuck Levera246b012005-08-11 16:25:23 -04001359 struct rpc_rqst *rovr;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001360 int repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001361 u32 _xid;
1362 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -04001363
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001364 repsize = skb->len;
Chuck Levera246b012005-08-11 16:25:23 -04001365 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001366 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001367 return;
Chuck Levera246b012005-08-11 16:25:23 -04001368 }
1369
1370 /* Copy the XID from the skb... */
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001371 xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
Chuck Levera246b012005-08-11 16:25:23 -04001372 if (xp == NULL)
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001373 return;
Chuck Levera246b012005-08-11 16:25:23 -04001374
1375 /* Look up and lock the request corresponding to the given XID */
Trond Myklebust75c84152018-08-31 10:21:00 -04001376 spin_lock(&xprt->queue_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001377 rovr = xprt_lookup_rqst(xprt, *xp);
1378 if (!rovr)
1379 goto out_unlock;
Trond Myklebust729749b2017-08-13 10:03:59 -04001380 xprt_pin_rqst(rovr);
Chuck Leverecd465e2018-03-05 15:12:57 -05001381 xprt_update_rtt(rovr->rq_task);
Trond Myklebust75c84152018-08-31 10:21:00 -04001382 spin_unlock(&xprt->queue_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001383 task = rovr->rq_task;
1384
Chuck Levera246b012005-08-11 16:25:23 -04001385 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1386 copied = repsize;
1387
1388 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001389 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
Trond Myklebust75c84152018-08-31 10:21:00 -04001390 spin_lock(&xprt->queue_lock);
Trond Myklebust0afa6b42018-02-09 09:39:42 -05001391 __UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
Trond Myklebust729749b2017-08-13 10:03:59 -04001392 goto out_unpin;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001393 }
1394
Chuck Levera246b012005-08-11 16:25:23 -04001395
Trond Myklebust729749b2017-08-13 10:03:59 -04001396 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001397 xprt_adjust_cwnd(xprt, task, copied);
Trond Myklebustce7c2522017-08-16 15:30:35 -04001398 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust75c84152018-08-31 10:21:00 -04001399 spin_lock(&xprt->queue_lock);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001400 xprt_complete_rqst(task, copied);
Trond Myklebust0afa6b42018-02-09 09:39:42 -05001401 __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
Trond Myklebust729749b2017-08-13 10:03:59 -04001402out_unpin:
1403 xprt_unpin_rqst(rovr);
Chuck Levera246b012005-08-11 16:25:23 -04001404 out_unlock:
Trond Myklebust75c84152018-08-31 10:21:00 -04001405 spin_unlock(&xprt->queue_lock);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001406}
1407
1408static void xs_udp_data_receive(struct sock_xprt *transport)
1409{
1410 struct sk_buff *skb;
1411 struct sock *sk;
1412 int err;
1413
1414 mutex_lock(&transport->recv_mutex);
1415 sk = transport->inet;
1416 if (sk == NULL)
1417 goto out;
1418 for (;;) {
Paolo Abeni7c13f972016-11-04 11:28:59 +01001419 skb = skb_recv_udp(sk, 0, 1, &err);
Trond Myklebust4f546142018-09-14 17:45:23 -04001420 if (skb == NULL)
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001421 break;
Trond Myklebust4f546142018-09-14 17:45:23 -04001422 xs_udp_data_read_skb(&transport->xprt, sk, skb);
1423 consume_skb(skb);
1424 cond_resched();
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001425 }
Trond Myklebust0ffe86f2019-01-30 14:51:26 -05001426 xs_poll_check_readable(transport);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001427out:
1428 mutex_unlock(&transport->recv_mutex);
1429}
1430
1431static void xs_udp_data_receive_workfn(struct work_struct *work)
1432{
1433 struct sock_xprt *transport =
1434 container_of(work, struct sock_xprt, recv_worker);
Trond Myklebusta1231fd2019-02-18 10:02:29 -05001435 unsigned int pflags = memalloc_nofs_save();
1436
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001437 xs_udp_data_receive(transport);
Trond Myklebusta1231fd2019-02-18 10:02:29 -05001438 memalloc_nofs_restore(pflags);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001439}
1440
1441/**
1442 * xs_data_ready - "data ready" callback for UDP sockets
1443 * @sk: socket with data to read
1444 *
1445 */
1446static void xs_data_ready(struct sock *sk)
1447{
1448 struct rpc_xprt *xprt;
1449
1450 read_lock_bh(&sk->sk_callback_lock);
1451 dprintk("RPC: xs_data_ready...\n");
1452 xprt = xprt_from_sock(sk);
1453 if (xprt != NULL) {
1454 struct sock_xprt *transport = container_of(xprt,
1455 struct sock_xprt, xprt);
Trond Myklebust5157b952016-05-29 10:13:24 -04001456 transport->old_data_ready(sk);
1457 /* Any data means we had a useful conversation, so
1458 * then we don't need to delay the next reconnect
1459 */
1460 if (xprt->reestablish_timeout)
1461 xprt->reestablish_timeout = 0;
Trond Myklebust42d42a52016-05-23 09:24:55 -04001462 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebust40a5f1b2016-05-27 10:39:50 -04001463 queue_work(xprtiod_workqueue, &transport->recv_worker);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001464 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001465 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001466}
1467
Trond Myklebusta519fc72012-09-12 16:49:15 -04001468/*
1469 * Helper function to force a TCP close if the server is sending
1470 * junk and/or it has put us in CLOSE_WAIT
1471 */
1472static void xs_tcp_force_close(struct rpc_xprt *xprt)
1473{
Trond Myklebusta519fc72012-09-12 16:49:15 -04001474 xprt_force_disconnect(xprt);
1475}
1476
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001477#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Chuck Lever6b26cc82016-05-02 14:40:40 -04001478static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
1479{
1480 return PAGE_SIZE;
1481}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001482#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001483
Chuck Lever9903cd12005-08-11 16:25:26 -04001484/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001485 * xs_tcp_state_change - callback to handle TCP socket state changes
1486 * @sk: socket whose state has changed
1487 *
1488 */
1489static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001490{
Chuck Lever9903cd12005-08-11 16:25:26 -04001491 struct rpc_xprt *xprt;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001492 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001493
Eric Dumazetf064af12010-09-22 12:43:39 +00001494 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001495 if (!(xprt = xprt_from_sock(sk)))
1496 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001497 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001498 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001499 sk->sk_state, xprt_connected(xprt),
1500 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001501 sock_flag(sk, SOCK_ZAPPED),
1502 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001503
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001504 transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001505 trace_rpc_socket_state_change(xprt, sk->sk_socket);
Chuck Levera246b012005-08-11 16:25:23 -04001506 switch (sk->sk_state) {
1507 case TCP_ESTABLISHED:
Eric Dumazetf064af12010-09-22 12:43:39 +00001508 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001509 if (!xprt_test_and_set_connected(xprt)) {
Trond Myklebust8b717982013-09-26 10:18:04 -04001510 xprt->connect_cookie++;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001511 clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
1512 xprt_clear_connecting(xprt);
Chuck Lever51971132006-12-05 16:35:19 -05001513
Chuck Lever3968a8a2018-10-01 14:25:36 -04001514 xprt->stat.connect_count++;
1515 xprt->stat.connect_time += (long)jiffies -
1516 xprt->stat.connect_start;
Trond Myklebust2a491992009-03-11 14:38:00 -04001517 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001518 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001519 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001520 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001521 case TCP_FIN_WAIT1:
1522 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001523 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001524 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001525 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001526 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001527 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001528 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001529 smp_mb__after_atomic();
Chuck Levera246b012005-08-11 16:25:23 -04001530 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001531 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001532 /* The server initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001533 xprt->connect_cookie++;
Trond Myklebustd0bea452012-10-23 11:35:47 -04001534 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebusta519fc72012-09-12 16:49:15 -04001535 xs_tcp_force_close(xprt);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001536 /* fall through */
Trond Myklebust663b8852008-01-01 18:42:12 -05001537 case TCP_CLOSING:
1538 /*
1539 * If the server closed down the connection, make sure that
1540 * we back off before reconnecting
1541 */
1542 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1543 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001544 break;
1545 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001546 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001547 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001548 clear_bit(XPRT_CONNECTED, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001549 smp_mb__after_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001550 break;
1551 case TCP_CLOSE:
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001552 if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
1553 &transport->sock_state))
1554 xprt_clear_connecting(xprt);
Trond Myklebust9b308892018-02-05 10:20:06 -05001555 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust9b308892018-02-05 10:20:06 -05001556 /* Trigger the socket release */
1557 xs_tcp_force_close(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001558 }
1559 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001560 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001561}
1562
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001563static void xs_write_space(struct sock *sk)
1564{
Trond Myklebust13331a52016-01-06 08:57:06 -05001565 struct socket_wq *wq;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001566 struct rpc_xprt *xprt;
1567
Trond Myklebust13331a52016-01-06 08:57:06 -05001568 if (!sk->sk_socket)
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001569 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001570 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001571
1572 if (unlikely(!(xprt = xprt_from_sock(sk))))
1573 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001574 rcu_read_lock();
1575 wq = rcu_dereference(sk->sk_wq);
1576 if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0)
1577 goto out;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001578
Trond Myklebustc5445772018-09-03 23:39:27 -04001579 if (xprt_write_space(xprt))
1580 sk->sk_write_pending--;
Trond Myklebust13331a52016-01-06 08:57:06 -05001581out:
1582 rcu_read_unlock();
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001583}
1584
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001585/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001586 * xs_udp_write_space - callback invoked when socket buffer space
1587 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001588 * @sk: socket whose state has changed
1589 *
Chuck Levera246b012005-08-11 16:25:23 -04001590 * Called when more output buffer space is available for this socket.
1591 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001592 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001593 * with a bunch of small requests.
1594 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001595static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001596{
Eric Dumazetf064af12010-09-22 12:43:39 +00001597 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001598
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001599 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001600 if (sock_writeable(sk))
1601 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001602
Eric Dumazetf064af12010-09-22 12:43:39 +00001603 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001604}
Chuck Levera246b012005-08-11 16:25:23 -04001605
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001606/**
1607 * xs_tcp_write_space - callback invoked when socket buffer space
1608 * becomes available
1609 * @sk: socket whose state has changed
1610 *
1611 * Called when more output buffer space is available for this socket.
1612 * We try not to wake our writers until they can make "significant"
1613 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1614 * with a bunch of small requests.
1615 */
1616static void xs_tcp_write_space(struct sock *sk)
1617{
Eric Dumazetf064af12010-09-22 12:43:39 +00001618 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001619
1620 /* from net/core/stream.c:sk_stream_write_space */
Eric Dumazet64dc6132013-07-22 20:26:31 -07001621 if (sk_stream_is_writeable(sk))
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001622 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001623
Eric Dumazetf064af12010-09-22 12:43:39 +00001624 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001625}
1626
Chuck Lever470056c2005-08-25 16:25:56 -07001627static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001628{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001629 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1630 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001631
Chuck Lever7c6e0662006-12-05 16:35:30 -05001632 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001633 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001634 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001635 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001636 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001637 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001638 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001639 sk->sk_write_space(sk);
1640 }
1641}
1642
Chuck Lever43118c22005-08-25 16:25:49 -07001643/**
Chuck Lever470056c2005-08-25 16:25:56 -07001644 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001645 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001646 * @sndsize: requested size of send buffer, in bytes
1647 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001648 *
Chuck Lever470056c2005-08-25 16:25:56 -07001649 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001650 */
Chuck Lever470056c2005-08-25 16:25:56 -07001651static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001652{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001653 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1654
1655 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001656 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001657 transport->sndsize = sndsize + 1024;
1658 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001659 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001660 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001661
1662 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001663}
1664
Chuck Lever46c0ee82005-08-25 16:25:52 -07001665/**
1666 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
Chuck Leveracf0a392018-12-19 11:00:22 -05001667 * @xprt: controlling transport
Chuck Lever46c0ee82005-08-25 16:25:52 -07001668 * @task: task that timed out
1669 *
1670 * Adjust the congestion window after a retransmit timeout has occurred.
1671 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001672static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever46c0ee82005-08-25 16:25:52 -07001673{
Chuck Leverb977b642017-02-08 17:00:51 -05001674 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001675 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
Chuck Leverb977b642017-02-08 17:00:51 -05001676 spin_unlock_bh(&xprt->transport_lock);
Chuck Lever46c0ee82005-08-25 16:25:52 -07001677}
1678
J. Bruce Fields826799e2018-10-18 15:27:02 -04001679static int xs_get_random_port(void)
Chuck Leverb85d8802006-05-25 01:40:49 -04001680{
J. Bruce Fields826799e2018-10-18 15:27:02 -04001681 unsigned short min = xprt_min_resvport, max = xprt_max_resvport;
1682 unsigned short range;
1683 unsigned short rand;
1684
1685 if (max < min)
1686 return -EADDRINUSE;
1687 range = max - min + 1;
1688 rand = (unsigned short) prandom_u32() % range;
1689 return rand + min;
Chuck Leverb85d8802006-05-25 01:40:49 -04001690}
1691
Chuck Lever92200412006-01-03 09:55:51 +01001692/**
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001693 * xs_set_reuseaddr_port - set the socket's port and address reuse options
1694 * @sock: socket
1695 *
1696 * Note that this function has to be called on all sockets that share the
1697 * same port, and it must be called before binding.
1698 */
1699static void xs_sock_set_reuseport(struct socket *sock)
1700{
Trond Myklebust402e23b2015-02-09 17:20:14 -05001701 int opt = 1;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001702
Trond Myklebust402e23b2015-02-09 17:20:14 -05001703 kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
1704 (char *)&opt, sizeof(opt));
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001705}
1706
1707static unsigned short xs_sock_getport(struct socket *sock)
1708{
1709 struct sockaddr_storage buf;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001710 unsigned short port = 0;
1711
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001712 if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001713 goto out;
1714 switch (buf.ss_family) {
1715 case AF_INET6:
1716 port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
1717 break;
1718 case AF_INET:
1719 port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
1720 }
1721out:
1722 return port;
1723}
1724
1725/**
Chuck Lever92200412006-01-03 09:55:51 +01001726 * xs_set_port - reset the port number in the remote endpoint address
1727 * @xprt: generic transport
1728 * @port: new port number
1729 *
1730 */
1731static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1732{
Chuck Lever46121cf2007-01-31 12:14:08 -05001733 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001734
Chuck Lever9dc3b092009-08-09 15:09:46 -04001735 rpc_set_port(xs_addr(xprt), port);
1736 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001737}
1738
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001739static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
1740{
1741 if (transport->srcport == 0)
1742 transport->srcport = xs_sock_getport(sock);
1743}
1744
J. Bruce Fields826799e2018-10-18 15:27:02 -04001745static int xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001746{
J. Bruce Fields826799e2018-10-18 15:27:02 -04001747 int port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001748
1749 if (port == 0 && transport->xprt.resvport)
1750 port = xs_get_random_port();
1751 return port;
1752}
1753
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001754static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001755{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001756 if (transport->srcport != 0)
1757 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001758 if (!transport->xprt.resvport)
1759 return 0;
1760 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1761 return xprt_max_resvport;
1762 return --port;
1763}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001764static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001765{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001766 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001767 int err, nloop = 0;
J. Bruce Fields826799e2018-10-18 15:27:02 -04001768 int port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001769 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001770
Chris Perl0f7a6222014-09-05 15:40:21 -04001771 /*
1772 * If we are asking for any ephemeral port (i.e. port == 0 &&
1773 * transport->xprt.resvport == 0), don't bind. Let the local
1774 * port selection happen implicitly when the socket is used
1775 * (for example at connect time).
1776 *
1777 * This ensures that we can continue to establish TCP
1778 * connections even when all local ephemeral ports are already
1779 * a part of some TCP connection. This makes no difference
1780 * for UDP sockets, but also doens't harm them.
1781 *
1782 * If we're asking for any reserved port (i.e. port == 0 &&
1783 * transport->xprt.resvport == 1) xs_get_srcport above will
1784 * ensure that port is non-zero and we will bind as needed.
1785 */
J. Bruce Fields826799e2018-10-18 15:27:02 -04001786 if (port <= 0)
1787 return port;
Chris Perl0f7a6222014-09-05 15:40:21 -04001788
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001789 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001790 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001791 rpc_set_port((struct sockaddr *)&myaddr, port);
1792 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1793 transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001794 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001795 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001796 break;
Chuck Levera246b012005-08-11 16:25:23 -04001797 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001798 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001799 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001800 if (port > last)
1801 nloop++;
1802 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001803
Chuck Lever4232e862010-10-20 11:52:51 -04001804 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001805 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1806 &((struct sockaddr_in *)&myaddr)->sin_addr,
1807 port, err ? "failed" : "ok", err);
1808 else
1809 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1810 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1811 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001812 return err;
1813}
1814
Chuck Lever176e21e2011-05-09 15:22:44 -04001815/*
1816 * We don't support autobind on AF_LOCAL sockets
1817 */
1818static void xs_local_rpcbind(struct rpc_task *task)
1819{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001820 xprt_set_bound(task->tk_xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04001821}
1822
1823static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1824{
1825}
Chuck Levera246b012005-08-11 16:25:23 -04001826
Peter Zijlstraed075362006-12-06 20:35:24 -08001827#ifdef CONFIG_DEBUG_LOCK_ALLOC
1828static struct lock_class_key xs_key[2];
1829static struct lock_class_key xs_slock_key[2];
1830
Chuck Lever176e21e2011-05-09 15:22:44 -04001831static inline void xs_reclassify_socketu(struct socket *sock)
1832{
1833 struct sock *sk = sock->sk;
1834
Chuck Lever176e21e2011-05-09 15:22:44 -04001835 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1836 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1837}
1838
Chuck Lever8945ee52007-08-06 11:58:04 -04001839static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001840{
1841 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001842
Chuck Lever8945ee52007-08-06 11:58:04 -04001843 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1844 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1845}
Peter Zijlstraed075362006-12-06 20:35:24 -08001846
Chuck Lever8945ee52007-08-06 11:58:04 -04001847static inline void xs_reclassify_socket6(struct socket *sock)
1848{
1849 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001850
Chuck Lever8945ee52007-08-06 11:58:04 -04001851 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1852 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001853}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001854
1855static inline void xs_reclassify_socket(int family, struct socket *sock)
1856{
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02001857 if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -04001858 return;
1859
Chuck Lever4232e862010-10-20 11:52:51 -04001860 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001861 case AF_LOCAL:
1862 xs_reclassify_socketu(sock);
1863 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001864 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001865 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001866 break;
1867 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001868 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001869 break;
1870 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001871}
Peter Zijlstraed075362006-12-06 20:35:24 -08001872#else
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001873static inline void xs_reclassify_socket(int family, struct socket *sock)
1874{
1875}
Peter Zijlstraed075362006-12-06 20:35:24 -08001876#endif
1877
NeilBrown93dc41b2013-10-31 16:14:36 +11001878static void xs_dummy_setup_socket(struct work_struct *work)
1879{
1880}
1881
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001882static struct socket *xs_create_sock(struct rpc_xprt *xprt,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001883 struct sock_xprt *transport, int family, int type,
1884 int protocol, bool reuseport)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001885{
1886 struct socket *sock;
1887 int err;
1888
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001889 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001890 if (err < 0) {
1891 dprintk("RPC: can't create %d transport socket (%d).\n",
1892 protocol, -err);
1893 goto out;
1894 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001895 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001896
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001897 if (reuseport)
1898 xs_sock_set_reuseport(sock);
1899
Ben Hutchings4cea2882011-02-22 21:54:34 +00001900 err = xs_bind(transport, sock);
1901 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001902 sock_release(sock);
1903 goto out;
1904 }
1905
1906 return sock;
1907out:
1908 return ERR_PTR(err);
1909}
1910
Chuck Lever176e21e2011-05-09 15:22:44 -04001911static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1912 struct socket *sock)
1913{
1914 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1915 xprt);
1916
1917 if (!transport->inet) {
1918 struct sock *sk = sock->sk;
1919
1920 write_lock_bh(&sk->sk_callback_lock);
1921
1922 xs_save_old_callbacks(transport, sk);
1923
1924 sk->sk_user_data = xprt;
Trond Myklebusta2648092015-10-06 17:03:00 -04001925 sk->sk_data_ready = xs_data_ready;
Chuck Lever176e21e2011-05-09 15:22:44 -04001926 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07001927 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05001928 sk->sk_error_report = xs_error_report;
Chuck Lever176e21e2011-05-09 15:22:44 -04001929
1930 xprt_clear_connected(xprt);
1931
1932 /* Reset to new socket */
1933 transport->sock = sock;
1934 transport->inet = sk;
1935
1936 write_unlock_bh(&sk->sk_callback_lock);
1937 }
1938
Trond Myklebustae053552019-02-20 14:56:20 -05001939 xs_stream_start_connect(transport);
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001940
Chuck Lever176e21e2011-05-09 15:22:44 -04001941 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1942}
1943
1944/**
1945 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
Chuck Lever176e21e2011-05-09 15:22:44 -04001946 * @transport: socket transport to connect
Chuck Lever176e21e2011-05-09 15:22:44 -04001947 */
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001948static int xs_local_setup_socket(struct sock_xprt *transport)
Chuck Lever176e21e2011-05-09 15:22:44 -04001949{
Chuck Lever176e21e2011-05-09 15:22:44 -04001950 struct rpc_xprt *xprt = &transport->xprt;
1951 struct socket *sock;
1952 int status = -EIO;
1953
Chuck Lever176e21e2011-05-09 15:22:44 -04001954 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1955 SOCK_STREAM, 0, &sock, 1);
1956 if (status < 0) {
1957 dprintk("RPC: can't create AF_LOCAL "
1958 "transport socket (%d).\n", -status);
1959 goto out;
1960 }
Stefan Hajnoczid1358912015-12-02 14:17:52 +08001961 xs_reclassify_socket(AF_LOCAL, sock);
Chuck Lever176e21e2011-05-09 15:22:44 -04001962
1963 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1964 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1965
1966 status = xs_local_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001967 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever176e21e2011-05-09 15:22:44 -04001968 switch (status) {
1969 case 0:
1970 dprintk("RPC: xprt %p connected to %s\n",
1971 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
Chuck Lever3968a8a2018-10-01 14:25:36 -04001972 xprt->stat.connect_count++;
1973 xprt->stat.connect_time += (long)jiffies -
1974 xprt->stat.connect_start;
Chuck Lever176e21e2011-05-09 15:22:44 -04001975 xprt_set_connected(xprt);
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001976 case -ENOBUFS:
Chuck Lever176e21e2011-05-09 15:22:44 -04001977 break;
1978 case -ENOENT:
1979 dprintk("RPC: xprt %p: socket %s does not exist\n",
1980 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1981 break;
Trond Myklebust4a20a982012-12-15 17:02:29 -05001982 case -ECONNREFUSED:
1983 dprintk("RPC: xprt %p: connection refused for %s\n",
1984 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1985 break;
Chuck Lever176e21e2011-05-09 15:22:44 -04001986 default:
1987 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1988 __func__, -status,
1989 xprt->address_strings[RPC_DISPLAY_ADDR]);
1990 }
1991
1992out:
1993 xprt_clear_connecting(xprt);
1994 xprt_wake_pending_tasks(xprt, status);
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001995 return status;
1996}
1997
Linus Torvaldsb6669732013-02-28 18:02:55 -08001998static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001999{
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002000 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2001 int ret;
2002
2003 if (RPC_IS_ASYNC(task)) {
2004 /*
2005 * We want the AF_LOCAL connect to be resolved in the
2006 * filesystem namespace of the process making the rpc
2007 * call. Thus we connect synchronously.
2008 *
2009 * If we want to support asynchronous AF_LOCAL calls,
2010 * we'll need to figure out how to pass a namespace to
2011 * connect.
2012 */
2013 rpc_exit(task, -ENOTCONN);
2014 return;
2015 }
2016 ret = xs_local_setup_socket(transport);
2017 if (ret && !RPC_IS_SOFTCONN(task))
2018 msleep_interruptible(15000);
Chuck Lever176e21e2011-05-09 15:22:44 -04002019}
2020
Jeff Layton3c87ef62015-06-03 16:14:25 -04002021#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Jeff Laytond6e971d2015-06-03 16:14:28 -04002022/*
2023 * Note that this should be called with XPRT_LOCKED held (or when we otherwise
2024 * know that we have exclusive access to the socket), to guard against
2025 * races with xs_reset_transport.
2026 */
Mel Gormana564b8f2012-07-31 16:45:12 -07002027static void xs_set_memalloc(struct rpc_xprt *xprt)
2028{
2029 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2030 xprt);
2031
Jeff Laytond6e971d2015-06-03 16:14:28 -04002032 /*
2033 * If there's no sock, then we have nothing to set. The
2034 * reconnecting process will get it for us.
2035 */
2036 if (!transport->inet)
2037 return;
Jeff Layton8e228132015-06-03 16:14:26 -04002038 if (atomic_read(&xprt->swapper))
Mel Gormana564b8f2012-07-31 16:45:12 -07002039 sk_set_memalloc(transport->inet);
2040}
2041
2042/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002043 * xs_enable_swap - Tag this transport as being used for swap.
Mel Gormana564b8f2012-07-31 16:45:12 -07002044 * @xprt: transport to tag
Mel Gormana564b8f2012-07-31 16:45:12 -07002045 *
Jeff Layton8e228132015-06-03 16:14:26 -04002046 * Take a reference to this transport on behalf of the rpc_clnt, and
2047 * optionally mark it for swapping if it wasn't already.
Mel Gormana564b8f2012-07-31 16:45:12 -07002048 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002049static int
2050xs_enable_swap(struct rpc_xprt *xprt)
Mel Gormana564b8f2012-07-31 16:45:12 -07002051{
Jeff Laytond6e971d2015-06-03 16:14:28 -04002052 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Mel Gormana564b8f2012-07-31 16:45:12 -07002053
Jeff Laytond6e971d2015-06-03 16:14:28 -04002054 if (atomic_inc_return(&xprt->swapper) != 1)
2055 return 0;
2056 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2057 return -ERESTARTSYS;
2058 if (xs->inet)
2059 sk_set_memalloc(xs->inet);
2060 xprt_release_xprt(xprt, NULL);
Jeff Layton8e228132015-06-03 16:14:26 -04002061 return 0;
Mel Gormana564b8f2012-07-31 16:45:12 -07002062}
Jeff Layton8e228132015-06-03 16:14:26 -04002063
2064/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002065 * xs_disable_swap - Untag this transport as being used for swap.
Jeff Layton8e228132015-06-03 16:14:26 -04002066 * @xprt: transport to tag
2067 *
2068 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
2069 * swapper refcount goes to 0, untag the socket as a memalloc socket.
2070 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002071static void
2072xs_disable_swap(struct rpc_xprt *xprt)
Jeff Layton8e228132015-06-03 16:14:26 -04002073{
Jeff Laytond6e971d2015-06-03 16:14:28 -04002074 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Jeff Layton8e228132015-06-03 16:14:26 -04002075
Jeff Laytond6e971d2015-06-03 16:14:28 -04002076 if (!atomic_dec_and_test(&xprt->swapper))
2077 return;
2078 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2079 return;
2080 if (xs->inet)
2081 sk_clear_memalloc(xs->inet);
2082 xprt_release_xprt(xprt, NULL);
Mel Gormana564b8f2012-07-31 16:45:12 -07002083}
Mel Gormana564b8f2012-07-31 16:45:12 -07002084#else
2085static void xs_set_memalloc(struct rpc_xprt *xprt)
2086{
2087}
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002088
2089static int
2090xs_enable_swap(struct rpc_xprt *xprt)
2091{
2092 return -EINVAL;
2093}
2094
2095static void
2096xs_disable_swap(struct rpc_xprt *xprt)
2097{
2098}
Mel Gormana564b8f2012-07-31 16:45:12 -07002099#endif
2100
Chuck Lever16be2d22007-08-06 11:57:38 -04002101static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04002102{
Chuck Lever16be2d22007-08-06 11:57:38 -04002103 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04002104
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002105 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002106 struct sock *sk = sock->sk;
2107
2108 write_lock_bh(&sk->sk_callback_lock);
2109
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002110 xs_save_old_callbacks(transport, sk);
2111
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002112 sk->sk_user_data = xprt;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002113 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002114 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002115 sock_set_flag(sk, SOCK_FASYNC);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002116
2117 xprt_set_connected(xprt);
2118
2119 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002120 transport->sock = sock;
2121 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002122
Mel Gormana564b8f2012-07-31 16:45:12 -07002123 xs_set_memalloc(xprt);
2124
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002125 write_unlock_bh(&sk->sk_callback_lock);
2126 }
Chuck Lever470056c2005-08-25 16:25:56 -07002127 xs_udp_do_set_buffer_size(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002128
2129 xprt->stat.connect_start = jiffies;
Chuck Lever16be2d22007-08-06 11:57:38 -04002130}
2131
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002132static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04002133{
2134 struct sock_xprt *transport =
2135 container_of(work, struct sock_xprt, connect_worker.work);
2136 struct rpc_xprt *xprt = &transport->xprt;
Colin Ian Kingd099b8a2017-09-18 12:21:14 +01002137 struct socket *sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002138 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04002139
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002140 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002141 xs_addr(xprt)->sa_family, SOCK_DGRAM,
2142 IPPROTO_UDP, false);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002143 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04002144 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04002145
Chuck Leverc740eff2009-08-09 15:09:46 -04002146 dprintk("RPC: worker connecting xprt %p via %s to "
2147 "%s (port %s)\n", xprt,
2148 xprt->address_strings[RPC_DISPLAY_PROTO],
2149 xprt->address_strings[RPC_DISPLAY_ADDR],
2150 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04002151
2152 xs_udp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002153 trace_rpc_socket_connect(xprt, sock, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002154 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002155out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002156 xprt_clear_connecting(xprt);
Trond Myklebustcf767852018-12-17 17:38:51 -05002157 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002158 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002159}
2160
Trond Myklebust4876cc72015-06-19 16:17:57 -04002161/**
2162 * xs_tcp_shutdown - gracefully shut down a TCP socket
2163 * @xprt: transport
2164 *
2165 * Initiates a graceful shutdown of the TCP socket by calling the
2166 * equivalent of shutdown(SHUT_RDWR);
2167 */
2168static void xs_tcp_shutdown(struct rpc_xprt *xprt)
2169{
2170 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2171 struct socket *sock = transport->sock;
Trond Myklebust9b308892018-02-05 10:20:06 -05002172 int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
Trond Myklebust4876cc72015-06-19 16:17:57 -04002173
2174 if (sock == NULL)
2175 return;
Trond Myklebust9b308892018-02-05 10:20:06 -05002176 switch (skst) {
2177 default:
Trond Myklebust4876cc72015-06-19 16:17:57 -04002178 kernel_sock_shutdown(sock, SHUT_RDWR);
2179 trace_rpc_socket_shutdown(xprt, sock);
Trond Myklebust9b308892018-02-05 10:20:06 -05002180 break;
2181 case TCP_CLOSE:
2182 case TCP_TIME_WAIT:
Trond Myklebust4876cc72015-06-19 16:17:57 -04002183 xs_reset_transport(transport);
Trond Myklebust9b308892018-02-05 10:20:06 -05002184 }
Trond Myklebust4876cc72015-06-19 16:17:57 -04002185}
2186
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002187static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
2188 struct socket *sock)
2189{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002190 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2191 unsigned int keepidle;
2192 unsigned int keepcnt;
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002193 unsigned int opt_on = 1;
2194 unsigned int timeo;
2195
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002196 spin_lock_bh(&xprt->transport_lock);
2197 keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
2198 keepcnt = xprt->timeout->to_retries + 1;
2199 timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
2200 (xprt->timeout->to_retries + 1);
2201 clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2202 spin_unlock_bh(&xprt->transport_lock);
2203
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002204 /* TCP Keepalive options */
2205 kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
2206 (char *)&opt_on, sizeof(opt_on));
2207 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
2208 (char *)&keepidle, sizeof(keepidle));
2209 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
2210 (char *)&keepidle, sizeof(keepidle));
2211 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
2212 (char *)&keepcnt, sizeof(keepcnt));
2213
2214 /* TCP user timeout (see RFC5482) */
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002215 kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT,
2216 (char *)&timeo, sizeof(timeo));
2217}
2218
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002219static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
2220 unsigned long connect_timeout,
2221 unsigned long reconnect_timeout)
2222{
2223 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2224 struct rpc_timeout to;
2225 unsigned long initval;
2226
2227 spin_lock_bh(&xprt->transport_lock);
2228 if (reconnect_timeout < xprt->max_reconnect_timeout)
2229 xprt->max_reconnect_timeout = reconnect_timeout;
2230 if (connect_timeout < xprt->connect_timeout) {
2231 memcpy(&to, xprt->timeout, sizeof(to));
2232 initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
2233 /* Arbitrary lower limit */
2234 if (initval < XS_TCP_INIT_REEST_TO << 1)
2235 initval = XS_TCP_INIT_REEST_TO << 1;
2236 to.to_initval = initval;
2237 to.to_maxval = initval;
2238 memcpy(&transport->tcp_timeout, &to,
2239 sizeof(transport->tcp_timeout));
2240 xprt->timeout = &transport->tcp_timeout;
2241 xprt->connect_timeout = connect_timeout;
2242 }
2243 set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2244 spin_unlock_bh(&xprt->transport_lock);
2245}
2246
Chuck Lever16be2d22007-08-06 11:57:38 -04002247static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002248{
Chuck Lever16be2d22007-08-06 11:57:38 -04002249 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002250 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002251
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002252 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002253 struct sock *sk = sock->sk;
NeilBrownd88e4d82016-08-04 16:24:28 +10002254 unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC;
Trond Myklebust7f260e82013-09-24 11:25:22 -04002255
NeilBrownd88e4d82016-08-04 16:24:28 +10002256 /* Avoid temporary address, they are bad for long-lived
2257 * connections such as NFS mounts.
2258 * RFC4941, section 3.6 suggests that:
2259 * Individual applications, which have specific
2260 * knowledge about the normal duration of connections,
2261 * MAY override this as appropriate.
2262 */
2263 kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES,
2264 (char *)&addr_pref, sizeof(addr_pref));
2265
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002266 xs_tcp_set_socket_timeouts(xprt, sock);
Trond Myklebust775f06a2015-06-20 15:31:54 -04002267
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002268 write_lock_bh(&sk->sk_callback_lock);
2269
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002270 xs_save_old_callbacks(transport, sk);
2271
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002272 sk->sk_user_data = xprt;
Trond Myklebust5157b952016-05-29 10:13:24 -04002273 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002274 sk->sk_state_change = xs_tcp_state_change;
2275 sk->sk_write_space = xs_tcp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002276 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05002277 sk->sk_error_report = xs_error_report;
Chuck Lever3167e122005-08-25 16:25:55 -07002278
2279 /* socket options */
Chuck Lever3167e122005-08-25 16:25:55 -07002280 sock_reset_flag(sk, SOCK_LINGER);
Chuck Lever3167e122005-08-25 16:25:55 -07002281 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002282
2283 xprt_clear_connected(xprt);
2284
2285 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002286 transport->sock = sock;
2287 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002288
2289 write_unlock_bh(&sk->sk_callback_lock);
2290 }
2291
Trond Myklebust01d37c42009-03-11 14:09:39 -04002292 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002293 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002294
Mel Gormana564b8f2012-07-31 16:45:12 -07002295 xs_set_memalloc(xprt);
2296
Trond Myklebustae053552019-02-20 14:56:20 -05002297 xs_stream_start_connect(transport);
Trond Myklebuste1806c72018-08-13 16:50:49 -04002298
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002299 /* Tell the socket layer to start connecting... */
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04002300 set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002301 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2302 switch (ret) {
2303 case 0:
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002304 xs_set_srcport(transport, sock);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002305 /* fall through */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002306 case -EINPROGRESS:
2307 /* SYN_SENT! */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002308 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2309 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust1f4c17a2016-08-01 13:36:08 -04002310 break;
2311 case -EADDRNOTAVAIL:
2312 /* Source port number is unavailable. Try a new one! */
2313 transport->srcport = 0;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002314 }
2315out:
2316 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002317}
2318
2319/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002320 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
Chuck Leveracf0a392018-12-19 11:00:22 -05002321 * @work: queued work item
Chuck Lever16be2d22007-08-06 11:57:38 -04002322 *
2323 * Invoked by a work queue tasklet.
2324 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002325static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002326{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002327 struct sock_xprt *transport =
2328 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002329 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002330 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002331 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002332
Chuck Lever16be2d22007-08-06 11:57:38 -04002333 if (!sock) {
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002334 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002335 xs_addr(xprt)->sa_family, SOCK_STREAM,
2336 IPPROTO_TCP, true);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002337 if (IS_ERR(sock)) {
2338 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002339 goto out;
2340 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002341 }
2342
Chuck Leverc740eff2009-08-09 15:09:46 -04002343 dprintk("RPC: worker connecting xprt %p via %s to "
2344 "%s (port %s)\n", xprt,
2345 xprt->address_strings[RPC_DISPLAY_PROTO],
2346 xprt->address_strings[RPC_DISPLAY_ADDR],
2347 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002348
2349 status = xs_tcp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002350 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever46121cf2007-01-31 12:14:08 -05002351 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2352 xprt, -status, xprt_connected(xprt),
2353 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002354 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002355 default:
2356 printk("%s: connect returned unhandled error %d\n",
2357 __func__, status);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002358 /* fall through */
Trond Myklebustf75e6742009-04-21 17:18:20 -04002359 case -EADDRNOTAVAIL:
2360 /* We're probably in TIME_WAIT. Get rid of existing socket,
2361 * and retry
2362 */
Trond Myklebusta519fc72012-09-12 16:49:15 -04002363 xs_tcp_force_close(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002364 break;
Trond Myklebust2a491992009-03-11 14:38:00 -04002365 case 0:
2366 case -EINPROGRESS:
2367 case -EALREADY:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002368 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002369 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002370 case -EINVAL:
2371 /* Happens, for instance, if the user specified a link
2372 * local IPv6 address without a scope-id.
2373 */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002374 case -ECONNREFUSED:
2375 case -ECONNRESET:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002376 case -ENETDOWN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002377 case -ENETUNREACH:
Trond Myklebust4ba161a2017-11-24 12:00:24 -05002378 case -EHOSTUNREACH:
Trond Myklebust3913c782015-02-08 21:44:04 -05002379 case -EADDRINUSE:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002380 case -ENOBUFS:
NeilBrown6ea44ad2017-05-25 17:00:32 +10002381 /*
2382 * xs_tcp_force_close() wakes tasks with -EIO.
2383 * We need to wake them first to ensure the
2384 * correct error code.
2385 */
2386 xprt_wake_pending_tasks(xprt, status);
Trond Myklebust4efdd922015-02-08 15:34:28 -05002387 xs_tcp_force_close(xprt);
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002388 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002389 }
Trond Myklebust2a491992009-03-11 14:38:00 -04002390 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002391out:
Chuck Lever2226feb2005-08-11 16:25:38 -04002392 xprt_clear_connecting(xprt);
Trond Myklebustcf767852018-12-17 17:38:51 -05002393 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002394 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04002395}
2396
Trond Myklebust02910172016-08-04 00:00:33 -04002397static unsigned long xs_reconnect_delay(const struct rpc_xprt *xprt)
2398{
2399 unsigned long start, now = jiffies;
2400
2401 start = xprt->stat.connect_start + xprt->reestablish_timeout;
2402 if (time_after(start, now))
2403 return start - now;
2404 return 0;
2405}
2406
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002407static void xs_reconnect_backoff(struct rpc_xprt *xprt)
2408{
2409 xprt->reestablish_timeout <<= 1;
2410 if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
2411 xprt->reestablish_timeout = xprt->max_reconnect_timeout;
2412 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2413 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2414}
2415
Chuck Lever68e220b2007-08-06 11:57:48 -04002416/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002417 * xs_connect - connect a socket to a remote endpoint
Trond Myklebust1b092092013-01-08 09:26:49 -05002418 * @xprt: pointer to transport structure
Chuck Lever9903cd12005-08-11 16:25:26 -04002419 * @task: address of RPC task that manages state of connect request
2420 *
2421 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002422 *
2423 * UDP socket connects are synchronous, but we use a work queue anyway
2424 * to guarantee that even unprivileged user processes can set up a
2425 * socket on a privileged port.
2426 *
2427 * If a UDP socket connect fails, the delay behavior here prevents
2428 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002429 */
Trond Myklebust1b092092013-01-08 09:26:49 -05002430static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002431{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002432 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002433 unsigned long delay = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002434
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002435 WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2436
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002437 if (transport->sock != NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002438 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2439 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002440 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002441
2442 /* Start by resetting any existing state */
2443 xs_reset_transport(transport);
2444
Trond Myklebust02910172016-08-04 00:00:33 -04002445 delay = xs_reconnect_delay(xprt);
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002446 xs_reconnect_backoff(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002447
Trond Myklebust02910172016-08-04 00:00:33 -04002448 } else
Chuck Lever46121cf2007-01-31 12:14:08 -05002449 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002450
2451 queue_delayed_work(xprtiod_workqueue,
2452 &transport->connect_worker,
2453 delay);
Chuck Levera246b012005-08-11 16:25:23 -04002454}
2455
Chuck Lever262ca072006-03-20 13:44:16 -05002456/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002457 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2458 * @xprt: rpc_xprt struct containing statistics
2459 * @seq: output file
2460 *
2461 */
2462static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2463{
2464 long idle_time = 0;
2465
2466 if (xprt_connected(xprt))
2467 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2468
2469 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
Andy Adamson15a45202012-02-14 16:19:18 -05002470 "%llu %llu %lu %llu %llu\n",
Chuck Lever176e21e2011-05-09 15:22:44 -04002471 xprt->stat.bind_count,
2472 xprt->stat.connect_count,
Chuck Lever8440a882018-10-01 14:25:41 -04002473 xprt->stat.connect_time / HZ,
Chuck Lever176e21e2011-05-09 15:22:44 -04002474 idle_time,
2475 xprt->stat.sends,
2476 xprt->stat.recvs,
2477 xprt->stat.bad_xids,
2478 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002479 xprt->stat.bklog_u,
2480 xprt->stat.max_slots,
2481 xprt->stat.sending_u,
2482 xprt->stat.pending_u);
Chuck Lever176e21e2011-05-09 15:22:44 -04002483}
2484
2485/**
Chuck Lever262ca072006-03-20 13:44:16 -05002486 * xs_udp_print_stats - display UDP socket-specifc stats
2487 * @xprt: rpc_xprt struct containing statistics
2488 * @seq: output file
2489 *
2490 */
2491static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2492{
Chuck Leverc8475462006-12-05 16:35:26 -05002493 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2494
Andy Adamson15a45202012-02-14 16:19:18 -05002495 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2496 "%lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002497 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002498 xprt->stat.bind_count,
2499 xprt->stat.sends,
2500 xprt->stat.recvs,
2501 xprt->stat.bad_xids,
2502 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002503 xprt->stat.bklog_u,
2504 xprt->stat.max_slots,
2505 xprt->stat.sending_u,
2506 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002507}
2508
2509/**
2510 * xs_tcp_print_stats - display TCP socket-specifc stats
2511 * @xprt: rpc_xprt struct containing statistics
2512 * @seq: output file
2513 *
2514 */
2515static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2516{
Chuck Leverc8475462006-12-05 16:35:26 -05002517 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002518 long idle_time = 0;
2519
2520 if (xprt_connected(xprt))
2521 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2522
Andy Adamson15a45202012-02-14 16:19:18 -05002523 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2524 "%llu %llu %lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002525 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002526 xprt->stat.bind_count,
2527 xprt->stat.connect_count,
Chuck Lever8440a882018-10-01 14:25:41 -04002528 xprt->stat.connect_time / HZ,
Chuck Lever262ca072006-03-20 13:44:16 -05002529 idle_time,
2530 xprt->stat.sends,
2531 xprt->stat.recvs,
2532 xprt->stat.bad_xids,
2533 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002534 xprt->stat.bklog_u,
2535 xprt->stat.max_slots,
2536 xprt->stat.sending_u,
2537 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002538}
2539
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002540/*
2541 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2542 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2543 * to use the server side send routines.
2544 */
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002545static int bc_malloc(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002546{
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002547 struct rpc_rqst *rqst = task->tk_rqstp;
2548 size_t size = rqst->rq_callsize;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002549 struct page *page;
2550 struct rpc_buffer *buf;
2551
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002552 if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
2553 WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
2554 size);
2555 return -EINVAL;
2556 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002557
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002558 page = alloc_page(GFP_KERNEL);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002559 if (!page)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002560 return -ENOMEM;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002561
2562 buf = page_address(page);
2563 buf->len = PAGE_SIZE;
2564
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002565 rqst->rq_buffer = buf->data;
Jeff Layton18e601d2016-10-24 20:33:23 -04002566 rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002567 return 0;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002568}
2569
2570/*
2571 * Free the space allocated in the bc_alloc routine
2572 */
Chuck Lever3435c742016-09-15 10:55:29 -04002573static void bc_free(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002574{
Chuck Lever3435c742016-09-15 10:55:29 -04002575 void *buffer = task->tk_rqstp->rq_buffer;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002576 struct rpc_buffer *buf;
2577
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002578 buf = container_of(buffer, struct rpc_buffer, data);
2579 free_page((unsigned long)buf);
2580}
2581
2582/*
2583 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2584 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2585 */
2586static int bc_sendto(struct rpc_rqst *req)
2587{
2588 int len;
2589 struct xdr_buf *xbufp = &req->rq_snd_buf;
2590 struct rpc_xprt *xprt = req->rq_xprt;
2591 struct sock_xprt *transport =
2592 container_of(xprt, struct sock_xprt, xprt);
2593 struct socket *sock = transport->sock;
2594 unsigned long headoff;
2595 unsigned long tailoff;
2596
Chuck Lever61677ee2011-05-09 15:22:34 -04002597 xs_encode_stream_record_marker(xbufp);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002598
2599 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2600 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2601 len = svc_send_common(sock, xbufp,
2602 virt_to_page(xbufp->head[0].iov_base), headoff,
2603 xbufp->tail[0].iov_base, tailoff);
2604
2605 if (len != xbufp->len) {
2606 printk(KERN_NOTICE "Error sending entire callback!\n");
2607 len = -EAGAIN;
2608 }
2609
2610 return len;
2611}
2612
2613/*
2614 * The send routine. Borrows from svc_send
2615 */
Trond Myklebustadfa7142018-09-03 23:58:59 -04002616static int bc_send_request(struct rpc_rqst *req)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002617{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002618 struct svc_xprt *xprt;
Andrzej Hajda7fc56132015-09-24 16:00:09 +02002619 int len;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002620
2621 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2622 /*
2623 * Get the server socket associated with this callback xprt
2624 */
2625 xprt = req->rq_xprt->bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002626
2627 /*
2628 * Grab the mutex to serialize data as the connection is shared
2629 * with the fore channel
2630 */
Trond Myklebustc5445772018-09-03 23:39:27 -04002631 mutex_lock(&xprt->xpt_mutex);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002632 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2633 len = -ENOTCONN;
2634 else
2635 len = bc_sendto(req);
2636 mutex_unlock(&xprt->xpt_mutex);
2637
2638 if (len > 0)
2639 len = 0;
2640
2641 return len;
2642}
2643
2644/*
2645 * The close routine. Since this is client initiated, we do nothing
2646 */
2647
2648static void bc_close(struct rpc_xprt *xprt)
2649{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002650}
2651
2652/*
2653 * The xprt destroy routine. Again, because this connection is client
2654 * initiated, we do nothing
2655 */
2656
2657static void bc_destroy(struct rpc_xprt *xprt)
2658{
Kinglong Mee47f72ef2014-03-24 11:58:16 +08002659 dprintk("RPC: bc_destroy xprt %p\n", xprt);
2660
2661 xs_xprt_free(xprt);
2662 module_put(THIS_MODULE);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002663}
2664
Chuck Leverd31ae252017-08-01 12:00:39 -04002665static const struct rpc_xprt_ops xs_local_ops = {
Chuck Lever176e21e2011-05-09 15:22:44 -04002666 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebust4cd34e72018-08-31 10:00:02 -04002667 .release_xprt = xprt_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002668 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002669 .free_slot = xprt_free_slot,
Chuck Lever176e21e2011-05-09 15:22:44 -04002670 .rpcbind = xs_local_rpcbind,
2671 .set_port = xs_local_set_port,
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002672 .connect = xs_local_connect,
Chuck Lever176e21e2011-05-09 15:22:44 -04002673 .buf_alloc = rpc_malloc,
2674 .buf_free = rpc_free,
Trond Myklebust550aebf2018-09-14 14:32:45 -04002675 .prepare_request = xs_stream_prepare_request,
Chuck Lever176e21e2011-05-09 15:22:44 -04002676 .send_request = xs_local_send_request,
2677 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2678 .close = xs_close,
Trond Myklebusta1311d82013-10-31 09:18:49 -04002679 .destroy = xs_destroy,
Chuck Lever176e21e2011-05-09 15:22:44 -04002680 .print_stats = xs_local_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002681 .enable_swap = xs_enable_swap,
2682 .disable_swap = xs_disable_swap,
Chuck Lever176e21e2011-05-09 15:22:44 -04002683};
2684
Chuck Leverd31ae252017-08-01 12:00:39 -04002685static const struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002686 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002687 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002688 .release_xprt = xprt_release_xprt_cong,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002689 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002690 .free_slot = xprt_free_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002691 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002692 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002693 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002694 .buf_alloc = rpc_malloc,
2695 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002696 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002697 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002698 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002699 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002700 .close = xs_close,
2701 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002702 .print_stats = xs_udp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002703 .enable_swap = xs_enable_swap,
2704 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002705 .inject_disconnect = xs_inject_disconnect,
Chuck Lever262965f2005-08-11 16:25:56 -04002706};
2707
Chuck Leverd31ae252017-08-01 12:00:39 -04002708static const struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002709 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebust4cd34e72018-08-31 10:00:02 -04002710 .release_xprt = xprt_release_xprt,
Trond Myklebust36bd7de2018-09-03 18:41:32 -04002711 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002712 .free_slot = xprt_free_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002713 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002714 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002715 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002716 .buf_alloc = rpc_malloc,
2717 .buf_free = rpc_free,
Trond Myklebust277e4ab2018-09-14 09:49:06 -04002718 .prepare_request = xs_stream_prepare_request,
Chuck Lever262965f2005-08-11 16:25:56 -04002719 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002720 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustc627d312015-02-10 11:06:04 -05002721 .close = xs_tcp_shutdown,
Chuck Lever9903cd12005-08-11 16:25:26 -04002722 .destroy = xs_destroy,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002723 .set_connect_timeout = xs_tcp_set_connect_timeout,
Chuck Lever262ca072006-03-20 13:44:16 -05002724 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002725 .enable_swap = xs_enable_swap,
2726 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002727 .inject_disconnect = xs_inject_disconnect,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002728#ifdef CONFIG_SUNRPC_BACKCHANNEL
2729 .bc_setup = xprt_setup_bc,
Chuck Lever6b26cc82016-05-02 14:40:40 -04002730 .bc_maxpayload = xs_tcp_bc_maxpayload,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002731 .bc_free_rqst = xprt_free_bc_rqst,
2732 .bc_destroy = xprt_destroy_bc,
2733#endif
Chuck Levera246b012005-08-11 16:25:23 -04002734};
2735
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002736/*
2737 * The rpc_xprt_ops for the server backchannel
2738 */
2739
Chuck Leverd31ae252017-08-01 12:00:39 -04002740static const struct rpc_xprt_ops bc_tcp_ops = {
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002741 .reserve_xprt = xprt_reserve_xprt,
2742 .release_xprt = xprt_release_xprt,
Bryan Schumaker84e28a32012-09-24 13:39:01 -04002743 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002744 .free_slot = xprt_free_slot,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002745 .buf_alloc = bc_malloc,
2746 .buf_free = bc_free,
2747 .send_request = bc_send_request,
2748 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2749 .close = bc_close,
2750 .destroy = bc_destroy,
2751 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002752 .enable_swap = xs_enable_swap,
2753 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002754 .inject_disconnect = xs_inject_disconnect,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002755};
2756
Chuck Lever92476852010-10-20 11:53:01 -04002757static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2758{
2759 static const struct sockaddr_in sin = {
2760 .sin_family = AF_INET,
2761 .sin_addr.s_addr = htonl(INADDR_ANY),
2762 };
2763 static const struct sockaddr_in6 sin6 = {
2764 .sin6_family = AF_INET6,
2765 .sin6_addr = IN6ADDR_ANY_INIT,
2766 };
2767
2768 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002769 case AF_LOCAL:
2770 break;
Chuck Lever92476852010-10-20 11:53:01 -04002771 case AF_INET:
2772 memcpy(sap, &sin, sizeof(sin));
2773 break;
2774 case AF_INET6:
2775 memcpy(sap, &sin6, sizeof(sin6));
2776 break;
2777 default:
2778 dprintk("RPC: %s: Bad address family\n", __func__);
2779 return -EAFNOSUPPORT;
2780 }
2781 return 0;
2782}
2783
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002784static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002785 unsigned int slot_table_size,
2786 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002787{
2788 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002789 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002790
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002791 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002792 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002793 return ERR_PTR(-EBADF);
2794 }
2795
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002796 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2797 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002798 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002799 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2800 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002801 return ERR_PTR(-ENOMEM);
2802 }
2803
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002804 new = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002805 mutex_init(&new->recv_mutex);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002806 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2807 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002808 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002809 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002810 else {
2811 int err;
2812 err = xs_init_anyaddr(args->dstaddr->sa_family,
2813 (struct sockaddr *)&new->srcaddr);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002814 if (err != 0) {
2815 xprt_free(xprt);
Chuck Lever92476852010-10-20 11:53:01 -04002816 return ERR_PTR(err);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002817 }
Chuck Lever92476852010-10-20 11:53:01 -04002818 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002819
2820 return xprt;
2821}
2822
Chuck Lever176e21e2011-05-09 15:22:44 -04002823static const struct rpc_timeout xs_local_default_timeout = {
2824 .to_initval = 10 * HZ,
2825 .to_maxval = 10 * HZ,
2826 .to_retries = 2,
2827};
2828
2829/**
2830 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2831 * @args: rpc transport creation arguments
2832 *
2833 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2834 */
2835static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2836{
2837 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2838 struct sock_xprt *transport;
2839 struct rpc_xprt *xprt;
2840 struct rpc_xprt *ret;
2841
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002842 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2843 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002844 if (IS_ERR(xprt))
2845 return xprt;
2846 transport = container_of(xprt, struct sock_xprt, xprt);
2847
2848 xprt->prot = 0;
2849 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2850 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2851
2852 xprt->bind_timeout = XS_BIND_TO;
2853 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2854 xprt->idle_timeout = XS_IDLE_DISC_TO;
2855
2856 xprt->ops = &xs_local_ops;
2857 xprt->timeout = &xs_local_default_timeout;
2858
Trond Myklebust550aebf2018-09-14 14:32:45 -04002859 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
2860 INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket);
NeilBrown93dc41b2013-10-31 16:14:36 +11002861
Chuck Lever176e21e2011-05-09 15:22:44 -04002862 switch (sun->sun_family) {
2863 case AF_LOCAL:
2864 if (sun->sun_path[0] != '/') {
2865 dprintk("RPC: bad AF_LOCAL address: %s\n",
2866 sun->sun_path);
2867 ret = ERR_PTR(-EINVAL);
2868 goto out_err;
2869 }
2870 xprt_set_bound(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002871 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
J. Bruce Fields7073ea82013-02-21 10:14:22 -05002872 ret = ERR_PTR(xs_local_setup_socket(transport));
2873 if (ret)
2874 goto out_err;
Chuck Lever176e21e2011-05-09 15:22:44 -04002875 break;
2876 default:
2877 ret = ERR_PTR(-EAFNOSUPPORT);
2878 goto out_err;
2879 }
2880
2881 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2882 xprt->address_strings[RPC_DISPLAY_ADDR]);
2883
2884 if (try_module_get(THIS_MODULE))
2885 return xprt;
2886 ret = ERR_PTR(-EINVAL);
2887out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002888 xs_xprt_free(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002889 return ret;
2890}
2891
Trond Myklebust2881ae72007-12-20 16:03:54 -05002892static const struct rpc_timeout xs_udp_default_timeout = {
2893 .to_initval = 5 * HZ,
2894 .to_maxval = 30 * HZ,
2895 .to_increment = 5 * HZ,
2896 .to_retries = 5,
2897};
2898
Chuck Lever9903cd12005-08-11 16:25:26 -04002899/**
2900 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002901 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002902 *
2903 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002904static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002905{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002906 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002907 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002908 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002909 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002910
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002911 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2912 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002913 if (IS_ERR(xprt))
2914 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002915 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002916
Chuck Leverec739ef2006-08-22 20:06:15 -04002917 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002918 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002919 /* XXX: header size can vary due to auth type, IPv6, etc. */
2920 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2921
Chuck Lever03bf4b72005-08-25 16:25:55 -07002922 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002923 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2924 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002925
Chuck Lever262965f2005-08-11 16:25:56 -04002926 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002927
Trond Myklebustba7392b2007-12-20 16:03:55 -05002928 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002929
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002930 INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002931 INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2932
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002933 switch (addr->sa_family) {
2934 case AF_INET:
2935 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2936 xprt_set_bound(xprt);
2937
Chuck Lever9dc3b092009-08-09 15:09:46 -04002938 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002939 break;
2940 case AF_INET6:
2941 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2942 xprt_set_bound(xprt);
2943
Chuck Lever9dc3b092009-08-09 15:09:46 -04002944 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002945 break;
2946 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002947 ret = ERR_PTR(-EAFNOSUPPORT);
2948 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002949 }
2950
Chuck Leverc740eff2009-08-09 15:09:46 -04002951 if (xprt_bound(xprt))
2952 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2953 xprt->address_strings[RPC_DISPLAY_ADDR],
2954 xprt->address_strings[RPC_DISPLAY_PORT],
2955 xprt->address_strings[RPC_DISPLAY_PROTO]);
2956 else
2957 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2958 xprt->address_strings[RPC_DISPLAY_ADDR],
2959 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002960
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002961 if (try_module_get(THIS_MODULE))
2962 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002963 ret = ERR_PTR(-EINVAL);
2964out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002965 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002966 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002967}
2968
Trond Myklebust2881ae72007-12-20 16:03:54 -05002969static const struct rpc_timeout xs_tcp_default_timeout = {
2970 .to_initval = 60 * HZ,
2971 .to_maxval = 60 * HZ,
2972 .to_retries = 2,
2973};
2974
Chuck Lever9903cd12005-08-11 16:25:26 -04002975/**
2976 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002977 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002978 *
2979 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002980static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002981{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002982 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002983 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002984 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002985 struct rpc_xprt *ret;
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002986 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2987
2988 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2989 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Levera246b012005-08-11 16:25:23 -04002990
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002991 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002992 max_slot_table_size);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002993 if (IS_ERR(xprt))
2994 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002995 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002996
Chuck Leverec739ef2006-08-22 20:06:15 -04002997 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002998 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002999 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04003000
Chuck Lever03bf4b72005-08-25 16:25:55 -07003001 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07003002 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
3003 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04003004
Chuck Lever262965f2005-08-11 16:25:56 -04003005 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05003006 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04003007
Trond Myklebust3851f1c2016-08-04 00:08:45 -04003008 xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05003009 xprt->connect_timeout = xprt->timeout->to_initval *
3010 (xprt->timeout->to_retries + 1);
Trond Myklebust3851f1c2016-08-04 00:08:45 -04003011
Trond Myklebustc50b8ee2018-09-14 14:26:28 -04003012 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
Trond Myklebustedc1b012015-10-05 10:53:49 -04003013 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
3014
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003015 switch (addr->sa_family) {
3016 case AF_INET:
3017 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
3018 xprt_set_bound(xprt);
3019
Chuck Lever9dc3b092009-08-09 15:09:46 -04003020 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003021 break;
3022 case AF_INET6:
3023 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
3024 xprt_set_bound(xprt);
3025
Chuck Lever9dc3b092009-08-09 15:09:46 -04003026 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003027 break;
3028 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003029 ret = ERR_PTR(-EAFNOSUPPORT);
3030 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003031 }
3032
Chuck Leverc740eff2009-08-09 15:09:46 -04003033 if (xprt_bound(xprt))
3034 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]);
3038 else
3039 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
3040 xprt->address_strings[RPC_DISPLAY_ADDR],
3041 xprt->address_strings[RPC_DISPLAY_PROTO]);
3042
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003043 if (try_module_get(THIS_MODULE))
3044 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003045 ret = ERR_PTR(-EINVAL);
3046out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003047 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003048 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04003049}
Chuck Lever282b32e2006-12-05 16:35:51 -05003050
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003051/**
3052 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3053 * @args: rpc transport creation arguments
3054 *
3055 */
3056static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
3057{
3058 struct sockaddr *addr = args->dstaddr;
3059 struct rpc_xprt *xprt;
3060 struct sock_xprt *transport;
3061 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003062 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003063
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003064 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3065 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003066 if (IS_ERR(xprt))
3067 return xprt;
3068 transport = container_of(xprt, struct sock_xprt, xprt);
3069
3070 xprt->prot = IPPROTO_TCP;
3071 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
3072 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3073 xprt->timeout = &xs_tcp_default_timeout;
3074
3075 /* backchannel */
3076 xprt_set_bound(xprt);
3077 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003078 xprt->reestablish_timeout = 0;
3079 xprt->idle_timeout = 0;
3080
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003081 xprt->ops = &bc_tcp_ops;
3082
3083 switch (addr->sa_family) {
3084 case AF_INET:
3085 xs_format_peer_addresses(xprt, "tcp",
3086 RPCBIND_NETID_TCP);
3087 break;
3088 case AF_INET6:
3089 xs_format_peer_addresses(xprt, "tcp",
3090 RPCBIND_NETID_TCP6);
3091 break;
3092 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003093 ret = ERR_PTR(-EAFNOSUPPORT);
3094 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003095 }
3096
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04003097 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3098 xprt->address_strings[RPC_DISPLAY_ADDR],
3099 xprt->address_strings[RPC_DISPLAY_PORT],
3100 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003101
3102 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003103 * Once we've associated a backchannel xprt with a connection,
Weng Meiling28303ca2013-11-30 17:56:44 +08003104 * we want to keep it around as long as the connection lasts,
3105 * in case we need to start using it for a backchannel again;
3106 * this reference won't be dropped until bc_xprt is destroyed.
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003107 */
3108 xprt_get(xprt);
3109 args->bc_xprt->xpt_bc_xprt = xprt;
3110 xprt->bc_xprt = args->bc_xprt;
3111 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
3112 transport->sock = bc_sock->sk_sock;
3113 transport->inet = bc_sock->sk_sk;
3114
3115 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003116 * Since we don't want connections for the backchannel, we set
3117 * the xprt status to connected
3118 */
3119 xprt_set_connected(xprt);
3120
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003121 if (try_module_get(THIS_MODULE))
3122 return xprt;
Kinglong Mee642aab52014-03-24 12:00:28 +08003123
3124 args->bc_xprt->xpt_bc_xprt = NULL;
J. Bruce Fields39a9bea2016-05-17 12:38:21 -04003125 args->bc_xprt->xpt_bc_xps = NULL;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003126 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003127 ret = ERR_PTR(-EINVAL);
3128out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003129 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003130 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003131}
3132
Chuck Lever176e21e2011-05-09 15:22:44 -04003133static struct xprt_class xs_local_transport = {
3134 .list = LIST_HEAD_INIT(xs_local_transport.list),
3135 .name = "named UNIX socket",
3136 .owner = THIS_MODULE,
3137 .ident = XPRT_TRANSPORT_LOCAL,
3138 .setup = xs_setup_local,
3139};
3140
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003141static struct xprt_class xs_udp_transport = {
3142 .list = LIST_HEAD_INIT(xs_udp_transport.list),
3143 .name = "udp",
3144 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003145 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003146 .setup = xs_setup_udp,
3147};
3148
3149static struct xprt_class xs_tcp_transport = {
3150 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
3151 .name = "tcp",
3152 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003153 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003154 .setup = xs_setup_tcp,
3155};
3156
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003157static struct xprt_class xs_bc_tcp_transport = {
3158 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3159 .name = "tcp NFSv4.1 backchannel",
3160 .owner = THIS_MODULE,
3161 .ident = XPRT_TRANSPORT_BC_TCP,
3162 .setup = xs_setup_bc_tcp,
3163};
3164
Chuck Lever282b32e2006-12-05 16:35:51 -05003165/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003166 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05003167 *
3168 */
3169int init_socket_xprt(void)
3170{
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08003171 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08003172 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05003173
Chuck Lever176e21e2011-05-09 15:22:44 -04003174 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003175 xprt_register_transport(&xs_udp_transport);
3176 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003177 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003178
Chuck Lever282b32e2006-12-05 16:35:51 -05003179 return 0;
3180}
3181
3182/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003183 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05003184 *
3185 */
3186void cleanup_socket_xprt(void)
3187{
Chuck Leverfbf76682006-12-05 16:35:54 -05003188 if (sunrpc_table_header) {
3189 unregister_sysctl_table(sunrpc_table_header);
3190 sunrpc_table_header = NULL;
3191 }
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003192
Chuck Lever176e21e2011-05-09 15:22:44 -04003193 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003194 xprt_unregister_transport(&xs_udp_transport);
3195 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003196 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05003197}
Trond Myklebustcbf11072009-08-09 15:06:19 -04003198
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003199static int param_set_uint_minmax(const char *val,
3200 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003201 unsigned int min, unsigned int max)
3202{
Daniel Walter00cfaa92014-06-21 13:06:38 +01003203 unsigned int num;
Trond Myklebustcbf11072009-08-09 15:06:19 -04003204 int ret;
3205
3206 if (!val)
3207 return -EINVAL;
Daniel Walter00cfaa92014-06-21 13:06:38 +01003208 ret = kstrtouint(val, 0, &num);
Dan Carpenter9761a242017-02-19 00:34:59 +03003209 if (ret)
3210 return ret;
3211 if (num < min || num > max)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003212 return -EINVAL;
3213 *((unsigned int *)kp->arg) = num;
3214 return 0;
3215}
3216
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003217static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003218{
Frank Sorensonffb6ca32016-07-08 16:35:25 -05003219 return param_set_uint_minmax(val, kp,
J. Bruce Fields826799e2018-10-18 15:27:02 -04003220 RPC_MIN_RESVPORT,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003221 RPC_MAX_RESVPORT);
3222}
3223
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303224static const struct kernel_param_ops param_ops_portnr = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003225 .set = param_set_portnr,
3226 .get = param_get_uint,
3227};
3228
Trond Myklebustcbf11072009-08-09 15:06:19 -04003229#define param_check_portnr(name, p) \
3230 __param_check(name, p, unsigned int);
3231
3232module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3233module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3234
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003235static int param_set_slot_table_size(const char *val,
3236 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003237{
3238 return param_set_uint_minmax(val, kp,
3239 RPC_MIN_SLOT_TABLE,
3240 RPC_MAX_SLOT_TABLE);
3241}
3242
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303243static const struct kernel_param_ops param_ops_slot_table_size = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003244 .set = param_set_slot_table_size,
3245 .get = param_get_uint,
3246};
3247
Trond Myklebustcbf11072009-08-09 15:06:19 -04003248#define param_check_slot_table_size(name, p) \
3249 __param_check(name, p, unsigned int);
3250
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003251static int param_set_max_slot_table_size(const char *val,
3252 const struct kernel_param *kp)
3253{
3254 return param_set_uint_minmax(val, kp,
3255 RPC_MIN_SLOT_TABLE,
3256 RPC_MAX_SLOT_TABLE_LIMIT);
3257}
3258
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303259static const struct kernel_param_ops param_ops_max_slot_table_size = {
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003260 .set = param_set_max_slot_table_size,
3261 .get = param_get_uint,
3262};
3263
3264#define param_check_max_slot_table_size(name, p) \
3265 __param_check(name, p, unsigned int);
3266
Trond Myklebustcbf11072009-08-09 15:06:19 -04003267module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3268 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003269module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3270 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003271module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3272 slot_table_size, 0644);