blob: 42f45d33dc5675ce5980c994a891f310bcd7c8b8 [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 Myklebust277e4ab2018-09-14 09:49:06 -0400456 } else
Trond Myklebuste92053a2019-02-15 18:14:27 -0500457 offset = seek_init;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400458 ret = -EMSGSIZE;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400459out:
460 *read = offset - seek_init;
461 return ret;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400462sock_err:
463 offset += seek;
464 goto out;
465}
466
467static void
468xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf)
469{
470 if (!transport->recv.copied) {
471 if (buf->head[0].iov_len >= transport->recv.offset)
472 memcpy(buf->head[0].iov_base,
473 &transport->recv.xid,
474 transport->recv.offset);
475 transport->recv.copied = transport->recv.offset;
476 }
477}
478
479static bool
480xs_read_stream_request_done(struct sock_xprt *transport)
481{
482 return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT);
483}
484
Trond Myklebuste92053a2019-02-15 18:14:27 -0500485static void
486xs_read_stream_check_eor(struct sock_xprt *transport,
487 struct msghdr *msg)
488{
489 if (xs_read_stream_request_done(transport))
490 msg->msg_flags |= MSG_EOR;
491}
492
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400493static ssize_t
494xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg,
495 int flags, struct rpc_rqst *req)
496{
497 struct xdr_buf *buf = &req->rq_private_buf;
498 size_t want, read;
499 ssize_t ret;
500
501 xs_read_header(transport, buf);
502
503 want = transport->recv.len - transport->recv.offset;
Trond Myklebuste92053a2019-02-15 18:14:27 -0500504 if (want != 0) {
505 ret = xs_read_xdr_buf(transport->sock, msg, flags, buf,
506 transport->recv.copied + want,
507 transport->recv.copied,
508 &read);
509 transport->recv.offset += read;
510 transport->recv.copied += read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400511 }
512
Trond Myklebust727fcc62019-02-15 16:53:04 -0500513 if (transport->recv.offset == transport->recv.len)
514 xs_read_stream_check_eor(transport, msg);
515
Trond Myklebuste92053a2019-02-15 18:14:27 -0500516 if (want == 0)
517 return 0;
518
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400519 switch (ret) {
Trond Myklebustc4433052018-12-04 07:52:11 -0500520 default:
521 break;
Trond Myklebust26781ea2018-12-03 15:41:38 -0500522 case -EFAULT:
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400523 case -EMSGSIZE:
Trond Myklebust26781ea2018-12-03 15:41:38 -0500524 msg->msg_flags |= MSG_TRUNC;
Trond Myklebustc4433052018-12-04 07:52:11 -0500525 return read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400526 case 0:
527 return -ESHUTDOWN;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400528 }
Trond Myklebustc4433052018-12-04 07:52:11 -0500529 return ret < 0 ? ret : read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400530}
531
532static size_t
533xs_read_stream_headersize(bool isfrag)
534{
535 if (isfrag)
536 return sizeof(__be32);
537 return 3 * sizeof(__be32);
538}
539
540static ssize_t
541xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg,
542 int flags, size_t want, size_t seek)
543{
544 struct kvec kvec = {
545 .iov_base = &transport->recv.fraghdr,
546 .iov_len = want,
547 };
548 return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek);
549}
550
551#if defined(CONFIG_SUNRPC_BACKCHANNEL)
552static ssize_t
553xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
554{
555 struct rpc_xprt *xprt = &transport->xprt;
556 struct rpc_rqst *req;
557 ssize_t ret;
558
559 /* Look up and lock the request corresponding to the given XID */
560 req = xprt_lookup_bc_request(xprt, transport->recv.xid);
561 if (!req) {
562 printk(KERN_WARNING "Callback slot table overflowed\n");
563 return -ESHUTDOWN;
564 }
565
566 ret = xs_read_stream_request(transport, msg, flags, req);
567 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
Trond Myklebustc4433052018-12-04 07:52:11 -0500568 xprt_complete_bc_request(req, transport->recv.copied);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400569
570 return ret;
571}
572#else /* CONFIG_SUNRPC_BACKCHANNEL */
573static ssize_t
574xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
575{
576 return -ESHUTDOWN;
577}
578#endif /* CONFIG_SUNRPC_BACKCHANNEL */
579
580static ssize_t
581xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags)
582{
583 struct rpc_xprt *xprt = &transport->xprt;
584 struct rpc_rqst *req;
585 ssize_t ret = 0;
586
587 /* Look up and lock the request corresponding to the given XID */
588 spin_lock(&xprt->queue_lock);
589 req = xprt_lookup_rqst(xprt, transport->recv.xid);
590 if (!req) {
591 msg->msg_flags |= MSG_TRUNC;
592 goto out;
593 }
594 xprt_pin_rqst(req);
595 spin_unlock(&xprt->queue_lock);
596
597 ret = xs_read_stream_request(transport, msg, flags, req);
598
599 spin_lock(&xprt->queue_lock);
600 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
Trond Myklebustc4433052018-12-04 07:52:11 -0500601 xprt_complete_rqst(req->rq_task, transport->recv.copied);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400602 xprt_unpin_rqst(req);
603out:
604 spin_unlock(&xprt->queue_lock);
605 return ret;
606}
607
608static ssize_t
609xs_read_stream(struct sock_xprt *transport, int flags)
610{
611 struct msghdr msg = { 0 };
612 size_t want, read = 0;
613 ssize_t ret = 0;
614
615 if (transport->recv.len == 0) {
616 want = xs_read_stream_headersize(transport->recv.copied != 0);
617 ret = xs_read_stream_header(transport, &msg, flags, want,
618 transport->recv.offset);
619 if (ret <= 0)
620 goto out_err;
621 transport->recv.offset = ret;
Trond Myklebustc4433052018-12-04 07:52:11 -0500622 if (transport->recv.offset != want)
623 return transport->recv.offset;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400624 transport->recv.len = be32_to_cpu(transport->recv.fraghdr) &
625 RPC_FRAGMENT_SIZE_MASK;
626 transport->recv.offset -= sizeof(transport->recv.fraghdr);
627 read = ret;
628 }
629
630 switch (be32_to_cpu(transport->recv.calldir)) {
Trond Myklebustc4433052018-12-04 07:52:11 -0500631 default:
632 msg.msg_flags |= MSG_TRUNC;
633 break;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400634 case RPC_CALL:
635 ret = xs_read_stream_call(transport, &msg, flags);
636 break;
637 case RPC_REPLY:
638 ret = xs_read_stream_reply(transport, &msg, flags);
639 }
640 if (msg.msg_flags & MSG_TRUNC) {
641 transport->recv.calldir = cpu_to_be32(-1);
642 transport->recv.copied = -1;
643 }
644 if (ret < 0)
645 goto out_err;
646 read += ret;
647 if (transport->recv.offset < transport->recv.len) {
Trond Myklebustc4433052018-12-04 07:52:11 -0500648 if (!(msg.msg_flags & MSG_TRUNC))
649 return read;
Trond Myklebustb76a5af2018-12-03 15:58:58 -0500650 msg.msg_flags = 0;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400651 ret = xs_read_discard(transport->sock, &msg, flags,
652 transport->recv.len - transport->recv.offset);
653 if (ret <= 0)
654 goto out_err;
655 transport->recv.offset += ret;
656 read += ret;
657 if (transport->recv.offset != transport->recv.len)
Trond Myklebustc4433052018-12-04 07:52:11 -0500658 return read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400659 }
660 if (xs_read_stream_request_done(transport)) {
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400661 trace_xs_stream_read_request(transport);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400662 transport->recv.copied = 0;
663 }
664 transport->recv.offset = 0;
665 transport->recv.len = 0;
666 return read;
667out_err:
Trond Myklebust794628572018-12-03 18:49:00 -0500668 return ret != 0 ? ret : -ESHUTDOWN;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400669}
670
Trond Myklebust0ffe86f2019-01-30 14:51:26 -0500671static __poll_t xs_poll_socket(struct sock_xprt *transport)
672{
Trond Myklebusta73881c2019-02-26 06:33:02 -0500673 return transport->sock->ops->poll(transport->file, transport->sock,
674 NULL);
Trond Myklebust0ffe86f2019-01-30 14:51:26 -0500675}
676
677static bool xs_poll_socket_readable(struct sock_xprt *transport)
678{
679 __poll_t events = xs_poll_socket(transport);
680
681 return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP);
682}
683
684static void xs_poll_check_readable(struct sock_xprt *transport)
685{
686
687 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
688 if (!xs_poll_socket_readable(transport))
689 return;
690 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
691 queue_work(xprtiod_workqueue, &transport->recv_worker);
692}
693
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400694static void xs_stream_data_receive(struct sock_xprt *transport)
695{
696 size_t read = 0;
697 ssize_t ret = 0;
698
699 mutex_lock(&transport->recv_mutex);
700 if (transport->sock == NULL)
701 goto out;
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400702 for (;;) {
703 ret = xs_read_stream(transport, MSG_DONTWAIT);
Trond Myklebustc4433052018-12-04 07:52:11 -0500704 if (ret < 0)
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400705 break;
706 read += ret;
707 cond_resched();
708 }
Trond Myklebust5f52a9d2019-02-16 09:31:47 -0500709 if (ret == -ESHUTDOWN)
710 kernel_sock_shutdown(transport->sock, SHUT_RDWR);
711 else
712 xs_poll_check_readable(transport);
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400713out:
714 mutex_unlock(&transport->recv_mutex);
715 trace_xs_stream_read_data(&transport->xprt, ret, read);
716}
717
718static void xs_stream_data_receive_workfn(struct work_struct *work)
719{
720 struct sock_xprt *transport =
721 container_of(work, struct sock_xprt, recv_worker);
Trond Myklebusta1231fd2019-02-18 10:02:29 -0500722 unsigned int pflags = memalloc_nofs_save();
723
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400724 xs_stream_data_receive(transport);
Trond Myklebusta1231fd2019-02-18 10:02:29 -0500725 memalloc_nofs_restore(pflags);
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400726}
727
Trond Myklebust550aebf2018-09-14 14:32:45 -0400728static void
729xs_stream_reset_connect(struct sock_xprt *transport)
730{
731 transport->recv.offset = 0;
732 transport->recv.len = 0;
733 transport->recv.copied = 0;
734 transport->xmit.offset = 0;
Trond Myklebustae053552019-02-20 14:56:20 -0500735}
736
737static void
738xs_stream_start_connect(struct sock_xprt *transport)
739{
Trond Myklebust550aebf2018-09-14 14:32:45 -0400740 transport->xprt.stat.connect_count++;
741 transport->xprt.stat.connect_start = jiffies;
742}
743
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400744#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
745
Trond Myklebuste791f8e2019-02-19 12:39:40 -0500746static int xs_sendmsg(struct socket *sock, struct msghdr *msg, size_t seek)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400747{
Trond Myklebuste791f8e2019-02-19 12:39:40 -0500748 if (seek)
749 iov_iter_advance(&msg->msg_iter, seek);
750 return sock_sendmsg(sock, msg);
751}
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400752
Trond Myklebuste791f8e2019-02-19 12:39:40 -0500753static int xs_send_kvec(struct socket *sock, struct msghdr *msg, struct kvec *vec, size_t seek)
754{
Trond Myklebuste791f8e2019-02-19 12:39:40 -0500755 iov_iter_kvec(&msg->msg_iter, WRITE, vec, 1, vec->iov_len);
756 return xs_sendmsg(sock, msg, seek);
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400757}
758
Trond Myklebust0472e472019-02-19 13:00:13 -0500759static int xs_send_pagedata(struct socket *sock, struct msghdr *msg, struct xdr_buf *xdr, size_t base)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400760{
Jason Baronf279cd02014-09-24 18:08:00 +0000761 int err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400762
Trond Myklebust0472e472019-02-19 13:00:13 -0500763 err = xdr_alloc_bvec(xdr, GFP_KERNEL);
764 if (err < 0)
765 return err;
Trond Myklebust24c56842006-10-17 15:06:22 -0400766
Trond Myklebust0472e472019-02-19 13:00:13 -0500767 iov_iter_bvec(&msg->msg_iter, WRITE, xdr->bvec,
768 xdr_buf_pagecount(xdr),
769 xdr->page_len + xdr->page_base);
770 return xs_sendmsg(sock, msg, base + xdr->page_base);
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400771}
772
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500773#define xs_record_marker_len() sizeof(rpc_fraghdr)
774
Chuck Lever067fb112019-02-11 11:24:37 -0500775/* Common case:
776 * - stream transport
777 * - sending from byte 0 of the message
778 * - the message is wholly contained in @xdr's head iovec
779 */
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500780static int xs_send_rm_and_kvec(struct socket *sock, struct msghdr *msg,
781 rpc_fraghdr marker, struct kvec *vec, size_t base)
Chuck Lever067fb112019-02-11 11:24:37 -0500782{
Chuck Lever067fb112019-02-11 11:24:37 -0500783 struct kvec iov[2] = {
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500784 [0] = {
Chuck Lever067fb112019-02-11 11:24:37 -0500785 .iov_base = &marker,
786 .iov_len = sizeof(marker)
787 },
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500788 [1] = *vec,
Chuck Lever067fb112019-02-11 11:24:37 -0500789 };
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500790 size_t len = iov[0].iov_len + iov[1].iov_len;
Chuck Lever067fb112019-02-11 11:24:37 -0500791
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500792 iov_iter_kvec(&msg->msg_iter, WRITE, iov, 2, len);
793 return xs_sendmsg(sock, msg, base);
Chuck Levera246b012005-08-11 16:25:23 -0400794}
795
Chuck Lever9903cd12005-08-11 16:25:26 -0400796/**
797 * xs_sendpages - write pages directly to a socket
798 * @sock: socket to send on
799 * @addr: UDP only -- address of destination
800 * @addrlen: UDP only -- length of destination address
801 * @xdr: buffer containing this request
802 * @base: starting position in the buffer
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500803 * @rm: stream record marker field
Jason Baronf279cd02014-09-24 18:08:00 +0000804 * @sent_p: return the total number of bytes successfully queued for sending
Chuck Lever9903cd12005-08-11 16:25:26 -0400805 *
Chuck Levera246b012005-08-11 16:25:23 -0400806 */
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500807static 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 -0400808{
Trond Myklebuste791f8e2019-02-19 12:39:40 -0500809 struct msghdr msg = {
810 .msg_name = addr,
811 .msg_namelen = addrlen,
812 .msg_flags = XS_SENDMSG_FLAGS | MSG_MORE,
813 };
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500814 unsigned int rmsize = rm ? sizeof(rm) : 0;
815 unsigned int remainder = rmsize + xdr->len - base;
816 unsigned int want;
Jason Baronf279cd02014-09-24 18:08:00 +0000817 int err = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400818
Chuck Lever262965f2005-08-11 16:25:56 -0400819 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400820 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400821
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500822 want = xdr->head[0].iov_len + rmsize;
823 if (base < want) {
824 unsigned int len = want - base;
Trond Myklebust24c56842006-10-17 15:06:22 -0400825 remainder -= len;
Trond Myklebuste791f8e2019-02-19 12:39:40 -0500826 if (remainder == 0)
827 msg.msg_flags &= ~MSG_MORE;
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500828 if (rmsize)
829 err = xs_send_rm_and_kvec(sock, &msg, rm,
830 &xdr->head[0], base);
Chuck Lever067fb112019-02-11 11:24:37 -0500831 else
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500832 err = xs_send_kvec(sock, &msg, &xdr->head[0], base);
Trond Myklebust24c56842006-10-17 15:06:22 -0400833 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400834 goto out;
Jason Baronf279cd02014-09-24 18:08:00 +0000835 *sent_p += err;
Chuck Levera246b012005-08-11 16:25:23 -0400836 base = 0;
837 } else
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500838 base -= want;
Chuck Levera246b012005-08-11 16:25:23 -0400839
Trond Myklebust24c56842006-10-17 15:06:22 -0400840 if (base < xdr->page_len) {
841 unsigned int len = xdr->page_len - base;
842 remainder -= len;
Trond Myklebust0472e472019-02-19 13:00:13 -0500843 if (remainder == 0)
844 msg.msg_flags &= ~MSG_MORE;
845 err = xs_send_pagedata(sock, &msg, xdr, base);
846 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400847 goto out;
Trond Myklebust0472e472019-02-19 13:00:13 -0500848 *sent_p += err;
Chuck Levera246b012005-08-11 16:25:23 -0400849 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400850 } else
851 base -= xdr->page_len;
852
853 if (base >= xdr->tail[0].iov_len)
Jason Baronf279cd02014-09-24 18:08:00 +0000854 return 0;
Trond Myklebuste791f8e2019-02-19 12:39:40 -0500855 msg.msg_flags &= ~MSG_MORE;
856 err = xs_send_kvec(sock, &msg, &xdr->tail[0], base);
Chuck Levera246b012005-08-11 16:25:23 -0400857out:
Jason Baronf279cd02014-09-24 18:08:00 +0000858 if (err > 0) {
859 *sent_p += err;
860 err = 0;
861 }
862 return err;
Chuck Levera246b012005-08-11 16:25:23 -0400863}
864
Chuck Lever9903cd12005-08-11 16:25:26 -0400865/**
Trond Myklebustc5445772018-09-03 23:39:27 -0400866 * xs_nospace - handle transmit was incomplete
Trond Myklebust50f484e2018-08-30 13:27:29 -0400867 * @req: pointer to RPC request
Chuck Lever9903cd12005-08-11 16:25:26 -0400868 *
Chuck Levera246b012005-08-11 16:25:23 -0400869 */
Trond Myklebustc5445772018-09-03 23:39:27 -0400870static int xs_nospace(struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -0400871{
Chuck Lever262965f2005-08-11 16:25:56 -0400872 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500873 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500874 struct sock *sk = transport->inet;
Trond Myklebust24ca9a82011-11-22 14:44:28 +0200875 int ret = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400876
Chuck Lever46121cf2007-01-31 12:14:08 -0500877 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Trond Myklebustc5445772018-09-03 23:39:27 -0400878 req->rq_task->tk_pid,
879 req->rq_slen - transport->xmit.offset,
Chuck Lever262965f2005-08-11 16:25:56 -0400880 req->rq_slen);
881
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400882 /* Protect against races with write_space */
883 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400884
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400885 /* Don't race with disconnect */
886 if (xprt_connected(xprt)) {
Trond Myklebust13331a52016-01-06 08:57:06 -0500887 /* wait for more buffer space */
888 sk->sk_write_pending++;
Trond Myklebustc5445772018-09-03 23:39:27 -0400889 xprt_wait_for_buffer_space(xprt);
Trond Myklebust13331a52016-01-06 08:57:06 -0500890 } else
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400891 ret = -ENOTCONN;
Chuck Lever262965f2005-08-11 16:25:56 -0400892
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400893 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500894
895 /* Race breaker in case memory is freed before above code is called */
David Vrabeld48f9ce2016-09-19 13:58:30 +0100896 if (ret == -EAGAIN) {
897 struct socket_wq *wq;
898
899 rcu_read_lock();
900 wq = rcu_dereference(sk->sk_wq);
901 set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags);
902 rcu_read_unlock();
903
904 sk->sk_write_space(sk);
905 }
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400906 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400907}
908
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400909static void
910xs_stream_prepare_request(struct rpc_rqst *req)
911{
Trond Myklebust12a3ad62019-03-02 10:14:02 -0500912 req->rq_task->tk_status = xdr_alloc_bvec(&req->rq_rcv_buf, GFP_KERNEL);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400913}
914
Chuck Lever61677ee2011-05-09 15:22:34 -0400915/*
Trond Myklebust4cd34e72018-08-31 10:00:02 -0400916 * Determine if the previous message in the stream was aborted before it
917 * could complete transmission.
918 */
919static bool
920xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
921{
922 return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
923}
924
925/*
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500926 * Return the stream record marker field for a record of length < 2^31-1
Chuck Lever61677ee2011-05-09 15:22:34 -0400927 */
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500928static rpc_fraghdr
929xs_stream_record_marker(struct xdr_buf *xdr)
Chuck Lever61677ee2011-05-09 15:22:34 -0400930{
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500931 if (!xdr->len)
932 return 0;
933 return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len);
Chuck Lever61677ee2011-05-09 15:22:34 -0400934}
935
Chuck Lever262965f2005-08-11 16:25:56 -0400936/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400937 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
Trond Myklebust50f484e2018-08-30 13:27:29 -0400938 * @req: pointer to RPC request
Chuck Lever176e21e2011-05-09 15:22:44 -0400939 *
940 * Return values:
941 * 0: The request has been sent
942 * EAGAIN: The socket was blocked, please call again later to
943 * complete the request
944 * ENOTCONN: Caller needs to invoke connect logic then call again
945 * other: Some other error occured, the request was not sent
946 */
Trond Myklebustadfa7142018-09-03 23:58:59 -0400947static int xs_local_send_request(struct rpc_rqst *req)
Chuck Lever176e21e2011-05-09 15:22:44 -0400948{
Chuck Lever176e21e2011-05-09 15:22:44 -0400949 struct rpc_xprt *xprt = req->rq_xprt;
950 struct sock_xprt *transport =
951 container_of(xprt, struct sock_xprt, xprt);
952 struct xdr_buf *xdr = &req->rq_snd_buf;
953 int status;
Jason Baronf279cd02014-09-24 18:08:00 +0000954 int sent = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -0400955
Trond Myklebust4cd34e72018-08-31 10:00:02 -0400956 /* Close the stream if the previous transmission was incomplete */
957 if (xs_send_request_was_aborted(transport, req)) {
958 xs_close(xprt);
959 return -ENOTCONN;
960 }
961
Chuck Lever176e21e2011-05-09 15:22:44 -0400962 xs_pktdump("packet data:",
963 req->rq_svec->iov_base, req->rq_svec->iov_len);
964
Chuck Lever78215752018-03-05 15:13:07 -0500965 req->rq_xtime = ktime_get();
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400966 status = xs_sendpages(transport->sock, NULL, 0, xdr,
967 transport->xmit.offset,
Trond Myklebust06b5fc32019-02-25 08:39:26 -0500968 xs_stream_record_marker(xdr),
Trond Myklebust6f903b12019-02-19 13:21:38 -0500969 &sent);
Chuck Lever176e21e2011-05-09 15:22:44 -0400970 dprintk("RPC: %s(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400971 __func__, xdr->len - transport->xmit.offset, status);
NeilBrown743c69e2015-07-27 10:55:35 +1000972
973 if (status == -EAGAIN && sock_writeable(transport->inet))
974 status = -ENOBUFS;
975
Jason Baronf279cd02014-09-24 18:08:00 +0000976 if (likely(sent > 0) || status == 0) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400977 transport->xmit.offset += sent;
978 req->rq_bytes_sent = transport->xmit.offset;
Chuck Lever176e21e2011-05-09 15:22:44 -0400979 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400980 req->rq_xmit_bytes_sent += transport->xmit.offset;
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400981 transport->xmit.offset = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -0400982 return 0;
983 }
984 status = -EAGAIN;
985 }
986
987 switch (status) {
Trond Myklebust3601c4a2014-06-30 13:42:19 -0400988 case -ENOBUFS:
Trond Myklebustb5872f02015-07-03 09:32:23 -0400989 break;
Chuck Lever176e21e2011-05-09 15:22:44 -0400990 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -0400991 status = xs_nospace(req);
Chuck Lever176e21e2011-05-09 15:22:44 -0400992 break;
993 default:
994 dprintk("RPC: sendmsg returned unrecognized error %d\n",
995 -status);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -0500996 /* fall through */
Chuck Lever176e21e2011-05-09 15:22:44 -0400997 case -EPIPE:
998 xs_close(xprt);
999 status = -ENOTCONN;
1000 }
1001
1002 return status;
1003}
1004
1005/**
Chuck Lever262965f2005-08-11 16:25:56 -04001006 * xs_udp_send_request - write an RPC request to a UDP socket
Trond Myklebust50f484e2018-08-30 13:27:29 -04001007 * @req: pointer to RPC request
Chuck Lever262965f2005-08-11 16:25:56 -04001008 *
1009 * Return values:
1010 * 0: The request has been sent
1011 * EAGAIN: The socket was blocked, please call again later to
1012 * complete the request
1013 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001014 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -04001015 */
Trond Myklebustadfa7142018-09-03 23:58:59 -04001016static int xs_udp_send_request(struct rpc_rqst *req)
Chuck Lever262965f2005-08-11 16:25:56 -04001017{
Chuck Lever262965f2005-08-11 16:25:56 -04001018 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001019 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -04001020 struct xdr_buf *xdr = &req->rq_snd_buf;
Jason Baronf279cd02014-09-24 18:08:00 +00001021 int sent = 0;
Chuck Lever262965f2005-08-11 16:25:56 -04001022 int status;
Chuck Levera246b012005-08-11 16:25:23 -04001023
Chuck Lever9903cd12005-08-11 16:25:26 -04001024 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -04001025 req->rq_svec->iov_base,
1026 req->rq_svec->iov_len);
1027
Trond Myklebust01d37c42009-03-11 14:09:39 -04001028 if (!xprt_bound(xprt))
1029 return -ENOTCONN;
Trond Myklebust75891f52018-09-03 17:37:36 -04001030
1031 if (!xprt_request_get_cong(xprt, req))
1032 return -EBADSLT;
1033
Chuck Lever78215752018-03-05 15:13:07 -05001034 req->rq_xtime = ktime_get();
Jason Baronf279cd02014-09-24 18:08:00 +00001035 status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen,
Trond Myklebust06b5fc32019-02-25 08:39:26 -05001036 xdr, 0, 0, &sent);
Chuck Levera246b012005-08-11 16:25:23 -04001037
Chuck Lever46121cf2007-01-31 12:14:08 -05001038 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001039 xdr->len, status);
Chuck Levera246b012005-08-11 16:25:23 -04001040
Jason Baron3dedbb52014-09-24 18:08:04 +00001041 /* firewall is blocking us, don't return -EAGAIN or we end up looping */
1042 if (status == -EPERM)
1043 goto process_status;
1044
NeilBrown743c69e2015-07-27 10:55:35 +10001045 if (status == -EAGAIN && sock_writeable(transport->inet))
1046 status = -ENOBUFS;
1047
Jason Baronf279cd02014-09-24 18:08:00 +00001048 if (sent > 0 || status == 0) {
1049 req->rq_xmit_bytes_sent += sent;
1050 if (sent >= req->rq_slen)
Trond Myklebust21997002007-10-01 11:43:37 -04001051 return 0;
1052 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -04001053 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -04001054 }
Chuck Levera246b012005-08-11 16:25:23 -04001055
Jason Baron3dedbb52014-09-24 18:08:04 +00001056process_status:
Chuck Lever262965f2005-08-11 16:25:56 -04001057 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -04001058 case -ENOTSOCK:
1059 status = -ENOTCONN;
1060 /* Should we call xs_close() here? */
1061 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -04001062 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -04001063 status = xs_nospace(req);
Trond Myklebustb6ddf642008-04-17 18:52:19 -04001064 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001065 case -ENETUNREACH:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001066 case -ENOBUFS:
Chuck Lever262965f2005-08-11 16:25:56 -04001067 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -04001068 case -ECONNREFUSED:
Jason Baron3dedbb52014-09-24 18:08:04 +00001069 case -EPERM:
Chuck Levera246b012005-08-11 16:25:23 -04001070 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -04001071 * prompts ECONNREFUSED. */
Trond Myklebust13331a52016-01-06 08:57:06 -05001072 break;
1073 default:
1074 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1075 -status);
Chuck Levera246b012005-08-11 16:25:23 -04001076 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +08001077
Chuck Lever262965f2005-08-11 16:25:56 -04001078 return status;
Chuck Levera246b012005-08-11 16:25:23 -04001079}
1080
Trond Myklebuste06799f2007-11-05 15:44:12 -05001081/**
Chuck Lever262965f2005-08-11 16:25:56 -04001082 * xs_tcp_send_request - write an RPC request to a TCP socket
Trond Myklebust50f484e2018-08-30 13:27:29 -04001083 * @req: pointer to RPC request
Chuck Lever9903cd12005-08-11 16:25:26 -04001084 *
1085 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -04001086 * 0: The request has been sent
1087 * EAGAIN: The socket was blocked, please call again later to
1088 * complete the request
1089 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001090 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -04001091 *
1092 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -04001093 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -04001094 */
Trond Myklebustadfa7142018-09-03 23:58:59 -04001095static int xs_tcp_send_request(struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001096{
Chuck Levera246b012005-08-11 16:25:23 -04001097 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001098 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -04001099 struct xdr_buf *xdr = &req->rq_snd_buf;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001100 bool vm_wait = false;
Chuck Leverb595bb12007-08-06 11:56:42 -04001101 int status;
Jason Baronf279cd02014-09-24 18:08:00 +00001102 int sent;
Chuck Levera246b012005-08-11 16:25:23 -04001103
Trond Myklebust4cd34e72018-08-31 10:00:02 -04001104 /* Close the stream if the previous transmission was incomplete */
1105 if (xs_send_request_was_aborted(transport, req)) {
1106 if (transport->sock != NULL)
1107 kernel_sock_shutdown(transport->sock, SHUT_RDWR);
1108 return -ENOTCONN;
1109 }
1110
Chuck Lever262965f2005-08-11 16:25:56 -04001111 xs_pktdump("packet data:",
1112 req->rq_svec->iov_base,
1113 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -04001114
Trond Myklebust7196dbb2017-02-08 11:17:54 -05001115 if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
1116 xs_tcp_set_socket_timeouts(xprt, transport->sock);
1117
Chuck Levera246b012005-08-11 16:25:23 -04001118 /* Continue transmitting the packet/record. We must be careful
1119 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -04001120 * called sendmsg(). */
Chuck Lever78215752018-03-05 15:13:07 -05001121 req->rq_xtime = ktime_get();
Chuck Levera246b012005-08-11 16:25:23 -04001122 while (1) {
Jason Baronf279cd02014-09-24 18:08:00 +00001123 sent = 0;
1124 status = xs_sendpages(transport->sock, NULL, 0, xdr,
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001125 transport->xmit.offset,
Trond Myklebust06b5fc32019-02-25 08:39:26 -05001126 xs_stream_record_marker(xdr),
Trond Myklebust6f903b12019-02-19 13:21:38 -05001127 &sent);
Chuck Levera246b012005-08-11 16:25:23 -04001128
Chuck Lever46121cf2007-01-31 12:14:08 -05001129 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001130 xdr->len - transport->xmit.offset, status);
Chuck Lever262965f2005-08-11 16:25:56 -04001131
Chuck Lever262965f2005-08-11 16:25:56 -04001132 /* If we've sent the entire packet, immediately
1133 * reset the count of bytes sent. */
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001134 transport->xmit.offset += sent;
1135 req->rq_bytes_sent = transport->xmit.offset;
Chuck Lever262965f2005-08-11 16:25:56 -04001136 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001137 req->rq_xmit_bytes_sent += transport->xmit.offset;
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001138 transport->xmit.offset = 0;
Chuck Lever262965f2005-08-11 16:25:56 -04001139 return 0;
Chuck Levera246b012005-08-11 16:25:23 -04001140 }
1141
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001142 WARN_ON_ONCE(sent == 0 && status == 0);
1143
1144 if (status == -EAGAIN ) {
1145 /*
1146 * Return EAGAIN if we're sure we're hitting the
1147 * socket send buffer limits.
1148 */
1149 if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
1150 break;
1151 /*
1152 * Did we hit a memory allocation failure?
1153 */
1154 if (sent == 0) {
1155 status = -ENOBUFS;
1156 if (vm_wait)
1157 break;
1158 /* Retry, knowing now that we're below the
1159 * socket send buffer limit
1160 */
1161 vm_wait = true;
1162 }
1163 continue;
1164 }
Trond Myklebustf580dd02015-07-11 17:48:52 +02001165 if (status < 0)
1166 break;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001167 vm_wait = false;
Chuck Levera246b012005-08-11 16:25:23 -04001168 }
1169
Chuck Lever262965f2005-08-11 16:25:56 -04001170 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -04001171 case -ENOTSOCK:
1172 status = -ENOTCONN;
1173 /* Should we call xs_close() here? */
1174 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001175 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -04001176 status = xs_nospace(req);
Chuck Lever262965f2005-08-11 16:25:56 -04001177 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001178 case -ECONNRESET:
Chuck Lever262965f2005-08-11 16:25:56 -04001179 case -ECONNREFUSED:
1180 case -ENOTCONN:
Trond Myklebust3913c782015-02-08 21:44:04 -05001181 case -EADDRINUSE:
Trond Myklebustb5872f02015-07-03 09:32:23 -04001182 case -ENOBUFS:
Trond Myklebustb9d2bb22012-10-23 11:40:02 -04001183 case -EPIPE:
Trond Myklebust13331a52016-01-06 08:57:06 -05001184 break;
1185 default:
1186 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1187 -status);
Chuck Levera246b012005-08-11 16:25:23 -04001188 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +08001189
Chuck Levera246b012005-08-11 16:25:23 -04001190 return status;
1191}
1192
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001193static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1194{
1195 transport->old_data_ready = sk->sk_data_ready;
1196 transport->old_state_change = sk->sk_state_change;
1197 transport->old_write_space = sk->sk_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -05001198 transport->old_error_report = sk->sk_error_report;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001199}
1200
1201static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1202{
1203 sk->sk_data_ready = transport->old_data_ready;
1204 sk->sk_state_change = transport->old_state_change;
1205 sk->sk_write_space = transport->old_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -05001206 sk->sk_error_report = transport->old_error_report;
1207}
1208
Trond Myklebust42d42a52016-05-23 09:24:55 -04001209static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
1210{
1211 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1212
1213 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
1214}
1215
Trond Myklebustb70ae912015-02-09 09:41:32 -05001216static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1217{
1218 smp_mb__before_atomic();
1219 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1220 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001221 xs_sock_reset_state_flags(xprt);
Trond Myklebustb70ae912015-02-09 09:41:32 -05001222 smp_mb__after_atomic();
1223}
1224
Trond Myklebust21180712013-12-31 13:22:59 -05001225/**
1226 * xs_error_report - callback to handle TCP socket state errors
1227 * @sk: socket
1228 *
1229 * Note: we don't call sock_error() since there may be a rpc_task
1230 * using the socket, and so we don't want to clear sk->sk_err.
1231 */
1232static void xs_error_report(struct sock *sk)
1233{
1234 struct rpc_xprt *xprt;
1235 int err;
1236
1237 read_lock_bh(&sk->sk_callback_lock);
1238 if (!(xprt = xprt_from_sock(sk)))
1239 goto out;
1240
1241 err = -sk->sk_err;
1242 if (err == 0)
1243 goto out;
1244 dprintk("RPC: xs_error_report client %p, error=%d...\n",
1245 xprt, -err);
Trond Myklebuste8353c762013-12-31 13:39:22 -05001246 trace_rpc_socket_error(xprt, sk->sk_socket, err);
Trond Myklebust21180712013-12-31 13:22:59 -05001247 xprt_wake_pending_tasks(xprt, err);
1248 out:
1249 read_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001250}
1251
Chuck Leverfe315e762009-03-11 14:10:21 -04001252static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001253{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001254 struct socket *sock = transport->sock;
1255 struct sock *sk = transport->inet;
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001256 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebusta73881c2019-02-26 06:33:02 -05001257 struct file *filp = transport->file;
Chuck Levera246b012005-08-11 16:25:23 -04001258
Chuck Leverfe315e762009-03-11 14:10:21 -04001259 if (sk == NULL)
1260 return;
Chuck Lever9903cd12005-08-11 16:25:26 -04001261
Jeff Layton264d1df2015-06-03 16:14:27 -04001262 if (atomic_read(&transport->xprt.swapper))
1263 sk_clear_memalloc(sk);
1264
Trond Myklebust09939202015-08-29 19:11:21 -07001265 kernel_sock_shutdown(sock, SHUT_RDWR);
1266
Trond Myklebustedc1b012015-10-05 10:53:49 -04001267 mutex_lock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -04001268 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001269 transport->inet = NULL;
1270 transport->sock = NULL;
Trond Myklebusta73881c2019-02-26 06:33:02 -05001271 transport->file = NULL;
Chuck Levera246b012005-08-11 16:25:23 -04001272
Chuck Lever9903cd12005-08-11 16:25:26 -04001273 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001274
1275 xs_restore_old_callbacks(transport, sk);
Trond Myklebust0c787892015-08-29 13:36:30 -07001276 xprt_clear_connected(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001277 write_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001278 xs_sock_reset_connection_flags(xprt);
Trond Myklebustae053552019-02-20 14:56:20 -05001279 /* Reset stream record info */
1280 xs_stream_reset_connect(transport);
Trond Myklebustedc1b012015-10-05 10:53:49 -04001281 mutex_unlock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -04001282
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001283 trace_rpc_socket_close(xprt, sock);
Trond Myklebusta73881c2019-02-26 06:33:02 -05001284 fput(filp);
Trond Myklebust0445f922018-12-17 13:34:59 -05001285
1286 xprt_disconnect_done(xprt);
Chuck Leverfe315e762009-03-11 14:10:21 -04001287}
1288
1289/**
1290 * xs_close - close a socket
1291 * @xprt: transport
1292 *
1293 * This is used when all requests are complete; ie, no DRC state remains
1294 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -04001295 *
1296 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1297 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e762009-03-11 14:10:21 -04001298 */
1299static void xs_close(struct rpc_xprt *xprt)
1300{
1301 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1302
1303 dprintk("RPC: xs_close xprt %p\n", xprt);
1304
1305 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -04001306 xprt->reestablish_timeout = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001307}
1308
Chuck Lever4a068252015-05-11 14:02:25 -04001309static void xs_inject_disconnect(struct rpc_xprt *xprt)
1310{
1311 dprintk("RPC: injecting transport disconnect on xprt=%p\n",
1312 xprt);
1313 xprt_disconnect_done(xprt);
1314}
1315
Kinglong Mee315f3812014-03-24 11:07:22 +08001316static void xs_xprt_free(struct rpc_xprt *xprt)
1317{
1318 xs_free_peer_addresses(xprt);
1319 xprt_free(xprt);
1320}
1321
Chuck Lever9903cd12005-08-11 16:25:26 -04001322/**
1323 * xs_destroy - prepare to shutdown a transport
1324 * @xprt: doomed transport
1325 *
1326 */
1327static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001328{
Trond Myklebust03c788272015-09-17 10:42:27 -04001329 struct sock_xprt *transport = container_of(xprt,
1330 struct sock_xprt, xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05001331 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001332
Trond Myklebust03c788272015-09-17 10:42:27 -04001333 cancel_delayed_work_sync(&transport->connect_worker);
Trond Myklebusta1311d82013-10-31 09:18:49 -04001334 xs_close(xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04001335 cancel_work_sync(&transport->recv_worker);
Kinglong Mee315f3812014-03-24 11:07:22 +08001336 xs_xprt_free(xprt);
Trond Myklebusta1311d82013-10-31 09:18:49 -04001337 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -04001338}
1339
Chuck Lever9903cd12005-08-11 16:25:26 -04001340/**
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001341 * xs_udp_data_read_skb - receive callback for UDP sockets
1342 * @xprt: transport
1343 * @sk: socket
1344 * @skb: skbuff
Chuck Lever9903cd12005-08-11 16:25:26 -04001345 *
1346 */
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001347static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1348 struct sock *sk,
1349 struct sk_buff *skb)
Chuck Lever9903cd12005-08-11 16:25:26 -04001350{
1351 struct rpc_task *task;
Chuck Levera246b012005-08-11 16:25:23 -04001352 struct rpc_rqst *rovr;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001353 int repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001354 u32 _xid;
1355 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -04001356
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001357 repsize = skb->len;
Chuck Levera246b012005-08-11 16:25:23 -04001358 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001359 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001360 return;
Chuck Levera246b012005-08-11 16:25:23 -04001361 }
1362
1363 /* Copy the XID from the skb... */
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001364 xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
Chuck Levera246b012005-08-11 16:25:23 -04001365 if (xp == NULL)
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001366 return;
Chuck Levera246b012005-08-11 16:25:23 -04001367
1368 /* Look up and lock the request corresponding to the given XID */
Trond Myklebust75c84152018-08-31 10:21:00 -04001369 spin_lock(&xprt->queue_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001370 rovr = xprt_lookup_rqst(xprt, *xp);
1371 if (!rovr)
1372 goto out_unlock;
Trond Myklebust729749b2017-08-13 10:03:59 -04001373 xprt_pin_rqst(rovr);
Chuck Leverecd465e2018-03-05 15:12:57 -05001374 xprt_update_rtt(rovr->rq_task);
Trond Myklebust75c84152018-08-31 10:21:00 -04001375 spin_unlock(&xprt->queue_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001376 task = rovr->rq_task;
1377
Chuck Levera246b012005-08-11 16:25:23 -04001378 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1379 copied = repsize;
1380
1381 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001382 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
Trond Myklebust75c84152018-08-31 10:21:00 -04001383 spin_lock(&xprt->queue_lock);
Trond Myklebust0afa6b42018-02-09 09:39:42 -05001384 __UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
Trond Myklebust729749b2017-08-13 10:03:59 -04001385 goto out_unpin;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001386 }
1387
Chuck Levera246b012005-08-11 16:25:23 -04001388
Trond Myklebust729749b2017-08-13 10:03:59 -04001389 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001390 xprt_adjust_cwnd(xprt, task, copied);
Trond Myklebustce7c2522017-08-16 15:30:35 -04001391 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust75c84152018-08-31 10:21:00 -04001392 spin_lock(&xprt->queue_lock);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001393 xprt_complete_rqst(task, copied);
Trond Myklebust0afa6b42018-02-09 09:39:42 -05001394 __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
Trond Myklebust729749b2017-08-13 10:03:59 -04001395out_unpin:
1396 xprt_unpin_rqst(rovr);
Chuck Levera246b012005-08-11 16:25:23 -04001397 out_unlock:
Trond Myklebust75c84152018-08-31 10:21:00 -04001398 spin_unlock(&xprt->queue_lock);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001399}
1400
1401static void xs_udp_data_receive(struct sock_xprt *transport)
1402{
1403 struct sk_buff *skb;
1404 struct sock *sk;
1405 int err;
1406
1407 mutex_lock(&transport->recv_mutex);
1408 sk = transport->inet;
1409 if (sk == NULL)
1410 goto out;
1411 for (;;) {
Paolo Abeni7c13f972016-11-04 11:28:59 +01001412 skb = skb_recv_udp(sk, 0, 1, &err);
Trond Myklebust4f546142018-09-14 17:45:23 -04001413 if (skb == NULL)
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001414 break;
Trond Myklebust4f546142018-09-14 17:45:23 -04001415 xs_udp_data_read_skb(&transport->xprt, sk, skb);
1416 consume_skb(skb);
1417 cond_resched();
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001418 }
Trond Myklebust0ffe86f2019-01-30 14:51:26 -05001419 xs_poll_check_readable(transport);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001420out:
1421 mutex_unlock(&transport->recv_mutex);
1422}
1423
1424static void xs_udp_data_receive_workfn(struct work_struct *work)
1425{
1426 struct sock_xprt *transport =
1427 container_of(work, struct sock_xprt, recv_worker);
Trond Myklebusta1231fd2019-02-18 10:02:29 -05001428 unsigned int pflags = memalloc_nofs_save();
1429
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001430 xs_udp_data_receive(transport);
Trond Myklebusta1231fd2019-02-18 10:02:29 -05001431 memalloc_nofs_restore(pflags);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001432}
1433
1434/**
1435 * xs_data_ready - "data ready" callback for UDP sockets
1436 * @sk: socket with data to read
1437 *
1438 */
1439static void xs_data_ready(struct sock *sk)
1440{
1441 struct rpc_xprt *xprt;
1442
1443 read_lock_bh(&sk->sk_callback_lock);
1444 dprintk("RPC: xs_data_ready...\n");
1445 xprt = xprt_from_sock(sk);
1446 if (xprt != NULL) {
1447 struct sock_xprt *transport = container_of(xprt,
1448 struct sock_xprt, xprt);
Trond Myklebust5157b952016-05-29 10:13:24 -04001449 transport->old_data_ready(sk);
1450 /* Any data means we had a useful conversation, so
1451 * then we don't need to delay the next reconnect
1452 */
1453 if (xprt->reestablish_timeout)
1454 xprt->reestablish_timeout = 0;
Trond Myklebust42d42a52016-05-23 09:24:55 -04001455 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebust40a5f1b2016-05-27 10:39:50 -04001456 queue_work(xprtiod_workqueue, &transport->recv_worker);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001457 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001458 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001459}
1460
Trond Myklebusta519fc72012-09-12 16:49:15 -04001461/*
1462 * Helper function to force a TCP close if the server is sending
1463 * junk and/or it has put us in CLOSE_WAIT
1464 */
1465static void xs_tcp_force_close(struct rpc_xprt *xprt)
1466{
Trond Myklebusta519fc72012-09-12 16:49:15 -04001467 xprt_force_disconnect(xprt);
1468}
1469
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001470#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Chuck Lever6b26cc82016-05-02 14:40:40 -04001471static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
1472{
1473 return PAGE_SIZE;
1474}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001475#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001476
Chuck Lever9903cd12005-08-11 16:25:26 -04001477/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001478 * xs_tcp_state_change - callback to handle TCP socket state changes
1479 * @sk: socket whose state has changed
1480 *
1481 */
1482static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001483{
Chuck Lever9903cd12005-08-11 16:25:26 -04001484 struct rpc_xprt *xprt;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001485 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001486
Eric Dumazetf064af12010-09-22 12:43:39 +00001487 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001488 if (!(xprt = xprt_from_sock(sk)))
1489 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001490 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001491 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001492 sk->sk_state, xprt_connected(xprt),
1493 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001494 sock_flag(sk, SOCK_ZAPPED),
1495 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001496
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001497 transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001498 trace_rpc_socket_state_change(xprt, sk->sk_socket);
Chuck Levera246b012005-08-11 16:25:23 -04001499 switch (sk->sk_state) {
1500 case TCP_ESTABLISHED:
Eric Dumazetf064af12010-09-22 12:43:39 +00001501 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001502 if (!xprt_test_and_set_connected(xprt)) {
Trond Myklebust8b717982013-09-26 10:18:04 -04001503 xprt->connect_cookie++;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001504 clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
1505 xprt_clear_connecting(xprt);
Chuck Lever51971132006-12-05 16:35:19 -05001506
Chuck Lever3968a8a2018-10-01 14:25:36 -04001507 xprt->stat.connect_count++;
1508 xprt->stat.connect_time += (long)jiffies -
1509 xprt->stat.connect_start;
Trond Myklebust2a491992009-03-11 14:38:00 -04001510 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001511 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001512 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001513 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001514 case TCP_FIN_WAIT1:
1515 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001516 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001517 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001518 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001519 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001520 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001521 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001522 smp_mb__after_atomic();
Chuck Levera246b012005-08-11 16:25:23 -04001523 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001524 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001525 /* The server initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001526 xprt->connect_cookie++;
Trond Myklebustd0bea452012-10-23 11:35:47 -04001527 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebusta519fc72012-09-12 16:49:15 -04001528 xs_tcp_force_close(xprt);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001529 /* fall through */
Trond Myklebust663b8852008-01-01 18:42:12 -05001530 case TCP_CLOSING:
1531 /*
1532 * If the server closed down the connection, make sure that
1533 * we back off before reconnecting
1534 */
1535 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1536 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001537 break;
1538 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001539 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001540 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001541 clear_bit(XPRT_CONNECTED, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001542 smp_mb__after_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001543 break;
1544 case TCP_CLOSE:
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001545 if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
1546 &transport->sock_state))
1547 xprt_clear_connecting(xprt);
Trond Myklebust9b308892018-02-05 10:20:06 -05001548 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust9b308892018-02-05 10:20:06 -05001549 /* Trigger the socket release */
1550 xs_tcp_force_close(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001551 }
1552 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001553 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001554}
1555
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001556static void xs_write_space(struct sock *sk)
1557{
Trond Myklebust13331a52016-01-06 08:57:06 -05001558 struct socket_wq *wq;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001559 struct rpc_xprt *xprt;
1560
Trond Myklebust13331a52016-01-06 08:57:06 -05001561 if (!sk->sk_socket)
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001562 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001563 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001564
1565 if (unlikely(!(xprt = xprt_from_sock(sk))))
1566 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001567 rcu_read_lock();
1568 wq = rcu_dereference(sk->sk_wq);
1569 if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0)
1570 goto out;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001571
Trond Myklebustc5445772018-09-03 23:39:27 -04001572 if (xprt_write_space(xprt))
1573 sk->sk_write_pending--;
Trond Myklebust13331a52016-01-06 08:57:06 -05001574out:
1575 rcu_read_unlock();
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001576}
1577
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001578/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001579 * xs_udp_write_space - callback invoked when socket buffer space
1580 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001581 * @sk: socket whose state has changed
1582 *
Chuck Levera246b012005-08-11 16:25:23 -04001583 * Called when more output buffer space is available for this socket.
1584 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001585 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001586 * with a bunch of small requests.
1587 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001588static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001589{
Eric Dumazetf064af12010-09-22 12:43:39 +00001590 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001591
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001592 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001593 if (sock_writeable(sk))
1594 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001595
Eric Dumazetf064af12010-09-22 12:43:39 +00001596 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001597}
Chuck Levera246b012005-08-11 16:25:23 -04001598
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001599/**
1600 * xs_tcp_write_space - callback invoked when socket buffer space
1601 * becomes available
1602 * @sk: socket whose state has changed
1603 *
1604 * Called when more output buffer space is available for this socket.
1605 * We try not to wake our writers until they can make "significant"
1606 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1607 * with a bunch of small requests.
1608 */
1609static void xs_tcp_write_space(struct sock *sk)
1610{
Eric Dumazetf064af12010-09-22 12:43:39 +00001611 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001612
1613 /* from net/core/stream.c:sk_stream_write_space */
Eric Dumazet64dc6132013-07-22 20:26:31 -07001614 if (sk_stream_is_writeable(sk))
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001615 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 Levera246b012005-08-11 16:25:23 -04001618}
1619
Chuck Lever470056c2005-08-25 16:25:56 -07001620static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001621{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001622 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1623 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001624
Chuck Lever7c6e0662006-12-05 16:35:30 -05001625 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001626 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001627 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001628 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001629 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001630 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001631 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001632 sk->sk_write_space(sk);
1633 }
1634}
1635
Chuck Lever43118c22005-08-25 16:25:49 -07001636/**
Chuck Lever470056c2005-08-25 16:25:56 -07001637 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001638 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001639 * @sndsize: requested size of send buffer, in bytes
1640 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001641 *
Chuck Lever470056c2005-08-25 16:25:56 -07001642 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001643 */
Chuck Lever470056c2005-08-25 16:25:56 -07001644static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001645{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001646 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1647
1648 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001649 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001650 transport->sndsize = sndsize + 1024;
1651 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001652 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001653 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001654
1655 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001656}
1657
Chuck Lever46c0ee82005-08-25 16:25:52 -07001658/**
1659 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
Chuck Leveracf0a392018-12-19 11:00:22 -05001660 * @xprt: controlling transport
Chuck Lever46c0ee82005-08-25 16:25:52 -07001661 * @task: task that timed out
1662 *
1663 * Adjust the congestion window after a retransmit timeout has occurred.
1664 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001665static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever46c0ee82005-08-25 16:25:52 -07001666{
Chuck Leverb977b642017-02-08 17:00:51 -05001667 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001668 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
Chuck Leverb977b642017-02-08 17:00:51 -05001669 spin_unlock_bh(&xprt->transport_lock);
Chuck Lever46c0ee82005-08-25 16:25:52 -07001670}
1671
J. Bruce Fields826799e2018-10-18 15:27:02 -04001672static int xs_get_random_port(void)
Chuck Leverb85d8802006-05-25 01:40:49 -04001673{
J. Bruce Fields826799e2018-10-18 15:27:02 -04001674 unsigned short min = xprt_min_resvport, max = xprt_max_resvport;
1675 unsigned short range;
1676 unsigned short rand;
1677
1678 if (max < min)
1679 return -EADDRINUSE;
1680 range = max - min + 1;
1681 rand = (unsigned short) prandom_u32() % range;
1682 return rand + min;
Chuck Leverb85d8802006-05-25 01:40:49 -04001683}
1684
Chuck Lever92200412006-01-03 09:55:51 +01001685/**
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001686 * xs_set_reuseaddr_port - set the socket's port and address reuse options
1687 * @sock: socket
1688 *
1689 * Note that this function has to be called on all sockets that share the
1690 * same port, and it must be called before binding.
1691 */
1692static void xs_sock_set_reuseport(struct socket *sock)
1693{
Trond Myklebust402e23b2015-02-09 17:20:14 -05001694 int opt = 1;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001695
Trond Myklebust402e23b2015-02-09 17:20:14 -05001696 kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
1697 (char *)&opt, sizeof(opt));
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001698}
1699
1700static unsigned short xs_sock_getport(struct socket *sock)
1701{
1702 struct sockaddr_storage buf;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001703 unsigned short port = 0;
1704
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001705 if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001706 goto out;
1707 switch (buf.ss_family) {
1708 case AF_INET6:
1709 port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
1710 break;
1711 case AF_INET:
1712 port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
1713 }
1714out:
1715 return port;
1716}
1717
1718/**
Chuck Lever92200412006-01-03 09:55:51 +01001719 * xs_set_port - reset the port number in the remote endpoint address
1720 * @xprt: generic transport
1721 * @port: new port number
1722 *
1723 */
1724static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1725{
Chuck Lever46121cf2007-01-31 12:14:08 -05001726 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001727
Chuck Lever9dc3b092009-08-09 15:09:46 -04001728 rpc_set_port(xs_addr(xprt), port);
1729 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001730}
1731
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001732static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
1733{
1734 if (transport->srcport == 0)
1735 transport->srcport = xs_sock_getport(sock);
1736}
1737
J. Bruce Fields826799e2018-10-18 15:27:02 -04001738static int xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001739{
J. Bruce Fields826799e2018-10-18 15:27:02 -04001740 int port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001741
1742 if (port == 0 && transport->xprt.resvport)
1743 port = xs_get_random_port();
1744 return port;
1745}
1746
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001747static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001748{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001749 if (transport->srcport != 0)
1750 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001751 if (!transport->xprt.resvport)
1752 return 0;
1753 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1754 return xprt_max_resvport;
1755 return --port;
1756}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001757static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001758{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001759 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001760 int err, nloop = 0;
J. Bruce Fields826799e2018-10-18 15:27:02 -04001761 int port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001762 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001763
Chris Perl0f7a6222014-09-05 15:40:21 -04001764 /*
1765 * If we are asking for any ephemeral port (i.e. port == 0 &&
1766 * transport->xprt.resvport == 0), don't bind. Let the local
1767 * port selection happen implicitly when the socket is used
1768 * (for example at connect time).
1769 *
1770 * This ensures that we can continue to establish TCP
1771 * connections even when all local ephemeral ports are already
1772 * a part of some TCP connection. This makes no difference
1773 * for UDP sockets, but also doens't harm them.
1774 *
1775 * If we're asking for any reserved port (i.e. port == 0 &&
1776 * transport->xprt.resvport == 1) xs_get_srcport above will
1777 * ensure that port is non-zero and we will bind as needed.
1778 */
J. Bruce Fields826799e2018-10-18 15:27:02 -04001779 if (port <= 0)
1780 return port;
Chris Perl0f7a6222014-09-05 15:40:21 -04001781
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001782 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001783 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001784 rpc_set_port((struct sockaddr *)&myaddr, port);
1785 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1786 transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001787 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001788 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001789 break;
Chuck Levera246b012005-08-11 16:25:23 -04001790 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001791 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001792 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001793 if (port > last)
1794 nloop++;
1795 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001796
Chuck Lever4232e862010-10-20 11:52:51 -04001797 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001798 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1799 &((struct sockaddr_in *)&myaddr)->sin_addr,
1800 port, err ? "failed" : "ok", err);
1801 else
1802 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1803 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1804 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001805 return err;
1806}
1807
Chuck Lever176e21e2011-05-09 15:22:44 -04001808/*
1809 * We don't support autobind on AF_LOCAL sockets
1810 */
1811static void xs_local_rpcbind(struct rpc_task *task)
1812{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001813 xprt_set_bound(task->tk_xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04001814}
1815
1816static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1817{
1818}
Chuck Levera246b012005-08-11 16:25:23 -04001819
Peter Zijlstraed075362006-12-06 20:35:24 -08001820#ifdef CONFIG_DEBUG_LOCK_ALLOC
1821static struct lock_class_key xs_key[2];
1822static struct lock_class_key xs_slock_key[2];
1823
Chuck Lever176e21e2011-05-09 15:22:44 -04001824static inline void xs_reclassify_socketu(struct socket *sock)
1825{
1826 struct sock *sk = sock->sk;
1827
Chuck Lever176e21e2011-05-09 15:22:44 -04001828 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1829 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1830}
1831
Chuck Lever8945ee52007-08-06 11:58:04 -04001832static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001833{
1834 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001835
Chuck Lever8945ee52007-08-06 11:58:04 -04001836 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1837 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1838}
Peter Zijlstraed075362006-12-06 20:35:24 -08001839
Chuck Lever8945ee52007-08-06 11:58:04 -04001840static inline void xs_reclassify_socket6(struct socket *sock)
1841{
1842 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001843
Chuck Lever8945ee52007-08-06 11:58:04 -04001844 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1845 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001846}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001847
1848static inline void xs_reclassify_socket(int family, struct socket *sock)
1849{
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02001850 if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -04001851 return;
1852
Chuck Lever4232e862010-10-20 11:52:51 -04001853 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001854 case AF_LOCAL:
1855 xs_reclassify_socketu(sock);
1856 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001857 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001858 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001859 break;
1860 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001861 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001862 break;
1863 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001864}
Peter Zijlstraed075362006-12-06 20:35:24 -08001865#else
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001866static inline void xs_reclassify_socket(int family, struct socket *sock)
1867{
1868}
Peter Zijlstraed075362006-12-06 20:35:24 -08001869#endif
1870
NeilBrown93dc41b2013-10-31 16:14:36 +11001871static void xs_dummy_setup_socket(struct work_struct *work)
1872{
1873}
1874
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001875static struct socket *xs_create_sock(struct rpc_xprt *xprt,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001876 struct sock_xprt *transport, int family, int type,
1877 int protocol, bool reuseport)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001878{
Trond Myklebusta73881c2019-02-26 06:33:02 -05001879 struct file *filp;
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001880 struct socket *sock;
1881 int err;
1882
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001883 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001884 if (err < 0) {
1885 dprintk("RPC: can't create %d transport socket (%d).\n",
1886 protocol, -err);
1887 goto out;
1888 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001889 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001890
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001891 if (reuseport)
1892 xs_sock_set_reuseport(sock);
1893
Ben Hutchings4cea2882011-02-22 21:54:34 +00001894 err = xs_bind(transport, sock);
1895 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001896 sock_release(sock);
1897 goto out;
1898 }
1899
Trond Myklebusta73881c2019-02-26 06:33:02 -05001900 filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1901 if (IS_ERR(filp))
1902 return ERR_CAST(filp);
1903 transport->file = filp;
1904
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001905 return sock;
1906out:
1907 return ERR_PTR(err);
1908}
1909
Chuck Lever176e21e2011-05-09 15:22:44 -04001910static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1911 struct socket *sock)
1912{
1913 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1914 xprt);
1915
1916 if (!transport->inet) {
1917 struct sock *sk = sock->sk;
1918
1919 write_lock_bh(&sk->sk_callback_lock);
1920
1921 xs_save_old_callbacks(transport, sk);
1922
1923 sk->sk_user_data = xprt;
Trond Myklebusta2648092015-10-06 17:03:00 -04001924 sk->sk_data_ready = xs_data_ready;
Chuck Lever176e21e2011-05-09 15:22:44 -04001925 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07001926 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05001927 sk->sk_error_report = xs_error_report;
Chuck Lever176e21e2011-05-09 15:22:44 -04001928
1929 xprt_clear_connected(xprt);
1930
1931 /* Reset to new socket */
1932 transport->sock = sock;
1933 transport->inet = sk;
1934
1935 write_unlock_bh(&sk->sk_callback_lock);
1936 }
1937
Trond Myklebustae053552019-02-20 14:56:20 -05001938 xs_stream_start_connect(transport);
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001939
Chuck Lever176e21e2011-05-09 15:22:44 -04001940 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1941}
1942
1943/**
1944 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
Chuck Lever176e21e2011-05-09 15:22:44 -04001945 * @transport: socket transport to connect
Chuck Lever176e21e2011-05-09 15:22:44 -04001946 */
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001947static int xs_local_setup_socket(struct sock_xprt *transport)
Chuck Lever176e21e2011-05-09 15:22:44 -04001948{
Chuck Lever176e21e2011-05-09 15:22:44 -04001949 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebusta73881c2019-02-26 06:33:02 -05001950 struct file *filp;
Chuck Lever176e21e2011-05-09 15:22:44 -04001951 struct socket *sock;
1952 int status = -EIO;
1953
Chuck Lever176e21e2011-05-09 15:22:44 -04001954 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1955 SOCK_STREAM, 0, &sock, 1);
1956 if (status < 0) {
1957 dprintk("RPC: can't create AF_LOCAL "
1958 "transport socket (%d).\n", -status);
1959 goto out;
1960 }
Stefan Hajnoczid1358912015-12-02 14:17:52 +08001961 xs_reclassify_socket(AF_LOCAL, sock);
Chuck Lever176e21e2011-05-09 15:22:44 -04001962
Trond Myklebusta73881c2019-02-26 06:33:02 -05001963 filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1964 if (IS_ERR(filp)) {
1965 status = PTR_ERR(filp);
1966 goto out;
1967 }
1968 transport->file = filp;
1969
Chuck Lever176e21e2011-05-09 15:22:44 -04001970 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1971 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1972
1973 status = xs_local_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001974 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever176e21e2011-05-09 15:22:44 -04001975 switch (status) {
1976 case 0:
1977 dprintk("RPC: xprt %p connected to %s\n",
1978 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
Chuck Lever3968a8a2018-10-01 14:25:36 -04001979 xprt->stat.connect_count++;
1980 xprt->stat.connect_time += (long)jiffies -
1981 xprt->stat.connect_start;
Chuck Lever176e21e2011-05-09 15:22:44 -04001982 xprt_set_connected(xprt);
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001983 case -ENOBUFS:
Chuck Lever176e21e2011-05-09 15:22:44 -04001984 break;
1985 case -ENOENT:
1986 dprintk("RPC: xprt %p: socket %s does not exist\n",
1987 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1988 break;
Trond Myklebust4a20a982012-12-15 17:02:29 -05001989 case -ECONNREFUSED:
1990 dprintk("RPC: xprt %p: connection refused for %s\n",
1991 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1992 break;
Chuck Lever176e21e2011-05-09 15:22:44 -04001993 default:
1994 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1995 __func__, -status,
1996 xprt->address_strings[RPC_DISPLAY_ADDR]);
1997 }
1998
1999out:
2000 xprt_clear_connecting(xprt);
2001 xprt_wake_pending_tasks(xprt, status);
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002002 return status;
2003}
2004
Linus Torvaldsb6669732013-02-28 18:02:55 -08002005static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002006{
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002007 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2008 int ret;
2009
2010 if (RPC_IS_ASYNC(task)) {
2011 /*
2012 * We want the AF_LOCAL connect to be resolved in the
2013 * filesystem namespace of the process making the rpc
2014 * call. Thus we connect synchronously.
2015 *
2016 * If we want to support asynchronous AF_LOCAL calls,
2017 * we'll need to figure out how to pass a namespace to
2018 * connect.
2019 */
2020 rpc_exit(task, -ENOTCONN);
2021 return;
2022 }
2023 ret = xs_local_setup_socket(transport);
2024 if (ret && !RPC_IS_SOFTCONN(task))
2025 msleep_interruptible(15000);
Chuck Lever176e21e2011-05-09 15:22:44 -04002026}
2027
Jeff Layton3c87ef62015-06-03 16:14:25 -04002028#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Jeff Laytond6e971d2015-06-03 16:14:28 -04002029/*
2030 * Note that this should be called with XPRT_LOCKED held (or when we otherwise
2031 * know that we have exclusive access to the socket), to guard against
2032 * races with xs_reset_transport.
2033 */
Mel Gormana564b8f2012-07-31 16:45:12 -07002034static void xs_set_memalloc(struct rpc_xprt *xprt)
2035{
2036 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2037 xprt);
2038
Jeff Laytond6e971d2015-06-03 16:14:28 -04002039 /*
2040 * If there's no sock, then we have nothing to set. The
2041 * reconnecting process will get it for us.
2042 */
2043 if (!transport->inet)
2044 return;
Jeff Layton8e228132015-06-03 16:14:26 -04002045 if (atomic_read(&xprt->swapper))
Mel Gormana564b8f2012-07-31 16:45:12 -07002046 sk_set_memalloc(transport->inet);
2047}
2048
2049/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002050 * xs_enable_swap - Tag this transport as being used for swap.
Mel Gormana564b8f2012-07-31 16:45:12 -07002051 * @xprt: transport to tag
Mel Gormana564b8f2012-07-31 16:45:12 -07002052 *
Jeff Layton8e228132015-06-03 16:14:26 -04002053 * Take a reference to this transport on behalf of the rpc_clnt, and
2054 * optionally mark it for swapping if it wasn't already.
Mel Gormana564b8f2012-07-31 16:45:12 -07002055 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002056static int
2057xs_enable_swap(struct rpc_xprt *xprt)
Mel Gormana564b8f2012-07-31 16:45:12 -07002058{
Jeff Laytond6e971d2015-06-03 16:14:28 -04002059 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Mel Gormana564b8f2012-07-31 16:45:12 -07002060
Jeff Laytond6e971d2015-06-03 16:14:28 -04002061 if (atomic_inc_return(&xprt->swapper) != 1)
2062 return 0;
2063 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2064 return -ERESTARTSYS;
2065 if (xs->inet)
2066 sk_set_memalloc(xs->inet);
2067 xprt_release_xprt(xprt, NULL);
Jeff Layton8e228132015-06-03 16:14:26 -04002068 return 0;
Mel Gormana564b8f2012-07-31 16:45:12 -07002069}
Jeff Layton8e228132015-06-03 16:14:26 -04002070
2071/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002072 * xs_disable_swap - Untag this transport as being used for swap.
Jeff Layton8e228132015-06-03 16:14:26 -04002073 * @xprt: transport to tag
2074 *
2075 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
2076 * swapper refcount goes to 0, untag the socket as a memalloc socket.
2077 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002078static void
2079xs_disable_swap(struct rpc_xprt *xprt)
Jeff Layton8e228132015-06-03 16:14:26 -04002080{
Jeff Laytond6e971d2015-06-03 16:14:28 -04002081 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Jeff Layton8e228132015-06-03 16:14:26 -04002082
Jeff Laytond6e971d2015-06-03 16:14:28 -04002083 if (!atomic_dec_and_test(&xprt->swapper))
2084 return;
2085 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2086 return;
2087 if (xs->inet)
2088 sk_clear_memalloc(xs->inet);
2089 xprt_release_xprt(xprt, NULL);
Mel Gormana564b8f2012-07-31 16:45:12 -07002090}
Mel Gormana564b8f2012-07-31 16:45:12 -07002091#else
2092static void xs_set_memalloc(struct rpc_xprt *xprt)
2093{
2094}
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002095
2096static int
2097xs_enable_swap(struct rpc_xprt *xprt)
2098{
2099 return -EINVAL;
2100}
2101
2102static void
2103xs_disable_swap(struct rpc_xprt *xprt)
2104{
2105}
Mel Gormana564b8f2012-07-31 16:45:12 -07002106#endif
2107
Chuck Lever16be2d22007-08-06 11:57:38 -04002108static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04002109{
Chuck Lever16be2d22007-08-06 11:57:38 -04002110 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04002111
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002112 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002113 struct sock *sk = sock->sk;
2114
2115 write_lock_bh(&sk->sk_callback_lock);
2116
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002117 xs_save_old_callbacks(transport, sk);
2118
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002119 sk->sk_user_data = xprt;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002120 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002121 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002122 sock_set_flag(sk, SOCK_FASYNC);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002123
2124 xprt_set_connected(xprt);
2125
2126 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002127 transport->sock = sock;
2128 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002129
Mel Gormana564b8f2012-07-31 16:45:12 -07002130 xs_set_memalloc(xprt);
2131
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002132 write_unlock_bh(&sk->sk_callback_lock);
2133 }
Chuck Lever470056c2005-08-25 16:25:56 -07002134 xs_udp_do_set_buffer_size(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002135
2136 xprt->stat.connect_start = jiffies;
Chuck Lever16be2d22007-08-06 11:57:38 -04002137}
2138
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002139static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04002140{
2141 struct sock_xprt *transport =
2142 container_of(work, struct sock_xprt, connect_worker.work);
2143 struct rpc_xprt *xprt = &transport->xprt;
Colin Ian Kingd099b8a2017-09-18 12:21:14 +01002144 struct socket *sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002145 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04002146
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002147 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002148 xs_addr(xprt)->sa_family, SOCK_DGRAM,
2149 IPPROTO_UDP, false);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002150 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04002151 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04002152
Chuck Leverc740eff2009-08-09 15:09:46 -04002153 dprintk("RPC: worker connecting xprt %p via %s to "
2154 "%s (port %s)\n", xprt,
2155 xprt->address_strings[RPC_DISPLAY_PROTO],
2156 xprt->address_strings[RPC_DISPLAY_ADDR],
2157 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04002158
2159 xs_udp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002160 trace_rpc_socket_connect(xprt, sock, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002161 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002162out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002163 xprt_clear_connecting(xprt);
Trond Myklebustcf767852018-12-17 17:38:51 -05002164 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002165 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002166}
2167
Trond Myklebust4876cc72015-06-19 16:17:57 -04002168/**
2169 * xs_tcp_shutdown - gracefully shut down a TCP socket
2170 * @xprt: transport
2171 *
2172 * Initiates a graceful shutdown of the TCP socket by calling the
2173 * equivalent of shutdown(SHUT_RDWR);
2174 */
2175static void xs_tcp_shutdown(struct rpc_xprt *xprt)
2176{
2177 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2178 struct socket *sock = transport->sock;
Trond Myklebust9b308892018-02-05 10:20:06 -05002179 int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
Trond Myklebust4876cc72015-06-19 16:17:57 -04002180
2181 if (sock == NULL)
2182 return;
Trond Myklebust9b308892018-02-05 10:20:06 -05002183 switch (skst) {
2184 default:
Trond Myklebust4876cc72015-06-19 16:17:57 -04002185 kernel_sock_shutdown(sock, SHUT_RDWR);
2186 trace_rpc_socket_shutdown(xprt, sock);
Trond Myklebust9b308892018-02-05 10:20:06 -05002187 break;
2188 case TCP_CLOSE:
2189 case TCP_TIME_WAIT:
Trond Myklebust4876cc72015-06-19 16:17:57 -04002190 xs_reset_transport(transport);
Trond Myklebust9b308892018-02-05 10:20:06 -05002191 }
Trond Myklebust4876cc72015-06-19 16:17:57 -04002192}
2193
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002194static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
2195 struct socket *sock)
2196{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002197 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2198 unsigned int keepidle;
2199 unsigned int keepcnt;
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002200 unsigned int opt_on = 1;
2201 unsigned int timeo;
2202
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002203 spin_lock_bh(&xprt->transport_lock);
2204 keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
2205 keepcnt = xprt->timeout->to_retries + 1;
2206 timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
2207 (xprt->timeout->to_retries + 1);
2208 clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2209 spin_unlock_bh(&xprt->transport_lock);
2210
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002211 /* TCP Keepalive options */
2212 kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
2213 (char *)&opt_on, sizeof(opt_on));
2214 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
2215 (char *)&keepidle, sizeof(keepidle));
2216 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
2217 (char *)&keepidle, sizeof(keepidle));
2218 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
2219 (char *)&keepcnt, sizeof(keepcnt));
2220
2221 /* TCP user timeout (see RFC5482) */
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002222 kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT,
2223 (char *)&timeo, sizeof(timeo));
2224}
2225
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002226static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
2227 unsigned long connect_timeout,
2228 unsigned long reconnect_timeout)
2229{
2230 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2231 struct rpc_timeout to;
2232 unsigned long initval;
2233
2234 spin_lock_bh(&xprt->transport_lock);
2235 if (reconnect_timeout < xprt->max_reconnect_timeout)
2236 xprt->max_reconnect_timeout = reconnect_timeout;
2237 if (connect_timeout < xprt->connect_timeout) {
2238 memcpy(&to, xprt->timeout, sizeof(to));
2239 initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
2240 /* Arbitrary lower limit */
2241 if (initval < XS_TCP_INIT_REEST_TO << 1)
2242 initval = XS_TCP_INIT_REEST_TO << 1;
2243 to.to_initval = initval;
2244 to.to_maxval = initval;
2245 memcpy(&transport->tcp_timeout, &to,
2246 sizeof(transport->tcp_timeout));
2247 xprt->timeout = &transport->tcp_timeout;
2248 xprt->connect_timeout = connect_timeout;
2249 }
2250 set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2251 spin_unlock_bh(&xprt->transport_lock);
2252}
2253
Chuck Lever16be2d22007-08-06 11:57:38 -04002254static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002255{
Chuck Lever16be2d22007-08-06 11:57:38 -04002256 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002257 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002258
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002259 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002260 struct sock *sk = sock->sk;
NeilBrownd88e4d82016-08-04 16:24:28 +10002261 unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC;
Trond Myklebust7f260e82013-09-24 11:25:22 -04002262
NeilBrownd88e4d82016-08-04 16:24:28 +10002263 /* Avoid temporary address, they are bad for long-lived
2264 * connections such as NFS mounts.
2265 * RFC4941, section 3.6 suggests that:
2266 * Individual applications, which have specific
2267 * knowledge about the normal duration of connections,
2268 * MAY override this as appropriate.
2269 */
2270 kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES,
2271 (char *)&addr_pref, sizeof(addr_pref));
2272
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002273 xs_tcp_set_socket_timeouts(xprt, sock);
Trond Myklebust775f06a2015-06-20 15:31:54 -04002274
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002275 write_lock_bh(&sk->sk_callback_lock);
2276
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002277 xs_save_old_callbacks(transport, sk);
2278
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002279 sk->sk_user_data = xprt;
Trond Myklebust5157b952016-05-29 10:13:24 -04002280 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002281 sk->sk_state_change = xs_tcp_state_change;
2282 sk->sk_write_space = xs_tcp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002283 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05002284 sk->sk_error_report = xs_error_report;
Chuck Lever3167e122005-08-25 16:25:55 -07002285
2286 /* socket options */
Chuck Lever3167e122005-08-25 16:25:55 -07002287 sock_reset_flag(sk, SOCK_LINGER);
Chuck Lever3167e122005-08-25 16:25:55 -07002288 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002289
2290 xprt_clear_connected(xprt);
2291
2292 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002293 transport->sock = sock;
2294 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002295
2296 write_unlock_bh(&sk->sk_callback_lock);
2297 }
2298
Trond Myklebust01d37c42009-03-11 14:09:39 -04002299 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002300 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002301
Mel Gormana564b8f2012-07-31 16:45:12 -07002302 xs_set_memalloc(xprt);
2303
Trond Myklebustae053552019-02-20 14:56:20 -05002304 xs_stream_start_connect(transport);
Trond Myklebuste1806c72018-08-13 16:50:49 -04002305
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002306 /* Tell the socket layer to start connecting... */
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04002307 set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002308 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2309 switch (ret) {
2310 case 0:
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002311 xs_set_srcport(transport, sock);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002312 /* fall through */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002313 case -EINPROGRESS:
2314 /* SYN_SENT! */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002315 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2316 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust1f4c17a2016-08-01 13:36:08 -04002317 break;
2318 case -EADDRNOTAVAIL:
2319 /* Source port number is unavailable. Try a new one! */
2320 transport->srcport = 0;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002321 }
2322out:
2323 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002324}
2325
2326/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002327 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
Chuck Leveracf0a392018-12-19 11:00:22 -05002328 * @work: queued work item
Chuck Lever16be2d22007-08-06 11:57:38 -04002329 *
2330 * Invoked by a work queue tasklet.
2331 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002332static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002333{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002334 struct sock_xprt *transport =
2335 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002336 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002337 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002338 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002339
Chuck Lever16be2d22007-08-06 11:57:38 -04002340 if (!sock) {
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002341 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002342 xs_addr(xprt)->sa_family, SOCK_STREAM,
2343 IPPROTO_TCP, true);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002344 if (IS_ERR(sock)) {
2345 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002346 goto out;
2347 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002348 }
2349
Chuck Leverc740eff2009-08-09 15:09:46 -04002350 dprintk("RPC: worker connecting xprt %p via %s to "
2351 "%s (port %s)\n", xprt,
2352 xprt->address_strings[RPC_DISPLAY_PROTO],
2353 xprt->address_strings[RPC_DISPLAY_ADDR],
2354 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002355
2356 status = xs_tcp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002357 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever46121cf2007-01-31 12:14:08 -05002358 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2359 xprt, -status, xprt_connected(xprt),
2360 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002361 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002362 default:
2363 printk("%s: connect returned unhandled error %d\n",
2364 __func__, status);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002365 /* fall through */
Trond Myklebustf75e6742009-04-21 17:18:20 -04002366 case -EADDRNOTAVAIL:
2367 /* We're probably in TIME_WAIT. Get rid of existing socket,
2368 * and retry
2369 */
Trond Myklebusta519fc72012-09-12 16:49:15 -04002370 xs_tcp_force_close(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002371 break;
Trond Myklebust2a491992009-03-11 14:38:00 -04002372 case 0:
2373 case -EINPROGRESS:
2374 case -EALREADY:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002375 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002376 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002377 case -EINVAL:
2378 /* Happens, for instance, if the user specified a link
2379 * local IPv6 address without a scope-id.
2380 */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002381 case -ECONNREFUSED:
2382 case -ECONNRESET:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002383 case -ENETDOWN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002384 case -ENETUNREACH:
Trond Myklebust4ba161a2017-11-24 12:00:24 -05002385 case -EHOSTUNREACH:
Trond Myklebust3913c782015-02-08 21:44:04 -05002386 case -EADDRINUSE:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002387 case -ENOBUFS:
NeilBrown6ea44ad2017-05-25 17:00:32 +10002388 /*
2389 * xs_tcp_force_close() wakes tasks with -EIO.
2390 * We need to wake them first to ensure the
2391 * correct error code.
2392 */
2393 xprt_wake_pending_tasks(xprt, status);
Trond Myklebust4efdd922015-02-08 15:34:28 -05002394 xs_tcp_force_close(xprt);
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002395 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002396 }
Trond Myklebust2a491992009-03-11 14:38:00 -04002397 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002398out:
Chuck Lever2226feb2005-08-11 16:25:38 -04002399 xprt_clear_connecting(xprt);
Trond Myklebustcf767852018-12-17 17:38:51 -05002400 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002401 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04002402}
2403
Trond Myklebust02910172016-08-04 00:00:33 -04002404static unsigned long xs_reconnect_delay(const struct rpc_xprt *xprt)
2405{
2406 unsigned long start, now = jiffies;
2407
2408 start = xprt->stat.connect_start + xprt->reestablish_timeout;
2409 if (time_after(start, now))
2410 return start - now;
2411 return 0;
2412}
2413
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002414static void xs_reconnect_backoff(struct rpc_xprt *xprt)
2415{
2416 xprt->reestablish_timeout <<= 1;
2417 if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
2418 xprt->reestablish_timeout = xprt->max_reconnect_timeout;
2419 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2420 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2421}
2422
Chuck Lever68e220b2007-08-06 11:57:48 -04002423/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002424 * xs_connect - connect a socket to a remote endpoint
Trond Myklebust1b092092013-01-08 09:26:49 -05002425 * @xprt: pointer to transport structure
Chuck Lever9903cd12005-08-11 16:25:26 -04002426 * @task: address of RPC task that manages state of connect request
2427 *
2428 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002429 *
2430 * UDP socket connects are synchronous, but we use a work queue anyway
2431 * to guarantee that even unprivileged user processes can set up a
2432 * socket on a privileged port.
2433 *
2434 * If a UDP socket connect fails, the delay behavior here prevents
2435 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002436 */
Trond Myklebust1b092092013-01-08 09:26:49 -05002437static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002438{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002439 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002440 unsigned long delay = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002441
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002442 WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2443
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002444 if (transport->sock != NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002445 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2446 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002447 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002448
2449 /* Start by resetting any existing state */
2450 xs_reset_transport(transport);
2451
Trond Myklebust02910172016-08-04 00:00:33 -04002452 delay = xs_reconnect_delay(xprt);
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002453 xs_reconnect_backoff(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002454
Trond Myklebust02910172016-08-04 00:00:33 -04002455 } else
Chuck Lever46121cf2007-01-31 12:14:08 -05002456 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002457
2458 queue_delayed_work(xprtiod_workqueue,
2459 &transport->connect_worker,
2460 delay);
Chuck Levera246b012005-08-11 16:25:23 -04002461}
2462
Chuck Lever262ca072006-03-20 13:44:16 -05002463/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002464 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2465 * @xprt: rpc_xprt struct containing statistics
2466 * @seq: output file
2467 *
2468 */
2469static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2470{
2471 long idle_time = 0;
2472
2473 if (xprt_connected(xprt))
2474 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2475
2476 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
Andy Adamson15a45202012-02-14 16:19:18 -05002477 "%llu %llu %lu %llu %llu\n",
Chuck Lever176e21e2011-05-09 15:22:44 -04002478 xprt->stat.bind_count,
2479 xprt->stat.connect_count,
Chuck Lever8440a882018-10-01 14:25:41 -04002480 xprt->stat.connect_time / HZ,
Chuck Lever176e21e2011-05-09 15:22:44 -04002481 idle_time,
2482 xprt->stat.sends,
2483 xprt->stat.recvs,
2484 xprt->stat.bad_xids,
2485 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002486 xprt->stat.bklog_u,
2487 xprt->stat.max_slots,
2488 xprt->stat.sending_u,
2489 xprt->stat.pending_u);
Chuck Lever176e21e2011-05-09 15:22:44 -04002490}
2491
2492/**
Chuck Lever262ca072006-03-20 13:44:16 -05002493 * xs_udp_print_stats - display UDP socket-specifc stats
2494 * @xprt: rpc_xprt struct containing statistics
2495 * @seq: output file
2496 *
2497 */
2498static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2499{
Chuck Leverc8475462006-12-05 16:35:26 -05002500 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2501
Andy Adamson15a45202012-02-14 16:19:18 -05002502 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2503 "%lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002504 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002505 xprt->stat.bind_count,
2506 xprt->stat.sends,
2507 xprt->stat.recvs,
2508 xprt->stat.bad_xids,
2509 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002510 xprt->stat.bklog_u,
2511 xprt->stat.max_slots,
2512 xprt->stat.sending_u,
2513 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002514}
2515
2516/**
2517 * xs_tcp_print_stats - display TCP socket-specifc stats
2518 * @xprt: rpc_xprt struct containing statistics
2519 * @seq: output file
2520 *
2521 */
2522static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2523{
Chuck Leverc8475462006-12-05 16:35:26 -05002524 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002525 long idle_time = 0;
2526
2527 if (xprt_connected(xprt))
2528 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2529
Andy Adamson15a45202012-02-14 16:19:18 -05002530 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2531 "%llu %llu %lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002532 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002533 xprt->stat.bind_count,
2534 xprt->stat.connect_count,
Chuck Lever8440a882018-10-01 14:25:41 -04002535 xprt->stat.connect_time / HZ,
Chuck Lever262ca072006-03-20 13:44:16 -05002536 idle_time,
2537 xprt->stat.sends,
2538 xprt->stat.recvs,
2539 xprt->stat.bad_xids,
2540 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002541 xprt->stat.bklog_u,
2542 xprt->stat.max_slots,
2543 xprt->stat.sending_u,
2544 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002545}
2546
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002547/*
2548 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2549 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2550 * to use the server side send routines.
2551 */
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002552static int bc_malloc(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002553{
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002554 struct rpc_rqst *rqst = task->tk_rqstp;
2555 size_t size = rqst->rq_callsize;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002556 struct page *page;
2557 struct rpc_buffer *buf;
2558
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002559 if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
2560 WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
2561 size);
2562 return -EINVAL;
2563 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002564
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002565 page = alloc_page(GFP_KERNEL);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002566 if (!page)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002567 return -ENOMEM;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002568
2569 buf = page_address(page);
2570 buf->len = PAGE_SIZE;
2571
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002572 rqst->rq_buffer = buf->data;
Jeff Layton18e601d2016-10-24 20:33:23 -04002573 rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002574 return 0;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002575}
2576
2577/*
2578 * Free the space allocated in the bc_alloc routine
2579 */
Chuck Lever3435c742016-09-15 10:55:29 -04002580static void bc_free(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002581{
Chuck Lever3435c742016-09-15 10:55:29 -04002582 void *buffer = task->tk_rqstp->rq_buffer;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002583 struct rpc_buffer *buf;
2584
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002585 buf = container_of(buffer, struct rpc_buffer, data);
2586 free_page((unsigned long)buf);
2587}
2588
2589/*
2590 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2591 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2592 */
2593static int bc_sendto(struct rpc_rqst *req)
2594{
2595 int len;
2596 struct xdr_buf *xbufp = &req->rq_snd_buf;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002597 struct sock_xprt *transport =
Chuck Lever067fb112019-02-11 11:24:37 -05002598 container_of(req->rq_xprt, struct sock_xprt, xprt);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002599 unsigned long headoff;
2600 unsigned long tailoff;
Chuck Lever067fb112019-02-11 11:24:37 -05002601 struct page *tailpage;
2602 struct msghdr msg = {
2603 .msg_flags = MSG_MORE
2604 };
2605 rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
2606 (u32)xbufp->len);
2607 struct kvec iov = {
2608 .iov_base = &marker,
2609 .iov_len = sizeof(marker),
2610 };
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002611
Chuck Lever067fb112019-02-11 11:24:37 -05002612 len = kernel_sendmsg(transport->sock, &msg, &iov, 1, iov.iov_len);
2613 if (len != iov.iov_len)
2614 return -EAGAIN;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002615
Chuck Lever067fb112019-02-11 11:24:37 -05002616 tailpage = NULL;
2617 if (xbufp->tail[0].iov_len)
2618 tailpage = virt_to_page(xbufp->tail[0].iov_base);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002619 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2620 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
Chuck Lever067fb112019-02-11 11:24:37 -05002621 len = svc_send_common(transport->sock, xbufp,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002622 virt_to_page(xbufp->head[0].iov_base), headoff,
Chuck Lever067fb112019-02-11 11:24:37 -05002623 tailpage, tailoff);
2624 if (len != xbufp->len)
2625 return -EAGAIN;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002626 return len;
2627}
2628
2629/*
2630 * The send routine. Borrows from svc_send
2631 */
Trond Myklebustadfa7142018-09-03 23:58:59 -04002632static int bc_send_request(struct rpc_rqst *req)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002633{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002634 struct svc_xprt *xprt;
Andrzej Hajda7fc56132015-09-24 16:00:09 +02002635 int len;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002636
2637 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2638 /*
2639 * Get the server socket associated with this callback xprt
2640 */
2641 xprt = req->rq_xprt->bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002642
2643 /*
2644 * Grab the mutex to serialize data as the connection is shared
2645 * with the fore channel
2646 */
Trond Myklebustc5445772018-09-03 23:39:27 -04002647 mutex_lock(&xprt->xpt_mutex);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002648 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2649 len = -ENOTCONN;
2650 else
2651 len = bc_sendto(req);
2652 mutex_unlock(&xprt->xpt_mutex);
2653
2654 if (len > 0)
2655 len = 0;
2656
2657 return len;
2658}
2659
2660/*
2661 * The close routine. Since this is client initiated, we do nothing
2662 */
2663
2664static void bc_close(struct rpc_xprt *xprt)
2665{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002666}
2667
2668/*
2669 * The xprt destroy routine. Again, because this connection is client
2670 * initiated, we do nothing
2671 */
2672
2673static void bc_destroy(struct rpc_xprt *xprt)
2674{
Kinglong Mee47f72ef2014-03-24 11:58:16 +08002675 dprintk("RPC: bc_destroy xprt %p\n", xprt);
2676
2677 xs_xprt_free(xprt);
2678 module_put(THIS_MODULE);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002679}
2680
Chuck Leverd31ae252017-08-01 12:00:39 -04002681static const struct rpc_xprt_ops xs_local_ops = {
Chuck Lever176e21e2011-05-09 15:22:44 -04002682 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebust4cd34e72018-08-31 10:00:02 -04002683 .release_xprt = xprt_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002684 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002685 .free_slot = xprt_free_slot,
Chuck Lever176e21e2011-05-09 15:22:44 -04002686 .rpcbind = xs_local_rpcbind,
2687 .set_port = xs_local_set_port,
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002688 .connect = xs_local_connect,
Chuck Lever176e21e2011-05-09 15:22:44 -04002689 .buf_alloc = rpc_malloc,
2690 .buf_free = rpc_free,
Trond Myklebust550aebf2018-09-14 14:32:45 -04002691 .prepare_request = xs_stream_prepare_request,
Chuck Lever176e21e2011-05-09 15:22:44 -04002692 .send_request = xs_local_send_request,
2693 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2694 .close = xs_close,
Trond Myklebusta1311d82013-10-31 09:18:49 -04002695 .destroy = xs_destroy,
Chuck Lever176e21e2011-05-09 15:22:44 -04002696 .print_stats = xs_local_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002697 .enable_swap = xs_enable_swap,
2698 .disable_swap = xs_disable_swap,
Chuck Lever176e21e2011-05-09 15:22:44 -04002699};
2700
Chuck Leverd31ae252017-08-01 12:00:39 -04002701static const struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002702 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002703 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002704 .release_xprt = xprt_release_xprt_cong,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002705 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002706 .free_slot = xprt_free_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002707 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002708 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002709 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002710 .buf_alloc = rpc_malloc,
2711 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002712 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002713 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002714 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002715 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002716 .close = xs_close,
2717 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002718 .print_stats = xs_udp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002719 .enable_swap = xs_enable_swap,
2720 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002721 .inject_disconnect = xs_inject_disconnect,
Chuck Lever262965f2005-08-11 16:25:56 -04002722};
2723
Chuck Leverd31ae252017-08-01 12:00:39 -04002724static const struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002725 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebust4cd34e72018-08-31 10:00:02 -04002726 .release_xprt = xprt_release_xprt,
Trond Myklebust36bd7de2018-09-03 18:41:32 -04002727 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002728 .free_slot = xprt_free_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002729 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002730 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002731 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002732 .buf_alloc = rpc_malloc,
2733 .buf_free = rpc_free,
Trond Myklebust277e4ab2018-09-14 09:49:06 -04002734 .prepare_request = xs_stream_prepare_request,
Chuck Lever262965f2005-08-11 16:25:56 -04002735 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002736 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustc627d312015-02-10 11:06:04 -05002737 .close = xs_tcp_shutdown,
Chuck Lever9903cd12005-08-11 16:25:26 -04002738 .destroy = xs_destroy,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002739 .set_connect_timeout = xs_tcp_set_connect_timeout,
Chuck Lever262ca072006-03-20 13:44:16 -05002740 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002741 .enable_swap = xs_enable_swap,
2742 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002743 .inject_disconnect = xs_inject_disconnect,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002744#ifdef CONFIG_SUNRPC_BACKCHANNEL
2745 .bc_setup = xprt_setup_bc,
Chuck Lever6b26cc82016-05-02 14:40:40 -04002746 .bc_maxpayload = xs_tcp_bc_maxpayload,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002747 .bc_free_rqst = xprt_free_bc_rqst,
2748 .bc_destroy = xprt_destroy_bc,
2749#endif
Chuck Levera246b012005-08-11 16:25:23 -04002750};
2751
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002752/*
2753 * The rpc_xprt_ops for the server backchannel
2754 */
2755
Chuck Leverd31ae252017-08-01 12:00:39 -04002756static const struct rpc_xprt_ops bc_tcp_ops = {
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002757 .reserve_xprt = xprt_reserve_xprt,
2758 .release_xprt = xprt_release_xprt,
Bryan Schumaker84e28a32012-09-24 13:39:01 -04002759 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002760 .free_slot = xprt_free_slot,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002761 .buf_alloc = bc_malloc,
2762 .buf_free = bc_free,
2763 .send_request = bc_send_request,
2764 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2765 .close = bc_close,
2766 .destroy = bc_destroy,
2767 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002768 .enable_swap = xs_enable_swap,
2769 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002770 .inject_disconnect = xs_inject_disconnect,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002771};
2772
Chuck Lever92476852010-10-20 11:53:01 -04002773static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2774{
2775 static const struct sockaddr_in sin = {
2776 .sin_family = AF_INET,
2777 .sin_addr.s_addr = htonl(INADDR_ANY),
2778 };
2779 static const struct sockaddr_in6 sin6 = {
2780 .sin6_family = AF_INET6,
2781 .sin6_addr = IN6ADDR_ANY_INIT,
2782 };
2783
2784 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002785 case AF_LOCAL:
2786 break;
Chuck Lever92476852010-10-20 11:53:01 -04002787 case AF_INET:
2788 memcpy(sap, &sin, sizeof(sin));
2789 break;
2790 case AF_INET6:
2791 memcpy(sap, &sin6, sizeof(sin6));
2792 break;
2793 default:
2794 dprintk("RPC: %s: Bad address family\n", __func__);
2795 return -EAFNOSUPPORT;
2796 }
2797 return 0;
2798}
2799
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002800static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002801 unsigned int slot_table_size,
2802 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002803{
2804 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002805 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002806
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002807 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002808 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002809 return ERR_PTR(-EBADF);
2810 }
2811
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002812 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2813 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002814 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002815 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2816 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002817 return ERR_PTR(-ENOMEM);
2818 }
2819
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002820 new = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002821 mutex_init(&new->recv_mutex);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002822 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2823 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002824 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002825 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002826 else {
2827 int err;
2828 err = xs_init_anyaddr(args->dstaddr->sa_family,
2829 (struct sockaddr *)&new->srcaddr);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002830 if (err != 0) {
2831 xprt_free(xprt);
Chuck Lever92476852010-10-20 11:53:01 -04002832 return ERR_PTR(err);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002833 }
Chuck Lever92476852010-10-20 11:53:01 -04002834 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002835
2836 return xprt;
2837}
2838
Chuck Lever176e21e2011-05-09 15:22:44 -04002839static const struct rpc_timeout xs_local_default_timeout = {
2840 .to_initval = 10 * HZ,
2841 .to_maxval = 10 * HZ,
2842 .to_retries = 2,
2843};
2844
2845/**
2846 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2847 * @args: rpc transport creation arguments
2848 *
2849 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2850 */
2851static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2852{
2853 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2854 struct sock_xprt *transport;
2855 struct rpc_xprt *xprt;
2856 struct rpc_xprt *ret;
2857
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002858 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2859 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002860 if (IS_ERR(xprt))
2861 return xprt;
2862 transport = container_of(xprt, struct sock_xprt, xprt);
2863
2864 xprt->prot = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -04002865 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2866
2867 xprt->bind_timeout = XS_BIND_TO;
2868 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2869 xprt->idle_timeout = XS_IDLE_DISC_TO;
2870
2871 xprt->ops = &xs_local_ops;
2872 xprt->timeout = &xs_local_default_timeout;
2873
Trond Myklebust550aebf2018-09-14 14:32:45 -04002874 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
2875 INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket);
NeilBrown93dc41b2013-10-31 16:14:36 +11002876
Chuck Lever176e21e2011-05-09 15:22:44 -04002877 switch (sun->sun_family) {
2878 case AF_LOCAL:
2879 if (sun->sun_path[0] != '/') {
2880 dprintk("RPC: bad AF_LOCAL address: %s\n",
2881 sun->sun_path);
2882 ret = ERR_PTR(-EINVAL);
2883 goto out_err;
2884 }
2885 xprt_set_bound(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002886 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
J. Bruce Fields7073ea82013-02-21 10:14:22 -05002887 ret = ERR_PTR(xs_local_setup_socket(transport));
2888 if (ret)
2889 goto out_err;
Chuck Lever176e21e2011-05-09 15:22:44 -04002890 break;
2891 default:
2892 ret = ERR_PTR(-EAFNOSUPPORT);
2893 goto out_err;
2894 }
2895
2896 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2897 xprt->address_strings[RPC_DISPLAY_ADDR]);
2898
2899 if (try_module_get(THIS_MODULE))
2900 return xprt;
2901 ret = ERR_PTR(-EINVAL);
2902out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002903 xs_xprt_free(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002904 return ret;
2905}
2906
Trond Myklebust2881ae72007-12-20 16:03:54 -05002907static const struct rpc_timeout xs_udp_default_timeout = {
2908 .to_initval = 5 * HZ,
2909 .to_maxval = 30 * HZ,
2910 .to_increment = 5 * HZ,
2911 .to_retries = 5,
2912};
2913
Chuck Lever9903cd12005-08-11 16:25:26 -04002914/**
2915 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002916 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002917 *
2918 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002919static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002920{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002921 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002922 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002923 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002924 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002925
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002926 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2927 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002928 if (IS_ERR(xprt))
2929 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002930 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002931
Chuck Leverec739ef2006-08-22 20:06:15 -04002932 xprt->prot = IPPROTO_UDP;
Chuck Levera246b012005-08-11 16:25:23 -04002933 /* XXX: header size can vary due to auth type, IPv6, etc. */
2934 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2935
Chuck Lever03bf4b72005-08-25 16:25:55 -07002936 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002937 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2938 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002939
Chuck Lever262965f2005-08-11 16:25:56 -04002940 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002941
Trond Myklebustba7392b2007-12-20 16:03:55 -05002942 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002943
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002944 INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002945 INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2946
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002947 switch (addr->sa_family) {
2948 case AF_INET:
2949 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2950 xprt_set_bound(xprt);
2951
Chuck Lever9dc3b092009-08-09 15:09:46 -04002952 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002953 break;
2954 case AF_INET6:
2955 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2956 xprt_set_bound(xprt);
2957
Chuck Lever9dc3b092009-08-09 15:09:46 -04002958 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002959 break;
2960 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002961 ret = ERR_PTR(-EAFNOSUPPORT);
2962 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002963 }
2964
Chuck Leverc740eff2009-08-09 15:09:46 -04002965 if (xprt_bound(xprt))
2966 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2967 xprt->address_strings[RPC_DISPLAY_ADDR],
2968 xprt->address_strings[RPC_DISPLAY_PORT],
2969 xprt->address_strings[RPC_DISPLAY_PROTO]);
2970 else
2971 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2972 xprt->address_strings[RPC_DISPLAY_ADDR],
2973 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002974
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002975 if (try_module_get(THIS_MODULE))
2976 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002977 ret = ERR_PTR(-EINVAL);
2978out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002979 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002980 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002981}
2982
Trond Myklebust2881ae72007-12-20 16:03:54 -05002983static const struct rpc_timeout xs_tcp_default_timeout = {
2984 .to_initval = 60 * HZ,
2985 .to_maxval = 60 * HZ,
2986 .to_retries = 2,
2987};
2988
Chuck Lever9903cd12005-08-11 16:25:26 -04002989/**
2990 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002991 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002992 *
2993 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002994static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002995{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002996 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002997 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002998 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002999 struct rpc_xprt *ret;
Trond Myklebustb7993ce2013-04-14 11:42:00 -04003000 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
3001
3002 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
3003 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Levera246b012005-08-11 16:25:23 -04003004
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003005 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
Trond Myklebustb7993ce2013-04-14 11:42:00 -04003006 max_slot_table_size);
Chuck Leverc8541ec2006-10-17 14:44:27 -04003007 if (IS_ERR(xprt))
3008 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05003009 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04003010
Chuck Leverec739ef2006-08-22 20:06:15 -04003011 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07003012 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04003013
Chuck Lever03bf4b72005-08-25 16:25:55 -07003014 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07003015 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
3016 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04003017
Chuck Lever262965f2005-08-11 16:25:56 -04003018 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05003019 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04003020
Trond Myklebust3851f1c2016-08-04 00:08:45 -04003021 xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05003022 xprt->connect_timeout = xprt->timeout->to_initval *
3023 (xprt->timeout->to_retries + 1);
Trond Myklebust3851f1c2016-08-04 00:08:45 -04003024
Trond Myklebustc50b8ee2018-09-14 14:26:28 -04003025 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
Trond Myklebustedc1b012015-10-05 10:53:49 -04003026 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
3027
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003028 switch (addr->sa_family) {
3029 case AF_INET:
3030 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
3031 xprt_set_bound(xprt);
3032
Chuck Lever9dc3b092009-08-09 15:09:46 -04003033 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003034 break;
3035 case AF_INET6:
3036 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
3037 xprt_set_bound(xprt);
3038
Chuck Lever9dc3b092009-08-09 15:09:46 -04003039 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003040 break;
3041 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003042 ret = ERR_PTR(-EAFNOSUPPORT);
3043 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003044 }
3045
Chuck Leverc740eff2009-08-09 15:09:46 -04003046 if (xprt_bound(xprt))
3047 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3048 xprt->address_strings[RPC_DISPLAY_ADDR],
3049 xprt->address_strings[RPC_DISPLAY_PORT],
3050 xprt->address_strings[RPC_DISPLAY_PROTO]);
3051 else
3052 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
3053 xprt->address_strings[RPC_DISPLAY_ADDR],
3054 xprt->address_strings[RPC_DISPLAY_PROTO]);
3055
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003056 if (try_module_get(THIS_MODULE))
3057 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003058 ret = ERR_PTR(-EINVAL);
3059out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003060 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003061 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04003062}
Chuck Lever282b32e2006-12-05 16:35:51 -05003063
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003064/**
3065 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3066 * @args: rpc transport creation arguments
3067 *
3068 */
3069static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
3070{
3071 struct sockaddr *addr = args->dstaddr;
3072 struct rpc_xprt *xprt;
3073 struct sock_xprt *transport;
3074 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003075 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003076
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003077 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3078 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003079 if (IS_ERR(xprt))
3080 return xprt;
3081 transport = container_of(xprt, struct sock_xprt, xprt);
3082
3083 xprt->prot = IPPROTO_TCP;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003084 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3085 xprt->timeout = &xs_tcp_default_timeout;
3086
3087 /* backchannel */
3088 xprt_set_bound(xprt);
3089 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003090 xprt->reestablish_timeout = 0;
3091 xprt->idle_timeout = 0;
3092
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003093 xprt->ops = &bc_tcp_ops;
3094
3095 switch (addr->sa_family) {
3096 case AF_INET:
3097 xs_format_peer_addresses(xprt, "tcp",
3098 RPCBIND_NETID_TCP);
3099 break;
3100 case AF_INET6:
3101 xs_format_peer_addresses(xprt, "tcp",
3102 RPCBIND_NETID_TCP6);
3103 break;
3104 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003105 ret = ERR_PTR(-EAFNOSUPPORT);
3106 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003107 }
3108
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04003109 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3110 xprt->address_strings[RPC_DISPLAY_ADDR],
3111 xprt->address_strings[RPC_DISPLAY_PORT],
3112 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003113
3114 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003115 * Once we've associated a backchannel xprt with a connection,
Weng Meiling28303ca2013-11-30 17:56:44 +08003116 * we want to keep it around as long as the connection lasts,
3117 * in case we need to start using it for a backchannel again;
3118 * this reference won't be dropped until bc_xprt is destroyed.
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003119 */
3120 xprt_get(xprt);
3121 args->bc_xprt->xpt_bc_xprt = xprt;
3122 xprt->bc_xprt = args->bc_xprt;
3123 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
3124 transport->sock = bc_sock->sk_sock;
3125 transport->inet = bc_sock->sk_sk;
3126
3127 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003128 * Since we don't want connections for the backchannel, we set
3129 * the xprt status to connected
3130 */
3131 xprt_set_connected(xprt);
3132
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003133 if (try_module_get(THIS_MODULE))
3134 return xprt;
Kinglong Mee642aab52014-03-24 12:00:28 +08003135
3136 args->bc_xprt->xpt_bc_xprt = NULL;
J. Bruce Fields39a9bea2016-05-17 12:38:21 -04003137 args->bc_xprt->xpt_bc_xps = NULL;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003138 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003139 ret = ERR_PTR(-EINVAL);
3140out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003141 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003142 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003143}
3144
Chuck Lever176e21e2011-05-09 15:22:44 -04003145static struct xprt_class xs_local_transport = {
3146 .list = LIST_HEAD_INIT(xs_local_transport.list),
3147 .name = "named UNIX socket",
3148 .owner = THIS_MODULE,
3149 .ident = XPRT_TRANSPORT_LOCAL,
3150 .setup = xs_setup_local,
3151};
3152
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003153static struct xprt_class xs_udp_transport = {
3154 .list = LIST_HEAD_INIT(xs_udp_transport.list),
3155 .name = "udp",
3156 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003157 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003158 .setup = xs_setup_udp,
3159};
3160
3161static struct xprt_class xs_tcp_transport = {
3162 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
3163 .name = "tcp",
3164 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003165 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003166 .setup = xs_setup_tcp,
3167};
3168
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003169static struct xprt_class xs_bc_tcp_transport = {
3170 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3171 .name = "tcp NFSv4.1 backchannel",
3172 .owner = THIS_MODULE,
3173 .ident = XPRT_TRANSPORT_BC_TCP,
3174 .setup = xs_setup_bc_tcp,
3175};
3176
Chuck Lever282b32e2006-12-05 16:35:51 -05003177/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003178 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05003179 *
3180 */
3181int init_socket_xprt(void)
3182{
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08003183 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08003184 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05003185
Chuck Lever176e21e2011-05-09 15:22:44 -04003186 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003187 xprt_register_transport(&xs_udp_transport);
3188 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003189 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003190
Chuck Lever282b32e2006-12-05 16:35:51 -05003191 return 0;
3192}
3193
3194/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003195 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05003196 *
3197 */
3198void cleanup_socket_xprt(void)
3199{
Chuck Leverfbf76682006-12-05 16:35:54 -05003200 if (sunrpc_table_header) {
3201 unregister_sysctl_table(sunrpc_table_header);
3202 sunrpc_table_header = NULL;
3203 }
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003204
Chuck Lever176e21e2011-05-09 15:22:44 -04003205 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003206 xprt_unregister_transport(&xs_udp_transport);
3207 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003208 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05003209}
Trond Myklebustcbf11072009-08-09 15:06:19 -04003210
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003211static int param_set_uint_minmax(const char *val,
3212 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003213 unsigned int min, unsigned int max)
3214{
Daniel Walter00cfaa92014-06-21 13:06:38 +01003215 unsigned int num;
Trond Myklebustcbf11072009-08-09 15:06:19 -04003216 int ret;
3217
3218 if (!val)
3219 return -EINVAL;
Daniel Walter00cfaa92014-06-21 13:06:38 +01003220 ret = kstrtouint(val, 0, &num);
Dan Carpenter9761a242017-02-19 00:34:59 +03003221 if (ret)
3222 return ret;
3223 if (num < min || num > max)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003224 return -EINVAL;
3225 *((unsigned int *)kp->arg) = num;
3226 return 0;
3227}
3228
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003229static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003230{
Frank Sorensonffb6ca32016-07-08 16:35:25 -05003231 return param_set_uint_minmax(val, kp,
J. Bruce Fields826799e2018-10-18 15:27:02 -04003232 RPC_MIN_RESVPORT,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003233 RPC_MAX_RESVPORT);
3234}
3235
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303236static const struct kernel_param_ops param_ops_portnr = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003237 .set = param_set_portnr,
3238 .get = param_get_uint,
3239};
3240
Trond Myklebustcbf11072009-08-09 15:06:19 -04003241#define param_check_portnr(name, p) \
3242 __param_check(name, p, unsigned int);
3243
3244module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3245module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3246
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003247static int param_set_slot_table_size(const char *val,
3248 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003249{
3250 return param_set_uint_minmax(val, kp,
3251 RPC_MIN_SLOT_TABLE,
3252 RPC_MAX_SLOT_TABLE);
3253}
3254
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303255static const struct kernel_param_ops param_ops_slot_table_size = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003256 .set = param_set_slot_table_size,
3257 .get = param_get_uint,
3258};
3259
Trond Myklebustcbf11072009-08-09 15:06:19 -04003260#define param_check_slot_table_size(name, p) \
3261 __param_check(name, p, unsigned int);
3262
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003263static int param_set_max_slot_table_size(const char *val,
3264 const struct kernel_param *kp)
3265{
3266 return param_set_uint_minmax(val, kp,
3267 RPC_MIN_SLOT_TABLE,
3268 RPC_MAX_SLOT_TABLE_LIMIT);
3269}
3270
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303271static const struct kernel_param_ops param_ops_max_slot_table_size = {
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003272 .set = param_set_max_slot_table_size,
3273 .get = param_get_uint,
3274};
3275
3276#define param_check_max_slot_table_size(name, p) \
3277 __param_check(name, p, unsigned int);
3278
Trond Myklebustcbf11072009-08-09 15:06:19 -04003279module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3280 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003281module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3282 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003283module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3284 slot_table_size, 0644);