blob: 70e52f567b2a2c84e7a33787df3296c3ae23e0cc [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),
Trond Myklebust12a3ad62019-03-02 10:14:02 -0500426 GFP_KERNEL);
Trond Myklebust16e5e902018-12-02 15:22:48 -0500427 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 Myklebust9734ad52019-03-15 12:47:34 -0400456 } else if (offset < seek_init)
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;
Alakesh Haloi01f2f5b2019-03-26 02:00:01 +0000498 size_t want, uninitialized_var(read);
499 ssize_t uninitialized_var(ret);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400500
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 }
Trond Myklebust45835a62019-09-12 08:04:25 -0400565 if (transport->recv.copied && !req->rq_private_buf.len)
566 return -ESHUTDOWN;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400567
568 ret = xs_read_stream_request(transport, msg, flags, req);
569 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
Trond Myklebustc4433052018-12-04 07:52:11 -0500570 xprt_complete_bc_request(req, transport->recv.copied);
Trond Myklebust45835a62019-09-12 08:04:25 -0400571 else
572 req->rq_private_buf.len = transport->recv.copied;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400573
574 return ret;
575}
576#else /* CONFIG_SUNRPC_BACKCHANNEL */
577static ssize_t
578xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
579{
580 return -ESHUTDOWN;
581}
582#endif /* CONFIG_SUNRPC_BACKCHANNEL */
583
584static ssize_t
585xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags)
586{
587 struct rpc_xprt *xprt = &transport->xprt;
588 struct rpc_rqst *req;
589 ssize_t ret = 0;
590
591 /* Look up and lock the request corresponding to the given XID */
592 spin_lock(&xprt->queue_lock);
593 req = xprt_lookup_rqst(xprt, transport->recv.xid);
Trond Myklebust45835a62019-09-12 08:04:25 -0400594 if (!req || (transport->recv.copied && !req->rq_private_buf.len)) {
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400595 msg->msg_flags |= MSG_TRUNC;
596 goto out;
597 }
598 xprt_pin_rqst(req);
599 spin_unlock(&xprt->queue_lock);
600
601 ret = xs_read_stream_request(transport, msg, flags, req);
602
603 spin_lock(&xprt->queue_lock);
604 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
Trond Myklebustc4433052018-12-04 07:52:11 -0500605 xprt_complete_rqst(req->rq_task, transport->recv.copied);
Trond Myklebust45835a62019-09-12 08:04:25 -0400606 else
607 req->rq_private_buf.len = transport->recv.copied;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400608 xprt_unpin_rqst(req);
609out:
610 spin_unlock(&xprt->queue_lock);
611 return ret;
612}
613
614static ssize_t
615xs_read_stream(struct sock_xprt *transport, int flags)
616{
617 struct msghdr msg = { 0 };
618 size_t want, read = 0;
619 ssize_t ret = 0;
620
621 if (transport->recv.len == 0) {
622 want = xs_read_stream_headersize(transport->recv.copied != 0);
623 ret = xs_read_stream_header(transport, &msg, flags, want,
624 transport->recv.offset);
625 if (ret <= 0)
626 goto out_err;
627 transport->recv.offset = ret;
Trond Myklebustc4433052018-12-04 07:52:11 -0500628 if (transport->recv.offset != want)
629 return transport->recv.offset;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400630 transport->recv.len = be32_to_cpu(transport->recv.fraghdr) &
631 RPC_FRAGMENT_SIZE_MASK;
632 transport->recv.offset -= sizeof(transport->recv.fraghdr);
633 read = ret;
634 }
635
636 switch (be32_to_cpu(transport->recv.calldir)) {
Trond Myklebustc4433052018-12-04 07:52:11 -0500637 default:
638 msg.msg_flags |= MSG_TRUNC;
639 break;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400640 case RPC_CALL:
641 ret = xs_read_stream_call(transport, &msg, flags);
642 break;
643 case RPC_REPLY:
644 ret = xs_read_stream_reply(transport, &msg, flags);
645 }
646 if (msg.msg_flags & MSG_TRUNC) {
647 transport->recv.calldir = cpu_to_be32(-1);
648 transport->recv.copied = -1;
649 }
650 if (ret < 0)
651 goto out_err;
652 read += ret;
653 if (transport->recv.offset < transport->recv.len) {
Trond Myklebustc4433052018-12-04 07:52:11 -0500654 if (!(msg.msg_flags & MSG_TRUNC))
655 return read;
Trond Myklebustb76a5af2018-12-03 15:58:58 -0500656 msg.msg_flags = 0;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400657 ret = xs_read_discard(transport->sock, &msg, flags,
658 transport->recv.len - transport->recv.offset);
659 if (ret <= 0)
660 goto out_err;
661 transport->recv.offset += ret;
662 read += ret;
663 if (transport->recv.offset != transport->recv.len)
Trond Myklebustc4433052018-12-04 07:52:11 -0500664 return read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400665 }
666 if (xs_read_stream_request_done(transport)) {
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400667 trace_xs_stream_read_request(transport);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400668 transport->recv.copied = 0;
669 }
670 transport->recv.offset = 0;
671 transport->recv.len = 0;
672 return read;
673out_err:
Trond Myklebust794628572018-12-03 18:49:00 -0500674 return ret != 0 ? ret : -ESHUTDOWN;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400675}
676
Trond Myklebust0ffe86f2019-01-30 14:51:26 -0500677static __poll_t xs_poll_socket(struct sock_xprt *transport)
678{
Trond Myklebusta73881c2019-02-26 06:33:02 -0500679 return transport->sock->ops->poll(transport->file, transport->sock,
680 NULL);
Trond Myklebust0ffe86f2019-01-30 14:51:26 -0500681}
682
683static bool xs_poll_socket_readable(struct sock_xprt *transport)
684{
685 __poll_t events = xs_poll_socket(transport);
686
687 return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP);
688}
689
690static void xs_poll_check_readable(struct sock_xprt *transport)
691{
692
693 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
694 if (!xs_poll_socket_readable(transport))
695 return;
696 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
697 queue_work(xprtiod_workqueue, &transport->recv_worker);
698}
699
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400700static void xs_stream_data_receive(struct sock_xprt *transport)
701{
702 size_t read = 0;
703 ssize_t ret = 0;
704
705 mutex_lock(&transport->recv_mutex);
706 if (transport->sock == NULL)
707 goto out;
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400708 for (;;) {
709 ret = xs_read_stream(transport, MSG_DONTWAIT);
Trond Myklebustc4433052018-12-04 07:52:11 -0500710 if (ret < 0)
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400711 break;
712 read += ret;
713 cond_resched();
714 }
Trond Myklebust5f52a9d2019-02-16 09:31:47 -0500715 if (ret == -ESHUTDOWN)
716 kernel_sock_shutdown(transport->sock, SHUT_RDWR);
717 else
718 xs_poll_check_readable(transport);
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400719out:
720 mutex_unlock(&transport->recv_mutex);
721 trace_xs_stream_read_data(&transport->xprt, ret, read);
722}
723
724static void xs_stream_data_receive_workfn(struct work_struct *work)
725{
726 struct sock_xprt *transport =
727 container_of(work, struct sock_xprt, recv_worker);
Trond Myklebusta1231fd2019-02-18 10:02:29 -0500728 unsigned int pflags = memalloc_nofs_save();
729
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400730 xs_stream_data_receive(transport);
Trond Myklebusta1231fd2019-02-18 10:02:29 -0500731 memalloc_nofs_restore(pflags);
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400732}
733
Trond Myklebust550aebf2018-09-14 14:32:45 -0400734static void
735xs_stream_reset_connect(struct sock_xprt *transport)
736{
737 transport->recv.offset = 0;
738 transport->recv.len = 0;
739 transport->recv.copied = 0;
740 transport->xmit.offset = 0;
Trond Myklebustae053552019-02-20 14:56:20 -0500741}
742
743static void
744xs_stream_start_connect(struct sock_xprt *transport)
745{
Trond Myklebust550aebf2018-09-14 14:32:45 -0400746 transport->xprt.stat.connect_count++;
747 transport->xprt.stat.connect_start = jiffies;
748}
749
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400750#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
751
Trond Myklebuste791f8e2019-02-19 12:39:40 -0500752static int xs_sendmsg(struct socket *sock, struct msghdr *msg, size_t seek)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400753{
Trond Myklebuste791f8e2019-02-19 12:39:40 -0500754 if (seek)
755 iov_iter_advance(&msg->msg_iter, seek);
756 return sock_sendmsg(sock, msg);
757}
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400758
Trond Myklebuste791f8e2019-02-19 12:39:40 -0500759static int xs_send_kvec(struct socket *sock, struct msghdr *msg, struct kvec *vec, size_t seek)
760{
Trond Myklebuste791f8e2019-02-19 12:39:40 -0500761 iov_iter_kvec(&msg->msg_iter, WRITE, vec, 1, vec->iov_len);
762 return xs_sendmsg(sock, msg, seek);
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400763}
764
Trond Myklebust0472e472019-02-19 13:00:13 -0500765static int xs_send_pagedata(struct socket *sock, struct msghdr *msg, struct xdr_buf *xdr, size_t base)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400766{
Jason Baronf279cd02014-09-24 18:08:00 +0000767 int err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400768
Trond Myklebust0472e472019-02-19 13:00:13 -0500769 err = xdr_alloc_bvec(xdr, GFP_KERNEL);
770 if (err < 0)
771 return err;
Trond Myklebust24c56842006-10-17 15:06:22 -0400772
Trond Myklebust0472e472019-02-19 13:00:13 -0500773 iov_iter_bvec(&msg->msg_iter, WRITE, xdr->bvec,
774 xdr_buf_pagecount(xdr),
775 xdr->page_len + xdr->page_base);
776 return xs_sendmsg(sock, msg, base + xdr->page_base);
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400777}
778
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500779#define xs_record_marker_len() sizeof(rpc_fraghdr)
780
Chuck Lever067fb112019-02-11 11:24:37 -0500781/* Common case:
782 * - stream transport
783 * - sending from byte 0 of the message
784 * - the message is wholly contained in @xdr's head iovec
785 */
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500786static int xs_send_rm_and_kvec(struct socket *sock, struct msghdr *msg,
787 rpc_fraghdr marker, struct kvec *vec, size_t base)
Chuck Lever067fb112019-02-11 11:24:37 -0500788{
Chuck Lever067fb112019-02-11 11:24:37 -0500789 struct kvec iov[2] = {
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500790 [0] = {
Chuck Lever067fb112019-02-11 11:24:37 -0500791 .iov_base = &marker,
792 .iov_len = sizeof(marker)
793 },
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500794 [1] = *vec,
Chuck Lever067fb112019-02-11 11:24:37 -0500795 };
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500796 size_t len = iov[0].iov_len + iov[1].iov_len;
Chuck Lever067fb112019-02-11 11:24:37 -0500797
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500798 iov_iter_kvec(&msg->msg_iter, WRITE, iov, 2, len);
799 return xs_sendmsg(sock, msg, base);
Chuck Levera246b012005-08-11 16:25:23 -0400800}
801
Chuck Lever9903cd12005-08-11 16:25:26 -0400802/**
803 * xs_sendpages - write pages directly to a socket
804 * @sock: socket to send on
805 * @addr: UDP only -- address of destination
806 * @addrlen: UDP only -- length of destination address
807 * @xdr: buffer containing this request
808 * @base: starting position in the buffer
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500809 * @rm: stream record marker field
Jason Baronf279cd02014-09-24 18:08:00 +0000810 * @sent_p: return the total number of bytes successfully queued for sending
Chuck Lever9903cd12005-08-11 16:25:26 -0400811 *
Chuck Levera246b012005-08-11 16:25:23 -0400812 */
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500813static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, rpc_fraghdr rm, int *sent_p)
Chuck Levera246b012005-08-11 16:25:23 -0400814{
Trond Myklebuste791f8e2019-02-19 12:39:40 -0500815 struct msghdr msg = {
816 .msg_name = addr,
817 .msg_namelen = addrlen,
818 .msg_flags = XS_SENDMSG_FLAGS | MSG_MORE,
819 };
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500820 unsigned int rmsize = rm ? sizeof(rm) : 0;
821 unsigned int remainder = rmsize + xdr->len - base;
822 unsigned int want;
Jason Baronf279cd02014-09-24 18:08:00 +0000823 int err = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400824
Chuck Lever262965f2005-08-11 16:25:56 -0400825 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400826 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400827
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500828 want = xdr->head[0].iov_len + rmsize;
829 if (base < want) {
830 unsigned int len = want - base;
Trond Myklebust24c56842006-10-17 15:06:22 -0400831 remainder -= len;
Trond Myklebuste791f8e2019-02-19 12:39:40 -0500832 if (remainder == 0)
833 msg.msg_flags &= ~MSG_MORE;
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500834 if (rmsize)
835 err = xs_send_rm_and_kvec(sock, &msg, rm,
836 &xdr->head[0], base);
Chuck Lever067fb112019-02-11 11:24:37 -0500837 else
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500838 err = xs_send_kvec(sock, &msg, &xdr->head[0], base);
Trond Myklebust24c56842006-10-17 15:06:22 -0400839 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400840 goto out;
Jason Baronf279cd02014-09-24 18:08:00 +0000841 *sent_p += err;
Chuck Levera246b012005-08-11 16:25:23 -0400842 base = 0;
843 } else
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500844 base -= want;
Chuck Levera246b012005-08-11 16:25:23 -0400845
Trond Myklebust24c56842006-10-17 15:06:22 -0400846 if (base < xdr->page_len) {
847 unsigned int len = xdr->page_len - base;
848 remainder -= len;
Trond Myklebust0472e472019-02-19 13:00:13 -0500849 if (remainder == 0)
850 msg.msg_flags &= ~MSG_MORE;
851 err = xs_send_pagedata(sock, &msg, xdr, base);
852 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400853 goto out;
Trond Myklebust0472e472019-02-19 13:00:13 -0500854 *sent_p += err;
Chuck Levera246b012005-08-11 16:25:23 -0400855 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400856 } else
857 base -= xdr->page_len;
858
859 if (base >= xdr->tail[0].iov_len)
Jason Baronf279cd02014-09-24 18:08:00 +0000860 return 0;
Trond Myklebuste791f8e2019-02-19 12:39:40 -0500861 msg.msg_flags &= ~MSG_MORE;
862 err = xs_send_kvec(sock, &msg, &xdr->tail[0], base);
Chuck Levera246b012005-08-11 16:25:23 -0400863out:
Jason Baronf279cd02014-09-24 18:08:00 +0000864 if (err > 0) {
865 *sent_p += err;
866 err = 0;
867 }
868 return err;
Chuck Levera246b012005-08-11 16:25:23 -0400869}
870
Chuck Lever9903cd12005-08-11 16:25:26 -0400871/**
Trond Myklebustc5445772018-09-03 23:39:27 -0400872 * xs_nospace - handle transmit was incomplete
Trond Myklebust50f484e2018-08-30 13:27:29 -0400873 * @req: pointer to RPC request
Chuck Lever9903cd12005-08-11 16:25:26 -0400874 *
Chuck Levera246b012005-08-11 16:25:23 -0400875 */
Trond Myklebustc5445772018-09-03 23:39:27 -0400876static int xs_nospace(struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -0400877{
Chuck Lever262965f2005-08-11 16:25:56 -0400878 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500879 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500880 struct sock *sk = transport->inet;
Trond Myklebust24ca9a82011-11-22 14:44:28 +0200881 int ret = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400882
Chuck Lever46121cf2007-01-31 12:14:08 -0500883 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Trond Myklebustc5445772018-09-03 23:39:27 -0400884 req->rq_task->tk_pid,
885 req->rq_slen - transport->xmit.offset,
Chuck Lever262965f2005-08-11 16:25:56 -0400886 req->rq_slen);
887
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400888 /* Protect against races with write_space */
Trond Myklebustb5e92412019-05-02 11:21:08 -0400889 spin_lock(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400890
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400891 /* Don't race with disconnect */
892 if (xprt_connected(xprt)) {
Trond Myklebust13331a52016-01-06 08:57:06 -0500893 /* wait for more buffer space */
894 sk->sk_write_pending++;
Trond Myklebustc5445772018-09-03 23:39:27 -0400895 xprt_wait_for_buffer_space(xprt);
Trond Myklebust13331a52016-01-06 08:57:06 -0500896 } else
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400897 ret = -ENOTCONN;
Chuck Lever262965f2005-08-11 16:25:56 -0400898
Trond Myklebustb5e92412019-05-02 11:21:08 -0400899 spin_unlock(&xprt->transport_lock);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500900
901 /* Race breaker in case memory is freed before above code is called */
David Vrabeld48f9ce2016-09-19 13:58:30 +0100902 if (ret == -EAGAIN) {
903 struct socket_wq *wq;
904
905 rcu_read_lock();
906 wq = rcu_dereference(sk->sk_wq);
907 set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags);
908 rcu_read_unlock();
909
910 sk->sk_write_space(sk);
911 }
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400912 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400913}
914
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400915static void
916xs_stream_prepare_request(struct rpc_rqst *req)
917{
Trond Myklebust75369082019-07-17 21:22:38 -0400918 xdr_free_bvec(&req->rq_rcv_buf);
Trond Myklebust12a3ad62019-03-02 10:14:02 -0500919 req->rq_task->tk_status = xdr_alloc_bvec(&req->rq_rcv_buf, GFP_KERNEL);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400920}
921
Chuck Lever61677ee2011-05-09 15:22:34 -0400922/*
Trond Myklebust4cd34e72018-08-31 10:00:02 -0400923 * Determine if the previous message in the stream was aborted before it
924 * could complete transmission.
925 */
926static bool
927xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
928{
929 return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
930}
931
932/*
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500933 * Return the stream record marker field for a record of length < 2^31-1
Chuck Lever61677ee2011-05-09 15:22:34 -0400934 */
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500935static rpc_fraghdr
936xs_stream_record_marker(struct xdr_buf *xdr)
Chuck Lever61677ee2011-05-09 15:22:34 -0400937{
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500938 if (!xdr->len)
939 return 0;
940 return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len);
Chuck Lever61677ee2011-05-09 15:22:34 -0400941}
942
Chuck Lever262965f2005-08-11 16:25:56 -0400943/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400944 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
Trond Myklebust50f484e2018-08-30 13:27:29 -0400945 * @req: pointer to RPC request
Chuck Lever176e21e2011-05-09 15:22:44 -0400946 *
947 * Return values:
948 * 0: The request has been sent
949 * EAGAIN: The socket was blocked, please call again later to
950 * complete the request
951 * ENOTCONN: Caller needs to invoke connect logic then call again
952 * other: Some other error occured, the request was not sent
953 */
Trond Myklebustadfa7142018-09-03 23:58:59 -0400954static int xs_local_send_request(struct rpc_rqst *req)
Chuck Lever176e21e2011-05-09 15:22:44 -0400955{
Chuck Lever176e21e2011-05-09 15:22:44 -0400956 struct rpc_xprt *xprt = req->rq_xprt;
957 struct sock_xprt *transport =
958 container_of(xprt, struct sock_xprt, xprt);
959 struct xdr_buf *xdr = &req->rq_snd_buf;
Trond Myklebust7e3d3622019-06-24 19:15:44 -0400960 rpc_fraghdr rm = xs_stream_record_marker(xdr);
961 unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
Chuck Lever176e21e2011-05-09 15:22:44 -0400962 int status;
Jason Baronf279cd02014-09-24 18:08:00 +0000963 int sent = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -0400964
Trond Myklebust4cd34e72018-08-31 10:00:02 -0400965 /* Close the stream if the previous transmission was incomplete */
966 if (xs_send_request_was_aborted(transport, req)) {
967 xs_close(xprt);
968 return -ENOTCONN;
969 }
970
Chuck Lever176e21e2011-05-09 15:22:44 -0400971 xs_pktdump("packet data:",
972 req->rq_svec->iov_base, req->rq_svec->iov_len);
973
Chuck Lever78215752018-03-05 15:13:07 -0500974 req->rq_xtime = ktime_get();
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400975 status = xs_sendpages(transport->sock, NULL, 0, xdr,
Trond Myklebust7e3d3622019-06-24 19:15:44 -0400976 transport->xmit.offset, rm, &sent);
Chuck Lever176e21e2011-05-09 15:22:44 -0400977 dprintk("RPC: %s(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400978 __func__, xdr->len - transport->xmit.offset, status);
NeilBrown743c69e2015-07-27 10:55:35 +1000979
980 if (status == -EAGAIN && sock_writeable(transport->inet))
981 status = -ENOBUFS;
982
Jason Baronf279cd02014-09-24 18:08:00 +0000983 if (likely(sent > 0) || status == 0) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400984 transport->xmit.offset += sent;
985 req->rq_bytes_sent = transport->xmit.offset;
Trond Myklebust7e3d3622019-06-24 19:15:44 -0400986 if (likely(req->rq_bytes_sent >= msglen)) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400987 req->rq_xmit_bytes_sent += transport->xmit.offset;
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400988 transport->xmit.offset = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -0400989 return 0;
990 }
991 status = -EAGAIN;
992 }
993
994 switch (status) {
Trond Myklebust3601c4a2014-06-30 13:42:19 -0400995 case -ENOBUFS:
Trond Myklebustb5872f02015-07-03 09:32:23 -0400996 break;
Chuck Lever176e21e2011-05-09 15:22:44 -0400997 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -0400998 status = xs_nospace(req);
Chuck Lever176e21e2011-05-09 15:22:44 -0400999 break;
1000 default:
1001 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1002 -status);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001003 /* fall through */
Chuck Lever176e21e2011-05-09 15:22:44 -04001004 case -EPIPE:
1005 xs_close(xprt);
1006 status = -ENOTCONN;
1007 }
1008
1009 return status;
1010}
1011
1012/**
Chuck Lever262965f2005-08-11 16:25:56 -04001013 * xs_udp_send_request - write an RPC request to a UDP socket
Trond Myklebust50f484e2018-08-30 13:27:29 -04001014 * @req: pointer to RPC request
Chuck Lever262965f2005-08-11 16:25:56 -04001015 *
1016 * Return values:
1017 * 0: The request has been sent
1018 * EAGAIN: The socket was blocked, please call again later to
1019 * complete the request
1020 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001021 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -04001022 */
Trond Myklebustadfa7142018-09-03 23:58:59 -04001023static int xs_udp_send_request(struct rpc_rqst *req)
Chuck Lever262965f2005-08-11 16:25:56 -04001024{
Chuck Lever262965f2005-08-11 16:25:56 -04001025 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001026 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -04001027 struct xdr_buf *xdr = &req->rq_snd_buf;
Jason Baronf279cd02014-09-24 18:08:00 +00001028 int sent = 0;
Chuck Lever262965f2005-08-11 16:25:56 -04001029 int status;
Chuck Levera246b012005-08-11 16:25:23 -04001030
Chuck Lever9903cd12005-08-11 16:25:26 -04001031 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -04001032 req->rq_svec->iov_base,
1033 req->rq_svec->iov_len);
1034
Trond Myklebust01d37c42009-03-11 14:09:39 -04001035 if (!xprt_bound(xprt))
1036 return -ENOTCONN;
Trond Myklebust75891f52018-09-03 17:37:36 -04001037
1038 if (!xprt_request_get_cong(xprt, req))
1039 return -EBADSLT;
1040
Chuck Lever78215752018-03-05 15:13:07 -05001041 req->rq_xtime = ktime_get();
Jason Baronf279cd02014-09-24 18:08:00 +00001042 status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen,
Trond Myklebust06b5fc32019-02-25 08:39:26 -05001043 xdr, 0, 0, &sent);
Chuck Levera246b012005-08-11 16:25:23 -04001044
Chuck Lever46121cf2007-01-31 12:14:08 -05001045 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001046 xdr->len, status);
Chuck Levera246b012005-08-11 16:25:23 -04001047
Jason Baron3dedbb52014-09-24 18:08:04 +00001048 /* firewall is blocking us, don't return -EAGAIN or we end up looping */
1049 if (status == -EPERM)
1050 goto process_status;
1051
NeilBrown743c69e2015-07-27 10:55:35 +10001052 if (status == -EAGAIN && sock_writeable(transport->inet))
1053 status = -ENOBUFS;
1054
Jason Baronf279cd02014-09-24 18:08:00 +00001055 if (sent > 0 || status == 0) {
1056 req->rq_xmit_bytes_sent += sent;
1057 if (sent >= req->rq_slen)
Trond Myklebust21997002007-10-01 11:43:37 -04001058 return 0;
1059 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -04001060 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -04001061 }
Chuck Levera246b012005-08-11 16:25:23 -04001062
Jason Baron3dedbb52014-09-24 18:08:04 +00001063process_status:
Chuck Lever262965f2005-08-11 16:25:56 -04001064 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -04001065 case -ENOTSOCK:
1066 status = -ENOTCONN;
1067 /* Should we call xs_close() here? */
1068 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -04001069 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -04001070 status = xs_nospace(req);
Trond Myklebustb6ddf642008-04-17 18:52:19 -04001071 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001072 case -ENETUNREACH:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001073 case -ENOBUFS:
Chuck Lever262965f2005-08-11 16:25:56 -04001074 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -04001075 case -ECONNREFUSED:
Jason Baron3dedbb52014-09-24 18:08:04 +00001076 case -EPERM:
Chuck Levera246b012005-08-11 16:25:23 -04001077 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -04001078 * prompts ECONNREFUSED. */
Trond Myklebust13331a52016-01-06 08:57:06 -05001079 break;
1080 default:
1081 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1082 -status);
Chuck Levera246b012005-08-11 16:25:23 -04001083 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +08001084
Chuck Lever262965f2005-08-11 16:25:56 -04001085 return status;
Chuck Levera246b012005-08-11 16:25:23 -04001086}
1087
Trond Myklebuste06799f2007-11-05 15:44:12 -05001088/**
Chuck Lever262965f2005-08-11 16:25:56 -04001089 * xs_tcp_send_request - write an RPC request to a TCP socket
Trond Myklebust50f484e2018-08-30 13:27:29 -04001090 * @req: pointer to RPC request
Chuck Lever9903cd12005-08-11 16:25:26 -04001091 *
1092 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -04001093 * 0: The request has been sent
1094 * EAGAIN: The socket was blocked, please call again later to
1095 * complete the request
1096 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001097 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -04001098 *
1099 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -04001100 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -04001101 */
Trond Myklebustadfa7142018-09-03 23:58:59 -04001102static int xs_tcp_send_request(struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001103{
Chuck Levera246b012005-08-11 16:25:23 -04001104 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001105 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -04001106 struct xdr_buf *xdr = &req->rq_snd_buf;
Trond Myklebust7e3d3622019-06-24 19:15:44 -04001107 rpc_fraghdr rm = xs_stream_record_marker(xdr);
1108 unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001109 bool vm_wait = false;
Chuck Leverb595bb12007-08-06 11:56:42 -04001110 int status;
Jason Baronf279cd02014-09-24 18:08:00 +00001111 int sent;
Chuck Levera246b012005-08-11 16:25:23 -04001112
Trond Myklebust4cd34e72018-08-31 10:00:02 -04001113 /* Close the stream if the previous transmission was incomplete */
1114 if (xs_send_request_was_aborted(transport, req)) {
1115 if (transport->sock != NULL)
1116 kernel_sock_shutdown(transport->sock, SHUT_RDWR);
1117 return -ENOTCONN;
1118 }
1119
Chuck Lever262965f2005-08-11 16:25:56 -04001120 xs_pktdump("packet data:",
1121 req->rq_svec->iov_base,
1122 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -04001123
Trond Myklebust7196dbb2017-02-08 11:17:54 -05001124 if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
1125 xs_tcp_set_socket_timeouts(xprt, transport->sock);
1126
Chuck Levera246b012005-08-11 16:25:23 -04001127 /* Continue transmitting the packet/record. We must be careful
1128 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -04001129 * called sendmsg(). */
Chuck Lever78215752018-03-05 15:13:07 -05001130 req->rq_xtime = ktime_get();
Chuck Levera246b012005-08-11 16:25:23 -04001131 while (1) {
Jason Baronf279cd02014-09-24 18:08:00 +00001132 sent = 0;
1133 status = xs_sendpages(transport->sock, NULL, 0, xdr,
Trond Myklebust7e3d3622019-06-24 19:15:44 -04001134 transport->xmit.offset, rm, &sent);
Chuck Levera246b012005-08-11 16:25:23 -04001135
Chuck Lever46121cf2007-01-31 12:14:08 -05001136 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001137 xdr->len - transport->xmit.offset, status);
Chuck Lever262965f2005-08-11 16:25:56 -04001138
Chuck Lever262965f2005-08-11 16:25:56 -04001139 /* If we've sent the entire packet, immediately
1140 * reset the count of bytes sent. */
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001141 transport->xmit.offset += sent;
1142 req->rq_bytes_sent = transport->xmit.offset;
Trond Myklebust7e3d3622019-06-24 19:15:44 -04001143 if (likely(req->rq_bytes_sent >= msglen)) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001144 req->rq_xmit_bytes_sent += transport->xmit.offset;
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001145 transport->xmit.offset = 0;
Chuck Lever262965f2005-08-11 16:25:56 -04001146 return 0;
Chuck Levera246b012005-08-11 16:25:23 -04001147 }
1148
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001149 WARN_ON_ONCE(sent == 0 && status == 0);
1150
1151 if (status == -EAGAIN ) {
1152 /*
1153 * Return EAGAIN if we're sure we're hitting the
1154 * socket send buffer limits.
1155 */
1156 if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
1157 break;
1158 /*
1159 * Did we hit a memory allocation failure?
1160 */
1161 if (sent == 0) {
1162 status = -ENOBUFS;
1163 if (vm_wait)
1164 break;
1165 /* Retry, knowing now that we're below the
1166 * socket send buffer limit
1167 */
1168 vm_wait = true;
1169 }
1170 continue;
1171 }
Trond Myklebustf580dd02015-07-11 17:48:52 +02001172 if (status < 0)
1173 break;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001174 vm_wait = false;
Chuck Levera246b012005-08-11 16:25:23 -04001175 }
1176
Chuck Lever262965f2005-08-11 16:25:56 -04001177 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -04001178 case -ENOTSOCK:
1179 status = -ENOTCONN;
1180 /* Should we call xs_close() here? */
1181 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001182 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -04001183 status = xs_nospace(req);
Chuck Lever262965f2005-08-11 16:25:56 -04001184 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001185 case -ECONNRESET:
Chuck Lever262965f2005-08-11 16:25:56 -04001186 case -ECONNREFUSED:
1187 case -ENOTCONN:
Trond Myklebust3913c782015-02-08 21:44:04 -05001188 case -EADDRINUSE:
Trond Myklebustb5872f02015-07-03 09:32:23 -04001189 case -ENOBUFS:
Trond Myklebustb9d2bb22012-10-23 11:40:02 -04001190 case -EPIPE:
Trond Myklebust13331a52016-01-06 08:57:06 -05001191 break;
1192 default:
1193 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1194 -status);
Chuck Levera246b012005-08-11 16:25:23 -04001195 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +08001196
Chuck Levera246b012005-08-11 16:25:23 -04001197 return status;
1198}
1199
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001200static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1201{
1202 transport->old_data_ready = sk->sk_data_ready;
1203 transport->old_state_change = sk->sk_state_change;
1204 transport->old_write_space = sk->sk_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -05001205 transport->old_error_report = sk->sk_error_report;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001206}
1207
1208static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1209{
1210 sk->sk_data_ready = transport->old_data_ready;
1211 sk->sk_state_change = transport->old_state_change;
1212 sk->sk_write_space = transport->old_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -05001213 sk->sk_error_report = transport->old_error_report;
1214}
1215
Trond Myklebust42d42a52016-05-23 09:24:55 -04001216static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
1217{
1218 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1219
1220 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001221 clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state);
1222 clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state);
1223 clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state);
1224}
1225
1226static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr)
1227{
1228 set_bit(nr, &transport->sock_state);
1229 queue_work(xprtiod_workqueue, &transport->error_worker);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001230}
1231
Trond Myklebustb70ae912015-02-09 09:41:32 -05001232static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1233{
1234 smp_mb__before_atomic();
1235 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1236 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001237 xs_sock_reset_state_flags(xprt);
Trond Myklebustb70ae912015-02-09 09:41:32 -05001238 smp_mb__after_atomic();
1239}
1240
Trond Myklebust21180712013-12-31 13:22:59 -05001241/**
1242 * xs_error_report - callback to handle TCP socket state errors
1243 * @sk: socket
1244 *
1245 * Note: we don't call sock_error() since there may be a rpc_task
1246 * using the socket, and so we don't want to clear sk->sk_err.
1247 */
1248static void xs_error_report(struct sock *sk)
1249{
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001250 struct sock_xprt *transport;
Trond Myklebust21180712013-12-31 13:22:59 -05001251 struct rpc_xprt *xprt;
Trond Myklebust21180712013-12-31 13:22:59 -05001252
1253 read_lock_bh(&sk->sk_callback_lock);
1254 if (!(xprt = xprt_from_sock(sk)))
1255 goto out;
1256
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001257 transport = container_of(xprt, struct sock_xprt, xprt);
Benjamin Coddingtonaf845372019-10-02 10:40:55 -04001258 transport->xprt_err = -sk->sk_err;
1259 if (transport->xprt_err == 0)
Trond Myklebust21180712013-12-31 13:22:59 -05001260 goto out;
1261 dprintk("RPC: xs_error_report client %p, error=%d...\n",
Benjamin Coddingtonaf845372019-10-02 10:40:55 -04001262 xprt, -transport->xprt_err);
1263 trace_rpc_socket_error(xprt, sk->sk_socket, transport->xprt_err);
1264
1265 /* barrier ensures xprt_err is set before XPRT_SOCK_WAKE_ERROR */
1266 smp_mb__before_atomic();
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001267 xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR);
Trond Myklebust21180712013-12-31 13:22:59 -05001268 out:
1269 read_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001270}
1271
Chuck Leverfe315e762009-03-11 14:10:21 -04001272static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001273{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001274 struct socket *sock = transport->sock;
1275 struct sock *sk = transport->inet;
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001276 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebusta73881c2019-02-26 06:33:02 -05001277 struct file *filp = transport->file;
Chuck Levera246b012005-08-11 16:25:23 -04001278
Chuck Leverfe315e762009-03-11 14:10:21 -04001279 if (sk == NULL)
1280 return;
Chuck Lever9903cd12005-08-11 16:25:26 -04001281
Jeff Layton264d1df2015-06-03 16:14:27 -04001282 if (atomic_read(&transport->xprt.swapper))
1283 sk_clear_memalloc(sk);
1284
Trond Myklebust09939202015-08-29 19:11:21 -07001285 kernel_sock_shutdown(sock, SHUT_RDWR);
1286
Trond Myklebustedc1b012015-10-05 10:53:49 -04001287 mutex_lock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -04001288 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001289 transport->inet = NULL;
1290 transport->sock = NULL;
Trond Myklebusta73881c2019-02-26 06:33:02 -05001291 transport->file = NULL;
Chuck Levera246b012005-08-11 16:25:23 -04001292
Chuck Lever9903cd12005-08-11 16:25:26 -04001293 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001294
1295 xs_restore_old_callbacks(transport, sk);
Trond Myklebust0c787892015-08-29 13:36:30 -07001296 xprt_clear_connected(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001297 write_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001298 xs_sock_reset_connection_flags(xprt);
Trond Myklebustae053552019-02-20 14:56:20 -05001299 /* Reset stream record info */
1300 xs_stream_reset_connect(transport);
Trond Myklebustedc1b012015-10-05 10:53:49 -04001301 mutex_unlock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -04001302
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001303 trace_rpc_socket_close(xprt, sock);
Trond Myklebusta73881c2019-02-26 06:33:02 -05001304 fput(filp);
Trond Myklebust0445f922018-12-17 13:34:59 -05001305
1306 xprt_disconnect_done(xprt);
Chuck Leverfe315e762009-03-11 14:10:21 -04001307}
1308
1309/**
1310 * xs_close - close a socket
1311 * @xprt: transport
1312 *
1313 * This is used when all requests are complete; ie, no DRC state remains
1314 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -04001315 *
1316 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1317 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e762009-03-11 14:10:21 -04001318 */
1319static void xs_close(struct rpc_xprt *xprt)
1320{
1321 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1322
1323 dprintk("RPC: xs_close xprt %p\n", xprt);
1324
1325 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -04001326 xprt->reestablish_timeout = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001327}
1328
Chuck Lever4a068252015-05-11 14:02:25 -04001329static void xs_inject_disconnect(struct rpc_xprt *xprt)
1330{
1331 dprintk("RPC: injecting transport disconnect on xprt=%p\n",
1332 xprt);
1333 xprt_disconnect_done(xprt);
1334}
1335
Kinglong Mee315f3812014-03-24 11:07:22 +08001336static void xs_xprt_free(struct rpc_xprt *xprt)
1337{
1338 xs_free_peer_addresses(xprt);
1339 xprt_free(xprt);
1340}
1341
Chuck Lever9903cd12005-08-11 16:25:26 -04001342/**
1343 * xs_destroy - prepare to shutdown a transport
1344 * @xprt: doomed transport
1345 *
1346 */
1347static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001348{
Trond Myklebust03c788272015-09-17 10:42:27 -04001349 struct sock_xprt *transport = container_of(xprt,
1350 struct sock_xprt, xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05001351 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001352
Trond Myklebust03c788272015-09-17 10:42:27 -04001353 cancel_delayed_work_sync(&transport->connect_worker);
Trond Myklebusta1311d82013-10-31 09:18:49 -04001354 xs_close(xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04001355 cancel_work_sync(&transport->recv_worker);
Trond Myklebustb5e92412019-05-02 11:21:08 -04001356 cancel_work_sync(&transport->error_worker);
Kinglong Mee315f3812014-03-24 11:07:22 +08001357 xs_xprt_free(xprt);
Trond Myklebusta1311d82013-10-31 09:18:49 -04001358 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -04001359}
1360
Chuck Lever9903cd12005-08-11 16:25:26 -04001361/**
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001362 * xs_udp_data_read_skb - receive callback for UDP sockets
1363 * @xprt: transport
1364 * @sk: socket
1365 * @skb: skbuff
Chuck Lever9903cd12005-08-11 16:25:26 -04001366 *
1367 */
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001368static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1369 struct sock *sk,
1370 struct sk_buff *skb)
Chuck Lever9903cd12005-08-11 16:25:26 -04001371{
1372 struct rpc_task *task;
Chuck Levera246b012005-08-11 16:25:23 -04001373 struct rpc_rqst *rovr;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001374 int repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001375 u32 _xid;
1376 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -04001377
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001378 repsize = skb->len;
Chuck Levera246b012005-08-11 16:25:23 -04001379 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001380 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001381 return;
Chuck Levera246b012005-08-11 16:25:23 -04001382 }
1383
1384 /* Copy the XID from the skb... */
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001385 xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
Chuck Levera246b012005-08-11 16:25:23 -04001386 if (xp == NULL)
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001387 return;
Chuck Levera246b012005-08-11 16:25:23 -04001388
1389 /* Look up and lock the request corresponding to the given XID */
Trond Myklebust75c84152018-08-31 10:21:00 -04001390 spin_lock(&xprt->queue_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001391 rovr = xprt_lookup_rqst(xprt, *xp);
1392 if (!rovr)
1393 goto out_unlock;
Trond Myklebust729749b2017-08-13 10:03:59 -04001394 xprt_pin_rqst(rovr);
Chuck Leverecd465e2018-03-05 15:12:57 -05001395 xprt_update_rtt(rovr->rq_task);
Trond Myklebust75c84152018-08-31 10:21:00 -04001396 spin_unlock(&xprt->queue_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001397 task = rovr->rq_task;
1398
Chuck Levera246b012005-08-11 16:25:23 -04001399 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1400 copied = repsize;
1401
1402 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001403 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
Trond Myklebust75c84152018-08-31 10:21:00 -04001404 spin_lock(&xprt->queue_lock);
Trond Myklebust0afa6b42018-02-09 09:39:42 -05001405 __UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
Trond Myklebust729749b2017-08-13 10:03:59 -04001406 goto out_unpin;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001407 }
1408
Chuck Levera246b012005-08-11 16:25:23 -04001409
Trond Myklebustb5e92412019-05-02 11:21:08 -04001410 spin_lock(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001411 xprt_adjust_cwnd(xprt, task, copied);
Trond Myklebustb5e92412019-05-02 11:21:08 -04001412 spin_unlock(&xprt->transport_lock);
Trond Myklebust75c84152018-08-31 10:21:00 -04001413 spin_lock(&xprt->queue_lock);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001414 xprt_complete_rqst(task, copied);
Trond Myklebust0afa6b42018-02-09 09:39:42 -05001415 __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
Trond Myklebust729749b2017-08-13 10:03:59 -04001416out_unpin:
1417 xprt_unpin_rqst(rovr);
Chuck Levera246b012005-08-11 16:25:23 -04001418 out_unlock:
Trond Myklebust75c84152018-08-31 10:21:00 -04001419 spin_unlock(&xprt->queue_lock);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001420}
1421
1422static void xs_udp_data_receive(struct sock_xprt *transport)
1423{
1424 struct sk_buff *skb;
1425 struct sock *sk;
1426 int err;
1427
1428 mutex_lock(&transport->recv_mutex);
1429 sk = transport->inet;
1430 if (sk == NULL)
1431 goto out;
1432 for (;;) {
Paolo Abeni7c13f972016-11-04 11:28:59 +01001433 skb = skb_recv_udp(sk, 0, 1, &err);
Trond Myklebust4f546142018-09-14 17:45:23 -04001434 if (skb == NULL)
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001435 break;
Trond Myklebust4f546142018-09-14 17:45:23 -04001436 xs_udp_data_read_skb(&transport->xprt, sk, skb);
1437 consume_skb(skb);
1438 cond_resched();
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001439 }
Trond Myklebust0ffe86f2019-01-30 14:51:26 -05001440 xs_poll_check_readable(transport);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001441out:
1442 mutex_unlock(&transport->recv_mutex);
1443}
1444
1445static void xs_udp_data_receive_workfn(struct work_struct *work)
1446{
1447 struct sock_xprt *transport =
1448 container_of(work, struct sock_xprt, recv_worker);
Trond Myklebusta1231fd2019-02-18 10:02:29 -05001449 unsigned int pflags = memalloc_nofs_save();
1450
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001451 xs_udp_data_receive(transport);
Trond Myklebusta1231fd2019-02-18 10:02:29 -05001452 memalloc_nofs_restore(pflags);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001453}
1454
1455/**
1456 * xs_data_ready - "data ready" callback for UDP sockets
1457 * @sk: socket with data to read
1458 *
1459 */
1460static void xs_data_ready(struct sock *sk)
1461{
1462 struct rpc_xprt *xprt;
1463
1464 read_lock_bh(&sk->sk_callback_lock);
1465 dprintk("RPC: xs_data_ready...\n");
1466 xprt = xprt_from_sock(sk);
1467 if (xprt != NULL) {
1468 struct sock_xprt *transport = container_of(xprt,
1469 struct sock_xprt, xprt);
Trond Myklebust5157b952016-05-29 10:13:24 -04001470 transport->old_data_ready(sk);
1471 /* Any data means we had a useful conversation, so
1472 * then we don't need to delay the next reconnect
1473 */
1474 if (xprt->reestablish_timeout)
1475 xprt->reestablish_timeout = 0;
Trond Myklebust42d42a52016-05-23 09:24:55 -04001476 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebust40a5f1b2016-05-27 10:39:50 -04001477 queue_work(xprtiod_workqueue, &transport->recv_worker);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001478 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001479 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001480}
1481
Trond Myklebusta519fc72012-09-12 16:49:15 -04001482/*
1483 * Helper function to force a TCP close if the server is sending
1484 * junk and/or it has put us in CLOSE_WAIT
1485 */
1486static void xs_tcp_force_close(struct rpc_xprt *xprt)
1487{
Trond Myklebusta519fc72012-09-12 16:49:15 -04001488 xprt_force_disconnect(xprt);
1489}
1490
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001491#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Chuck Lever6b26cc82016-05-02 14:40:40 -04001492static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
1493{
1494 return PAGE_SIZE;
1495}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001496#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001497
Chuck Lever9903cd12005-08-11 16:25:26 -04001498/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001499 * xs_tcp_state_change - callback to handle TCP socket state changes
1500 * @sk: socket whose state has changed
1501 *
1502 */
1503static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001504{
Chuck Lever9903cd12005-08-11 16:25:26 -04001505 struct rpc_xprt *xprt;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001506 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001507
Eric Dumazetf064af12010-09-22 12:43:39 +00001508 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001509 if (!(xprt = xprt_from_sock(sk)))
1510 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001511 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001512 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001513 sk->sk_state, xprt_connected(xprt),
1514 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001515 sock_flag(sk, SOCK_ZAPPED),
1516 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001517
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001518 transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001519 trace_rpc_socket_state_change(xprt, sk->sk_socket);
Chuck Levera246b012005-08-11 16:25:23 -04001520 switch (sk->sk_state) {
1521 case TCP_ESTABLISHED:
Chuck Levera246b012005-08-11 16:25:23 -04001522 if (!xprt_test_and_set_connected(xprt)) {
Trond Myklebust8b717982013-09-26 10:18:04 -04001523 xprt->connect_cookie++;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001524 clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
1525 xprt_clear_connecting(xprt);
Chuck Lever51971132006-12-05 16:35:19 -05001526
Chuck Lever3968a8a2018-10-01 14:25:36 -04001527 xprt->stat.connect_count++;
1528 xprt->stat.connect_time += (long)jiffies -
1529 xprt->stat.connect_start;
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001530 xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING);
Chuck Levera246b012005-08-11 16:25:23 -04001531 }
Chuck Levera246b012005-08-11 16:25:23 -04001532 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001533 case TCP_FIN_WAIT1:
1534 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001535 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001536 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001537 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001538 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001539 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001540 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001541 smp_mb__after_atomic();
Chuck Levera246b012005-08-11 16:25:23 -04001542 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001543 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001544 /* The server initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001545 xprt->connect_cookie++;
Trond Myklebustd0bea452012-10-23 11:35:47 -04001546 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001547 xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001548 /* fall through */
Trond Myklebust663b8852008-01-01 18:42:12 -05001549 case TCP_CLOSING:
1550 /*
1551 * If the server closed down the connection, make sure that
1552 * we back off before reconnecting
1553 */
1554 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1555 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001556 break;
1557 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001558 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001559 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001560 clear_bit(XPRT_CONNECTED, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001561 smp_mb__after_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001562 break;
1563 case TCP_CLOSE:
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001564 if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
1565 &transport->sock_state))
1566 xprt_clear_connecting(xprt);
Trond Myklebust9b308892018-02-05 10:20:06 -05001567 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust9b308892018-02-05 10:20:06 -05001568 /* Trigger the socket release */
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001569 xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
Chuck Levera246b012005-08-11 16:25:23 -04001570 }
1571 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001572 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001573}
1574
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001575static void xs_write_space(struct sock *sk)
1576{
Trond Myklebust13331a52016-01-06 08:57:06 -05001577 struct socket_wq *wq;
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001578 struct sock_xprt *transport;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001579 struct rpc_xprt *xprt;
1580
Trond Myklebust13331a52016-01-06 08:57:06 -05001581 if (!sk->sk_socket)
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001582 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001583 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001584
1585 if (unlikely(!(xprt = xprt_from_sock(sk))))
1586 return;
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001587 transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust13331a52016-01-06 08:57:06 -05001588 rcu_read_lock();
1589 wq = rcu_dereference(sk->sk_wq);
1590 if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0)
1591 goto out;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001592
Trond Myklebust4f8943f2019-05-01 16:28:29 -04001593 xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE);
1594 sk->sk_write_pending--;
Trond Myklebust13331a52016-01-06 08:57:06 -05001595out:
1596 rcu_read_unlock();
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001597}
1598
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001599/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001600 * xs_udp_write_space - callback invoked when socket buffer space
1601 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001602 * @sk: socket whose state has changed
1603 *
Chuck Levera246b012005-08-11 16:25:23 -04001604 * Called when more output buffer space is available for this socket.
1605 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001606 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001607 * with a bunch of small requests.
1608 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001609static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001610{
Eric Dumazetf064af12010-09-22 12:43:39 +00001611 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001612
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001613 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001614 if (sock_writeable(sk))
1615 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001616
Eric Dumazetf064af12010-09-22 12:43:39 +00001617 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001618}
Chuck Levera246b012005-08-11 16:25:23 -04001619
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001620/**
1621 * xs_tcp_write_space - callback invoked when socket buffer space
1622 * becomes available
1623 * @sk: socket whose state has changed
1624 *
1625 * Called when more output buffer space is available for this socket.
1626 * We try not to wake our writers until they can make "significant"
1627 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1628 * with a bunch of small requests.
1629 */
1630static void xs_tcp_write_space(struct sock *sk)
1631{
Eric Dumazetf064af12010-09-22 12:43:39 +00001632 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001633
1634 /* from net/core/stream.c:sk_stream_write_space */
Eric Dumazet64dc6132013-07-22 20:26:31 -07001635 if (sk_stream_is_writeable(sk))
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001636 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001637
Eric Dumazetf064af12010-09-22 12:43:39 +00001638 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001639}
1640
Chuck Lever470056c2005-08-25 16:25:56 -07001641static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001642{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001643 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1644 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001645
Chuck Lever7c6e0662006-12-05 16:35:30 -05001646 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001647 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001648 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001649 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001650 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001651 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001652 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001653 sk->sk_write_space(sk);
1654 }
1655}
1656
Chuck Lever43118c22005-08-25 16:25:49 -07001657/**
Chuck Lever470056c2005-08-25 16:25:56 -07001658 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001659 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001660 * @sndsize: requested size of send buffer, in bytes
1661 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001662 *
Chuck Lever470056c2005-08-25 16:25:56 -07001663 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001664 */
Chuck Lever470056c2005-08-25 16:25:56 -07001665static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001666{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001667 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1668
1669 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001670 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001671 transport->sndsize = sndsize + 1024;
1672 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001673 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001674 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001675
1676 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001677}
1678
Chuck Lever46c0ee82005-08-25 16:25:52 -07001679/**
1680 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
Chuck Leveracf0a392018-12-19 11:00:22 -05001681 * @xprt: controlling transport
Chuck Lever46c0ee82005-08-25 16:25:52 -07001682 * @task: task that timed out
1683 *
1684 * Adjust the congestion window after a retransmit timeout has occurred.
1685 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001686static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever46c0ee82005-08-25 16:25:52 -07001687{
Trond Myklebustb5e92412019-05-02 11:21:08 -04001688 spin_lock(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001689 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
Trond Myklebustb5e92412019-05-02 11:21:08 -04001690 spin_unlock(&xprt->transport_lock);
Chuck Lever46c0ee82005-08-25 16:25:52 -07001691}
1692
J. Bruce Fields826799e2018-10-18 15:27:02 -04001693static int xs_get_random_port(void)
Chuck Leverb85d8802006-05-25 01:40:49 -04001694{
J. Bruce Fields826799e2018-10-18 15:27:02 -04001695 unsigned short min = xprt_min_resvport, max = xprt_max_resvport;
1696 unsigned short range;
1697 unsigned short rand;
1698
1699 if (max < min)
1700 return -EADDRINUSE;
1701 range = max - min + 1;
1702 rand = (unsigned short) prandom_u32() % range;
1703 return rand + min;
Chuck Leverb85d8802006-05-25 01:40:49 -04001704}
1705
Chuck Lever92200412006-01-03 09:55:51 +01001706/**
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001707 * xs_set_reuseaddr_port - set the socket's port and address reuse options
1708 * @sock: socket
1709 *
1710 * Note that this function has to be called on all sockets that share the
1711 * same port, and it must be called before binding.
1712 */
1713static void xs_sock_set_reuseport(struct socket *sock)
1714{
Trond Myklebust402e23b2015-02-09 17:20:14 -05001715 int opt = 1;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001716
Trond Myklebust402e23b2015-02-09 17:20:14 -05001717 kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
1718 (char *)&opt, sizeof(opt));
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001719}
1720
1721static unsigned short xs_sock_getport(struct socket *sock)
1722{
1723 struct sockaddr_storage buf;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001724 unsigned short port = 0;
1725
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001726 if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001727 goto out;
1728 switch (buf.ss_family) {
1729 case AF_INET6:
1730 port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
1731 break;
1732 case AF_INET:
1733 port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
1734 }
1735out:
1736 return port;
1737}
1738
1739/**
Chuck Lever92200412006-01-03 09:55:51 +01001740 * xs_set_port - reset the port number in the remote endpoint address
1741 * @xprt: generic transport
1742 * @port: new port number
1743 *
1744 */
1745static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1746{
Chuck Lever46121cf2007-01-31 12:14:08 -05001747 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001748
Chuck Lever9dc3b092009-08-09 15:09:46 -04001749 rpc_set_port(xs_addr(xprt), port);
1750 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001751}
1752
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001753static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
1754{
1755 if (transport->srcport == 0)
1756 transport->srcport = xs_sock_getport(sock);
1757}
1758
J. Bruce Fields826799e2018-10-18 15:27:02 -04001759static int xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001760{
J. Bruce Fields826799e2018-10-18 15:27:02 -04001761 int port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001762
1763 if (port == 0 && transport->xprt.resvport)
1764 port = xs_get_random_port();
1765 return port;
1766}
1767
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001768static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001769{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001770 if (transport->srcport != 0)
1771 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001772 if (!transport->xprt.resvport)
1773 return 0;
1774 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1775 return xprt_max_resvport;
1776 return --port;
1777}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001778static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001779{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001780 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001781 int err, nloop = 0;
J. Bruce Fields826799e2018-10-18 15:27:02 -04001782 int port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001783 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001784
Chris Perl0f7a6222014-09-05 15:40:21 -04001785 /*
1786 * If we are asking for any ephemeral port (i.e. port == 0 &&
1787 * transport->xprt.resvport == 0), don't bind. Let the local
1788 * port selection happen implicitly when the socket is used
1789 * (for example at connect time).
1790 *
1791 * This ensures that we can continue to establish TCP
1792 * connections even when all local ephemeral ports are already
1793 * a part of some TCP connection. This makes no difference
1794 * for UDP sockets, but also doens't harm them.
1795 *
1796 * If we're asking for any reserved port (i.e. port == 0 &&
1797 * transport->xprt.resvport == 1) xs_get_srcport above will
1798 * ensure that port is non-zero and we will bind as needed.
1799 */
J. Bruce Fields826799e2018-10-18 15:27:02 -04001800 if (port <= 0)
1801 return port;
Chris Perl0f7a6222014-09-05 15:40:21 -04001802
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001803 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001804 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001805 rpc_set_port((struct sockaddr *)&myaddr, port);
1806 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1807 transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001808 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001809 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001810 break;
Chuck Levera246b012005-08-11 16:25:23 -04001811 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001812 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001813 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001814 if (port > last)
1815 nloop++;
1816 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001817
Chuck Lever4232e862010-10-20 11:52:51 -04001818 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001819 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1820 &((struct sockaddr_in *)&myaddr)->sin_addr,
1821 port, err ? "failed" : "ok", err);
1822 else
1823 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1824 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1825 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001826 return err;
1827}
1828
Chuck Lever176e21e2011-05-09 15:22:44 -04001829/*
1830 * We don't support autobind on AF_LOCAL sockets
1831 */
1832static void xs_local_rpcbind(struct rpc_task *task)
1833{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001834 xprt_set_bound(task->tk_xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04001835}
1836
1837static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1838{
1839}
Chuck Levera246b012005-08-11 16:25:23 -04001840
Peter Zijlstraed075362006-12-06 20:35:24 -08001841#ifdef CONFIG_DEBUG_LOCK_ALLOC
1842static struct lock_class_key xs_key[2];
1843static struct lock_class_key xs_slock_key[2];
1844
Chuck Lever176e21e2011-05-09 15:22:44 -04001845static inline void xs_reclassify_socketu(struct socket *sock)
1846{
1847 struct sock *sk = sock->sk;
1848
Chuck Lever176e21e2011-05-09 15:22:44 -04001849 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1850 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1851}
1852
Chuck Lever8945ee52007-08-06 11:58:04 -04001853static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001854{
1855 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001856
Chuck Lever8945ee52007-08-06 11:58:04 -04001857 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1858 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1859}
Peter Zijlstraed075362006-12-06 20:35:24 -08001860
Chuck Lever8945ee52007-08-06 11:58:04 -04001861static inline void xs_reclassify_socket6(struct socket *sock)
1862{
1863 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001864
Chuck Lever8945ee52007-08-06 11:58:04 -04001865 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1866 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001867}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001868
1869static inline void xs_reclassify_socket(int family, struct socket *sock)
1870{
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02001871 if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -04001872 return;
1873
Chuck Lever4232e862010-10-20 11:52:51 -04001874 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001875 case AF_LOCAL:
1876 xs_reclassify_socketu(sock);
1877 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001878 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001879 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001880 break;
1881 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001882 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001883 break;
1884 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001885}
Peter Zijlstraed075362006-12-06 20:35:24 -08001886#else
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001887static inline void xs_reclassify_socket(int family, struct socket *sock)
1888{
1889}
Peter Zijlstraed075362006-12-06 20:35:24 -08001890#endif
1891
NeilBrown93dc41b2013-10-31 16:14:36 +11001892static void xs_dummy_setup_socket(struct work_struct *work)
1893{
1894}
1895
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001896static struct socket *xs_create_sock(struct rpc_xprt *xprt,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001897 struct sock_xprt *transport, int family, int type,
1898 int protocol, bool reuseport)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001899{
Trond Myklebusta73881c2019-02-26 06:33:02 -05001900 struct file *filp;
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001901 struct socket *sock;
1902 int err;
1903
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001904 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001905 if (err < 0) {
1906 dprintk("RPC: can't create %d transport socket (%d).\n",
1907 protocol, -err);
1908 goto out;
1909 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001910 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001911
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001912 if (reuseport)
1913 xs_sock_set_reuseport(sock);
1914
Ben Hutchings4cea2882011-02-22 21:54:34 +00001915 err = xs_bind(transport, sock);
1916 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001917 sock_release(sock);
1918 goto out;
1919 }
1920
Trond Myklebusta73881c2019-02-26 06:33:02 -05001921 filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1922 if (IS_ERR(filp))
1923 return ERR_CAST(filp);
1924 transport->file = filp;
1925
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001926 return sock;
1927out:
1928 return ERR_PTR(err);
1929}
1930
Chuck Lever176e21e2011-05-09 15:22:44 -04001931static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1932 struct socket *sock)
1933{
1934 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1935 xprt);
1936
1937 if (!transport->inet) {
1938 struct sock *sk = sock->sk;
1939
1940 write_lock_bh(&sk->sk_callback_lock);
1941
1942 xs_save_old_callbacks(transport, sk);
1943
1944 sk->sk_user_data = xprt;
Trond Myklebusta2648092015-10-06 17:03:00 -04001945 sk->sk_data_ready = xs_data_ready;
Chuck Lever176e21e2011-05-09 15:22:44 -04001946 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07001947 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05001948 sk->sk_error_report = xs_error_report;
Chuck Lever176e21e2011-05-09 15:22:44 -04001949
1950 xprt_clear_connected(xprt);
1951
1952 /* Reset to new socket */
1953 transport->sock = sock;
1954 transport->inet = sk;
1955
1956 write_unlock_bh(&sk->sk_callback_lock);
1957 }
1958
Trond Myklebustae053552019-02-20 14:56:20 -05001959 xs_stream_start_connect(transport);
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001960
Chuck Lever176e21e2011-05-09 15:22:44 -04001961 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1962}
1963
1964/**
1965 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
Chuck Lever176e21e2011-05-09 15:22:44 -04001966 * @transport: socket transport to connect
Chuck Lever176e21e2011-05-09 15:22:44 -04001967 */
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001968static int xs_local_setup_socket(struct sock_xprt *transport)
Chuck Lever176e21e2011-05-09 15:22:44 -04001969{
Chuck Lever176e21e2011-05-09 15:22:44 -04001970 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebusta73881c2019-02-26 06:33:02 -05001971 struct file *filp;
Chuck Lever176e21e2011-05-09 15:22:44 -04001972 struct socket *sock;
1973 int status = -EIO;
1974
Chuck Lever176e21e2011-05-09 15:22:44 -04001975 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1976 SOCK_STREAM, 0, &sock, 1);
1977 if (status < 0) {
1978 dprintk("RPC: can't create AF_LOCAL "
1979 "transport socket (%d).\n", -status);
1980 goto out;
1981 }
Stefan Hajnoczid1358912015-12-02 14:17:52 +08001982 xs_reclassify_socket(AF_LOCAL, sock);
Chuck Lever176e21e2011-05-09 15:22:44 -04001983
Trond Myklebusta73881c2019-02-26 06:33:02 -05001984 filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1985 if (IS_ERR(filp)) {
1986 status = PTR_ERR(filp);
1987 goto out;
1988 }
1989 transport->file = filp;
1990
Chuck Lever176e21e2011-05-09 15:22:44 -04001991 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1992 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1993
1994 status = xs_local_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001995 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever176e21e2011-05-09 15:22:44 -04001996 switch (status) {
1997 case 0:
1998 dprintk("RPC: xprt %p connected to %s\n",
1999 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
Chuck Lever3968a8a2018-10-01 14:25:36 -04002000 xprt->stat.connect_count++;
2001 xprt->stat.connect_time += (long)jiffies -
2002 xprt->stat.connect_start;
Chuck Lever176e21e2011-05-09 15:22:44 -04002003 xprt_set_connected(xprt);
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002004 case -ENOBUFS:
Chuck Lever176e21e2011-05-09 15:22:44 -04002005 break;
2006 case -ENOENT:
2007 dprintk("RPC: xprt %p: socket %s does not exist\n",
2008 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2009 break;
Trond Myklebust4a20a982012-12-15 17:02:29 -05002010 case -ECONNREFUSED:
2011 dprintk("RPC: xprt %p: connection refused for %s\n",
2012 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2013 break;
Chuck Lever176e21e2011-05-09 15:22:44 -04002014 default:
2015 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
2016 __func__, -status,
2017 xprt->address_strings[RPC_DISPLAY_ADDR]);
2018 }
2019
2020out:
2021 xprt_clear_connecting(xprt);
2022 xprt_wake_pending_tasks(xprt, status);
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002023 return status;
2024}
2025
Linus Torvaldsb6669732013-02-28 18:02:55 -08002026static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002027{
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002028 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2029 int ret;
2030
2031 if (RPC_IS_ASYNC(task)) {
2032 /*
2033 * We want the AF_LOCAL connect to be resolved in the
2034 * filesystem namespace of the process making the rpc
2035 * call. Thus we connect synchronously.
2036 *
2037 * If we want to support asynchronous AF_LOCAL calls,
2038 * we'll need to figure out how to pass a namespace to
2039 * connect.
2040 */
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002041 task->tk_rpc_status = -ENOTCONN;
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002042 rpc_exit(task, -ENOTCONN);
2043 return;
2044 }
2045 ret = xs_local_setup_socket(transport);
2046 if (ret && !RPC_IS_SOFTCONN(task))
2047 msleep_interruptible(15000);
Chuck Lever176e21e2011-05-09 15:22:44 -04002048}
2049
Jeff Layton3c87ef62015-06-03 16:14:25 -04002050#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Jeff Laytond6e971d2015-06-03 16:14:28 -04002051/*
2052 * Note that this should be called with XPRT_LOCKED held (or when we otherwise
2053 * know that we have exclusive access to the socket), to guard against
2054 * races with xs_reset_transport.
2055 */
Mel Gormana564b8f2012-07-31 16:45:12 -07002056static void xs_set_memalloc(struct rpc_xprt *xprt)
2057{
2058 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2059 xprt);
2060
Jeff Laytond6e971d2015-06-03 16:14:28 -04002061 /*
2062 * If there's no sock, then we have nothing to set. The
2063 * reconnecting process will get it for us.
2064 */
2065 if (!transport->inet)
2066 return;
Jeff Layton8e228132015-06-03 16:14:26 -04002067 if (atomic_read(&xprt->swapper))
Mel Gormana564b8f2012-07-31 16:45:12 -07002068 sk_set_memalloc(transport->inet);
2069}
2070
2071/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002072 * xs_enable_swap - Tag this transport as being used for swap.
Mel Gormana564b8f2012-07-31 16:45:12 -07002073 * @xprt: transport to tag
Mel Gormana564b8f2012-07-31 16:45:12 -07002074 *
Jeff Layton8e228132015-06-03 16:14:26 -04002075 * Take a reference to this transport on behalf of the rpc_clnt, and
2076 * optionally mark it for swapping if it wasn't already.
Mel Gormana564b8f2012-07-31 16:45:12 -07002077 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002078static int
2079xs_enable_swap(struct rpc_xprt *xprt)
Mel Gormana564b8f2012-07-31 16:45:12 -07002080{
Jeff Laytond6e971d2015-06-03 16:14:28 -04002081 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Mel Gormana564b8f2012-07-31 16:45:12 -07002082
Jeff Laytond6e971d2015-06-03 16:14:28 -04002083 if (atomic_inc_return(&xprt->swapper) != 1)
2084 return 0;
2085 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2086 return -ERESTARTSYS;
2087 if (xs->inet)
2088 sk_set_memalloc(xs->inet);
2089 xprt_release_xprt(xprt, NULL);
Jeff Layton8e228132015-06-03 16:14:26 -04002090 return 0;
Mel Gormana564b8f2012-07-31 16:45:12 -07002091}
Jeff Layton8e228132015-06-03 16:14:26 -04002092
2093/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002094 * xs_disable_swap - Untag this transport as being used for swap.
Jeff Layton8e228132015-06-03 16:14:26 -04002095 * @xprt: transport to tag
2096 *
2097 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
2098 * swapper refcount goes to 0, untag the socket as a memalloc socket.
2099 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002100static void
2101xs_disable_swap(struct rpc_xprt *xprt)
Jeff Layton8e228132015-06-03 16:14:26 -04002102{
Jeff Laytond6e971d2015-06-03 16:14:28 -04002103 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Jeff Layton8e228132015-06-03 16:14:26 -04002104
Jeff Laytond6e971d2015-06-03 16:14:28 -04002105 if (!atomic_dec_and_test(&xprt->swapper))
2106 return;
2107 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2108 return;
2109 if (xs->inet)
2110 sk_clear_memalloc(xs->inet);
2111 xprt_release_xprt(xprt, NULL);
Mel Gormana564b8f2012-07-31 16:45:12 -07002112}
Mel Gormana564b8f2012-07-31 16:45:12 -07002113#else
2114static void xs_set_memalloc(struct rpc_xprt *xprt)
2115{
2116}
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002117
2118static int
2119xs_enable_swap(struct rpc_xprt *xprt)
2120{
2121 return -EINVAL;
2122}
2123
2124static void
2125xs_disable_swap(struct rpc_xprt *xprt)
2126{
2127}
Mel Gormana564b8f2012-07-31 16:45:12 -07002128#endif
2129
Chuck Lever16be2d22007-08-06 11:57:38 -04002130static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04002131{
Chuck Lever16be2d22007-08-06 11:57:38 -04002132 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04002133
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002134 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002135 struct sock *sk = sock->sk;
2136
2137 write_lock_bh(&sk->sk_callback_lock);
2138
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002139 xs_save_old_callbacks(transport, sk);
2140
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002141 sk->sk_user_data = xprt;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002142 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002143 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002144 sock_set_flag(sk, SOCK_FASYNC);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002145
2146 xprt_set_connected(xprt);
2147
2148 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002149 transport->sock = sock;
2150 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002151
Mel Gormana564b8f2012-07-31 16:45:12 -07002152 xs_set_memalloc(xprt);
2153
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002154 write_unlock_bh(&sk->sk_callback_lock);
2155 }
Chuck Lever470056c2005-08-25 16:25:56 -07002156 xs_udp_do_set_buffer_size(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002157
2158 xprt->stat.connect_start = jiffies;
Chuck Lever16be2d22007-08-06 11:57:38 -04002159}
2160
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002161static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04002162{
2163 struct sock_xprt *transport =
2164 container_of(work, struct sock_xprt, connect_worker.work);
2165 struct rpc_xprt *xprt = &transport->xprt;
Colin Ian Kingd099b8a2017-09-18 12:21:14 +01002166 struct socket *sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002167 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04002168
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002169 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002170 xs_addr(xprt)->sa_family, SOCK_DGRAM,
2171 IPPROTO_UDP, false);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002172 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04002173 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04002174
Chuck Leverc740eff2009-08-09 15:09:46 -04002175 dprintk("RPC: worker connecting xprt %p via %s to "
2176 "%s (port %s)\n", xprt,
2177 xprt->address_strings[RPC_DISPLAY_PROTO],
2178 xprt->address_strings[RPC_DISPLAY_ADDR],
2179 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04002180
2181 xs_udp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002182 trace_rpc_socket_connect(xprt, sock, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002183 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002184out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002185 xprt_clear_connecting(xprt);
Trond Myklebustcf767852018-12-17 17:38:51 -05002186 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002187 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002188}
2189
Trond Myklebust4876cc72015-06-19 16:17:57 -04002190/**
2191 * xs_tcp_shutdown - gracefully shut down a TCP socket
2192 * @xprt: transport
2193 *
2194 * Initiates a graceful shutdown of the TCP socket by calling the
2195 * equivalent of shutdown(SHUT_RDWR);
2196 */
2197static void xs_tcp_shutdown(struct rpc_xprt *xprt)
2198{
2199 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2200 struct socket *sock = transport->sock;
Trond Myklebust9b308892018-02-05 10:20:06 -05002201 int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
Trond Myklebust4876cc72015-06-19 16:17:57 -04002202
2203 if (sock == NULL)
2204 return;
Trond Myklebust9b308892018-02-05 10:20:06 -05002205 switch (skst) {
2206 default:
Trond Myklebust4876cc72015-06-19 16:17:57 -04002207 kernel_sock_shutdown(sock, SHUT_RDWR);
2208 trace_rpc_socket_shutdown(xprt, sock);
Trond Myklebust9b308892018-02-05 10:20:06 -05002209 break;
2210 case TCP_CLOSE:
2211 case TCP_TIME_WAIT:
Trond Myklebust4876cc72015-06-19 16:17:57 -04002212 xs_reset_transport(transport);
Trond Myklebust9b308892018-02-05 10:20:06 -05002213 }
Trond Myklebust4876cc72015-06-19 16:17:57 -04002214}
2215
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002216static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
2217 struct socket *sock)
2218{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002219 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2220 unsigned int keepidle;
2221 unsigned int keepcnt;
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002222 unsigned int opt_on = 1;
2223 unsigned int timeo;
2224
Trond Myklebustb5e92412019-05-02 11:21:08 -04002225 spin_lock(&xprt->transport_lock);
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002226 keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
2227 keepcnt = xprt->timeout->to_retries + 1;
2228 timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
2229 (xprt->timeout->to_retries + 1);
2230 clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
Trond Myklebustb5e92412019-05-02 11:21:08 -04002231 spin_unlock(&xprt->transport_lock);
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002232
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002233 /* TCP Keepalive options */
2234 kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
2235 (char *)&opt_on, sizeof(opt_on));
2236 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
2237 (char *)&keepidle, sizeof(keepidle));
2238 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
2239 (char *)&keepidle, sizeof(keepidle));
2240 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
2241 (char *)&keepcnt, sizeof(keepcnt));
2242
2243 /* TCP user timeout (see RFC5482) */
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002244 kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT,
2245 (char *)&timeo, sizeof(timeo));
2246}
2247
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002248static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
2249 unsigned long connect_timeout,
2250 unsigned long reconnect_timeout)
2251{
2252 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2253 struct rpc_timeout to;
2254 unsigned long initval;
2255
Trond Myklebustb5e92412019-05-02 11:21:08 -04002256 spin_lock(&xprt->transport_lock);
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002257 if (reconnect_timeout < xprt->max_reconnect_timeout)
2258 xprt->max_reconnect_timeout = reconnect_timeout;
2259 if (connect_timeout < xprt->connect_timeout) {
2260 memcpy(&to, xprt->timeout, sizeof(to));
2261 initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
2262 /* Arbitrary lower limit */
2263 if (initval < XS_TCP_INIT_REEST_TO << 1)
2264 initval = XS_TCP_INIT_REEST_TO << 1;
2265 to.to_initval = initval;
2266 to.to_maxval = initval;
2267 memcpy(&transport->tcp_timeout, &to,
2268 sizeof(transport->tcp_timeout));
2269 xprt->timeout = &transport->tcp_timeout;
2270 xprt->connect_timeout = connect_timeout;
2271 }
2272 set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
Trond Myklebustb5e92412019-05-02 11:21:08 -04002273 spin_unlock(&xprt->transport_lock);
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002274}
2275
Chuck Lever16be2d22007-08-06 11:57:38 -04002276static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002277{
Chuck Lever16be2d22007-08-06 11:57:38 -04002278 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002279 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002280
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002281 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002282 struct sock *sk = sock->sk;
NeilBrownd88e4d82016-08-04 16:24:28 +10002283 unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC;
Trond Myklebust7f260e82013-09-24 11:25:22 -04002284
NeilBrownd88e4d82016-08-04 16:24:28 +10002285 /* Avoid temporary address, they are bad for long-lived
2286 * connections such as NFS mounts.
2287 * RFC4941, section 3.6 suggests that:
2288 * Individual applications, which have specific
2289 * knowledge about the normal duration of connections,
2290 * MAY override this as appropriate.
2291 */
2292 kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES,
2293 (char *)&addr_pref, sizeof(addr_pref));
2294
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002295 xs_tcp_set_socket_timeouts(xprt, sock);
Trond Myklebust775f06a2015-06-20 15:31:54 -04002296
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002297 write_lock_bh(&sk->sk_callback_lock);
2298
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002299 xs_save_old_callbacks(transport, sk);
2300
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002301 sk->sk_user_data = xprt;
Trond Myklebust5157b952016-05-29 10:13:24 -04002302 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002303 sk->sk_state_change = xs_tcp_state_change;
2304 sk->sk_write_space = xs_tcp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002305 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05002306 sk->sk_error_report = xs_error_report;
Chuck Lever3167e122005-08-25 16:25:55 -07002307
2308 /* socket options */
Chuck Lever3167e122005-08-25 16:25:55 -07002309 sock_reset_flag(sk, SOCK_LINGER);
Chuck Lever3167e122005-08-25 16:25:55 -07002310 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002311
2312 xprt_clear_connected(xprt);
2313
2314 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002315 transport->sock = sock;
2316 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002317
2318 write_unlock_bh(&sk->sk_callback_lock);
2319 }
2320
Trond Myklebust01d37c42009-03-11 14:09:39 -04002321 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002322 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002323
Mel Gormana564b8f2012-07-31 16:45:12 -07002324 xs_set_memalloc(xprt);
2325
Trond Myklebustae053552019-02-20 14:56:20 -05002326 xs_stream_start_connect(transport);
Trond Myklebuste1806c72018-08-13 16:50:49 -04002327
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002328 /* Tell the socket layer to start connecting... */
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04002329 set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002330 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2331 switch (ret) {
2332 case 0:
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002333 xs_set_srcport(transport, sock);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002334 /* fall through */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002335 case -EINPROGRESS:
2336 /* SYN_SENT! */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002337 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2338 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust1f4c17a2016-08-01 13:36:08 -04002339 break;
2340 case -EADDRNOTAVAIL:
2341 /* Source port number is unavailable. Try a new one! */
2342 transport->srcport = 0;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002343 }
2344out:
2345 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002346}
2347
2348/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002349 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
Chuck Leveracf0a392018-12-19 11:00:22 -05002350 * @work: queued work item
Chuck Lever16be2d22007-08-06 11:57:38 -04002351 *
2352 * Invoked by a work queue tasklet.
2353 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002354static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002355{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002356 struct sock_xprt *transport =
2357 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002358 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002359 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002360 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002361
Chuck Lever16be2d22007-08-06 11:57:38 -04002362 if (!sock) {
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002363 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002364 xs_addr(xprt)->sa_family, SOCK_STREAM,
2365 IPPROTO_TCP, true);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002366 if (IS_ERR(sock)) {
2367 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002368 goto out;
2369 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002370 }
2371
Chuck Leverc740eff2009-08-09 15:09:46 -04002372 dprintk("RPC: worker connecting xprt %p via %s to "
2373 "%s (port %s)\n", xprt,
2374 xprt->address_strings[RPC_DISPLAY_PROTO],
2375 xprt->address_strings[RPC_DISPLAY_ADDR],
2376 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002377
2378 status = xs_tcp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002379 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever46121cf2007-01-31 12:14:08 -05002380 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2381 xprt, -status, xprt_connected(xprt),
2382 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002383 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002384 default:
2385 printk("%s: connect returned unhandled error %d\n",
2386 __func__, status);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002387 /* fall through */
Trond Myklebustf75e6742009-04-21 17:18:20 -04002388 case -EADDRNOTAVAIL:
2389 /* We're probably in TIME_WAIT. Get rid of existing socket,
2390 * and retry
2391 */
Trond Myklebusta519fc72012-09-12 16:49:15 -04002392 xs_tcp_force_close(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002393 break;
Trond Myklebust2a491992009-03-11 14:38:00 -04002394 case 0:
2395 case -EINPROGRESS:
2396 case -EALREADY:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002397 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002398 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002399 case -EINVAL:
2400 /* Happens, for instance, if the user specified a link
2401 * local IPv6 address without a scope-id.
2402 */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002403 case -ECONNREFUSED:
2404 case -ECONNRESET:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002405 case -ENETDOWN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002406 case -ENETUNREACH:
Trond Myklebust4ba161a2017-11-24 12:00:24 -05002407 case -EHOSTUNREACH:
Trond Myklebust3913c782015-02-08 21:44:04 -05002408 case -EADDRINUSE:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002409 case -ENOBUFS:
NeilBrown6ea44ad2017-05-25 17:00:32 +10002410 /*
2411 * xs_tcp_force_close() wakes tasks with -EIO.
2412 * We need to wake them first to ensure the
2413 * correct error code.
2414 */
2415 xprt_wake_pending_tasks(xprt, status);
Trond Myklebust4efdd922015-02-08 15:34:28 -05002416 xs_tcp_force_close(xprt);
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002417 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002418 }
Trond Myklebust2a491992009-03-11 14:38:00 -04002419 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002420out:
Chuck Lever2226feb2005-08-11 16:25:38 -04002421 xprt_clear_connecting(xprt);
Trond Myklebustcf767852018-12-17 17:38:51 -05002422 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002423 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04002424}
2425
Chuck Lever68e220b2007-08-06 11:57:48 -04002426/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002427 * xs_connect - connect a socket to a remote endpoint
Trond Myklebust1b092092013-01-08 09:26:49 -05002428 * @xprt: pointer to transport structure
Chuck Lever9903cd12005-08-11 16:25:26 -04002429 * @task: address of RPC task that manages state of connect request
2430 *
2431 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002432 *
2433 * UDP socket connects are synchronous, but we use a work queue anyway
2434 * to guarantee that even unprivileged user processes can set up a
2435 * socket on a privileged port.
2436 *
2437 * If a UDP socket connect fails, the delay behavior here prevents
2438 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002439 */
Trond Myklebust1b092092013-01-08 09:26:49 -05002440static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002441{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002442 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002443 unsigned long delay = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002444
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002445 WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2446
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002447 if (transport->sock != NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002448 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2449 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002450 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002451
2452 /* Start by resetting any existing state */
2453 xs_reset_transport(transport);
2454
Chuck Lever675dd902019-06-19 10:33:42 -04002455 delay = xprt_reconnect_delay(xprt);
2456 xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO);
Trond Myklebust02910172016-08-04 00:00:33 -04002457
Trond Myklebust02910172016-08-04 00:00:33 -04002458 } else
Chuck Lever46121cf2007-01-31 12:14:08 -05002459 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002460
2461 queue_delayed_work(xprtiod_workqueue,
2462 &transport->connect_worker,
2463 delay);
Chuck Levera246b012005-08-11 16:25:23 -04002464}
2465
Trond Myklebust4f8943f2019-05-01 16:28:29 -04002466static void xs_wake_disconnect(struct sock_xprt *transport)
2467{
2468 if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state))
2469 xs_tcp_force_close(&transport->xprt);
2470}
2471
2472static void xs_wake_write(struct sock_xprt *transport)
2473{
2474 if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state))
2475 xprt_write_space(&transport->xprt);
2476}
2477
2478static void xs_wake_error(struct sock_xprt *transport)
2479{
2480 int sockerr;
Trond Myklebust4f8943f2019-05-01 16:28:29 -04002481
2482 if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
2483 return;
2484 mutex_lock(&transport->recv_mutex);
2485 if (transport->sock == NULL)
2486 goto out;
2487 if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
2488 goto out;
Benjamin Coddingtonaf845372019-10-02 10:40:55 -04002489 sockerr = xchg(&transport->xprt_err, 0);
Trond Myklebust4f8943f2019-05-01 16:28:29 -04002490 if (sockerr < 0)
2491 xprt_wake_pending_tasks(&transport->xprt, sockerr);
2492out:
2493 mutex_unlock(&transport->recv_mutex);
2494}
2495
2496static void xs_wake_pending(struct sock_xprt *transport)
2497{
2498 if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state))
2499 xprt_wake_pending_tasks(&transport->xprt, -EAGAIN);
2500}
2501
2502static void xs_error_handle(struct work_struct *work)
2503{
2504 struct sock_xprt *transport = container_of(work,
2505 struct sock_xprt, error_worker);
2506
2507 xs_wake_disconnect(transport);
2508 xs_wake_write(transport);
2509 xs_wake_error(transport);
2510 xs_wake_pending(transport);
2511}
2512
Chuck Lever262ca072006-03-20 13:44:16 -05002513/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002514 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2515 * @xprt: rpc_xprt struct containing statistics
2516 * @seq: output file
2517 *
2518 */
2519static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2520{
2521 long idle_time = 0;
2522
2523 if (xprt_connected(xprt))
2524 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2525
2526 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
Andy Adamson15a45202012-02-14 16:19:18 -05002527 "%llu %llu %lu %llu %llu\n",
Chuck Lever176e21e2011-05-09 15:22:44 -04002528 xprt->stat.bind_count,
2529 xprt->stat.connect_count,
Chuck Lever8440a882018-10-01 14:25:41 -04002530 xprt->stat.connect_time / HZ,
Chuck Lever176e21e2011-05-09 15:22:44 -04002531 idle_time,
2532 xprt->stat.sends,
2533 xprt->stat.recvs,
2534 xprt->stat.bad_xids,
2535 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002536 xprt->stat.bklog_u,
2537 xprt->stat.max_slots,
2538 xprt->stat.sending_u,
2539 xprt->stat.pending_u);
Chuck Lever176e21e2011-05-09 15:22:44 -04002540}
2541
2542/**
Chuck Lever262ca072006-03-20 13:44:16 -05002543 * xs_udp_print_stats - display UDP socket-specifc stats
2544 * @xprt: rpc_xprt struct containing statistics
2545 * @seq: output file
2546 *
2547 */
2548static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2549{
Chuck Leverc8475462006-12-05 16:35:26 -05002550 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2551
Andy Adamson15a45202012-02-14 16:19:18 -05002552 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2553 "%lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002554 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002555 xprt->stat.bind_count,
2556 xprt->stat.sends,
2557 xprt->stat.recvs,
2558 xprt->stat.bad_xids,
2559 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002560 xprt->stat.bklog_u,
2561 xprt->stat.max_slots,
2562 xprt->stat.sending_u,
2563 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002564}
2565
2566/**
2567 * xs_tcp_print_stats - display TCP socket-specifc stats
2568 * @xprt: rpc_xprt struct containing statistics
2569 * @seq: output file
2570 *
2571 */
2572static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2573{
Chuck Leverc8475462006-12-05 16:35:26 -05002574 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002575 long idle_time = 0;
2576
2577 if (xprt_connected(xprt))
2578 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2579
Andy Adamson15a45202012-02-14 16:19:18 -05002580 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2581 "%llu %llu %lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002582 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002583 xprt->stat.bind_count,
2584 xprt->stat.connect_count,
Chuck Lever8440a882018-10-01 14:25:41 -04002585 xprt->stat.connect_time / HZ,
Chuck Lever262ca072006-03-20 13:44:16 -05002586 idle_time,
2587 xprt->stat.sends,
2588 xprt->stat.recvs,
2589 xprt->stat.bad_xids,
2590 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002591 xprt->stat.bklog_u,
2592 xprt->stat.max_slots,
2593 xprt->stat.sending_u,
2594 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002595}
2596
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002597/*
2598 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2599 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2600 * to use the server side send routines.
2601 */
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002602static int bc_malloc(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002603{
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002604 struct rpc_rqst *rqst = task->tk_rqstp;
2605 size_t size = rqst->rq_callsize;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002606 struct page *page;
2607 struct rpc_buffer *buf;
2608
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002609 if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
2610 WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
2611 size);
2612 return -EINVAL;
2613 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002614
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002615 page = alloc_page(GFP_KERNEL);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002616 if (!page)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002617 return -ENOMEM;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002618
2619 buf = page_address(page);
2620 buf->len = PAGE_SIZE;
2621
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002622 rqst->rq_buffer = buf->data;
Jeff Layton18e601d2016-10-24 20:33:23 -04002623 rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002624 return 0;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002625}
2626
2627/*
2628 * Free the space allocated in the bc_alloc routine
2629 */
Chuck Lever3435c742016-09-15 10:55:29 -04002630static void bc_free(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002631{
Chuck Lever3435c742016-09-15 10:55:29 -04002632 void *buffer = task->tk_rqstp->rq_buffer;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002633 struct rpc_buffer *buf;
2634
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002635 buf = container_of(buffer, struct rpc_buffer, data);
2636 free_page((unsigned long)buf);
2637}
2638
2639/*
2640 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2641 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2642 */
2643static int bc_sendto(struct rpc_rqst *req)
2644{
2645 int len;
2646 struct xdr_buf *xbufp = &req->rq_snd_buf;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002647 struct sock_xprt *transport =
Chuck Lever067fb112019-02-11 11:24:37 -05002648 container_of(req->rq_xprt, struct sock_xprt, xprt);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002649 unsigned long headoff;
2650 unsigned long tailoff;
Chuck Lever067fb112019-02-11 11:24:37 -05002651 struct page *tailpage;
2652 struct msghdr msg = {
2653 .msg_flags = MSG_MORE
2654 };
2655 rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
2656 (u32)xbufp->len);
2657 struct kvec iov = {
2658 .iov_base = &marker,
2659 .iov_len = sizeof(marker),
2660 };
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002661
Chuck Lever067fb112019-02-11 11:24:37 -05002662 len = kernel_sendmsg(transport->sock, &msg, &iov, 1, iov.iov_len);
2663 if (len != iov.iov_len)
2664 return -EAGAIN;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002665
Chuck Lever067fb112019-02-11 11:24:37 -05002666 tailpage = NULL;
2667 if (xbufp->tail[0].iov_len)
2668 tailpage = virt_to_page(xbufp->tail[0].iov_base);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002669 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2670 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
Chuck Lever067fb112019-02-11 11:24:37 -05002671 len = svc_send_common(transport->sock, xbufp,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002672 virt_to_page(xbufp->head[0].iov_base), headoff,
Chuck Lever067fb112019-02-11 11:24:37 -05002673 tailpage, tailoff);
2674 if (len != xbufp->len)
2675 return -EAGAIN;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002676 return len;
2677}
2678
2679/*
2680 * The send routine. Borrows from svc_send
2681 */
Trond Myklebustadfa7142018-09-03 23:58:59 -04002682static int bc_send_request(struct rpc_rqst *req)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002683{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002684 struct svc_xprt *xprt;
Andrzej Hajda7fc56132015-09-24 16:00:09 +02002685 int len;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002686
2687 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2688 /*
2689 * Get the server socket associated with this callback xprt
2690 */
2691 xprt = req->rq_xprt->bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002692
2693 /*
2694 * Grab the mutex to serialize data as the connection is shared
2695 * with the fore channel
2696 */
Trond Myklebustc5445772018-09-03 23:39:27 -04002697 mutex_lock(&xprt->xpt_mutex);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002698 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2699 len = -ENOTCONN;
2700 else
2701 len = bc_sendto(req);
2702 mutex_unlock(&xprt->xpt_mutex);
2703
2704 if (len > 0)
2705 len = 0;
2706
2707 return len;
2708}
2709
2710/*
2711 * The close routine. Since this is client initiated, we do nothing
2712 */
2713
2714static void bc_close(struct rpc_xprt *xprt)
2715{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002716}
2717
2718/*
2719 * The xprt destroy routine. Again, because this connection is client
2720 * initiated, we do nothing
2721 */
2722
2723static void bc_destroy(struct rpc_xprt *xprt)
2724{
Kinglong Mee47f72ef2014-03-24 11:58:16 +08002725 dprintk("RPC: bc_destroy xprt %p\n", xprt);
2726
2727 xs_xprt_free(xprt);
2728 module_put(THIS_MODULE);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002729}
2730
Chuck Leverd31ae252017-08-01 12:00:39 -04002731static const struct rpc_xprt_ops xs_local_ops = {
Chuck Lever176e21e2011-05-09 15:22:44 -04002732 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebust4cd34e72018-08-31 10:00:02 -04002733 .release_xprt = xprt_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002734 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002735 .free_slot = xprt_free_slot,
Chuck Lever176e21e2011-05-09 15:22:44 -04002736 .rpcbind = xs_local_rpcbind,
2737 .set_port = xs_local_set_port,
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002738 .connect = xs_local_connect,
Chuck Lever176e21e2011-05-09 15:22:44 -04002739 .buf_alloc = rpc_malloc,
2740 .buf_free = rpc_free,
Trond Myklebust550aebf2018-09-14 14:32:45 -04002741 .prepare_request = xs_stream_prepare_request,
Chuck Lever176e21e2011-05-09 15:22:44 -04002742 .send_request = xs_local_send_request,
Trond Myklebust8ba6a922019-04-07 13:58:46 -04002743 .wait_for_reply_request = xprt_wait_for_reply_request_def,
Chuck Lever176e21e2011-05-09 15:22:44 -04002744 .close = xs_close,
Trond Myklebusta1311d82013-10-31 09:18:49 -04002745 .destroy = xs_destroy,
Chuck Lever176e21e2011-05-09 15:22:44 -04002746 .print_stats = xs_local_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002747 .enable_swap = xs_enable_swap,
2748 .disable_swap = xs_disable_swap,
Chuck Lever176e21e2011-05-09 15:22:44 -04002749};
2750
Chuck Leverd31ae252017-08-01 12:00:39 -04002751static const struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002752 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002753 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002754 .release_xprt = xprt_release_xprt_cong,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002755 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002756 .free_slot = xprt_free_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002757 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002758 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002759 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002760 .buf_alloc = rpc_malloc,
2761 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002762 .send_request = xs_udp_send_request,
Trond Myklebust8ba6a922019-04-07 13:58:46 -04002763 .wait_for_reply_request = xprt_wait_for_reply_request_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002764 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002765 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002766 .close = xs_close,
2767 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002768 .print_stats = xs_udp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002769 .enable_swap = xs_enable_swap,
2770 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002771 .inject_disconnect = xs_inject_disconnect,
Chuck Lever262965f2005-08-11 16:25:56 -04002772};
2773
Chuck Leverd31ae252017-08-01 12:00:39 -04002774static const struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002775 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebust4cd34e72018-08-31 10:00:02 -04002776 .release_xprt = xprt_release_xprt,
Trond Myklebust36bd7de2018-09-03 18:41:32 -04002777 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002778 .free_slot = xprt_free_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002779 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002780 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002781 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002782 .buf_alloc = rpc_malloc,
2783 .buf_free = rpc_free,
Trond Myklebust277e4ab2018-09-14 09:49:06 -04002784 .prepare_request = xs_stream_prepare_request,
Chuck Lever262965f2005-08-11 16:25:56 -04002785 .send_request = xs_tcp_send_request,
Trond Myklebust8ba6a922019-04-07 13:58:46 -04002786 .wait_for_reply_request = xprt_wait_for_reply_request_def,
Trond Myklebustc627d312015-02-10 11:06:04 -05002787 .close = xs_tcp_shutdown,
Chuck Lever9903cd12005-08-11 16:25:26 -04002788 .destroy = xs_destroy,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002789 .set_connect_timeout = xs_tcp_set_connect_timeout,
Chuck Lever262ca072006-03-20 13:44:16 -05002790 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002791 .enable_swap = xs_enable_swap,
2792 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002793 .inject_disconnect = xs_inject_disconnect,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002794#ifdef CONFIG_SUNRPC_BACKCHANNEL
2795 .bc_setup = xprt_setup_bc,
Chuck Lever6b26cc82016-05-02 14:40:40 -04002796 .bc_maxpayload = xs_tcp_bc_maxpayload,
Trond Myklebust7402a4f2019-07-16 13:51:29 -04002797 .bc_num_slots = xprt_bc_max_slots,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002798 .bc_free_rqst = xprt_free_bc_rqst,
2799 .bc_destroy = xprt_destroy_bc,
2800#endif
Chuck Levera246b012005-08-11 16:25:23 -04002801};
2802
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002803/*
2804 * The rpc_xprt_ops for the server backchannel
2805 */
2806
Chuck Leverd31ae252017-08-01 12:00:39 -04002807static const struct rpc_xprt_ops bc_tcp_ops = {
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002808 .reserve_xprt = xprt_reserve_xprt,
2809 .release_xprt = xprt_release_xprt,
Bryan Schumaker84e28a32012-09-24 13:39:01 -04002810 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002811 .free_slot = xprt_free_slot,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002812 .buf_alloc = bc_malloc,
2813 .buf_free = bc_free,
2814 .send_request = bc_send_request,
Trond Myklebust8ba6a922019-04-07 13:58:46 -04002815 .wait_for_reply_request = xprt_wait_for_reply_request_def,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002816 .close = bc_close,
2817 .destroy = bc_destroy,
2818 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002819 .enable_swap = xs_enable_swap,
2820 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002821 .inject_disconnect = xs_inject_disconnect,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002822};
2823
Chuck Lever92476852010-10-20 11:53:01 -04002824static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2825{
2826 static const struct sockaddr_in sin = {
2827 .sin_family = AF_INET,
2828 .sin_addr.s_addr = htonl(INADDR_ANY),
2829 };
2830 static const struct sockaddr_in6 sin6 = {
2831 .sin6_family = AF_INET6,
2832 .sin6_addr = IN6ADDR_ANY_INIT,
2833 };
2834
2835 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002836 case AF_LOCAL:
2837 break;
Chuck Lever92476852010-10-20 11:53:01 -04002838 case AF_INET:
2839 memcpy(sap, &sin, sizeof(sin));
2840 break;
2841 case AF_INET6:
2842 memcpy(sap, &sin6, sizeof(sin6));
2843 break;
2844 default:
2845 dprintk("RPC: %s: Bad address family\n", __func__);
2846 return -EAFNOSUPPORT;
2847 }
2848 return 0;
2849}
2850
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002851static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002852 unsigned int slot_table_size,
2853 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002854{
2855 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002856 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002857
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002858 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002859 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002860 return ERR_PTR(-EBADF);
2861 }
2862
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002863 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2864 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002865 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002866 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2867 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002868 return ERR_PTR(-ENOMEM);
2869 }
2870
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002871 new = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002872 mutex_init(&new->recv_mutex);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002873 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2874 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002875 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002876 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002877 else {
2878 int err;
2879 err = xs_init_anyaddr(args->dstaddr->sa_family,
2880 (struct sockaddr *)&new->srcaddr);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002881 if (err != 0) {
2882 xprt_free(xprt);
Chuck Lever92476852010-10-20 11:53:01 -04002883 return ERR_PTR(err);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002884 }
Chuck Lever92476852010-10-20 11:53:01 -04002885 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002886
2887 return xprt;
2888}
2889
Chuck Lever176e21e2011-05-09 15:22:44 -04002890static const struct rpc_timeout xs_local_default_timeout = {
2891 .to_initval = 10 * HZ,
2892 .to_maxval = 10 * HZ,
2893 .to_retries = 2,
2894};
2895
2896/**
2897 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2898 * @args: rpc transport creation arguments
2899 *
2900 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2901 */
2902static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2903{
2904 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2905 struct sock_xprt *transport;
2906 struct rpc_xprt *xprt;
2907 struct rpc_xprt *ret;
2908
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002909 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2910 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002911 if (IS_ERR(xprt))
2912 return xprt;
2913 transport = container_of(xprt, struct sock_xprt, xprt);
2914
2915 xprt->prot = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -04002916 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2917
2918 xprt->bind_timeout = XS_BIND_TO;
2919 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2920 xprt->idle_timeout = XS_IDLE_DISC_TO;
2921
2922 xprt->ops = &xs_local_ops;
2923 xprt->timeout = &xs_local_default_timeout;
2924
Trond Myklebust550aebf2018-09-14 14:32:45 -04002925 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
Trond Myklebust4f8943f2019-05-01 16:28:29 -04002926 INIT_WORK(&transport->error_worker, xs_error_handle);
Trond Myklebust550aebf2018-09-14 14:32:45 -04002927 INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket);
NeilBrown93dc41b2013-10-31 16:14:36 +11002928
Chuck Lever176e21e2011-05-09 15:22:44 -04002929 switch (sun->sun_family) {
2930 case AF_LOCAL:
2931 if (sun->sun_path[0] != '/') {
2932 dprintk("RPC: bad AF_LOCAL address: %s\n",
2933 sun->sun_path);
2934 ret = ERR_PTR(-EINVAL);
2935 goto out_err;
2936 }
2937 xprt_set_bound(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002938 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
J. Bruce Fields7073ea82013-02-21 10:14:22 -05002939 ret = ERR_PTR(xs_local_setup_socket(transport));
2940 if (ret)
2941 goto out_err;
Chuck Lever176e21e2011-05-09 15:22:44 -04002942 break;
2943 default:
2944 ret = ERR_PTR(-EAFNOSUPPORT);
2945 goto out_err;
2946 }
2947
2948 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2949 xprt->address_strings[RPC_DISPLAY_ADDR]);
2950
2951 if (try_module_get(THIS_MODULE))
2952 return xprt;
2953 ret = ERR_PTR(-EINVAL);
2954out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002955 xs_xprt_free(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002956 return ret;
2957}
2958
Trond Myklebust2881ae72007-12-20 16:03:54 -05002959static const struct rpc_timeout xs_udp_default_timeout = {
2960 .to_initval = 5 * HZ,
2961 .to_maxval = 30 * HZ,
2962 .to_increment = 5 * HZ,
2963 .to_retries = 5,
2964};
2965
Chuck Lever9903cd12005-08-11 16:25:26 -04002966/**
2967 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002968 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002969 *
2970 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002971static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002972{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002973 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002974 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002975 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002976 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002977
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002978 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2979 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002980 if (IS_ERR(xprt))
2981 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002982 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002983
Chuck Leverec739ef2006-08-22 20:06:15 -04002984 xprt->prot = IPPROTO_UDP;
Chuck Levera246b012005-08-11 16:25:23 -04002985 /* XXX: header size can vary due to auth type, IPv6, etc. */
2986 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2987
Chuck Lever03bf4b72005-08-25 16:25:55 -07002988 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002989 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2990 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002991
Chuck Lever262965f2005-08-11 16:25:56 -04002992 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002993
Trond Myklebustba7392b2007-12-20 16:03:55 -05002994 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002995
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002996 INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
Trond Myklebust4f8943f2019-05-01 16:28:29 -04002997 INIT_WORK(&transport->error_worker, xs_error_handle);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002998 INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2999
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003000 switch (addr->sa_family) {
3001 case AF_INET:
3002 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
3003 xprt_set_bound(xprt);
3004
Chuck Lever9dc3b092009-08-09 15:09:46 -04003005 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003006 break;
3007 case AF_INET6:
3008 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
3009 xprt_set_bound(xprt);
3010
Chuck Lever9dc3b092009-08-09 15:09:46 -04003011 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003012 break;
3013 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003014 ret = ERR_PTR(-EAFNOSUPPORT);
3015 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003016 }
3017
Chuck Leverc740eff2009-08-09 15:09:46 -04003018 if (xprt_bound(xprt))
3019 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3020 xprt->address_strings[RPC_DISPLAY_ADDR],
3021 xprt->address_strings[RPC_DISPLAY_PORT],
3022 xprt->address_strings[RPC_DISPLAY_PROTO]);
3023 else
3024 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
3025 xprt->address_strings[RPC_DISPLAY_ADDR],
3026 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04003027
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003028 if (try_module_get(THIS_MODULE))
3029 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003030 ret = ERR_PTR(-EINVAL);
3031out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003032 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003033 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04003034}
3035
Trond Myklebust2881ae72007-12-20 16:03:54 -05003036static const struct rpc_timeout xs_tcp_default_timeout = {
3037 .to_initval = 60 * HZ,
3038 .to_maxval = 60 * HZ,
3039 .to_retries = 2,
3040};
3041
Chuck Lever9903cd12005-08-11 16:25:26 -04003042/**
3043 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02003044 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04003045 *
3046 */
Adrian Bunk483066d2007-10-24 18:24:02 +02003047static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04003048{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003049 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04003050 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05003051 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003052 struct rpc_xprt *ret;
Trond Myklebustb7993ce2013-04-14 11:42:00 -04003053 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
3054
3055 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
3056 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Levera246b012005-08-11 16:25:23 -04003057
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003058 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
Trond Myklebustb7993ce2013-04-14 11:42:00 -04003059 max_slot_table_size);
Chuck Leverc8541ec2006-10-17 14:44:27 -04003060 if (IS_ERR(xprt))
3061 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05003062 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04003063
Chuck Leverec739ef2006-08-22 20:06:15 -04003064 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07003065 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04003066
Chuck Lever03bf4b72005-08-25 16:25:55 -07003067 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07003068 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
3069 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04003070
Chuck Lever262965f2005-08-11 16:25:56 -04003071 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05003072 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04003073
Trond Myklebust3851f1c2016-08-04 00:08:45 -04003074 xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05003075 xprt->connect_timeout = xprt->timeout->to_initval *
3076 (xprt->timeout->to_retries + 1);
Trond Myklebust3851f1c2016-08-04 00:08:45 -04003077
Trond Myklebustc50b8ee2018-09-14 14:26:28 -04003078 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
Trond Myklebust4f8943f2019-05-01 16:28:29 -04003079 INIT_WORK(&transport->error_worker, xs_error_handle);
Trond Myklebustedc1b012015-10-05 10:53:49 -04003080 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
3081
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003082 switch (addr->sa_family) {
3083 case AF_INET:
3084 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
3085 xprt_set_bound(xprt);
3086
Chuck Lever9dc3b092009-08-09 15:09:46 -04003087 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003088 break;
3089 case AF_INET6:
3090 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
3091 xprt_set_bound(xprt);
3092
Chuck Lever9dc3b092009-08-09 15:09:46 -04003093 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003094 break;
3095 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003096 ret = ERR_PTR(-EAFNOSUPPORT);
3097 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003098 }
3099
Chuck Leverc740eff2009-08-09 15:09:46 -04003100 if (xprt_bound(xprt))
3101 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3102 xprt->address_strings[RPC_DISPLAY_ADDR],
3103 xprt->address_strings[RPC_DISPLAY_PORT],
3104 xprt->address_strings[RPC_DISPLAY_PROTO]);
3105 else
3106 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
3107 xprt->address_strings[RPC_DISPLAY_ADDR],
3108 xprt->address_strings[RPC_DISPLAY_PROTO]);
3109
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003110 if (try_module_get(THIS_MODULE))
3111 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003112 ret = ERR_PTR(-EINVAL);
3113out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003114 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003115 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04003116}
Chuck Lever282b32e2006-12-05 16:35:51 -05003117
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003118/**
3119 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3120 * @args: rpc transport creation arguments
3121 *
3122 */
3123static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
3124{
3125 struct sockaddr *addr = args->dstaddr;
3126 struct rpc_xprt *xprt;
3127 struct sock_xprt *transport;
3128 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003129 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003130
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003131 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3132 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003133 if (IS_ERR(xprt))
3134 return xprt;
3135 transport = container_of(xprt, struct sock_xprt, xprt);
3136
3137 xprt->prot = IPPROTO_TCP;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003138 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3139 xprt->timeout = &xs_tcp_default_timeout;
3140
3141 /* backchannel */
3142 xprt_set_bound(xprt);
3143 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003144 xprt->reestablish_timeout = 0;
3145 xprt->idle_timeout = 0;
3146
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003147 xprt->ops = &bc_tcp_ops;
3148
3149 switch (addr->sa_family) {
3150 case AF_INET:
3151 xs_format_peer_addresses(xprt, "tcp",
3152 RPCBIND_NETID_TCP);
3153 break;
3154 case AF_INET6:
3155 xs_format_peer_addresses(xprt, "tcp",
3156 RPCBIND_NETID_TCP6);
3157 break;
3158 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003159 ret = ERR_PTR(-EAFNOSUPPORT);
3160 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003161 }
3162
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04003163 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3164 xprt->address_strings[RPC_DISPLAY_ADDR],
3165 xprt->address_strings[RPC_DISPLAY_PORT],
3166 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003167
3168 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003169 * Once we've associated a backchannel xprt with a connection,
Weng Meiling28303ca2013-11-30 17:56:44 +08003170 * we want to keep it around as long as the connection lasts,
3171 * in case we need to start using it for a backchannel again;
3172 * this reference won't be dropped until bc_xprt is destroyed.
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003173 */
3174 xprt_get(xprt);
3175 args->bc_xprt->xpt_bc_xprt = xprt;
3176 xprt->bc_xprt = args->bc_xprt;
3177 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
3178 transport->sock = bc_sock->sk_sock;
3179 transport->inet = bc_sock->sk_sk;
3180
3181 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003182 * Since we don't want connections for the backchannel, we set
3183 * the xprt status to connected
3184 */
3185 xprt_set_connected(xprt);
3186
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003187 if (try_module_get(THIS_MODULE))
3188 return xprt;
Kinglong Mee642aab52014-03-24 12:00:28 +08003189
3190 args->bc_xprt->xpt_bc_xprt = NULL;
J. Bruce Fields39a9bea2016-05-17 12:38:21 -04003191 args->bc_xprt->xpt_bc_xps = NULL;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003192 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003193 ret = ERR_PTR(-EINVAL);
3194out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003195 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003196 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003197}
3198
Chuck Lever176e21e2011-05-09 15:22:44 -04003199static struct xprt_class xs_local_transport = {
3200 .list = LIST_HEAD_INIT(xs_local_transport.list),
3201 .name = "named UNIX socket",
3202 .owner = THIS_MODULE,
3203 .ident = XPRT_TRANSPORT_LOCAL,
3204 .setup = xs_setup_local,
3205};
3206
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003207static struct xprt_class xs_udp_transport = {
3208 .list = LIST_HEAD_INIT(xs_udp_transport.list),
3209 .name = "udp",
3210 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003211 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003212 .setup = xs_setup_udp,
3213};
3214
3215static struct xprt_class xs_tcp_transport = {
3216 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
3217 .name = "tcp",
3218 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003219 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003220 .setup = xs_setup_tcp,
3221};
3222
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003223static struct xprt_class xs_bc_tcp_transport = {
3224 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3225 .name = "tcp NFSv4.1 backchannel",
3226 .owner = THIS_MODULE,
3227 .ident = XPRT_TRANSPORT_BC_TCP,
3228 .setup = xs_setup_bc_tcp,
3229};
3230
Chuck Lever282b32e2006-12-05 16:35:51 -05003231/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003232 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05003233 *
3234 */
3235int init_socket_xprt(void)
3236{
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08003237 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08003238 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05003239
Chuck Lever176e21e2011-05-09 15:22:44 -04003240 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003241 xprt_register_transport(&xs_udp_transport);
3242 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003243 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003244
Chuck Lever282b32e2006-12-05 16:35:51 -05003245 return 0;
3246}
3247
3248/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003249 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05003250 *
3251 */
3252void cleanup_socket_xprt(void)
3253{
Chuck Leverfbf76682006-12-05 16:35:54 -05003254 if (sunrpc_table_header) {
3255 unregister_sysctl_table(sunrpc_table_header);
3256 sunrpc_table_header = NULL;
3257 }
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003258
Chuck Lever176e21e2011-05-09 15:22:44 -04003259 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003260 xprt_unregister_transport(&xs_udp_transport);
3261 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003262 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05003263}
Trond Myklebustcbf11072009-08-09 15:06:19 -04003264
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003265static int param_set_uint_minmax(const char *val,
3266 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003267 unsigned int min, unsigned int max)
3268{
Daniel Walter00cfaa92014-06-21 13:06:38 +01003269 unsigned int num;
Trond Myklebustcbf11072009-08-09 15:06:19 -04003270 int ret;
3271
3272 if (!val)
3273 return -EINVAL;
Daniel Walter00cfaa92014-06-21 13:06:38 +01003274 ret = kstrtouint(val, 0, &num);
Dan Carpenter9761a242017-02-19 00:34:59 +03003275 if (ret)
3276 return ret;
3277 if (num < min || num > max)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003278 return -EINVAL;
3279 *((unsigned int *)kp->arg) = num;
3280 return 0;
3281}
3282
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003283static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003284{
Frank Sorensonffb6ca32016-07-08 16:35:25 -05003285 return param_set_uint_minmax(val, kp,
J. Bruce Fields826799e2018-10-18 15:27:02 -04003286 RPC_MIN_RESVPORT,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003287 RPC_MAX_RESVPORT);
3288}
3289
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303290static const struct kernel_param_ops param_ops_portnr = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003291 .set = param_set_portnr,
3292 .get = param_get_uint,
3293};
3294
Trond Myklebustcbf11072009-08-09 15:06:19 -04003295#define param_check_portnr(name, p) \
3296 __param_check(name, p, unsigned int);
3297
3298module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3299module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3300
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003301static int param_set_slot_table_size(const char *val,
3302 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003303{
3304 return param_set_uint_minmax(val, kp,
3305 RPC_MIN_SLOT_TABLE,
3306 RPC_MAX_SLOT_TABLE);
3307}
3308
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303309static const struct kernel_param_ops param_ops_slot_table_size = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003310 .set = param_set_slot_table_size,
3311 .get = param_get_uint,
3312};
3313
Trond Myklebustcbf11072009-08-09 15:06:19 -04003314#define param_check_slot_table_size(name, p) \
3315 __param_check(name, p, unsigned int);
3316
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003317static int param_set_max_slot_table_size(const char *val,
3318 const struct kernel_param *kp)
3319{
3320 return param_set_uint_minmax(val, kp,
3321 RPC_MIN_SLOT_TABLE,
3322 RPC_MAX_SLOT_TABLE_LIMIT);
3323}
3324
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303325static const struct kernel_param_ops param_ops_max_slot_table_size = {
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003326 .set = param_set_max_slot_table_size,
3327 .get = param_get_uint,
3328};
3329
3330#define param_check_max_slot_table_size(name, p) \
3331 __param_check(name, p, unsigned int);
3332
Trond Myklebustcbf11072009-08-09 15:06:19 -04003333module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3334 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003335module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3336 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003337module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3338 slot_table_size, 0644);