blob: ae09d850cd11b3148f11ffd09db8fb31d900f6ae [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>
Chuck Levera246b012005-08-11 16:25:23 -040053
Trond Myklebust40b5ea02013-09-04 12:16:23 -040054#include <trace/events/sunrpc.h>
55
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030056#include "sunrpc.h"
Chuck Lever176e21e2011-05-09 15:22:44 -040057
58static void xs_close(struct rpc_xprt *xprt);
Trond Myklebust7196dbb2017-02-08 11:17:54 -050059static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
60 struct socket *sock);
Chuck Lever176e21e2011-05-09 15:22:44 -040061
Chuck Lever9903cd12005-08-11 16:25:26 -040062/*
Chuck Leverc556b752005-11-01 12:24:48 -050063 * xprtsock tunables
64 */
Trond Myklebust09acfea2012-03-11 15:22:54 -040065static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
66static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
67static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
Chuck Leverc556b752005-11-01 12:24:48 -050068
Trond Myklebust09acfea2012-03-11 15:22:54 -040069static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
70static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
Chuck Leverc556b752005-11-01 12:24:48 -050071
Trond Myklebust7d1e8252009-03-11 14:38:03 -040072#define XS_TCP_LINGER_TO (15U * HZ)
Trond Myklebust25fe6142009-03-11 14:38:03 -040073static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
Trond Myklebust7d1e8252009-03-11 14:38:03 -040074
Chuck Leverc556b752005-11-01 12:24:48 -050075/*
Chuck Leverfbf76682006-12-05 16:35:54 -050076 * We can register our own files under /proc/sys/sunrpc by
77 * calling register_sysctl_table() again. The files in that
78 * directory become the union of all files registered there.
79 *
80 * We simply need to make sure that we don't collide with
81 * someone else's file names!
82 */
83
Chuck Leverfbf76682006-12-05 16:35:54 -050084static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
85static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
Trond Myklebustd9ba1312011-07-17 18:11:30 -040086static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Leverfbf76682006-12-05 16:35:54 -050087static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
88static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
89
90static struct ctl_table_header *sunrpc_table_header;
91
92/*
93 * FIXME: changing the UDP slot table size should also resize the UDP
94 * socket buffers for existing UDP transports
95 */
Joe Perchesfe2c6332013-06-11 23:04:25 -070096static struct ctl_table xs_tunables_table[] = {
Chuck Leverfbf76682006-12-05 16:35:54 -050097 {
Chuck Leverfbf76682006-12-05 16:35:54 -050098 .procname = "udp_slot_table_entries",
99 .data = &xprt_udp_slot_table_entries,
100 .maxlen = sizeof(unsigned int),
101 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800102 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500103 .extra1 = &min_slot_table_size,
104 .extra2 = &max_slot_table_size
105 },
106 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500107 .procname = "tcp_slot_table_entries",
108 .data = &xprt_tcp_slot_table_entries,
109 .maxlen = sizeof(unsigned int),
110 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800111 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500112 .extra1 = &min_slot_table_size,
113 .extra2 = &max_slot_table_size
114 },
115 {
Trond Myklebustd9ba1312011-07-17 18:11:30 -0400116 .procname = "tcp_max_slot_table_entries",
117 .data = &xprt_max_tcp_slot_table_entries,
118 .maxlen = sizeof(unsigned int),
119 .mode = 0644,
120 .proc_handler = proc_dointvec_minmax,
121 .extra1 = &min_slot_table_size,
122 .extra2 = &max_tcp_slot_table_limit
123 },
124 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500125 .procname = "min_resvport",
126 .data = &xprt_min_resvport,
127 .maxlen = sizeof(unsigned int),
128 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800129 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500130 .extra1 = &xprt_min_resvport_limit,
J. Bruce Fields826799e2018-10-18 15:27:02 -0400131 .extra2 = &xprt_max_resvport_limit
Chuck Leverfbf76682006-12-05 16:35:54 -0500132 },
133 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500134 .procname = "max_resvport",
135 .data = &xprt_max_resvport,
136 .maxlen = sizeof(unsigned int),
137 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800138 .proc_handler = proc_dointvec_minmax,
J. Bruce Fields826799e2018-10-18 15:27:02 -0400139 .extra1 = &xprt_min_resvport_limit,
Chuck Leverfbf76682006-12-05 16:35:54 -0500140 .extra2 = &xprt_max_resvport_limit
141 },
142 {
Trond Myklebust25fe6142009-03-11 14:38:03 -0400143 .procname = "tcp_fin_timeout",
144 .data = &xs_tcp_fin_timeout,
145 .maxlen = sizeof(xs_tcp_fin_timeout),
146 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800147 .proc_handler = proc_dointvec_jiffies,
Trond Myklebust25fe6142009-03-11 14:38:03 -0400148 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800149 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500150};
151
Joe Perchesfe2c6332013-06-11 23:04:25 -0700152static struct ctl_table sunrpc_table[] = {
Chuck Leverfbf76682006-12-05 16:35:54 -0500153 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500154 .procname = "sunrpc",
155 .mode = 0555,
156 .child = xs_tunables_table
157 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800158 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500159};
160
Chuck Leverfbf76682006-12-05 16:35:54 -0500161/*
Chuck Lever03bf4b72005-08-25 16:25:55 -0700162 * Wait duration for a reply from the RPC portmapper.
163 */
164#define XS_BIND_TO (60U * HZ)
165
166/*
167 * Delay if a UDP socket connect error occurs. This is most likely some
168 * kind of resource problem on the local host.
169 */
170#define XS_UDP_REEST_TO (2U * HZ)
171
172/*
173 * The reestablish timeout allows clients to delay for a bit before attempting
174 * to reconnect to a server that just dropped our connection.
175 *
176 * We implement an exponential backoff when trying to reestablish a TCP
177 * transport connection with the server. Some servers like to drop a TCP
178 * connection when they are overworked, so we start with a short timeout and
179 * increase over time if the server is down or not responding.
180 */
181#define XS_TCP_INIT_REEST_TO (3U * HZ)
Chuck Lever03bf4b72005-08-25 16:25:55 -0700182
183/*
184 * TCP idle timeout; client drops the transport socket if it is idle
185 * for this long. Note that we also timeout UDP sockets to prevent
186 * holding port numbers when there is no RPC traffic.
187 */
188#define XS_IDLE_DISC_TO (5U * 60 * HZ)
189
Jeff Laytonf895b252014-11-17 16:58:04 -0500190#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Levera246b012005-08-11 16:25:23 -0400191# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400192# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400193#endif
194
Chuck Levera246b012005-08-11 16:25:23 -0400195#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400196static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400197{
Chuck Lever9903cd12005-08-11 16:25:26 -0400198 u8 *buf = (u8 *) packet;
199 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400200
Chuck Lever46121cf2007-01-31 12:14:08 -0500201 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400202 for (j = 0; j < count && j < 128; j += 4) {
203 if (!(j & 31)) {
204 if (j)
205 dprintk("\n");
206 dprintk("0x%04x ", j);
207 }
208 dprintk("%02x%02x%02x%02x ",
209 buf[j], buf[j+1], buf[j+2], buf[j+3]);
210 }
211 dprintk("\n");
212}
213#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400214static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400215{
216 /* NOP */
217}
218#endif
219
Trond Myklebust21180712013-12-31 13:22:59 -0500220static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
221{
222 return (struct rpc_xprt *) sk->sk_user_data;
223}
224
Chuck Lever95392c52007-08-06 11:57:58 -0400225static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400226{
Chuck Lever95392c52007-08-06 11:57:58 -0400227 return (struct sockaddr *) &xprt->addr;
228}
229
Chuck Lever176e21e2011-05-09 15:22:44 -0400230static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
231{
232 return (struct sockaddr_un *) &xprt->addr;
233}
234
Chuck Lever95392c52007-08-06 11:57:58 -0400235static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
236{
237 return (struct sockaddr_in *) &xprt->addr;
238}
239
240static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
241{
242 return (struct sockaddr_in6 *) &xprt->addr;
243}
244
Chuck Leverc877b842009-08-09 15:09:36 -0400245static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400246{
Chuck Leverc877b842009-08-09 15:09:36 -0400247 struct sockaddr *sap = xs_addr(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400248 struct sockaddr_in6 *sin6;
249 struct sockaddr_in *sin;
Chuck Lever176e21e2011-05-09 15:22:44 -0400250 struct sockaddr_un *sun;
Chuck Leverc877b842009-08-09 15:09:36 -0400251 char buf[128];
Chuck Leveredb267a2006-08-22 20:06:18 -0400252
Chuck Lever9dc3b092009-08-09 15:09:46 -0400253 switch (sap->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400254 case AF_LOCAL:
255 sun = xs_addr_un(xprt);
256 strlcpy(buf, sun->sun_path, sizeof(buf));
257 xprt->address_strings[RPC_DISPLAY_ADDR] =
258 kstrdup(buf, GFP_KERNEL);
259 break;
Chuck Lever9dc3b092009-08-09 15:09:46 -0400260 case AF_INET:
Chuck Lever176e21e2011-05-09 15:22:44 -0400261 (void)rpc_ntop(sap, buf, sizeof(buf));
262 xprt->address_strings[RPC_DISPLAY_ADDR] =
263 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400264 sin = xs_addr_in(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800265 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
Chuck Lever9dc3b092009-08-09 15:09:46 -0400266 break;
267 case AF_INET6:
Chuck Lever176e21e2011-05-09 15:22:44 -0400268 (void)rpc_ntop(sap, buf, sizeof(buf));
269 xprt->address_strings[RPC_DISPLAY_ADDR] =
270 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400271 sin6 = xs_addr_in6(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800272 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400273 break;
274 default:
275 BUG();
Chuck Leveredb267a2006-08-22 20:06:18 -0400276 }
Chuck Lever176e21e2011-05-09 15:22:44 -0400277
Chuck Lever9dc3b092009-08-09 15:09:46 -0400278 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
Chuck Leveredb267a2006-08-22 20:06:18 -0400279}
280
Chuck Lever9dc3b092009-08-09 15:09:46 -0400281static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400282{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400283 struct sockaddr *sap = xs_addr(xprt);
284 char buf[128];
Chuck Lever4b6473f2007-08-06 11:57:12 -0400285
Joe Perches81160e662010-03-08 12:15:59 -0800286 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400287 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400288
Joe Perches81160e662010-03-08 12:15:59 -0800289 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400290 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
291}
Chuck Lever4b6473f2007-08-06 11:57:12 -0400292
Chuck Lever9dc3b092009-08-09 15:09:46 -0400293static void xs_format_peer_addresses(struct rpc_xprt *xprt,
294 const char *protocol,
295 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400296{
Chuck Leverb454ae92008-01-07 18:34:48 -0500297 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leverb454ae92008-01-07 18:34:48 -0500298 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leverc877b842009-08-09 15:09:36 -0400299 xs_format_common_peer_addresses(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400300 xs_format_common_peer_ports(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400301}
302
Chuck Lever9dc3b092009-08-09 15:09:46 -0400303static void xs_update_peer_port(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400304{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400305 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
306 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400307
Chuck Lever9dc3b092009-08-09 15:09:46 -0400308 xs_format_common_peer_ports(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400309}
310
311static void xs_free_peer_addresses(struct rpc_xprt *xprt)
312{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500313 unsigned int i;
314
315 for (i = 0; i < RPC_DISPLAY_MAX; i++)
316 switch (i) {
317 case RPC_DISPLAY_PROTO:
318 case RPC_DISPLAY_NETID:
319 continue;
320 default:
321 kfree(xprt->address_strings[i]);
322 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400323}
324
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400325static size_t
326xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp)
327{
328 size_t i,n;
329
Trond Myklebust16e5e902018-12-02 15:22:48 -0500330 if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES))
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400331 return want;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400332 n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT;
333 for (i = 0; i < n; i++) {
334 if (buf->pages[i])
335 continue;
336 buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp);
337 if (!buf->pages[i]) {
Trond Myklebust16e5e902018-12-02 15:22:48 -0500338 i *= PAGE_SIZE;
339 return i > buf->page_base ? i - buf->page_base : 0;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400340 }
341 }
342 return want;
343}
344
345static ssize_t
346xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek)
347{
348 ssize_t ret;
349 if (seek != 0)
350 iov_iter_advance(&msg->msg_iter, seek);
351 ret = sock_recvmsg(sock, msg, flags);
352 return ret > 0 ? ret + seek : ret;
353}
354
355static ssize_t
356xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags,
357 struct kvec *kvec, size_t count, size_t seek)
358{
Al Viro0e9b4a82018-11-01 18:19:03 -0400359 iov_iter_kvec(&msg->msg_iter, READ, kvec, 1, count);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400360 return xs_sock_recvmsg(sock, msg, flags, seek);
361}
362
363static ssize_t
364xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags,
365 struct bio_vec *bvec, unsigned long nr, size_t count,
366 size_t seek)
367{
Al Viro0e9b4a82018-11-01 18:19:03 -0400368 iov_iter_bvec(&msg->msg_iter, READ, bvec, nr, count);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400369 return xs_sock_recvmsg(sock, msg, flags, seek);
370}
371
372static ssize_t
373xs_read_discard(struct socket *sock, struct msghdr *msg, int flags,
374 size_t count)
375{
Trond Myklebustb76a5af2018-12-03 15:58:58 -0500376 iov_iter_discard(&msg->msg_iter, READ, count);
377 return sock_recvmsg(sock, msg, flags);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400378}
379
Trond Myklebust6a829eb2019-01-03 09:04:45 -0500380#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
381static void
382xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
383{
384 struct bvec_iter bi = {
385 .bi_size = count,
386 };
387 struct bio_vec bv;
388
389 bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
390 for_each_bvec(bv, bvec, bi, bi)
391 flush_dcache_page(bv.bv_page);
392}
393#else
394static inline void
395xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
396{
397}
398#endif
399
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400400static ssize_t
401xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags,
402 struct xdr_buf *buf, size_t count, size_t seek, size_t *read)
403{
404 size_t want, seek_init = seek, offset = 0;
405 ssize_t ret;
406
407 if (seek < buf->head[0].iov_len) {
408 want = min_t(size_t, count, buf->head[0].iov_len);
409 ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek);
410 if (ret <= 0)
411 goto sock_err;
412 offset += ret;
413 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
414 goto out;
415 if (ret != want)
Trond Myklebustc4433052018-12-04 07:52:11 -0500416 goto out;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400417 seek = 0;
418 } else {
419 seek -= buf->head[0].iov_len;
420 offset += buf->head[0].iov_len;
421 }
Trond Myklebust16e5e902018-12-02 15:22:48 -0500422
423 want = xs_alloc_sparse_pages(buf,
424 min_t(size_t, count - offset, buf->page_len),
425 GFP_NOWAIT);
426 if (seek < want) {
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400427 ret = xs_read_bvec(sock, msg, flags, buf->bvec,
428 xdr_buf_pagecount(buf),
429 want + buf->page_base,
430 seek + buf->page_base);
431 if (ret <= 0)
432 goto sock_err;
Trond Myklebust6a829eb2019-01-03 09:04:45 -0500433 xs_flush_bvec(buf->bvec, ret, seek + buf->page_base);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400434 offset += ret - buf->page_base;
435 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
436 goto out;
437 if (ret != want)
Trond Myklebustc4433052018-12-04 07:52:11 -0500438 goto out;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400439 seek = 0;
440 } else {
Trond Myklebust16e5e902018-12-02 15:22:48 -0500441 seek -= want;
442 offset += want;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400443 }
Trond Myklebust16e5e902018-12-02 15:22:48 -0500444
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400445 if (seek < buf->tail[0].iov_len) {
446 want = min_t(size_t, count - offset, buf->tail[0].iov_len);
447 ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek);
448 if (ret <= 0)
449 goto sock_err;
450 offset += ret;
451 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
452 goto out;
453 if (ret != want)
Trond Myklebustc4433052018-12-04 07:52:11 -0500454 goto out;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400455 } else
456 offset += buf->tail[0].iov_len;
457 ret = -EMSGSIZE;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400458out:
459 *read = offset - seek_init;
460 return ret;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400461sock_err:
462 offset += seek;
463 goto out;
464}
465
466static void
467xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf)
468{
469 if (!transport->recv.copied) {
470 if (buf->head[0].iov_len >= transport->recv.offset)
471 memcpy(buf->head[0].iov_base,
472 &transport->recv.xid,
473 transport->recv.offset);
474 transport->recv.copied = transport->recv.offset;
475 }
476}
477
478static bool
479xs_read_stream_request_done(struct sock_xprt *transport)
480{
481 return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT);
482}
483
484static ssize_t
485xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg,
486 int flags, struct rpc_rqst *req)
487{
488 struct xdr_buf *buf = &req->rq_private_buf;
489 size_t want, read;
490 ssize_t ret;
491
492 xs_read_header(transport, buf);
493
494 want = transport->recv.len - transport->recv.offset;
495 ret = xs_read_xdr_buf(transport->sock, msg, flags, buf,
496 transport->recv.copied + want, transport->recv.copied,
497 &read);
498 transport->recv.offset += read;
499 transport->recv.copied += read;
500 if (transport->recv.offset == transport->recv.len) {
501 if (xs_read_stream_request_done(transport))
502 msg->msg_flags |= MSG_EOR;
Trond Myklebustc4433052018-12-04 07:52:11 -0500503 return read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400504 }
505
506 switch (ret) {
Trond Myklebustc4433052018-12-04 07:52:11 -0500507 default:
508 break;
Trond Myklebust26781ea2018-12-03 15:41:38 -0500509 case -EFAULT:
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400510 case -EMSGSIZE:
Trond Myklebust26781ea2018-12-03 15:41:38 -0500511 msg->msg_flags |= MSG_TRUNC;
Trond Myklebustc4433052018-12-04 07:52:11 -0500512 return read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400513 case 0:
514 return -ESHUTDOWN;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400515 }
Trond Myklebustc4433052018-12-04 07:52:11 -0500516 return ret < 0 ? ret : read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400517}
518
519static size_t
520xs_read_stream_headersize(bool isfrag)
521{
522 if (isfrag)
523 return sizeof(__be32);
524 return 3 * sizeof(__be32);
525}
526
527static ssize_t
528xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg,
529 int flags, size_t want, size_t seek)
530{
531 struct kvec kvec = {
532 .iov_base = &transport->recv.fraghdr,
533 .iov_len = want,
534 };
535 return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek);
536}
537
538#if defined(CONFIG_SUNRPC_BACKCHANNEL)
539static ssize_t
540xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
541{
542 struct rpc_xprt *xprt = &transport->xprt;
543 struct rpc_rqst *req;
544 ssize_t ret;
545
546 /* Look up and lock the request corresponding to the given XID */
547 req = xprt_lookup_bc_request(xprt, transport->recv.xid);
548 if (!req) {
549 printk(KERN_WARNING "Callback slot table overflowed\n");
550 return -ESHUTDOWN;
551 }
552
553 ret = xs_read_stream_request(transport, msg, flags, req);
554 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
Trond Myklebustc4433052018-12-04 07:52:11 -0500555 xprt_complete_bc_request(req, transport->recv.copied);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400556
557 return ret;
558}
559#else /* CONFIG_SUNRPC_BACKCHANNEL */
560static ssize_t
561xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
562{
563 return -ESHUTDOWN;
564}
565#endif /* CONFIG_SUNRPC_BACKCHANNEL */
566
567static ssize_t
568xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags)
569{
570 struct rpc_xprt *xprt = &transport->xprt;
571 struct rpc_rqst *req;
572 ssize_t ret = 0;
573
574 /* Look up and lock the request corresponding to the given XID */
575 spin_lock(&xprt->queue_lock);
576 req = xprt_lookup_rqst(xprt, transport->recv.xid);
577 if (!req) {
578 msg->msg_flags |= MSG_TRUNC;
579 goto out;
580 }
581 xprt_pin_rqst(req);
582 spin_unlock(&xprt->queue_lock);
583
584 ret = xs_read_stream_request(transport, msg, flags, req);
585
586 spin_lock(&xprt->queue_lock);
587 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
Trond Myklebustc4433052018-12-04 07:52:11 -0500588 xprt_complete_rqst(req->rq_task, transport->recv.copied);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400589 xprt_unpin_rqst(req);
590out:
591 spin_unlock(&xprt->queue_lock);
592 return ret;
593}
594
595static ssize_t
596xs_read_stream(struct sock_xprt *transport, int flags)
597{
598 struct msghdr msg = { 0 };
599 size_t want, read = 0;
600 ssize_t ret = 0;
601
602 if (transport->recv.len == 0) {
603 want = xs_read_stream_headersize(transport->recv.copied != 0);
604 ret = xs_read_stream_header(transport, &msg, flags, want,
605 transport->recv.offset);
606 if (ret <= 0)
607 goto out_err;
608 transport->recv.offset = ret;
Trond Myklebustc4433052018-12-04 07:52:11 -0500609 if (transport->recv.offset != want)
610 return transport->recv.offset;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400611 transport->recv.len = be32_to_cpu(transport->recv.fraghdr) &
612 RPC_FRAGMENT_SIZE_MASK;
613 transport->recv.offset -= sizeof(transport->recv.fraghdr);
614 read = ret;
615 }
616
617 switch (be32_to_cpu(transport->recv.calldir)) {
Trond Myklebustc4433052018-12-04 07:52:11 -0500618 default:
619 msg.msg_flags |= MSG_TRUNC;
620 break;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400621 case RPC_CALL:
622 ret = xs_read_stream_call(transport, &msg, flags);
623 break;
624 case RPC_REPLY:
625 ret = xs_read_stream_reply(transport, &msg, flags);
626 }
627 if (msg.msg_flags & MSG_TRUNC) {
628 transport->recv.calldir = cpu_to_be32(-1);
629 transport->recv.copied = -1;
630 }
631 if (ret < 0)
632 goto out_err;
633 read += ret;
634 if (transport->recv.offset < transport->recv.len) {
Trond Myklebustc4433052018-12-04 07:52:11 -0500635 if (!(msg.msg_flags & MSG_TRUNC))
636 return read;
Trond Myklebustb76a5af2018-12-03 15:58:58 -0500637 msg.msg_flags = 0;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400638 ret = xs_read_discard(transport->sock, &msg, flags,
639 transport->recv.len - transport->recv.offset);
640 if (ret <= 0)
641 goto out_err;
642 transport->recv.offset += ret;
643 read += ret;
644 if (transport->recv.offset != transport->recv.len)
Trond Myklebustc4433052018-12-04 07:52:11 -0500645 return read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400646 }
647 if (xs_read_stream_request_done(transport)) {
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400648 trace_xs_stream_read_request(transport);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400649 transport->recv.copied = 0;
650 }
651 transport->recv.offset = 0;
652 transport->recv.len = 0;
653 return read;
654out_err:
Trond Myklebust794628572018-12-03 18:49:00 -0500655 return ret != 0 ? ret : -ESHUTDOWN;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400656}
657
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400658static void xs_stream_data_receive(struct sock_xprt *transport)
659{
660 size_t read = 0;
661 ssize_t ret = 0;
662
663 mutex_lock(&transport->recv_mutex);
Trond Myklebustdfcf0382018-12-04 07:50:06 -0500664 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400665 if (transport->sock == NULL)
666 goto out;
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400667 for (;;) {
668 ret = xs_read_stream(transport, MSG_DONTWAIT);
Trond Myklebustc4433052018-12-04 07:52:11 -0500669 if (ret < 0)
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400670 break;
671 read += ret;
672 cond_resched();
673 }
674out:
675 mutex_unlock(&transport->recv_mutex);
676 trace_xs_stream_read_data(&transport->xprt, ret, read);
677}
678
679static void xs_stream_data_receive_workfn(struct work_struct *work)
680{
681 struct sock_xprt *transport =
682 container_of(work, struct sock_xprt, recv_worker);
683 xs_stream_data_receive(transport);
684}
685
Trond Myklebust550aebf2018-09-14 14:32:45 -0400686static void
687xs_stream_reset_connect(struct sock_xprt *transport)
688{
689 transport->recv.offset = 0;
690 transport->recv.len = 0;
691 transport->recv.copied = 0;
692 transport->xmit.offset = 0;
693 transport->xprt.stat.connect_count++;
694 transport->xprt.stat.connect_start = jiffies;
695}
696
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400697#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
698
Chuck Lever067fb112019-02-11 11:24:37 -0500699/* Common case:
700 * - stream transport
701 * - sending from byte 0 of the message
702 * - the message is wholly contained in @xdr's head iovec
703 */
704static int xs_send_rm_and_kvec(struct socket *sock, struct xdr_buf *xdr,
705 unsigned int remainder)
706{
707 struct msghdr msg = {
708 .msg_flags = XS_SENDMSG_FLAGS | (remainder ? MSG_MORE : 0)
709 };
710 rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
711 (u32)xdr->len);
712 struct kvec iov[2] = {
713 {
714 .iov_base = &marker,
715 .iov_len = sizeof(marker)
716 },
717 {
718 .iov_base = xdr->head[0].iov_base,
719 .iov_len = xdr->head[0].iov_len
720 },
721 };
722 int ret;
723
724 ret = kernel_sendmsg(sock, &msg, iov, 2,
725 iov[0].iov_len + iov[1].iov_len);
726 if (ret < 0)
727 return ret;
728 if (ret < iov[0].iov_len)
729 return -EPIPE;
730 return ret - iov[0].iov_len;
731}
732
Trond Myklebust24c56842006-10-17 15:06:22 -0400733static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400734{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400735 struct msghdr msg = {
736 .msg_name = addr,
737 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400738 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
739 };
740 struct kvec iov = {
741 .iov_base = vec->iov_base + base,
742 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400743 };
744
Trond Myklebust24c56842006-10-17 15:06:22 -0400745 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400746 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
747 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
748}
749
Jason Baronf279cd02014-09-24 18:08:00 +0000750static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more, bool zerocopy, int *sent_p)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400751{
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500752 ssize_t (*do_sendpage)(struct socket *sock, struct page *page,
753 int offset, size_t size, int flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400754 struct page **ppage;
755 unsigned int remainder;
Jason Baronf279cd02014-09-24 18:08:00 +0000756 int err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400757
Trond Myklebust24c56842006-10-17 15:06:22 -0400758 remainder = xdr->page_len - base;
759 base += xdr->page_base;
760 ppage = xdr->pages + (base >> PAGE_SHIFT);
761 base &= ~PAGE_MASK;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500762 do_sendpage = sock->ops->sendpage;
763 if (!zerocopy)
764 do_sendpage = sock_no_sendpage;
Trond Myklebust24c56842006-10-17 15:06:22 -0400765 for(;;) {
766 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
767 int flags = XS_SENDMSG_FLAGS;
768
769 remainder -= len;
Trond Myklebust31303d62015-10-06 15:59:20 -0400770 if (more)
Trond Myklebust24c56842006-10-17 15:06:22 -0400771 flags |= MSG_MORE;
Trond Myklebust31303d62015-10-06 15:59:20 -0400772 if (remainder != 0)
773 flags |= MSG_SENDPAGE_NOTLAST | MSG_MORE;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500774 err = do_sendpage(sock, *ppage, base, len, flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400775 if (remainder == 0 || err != len)
776 break;
Jason Baronf279cd02014-09-24 18:08:00 +0000777 *sent_p += err;
Trond Myklebust24c56842006-10-17 15:06:22 -0400778 ppage++;
779 base = 0;
780 }
Jason Baronf279cd02014-09-24 18:08:00 +0000781 if (err > 0) {
782 *sent_p += err;
783 err = 0;
784 }
785 return err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400786}
787
Chuck Lever9903cd12005-08-11 16:25:26 -0400788/**
789 * xs_sendpages - write pages directly to a socket
790 * @sock: socket to send on
791 * @addr: UDP only -- address of destination
792 * @addrlen: UDP only -- length of destination address
793 * @xdr: buffer containing this request
794 * @base: starting position in the buffer
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500795 * @zerocopy: true if it is safe to use sendpage()
Jason Baronf279cd02014-09-24 18:08:00 +0000796 * @sent_p: return the total number of bytes successfully queued for sending
Chuck Lever9903cd12005-08-11 16:25:26 -0400797 *
Chuck Levera246b012005-08-11 16:25:23 -0400798 */
Jason Baronf279cd02014-09-24 18:08:00 +0000799static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, bool zerocopy, int *sent_p)
Chuck Levera246b012005-08-11 16:25:23 -0400800{
Trond Myklebust24c56842006-10-17 15:06:22 -0400801 unsigned int remainder = xdr->len - base;
Jason Baronf279cd02014-09-24 18:08:00 +0000802 int err = 0;
803 int sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400804
Chuck Lever262965f2005-08-11 16:25:56 -0400805 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400806 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400807
Trond Myklebust24c56842006-10-17 15:06:22 -0400808 if (base != 0) {
809 addr = NULL;
810 addrlen = 0;
811 }
Chuck Lever262965f2005-08-11 16:25:56 -0400812
Trond Myklebust24c56842006-10-17 15:06:22 -0400813 if (base < xdr->head[0].iov_len || addr != NULL) {
814 unsigned int len = xdr->head[0].iov_len - base;
815 remainder -= len;
Chuck Lever067fb112019-02-11 11:24:37 -0500816 if (!base && !addr)
817 err = xs_send_rm_and_kvec(sock, xdr, remainder);
818 else
819 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0],
820 base, remainder != 0);
Trond Myklebust24c56842006-10-17 15:06:22 -0400821 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400822 goto out;
Jason Baronf279cd02014-09-24 18:08:00 +0000823 *sent_p += err;
Chuck Levera246b012005-08-11 16:25:23 -0400824 base = 0;
825 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400826 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400827
Trond Myklebust24c56842006-10-17 15:06:22 -0400828 if (base < xdr->page_len) {
829 unsigned int len = xdr->page_len - base;
830 remainder -= len;
Jason Baronf279cd02014-09-24 18:08:00 +0000831 err = xs_send_pagedata(sock, xdr, base, remainder != 0, zerocopy, &sent);
832 *sent_p += sent;
833 if (remainder == 0 || sent != len)
Chuck Levera246b012005-08-11 16:25:23 -0400834 goto out;
835 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400836 } else
837 base -= xdr->page_len;
838
839 if (base >= xdr->tail[0].iov_len)
Jason Baronf279cd02014-09-24 18:08:00 +0000840 return 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400841 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400842out:
Jason Baronf279cd02014-09-24 18:08:00 +0000843 if (err > 0) {
844 *sent_p += err;
845 err = 0;
846 }
847 return err;
Chuck Levera246b012005-08-11 16:25:23 -0400848}
849
Chuck Lever9903cd12005-08-11 16:25:26 -0400850/**
Trond Myklebustc5445772018-09-03 23:39:27 -0400851 * xs_nospace - handle transmit was incomplete
Trond Myklebust50f484e2018-08-30 13:27:29 -0400852 * @req: pointer to RPC request
Chuck Lever9903cd12005-08-11 16:25:26 -0400853 *
Chuck Levera246b012005-08-11 16:25:23 -0400854 */
Trond Myklebustc5445772018-09-03 23:39:27 -0400855static int xs_nospace(struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -0400856{
Chuck Lever262965f2005-08-11 16:25:56 -0400857 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500858 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500859 struct sock *sk = transport->inet;
Trond Myklebust24ca9a82011-11-22 14:44:28 +0200860 int ret = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400861
Chuck Lever46121cf2007-01-31 12:14:08 -0500862 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Trond Myklebustc5445772018-09-03 23:39:27 -0400863 req->rq_task->tk_pid,
864 req->rq_slen - transport->xmit.offset,
Chuck Lever262965f2005-08-11 16:25:56 -0400865 req->rq_slen);
866
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400867 /* Protect against races with write_space */
868 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400869
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400870 /* Don't race with disconnect */
871 if (xprt_connected(xprt)) {
Trond Myklebust13331a52016-01-06 08:57:06 -0500872 /* wait for more buffer space */
873 sk->sk_write_pending++;
Trond Myklebustc5445772018-09-03 23:39:27 -0400874 xprt_wait_for_buffer_space(xprt);
Trond Myklebust13331a52016-01-06 08:57:06 -0500875 } else
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400876 ret = -ENOTCONN;
Chuck Lever262965f2005-08-11 16:25:56 -0400877
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400878 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500879
880 /* Race breaker in case memory is freed before above code is called */
David Vrabeld48f9ce2016-09-19 13:58:30 +0100881 if (ret == -EAGAIN) {
882 struct socket_wq *wq;
883
884 rcu_read_lock();
885 wq = rcu_dereference(sk->sk_wq);
886 set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags);
887 rcu_read_unlock();
888
889 sk->sk_write_space(sk);
890 }
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400891 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400892}
893
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400894static void
895xs_stream_prepare_request(struct rpc_rqst *req)
896{
897 req->rq_task->tk_status = xdr_alloc_bvec(&req->rq_rcv_buf, GFP_NOIO);
898}
899
Chuck Lever61677ee2011-05-09 15:22:34 -0400900/*
Trond Myklebust4cd34e72018-08-31 10:00:02 -0400901 * Determine if the previous message in the stream was aborted before it
902 * could complete transmission.
903 */
904static bool
905xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
906{
907 return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
908}
909
Chuck Lever262965f2005-08-11 16:25:56 -0400910/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400911 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
Trond Myklebust50f484e2018-08-30 13:27:29 -0400912 * @req: pointer to RPC request
Chuck Lever176e21e2011-05-09 15:22:44 -0400913 *
914 * Return values:
915 * 0: The request has been sent
916 * EAGAIN: The socket was blocked, please call again later to
917 * complete the request
918 * ENOTCONN: Caller needs to invoke connect logic then call again
919 * other: Some other error occured, the request was not sent
920 */
Trond Myklebustadfa7142018-09-03 23:58:59 -0400921static int xs_local_send_request(struct rpc_rqst *req)
Chuck Lever176e21e2011-05-09 15:22:44 -0400922{
Chuck Lever176e21e2011-05-09 15:22:44 -0400923 struct rpc_xprt *xprt = req->rq_xprt;
924 struct sock_xprt *transport =
925 container_of(xprt, struct sock_xprt, xprt);
926 struct xdr_buf *xdr = &req->rq_snd_buf;
927 int status;
Jason Baronf279cd02014-09-24 18:08:00 +0000928 int sent = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -0400929
Trond Myklebust4cd34e72018-08-31 10:00:02 -0400930 /* Close the stream if the previous transmission was incomplete */
931 if (xs_send_request_was_aborted(transport, req)) {
932 xs_close(xprt);
933 return -ENOTCONN;
934 }
935
Chuck Lever176e21e2011-05-09 15:22:44 -0400936 xs_pktdump("packet data:",
937 req->rq_svec->iov_base, req->rq_svec->iov_len);
938
Chuck Lever78215752018-03-05 15:13:07 -0500939 req->rq_xtime = ktime_get();
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400940 status = xs_sendpages(transport->sock, NULL, 0, xdr,
941 transport->xmit.offset,
Jason Baronf279cd02014-09-24 18:08:00 +0000942 true, &sent);
Chuck Lever176e21e2011-05-09 15:22:44 -0400943 dprintk("RPC: %s(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400944 __func__, xdr->len - transport->xmit.offset, status);
NeilBrown743c69e2015-07-27 10:55:35 +1000945
946 if (status == -EAGAIN && sock_writeable(transport->inet))
947 status = -ENOBUFS;
948
Jason Baronf279cd02014-09-24 18:08:00 +0000949 if (likely(sent > 0) || status == 0) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400950 transport->xmit.offset += sent;
951 req->rq_bytes_sent = transport->xmit.offset;
Chuck Lever176e21e2011-05-09 15:22:44 -0400952 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400953 req->rq_xmit_bytes_sent += transport->xmit.offset;
Chuck Lever176e21e2011-05-09 15:22:44 -0400954 req->rq_bytes_sent = 0;
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400955 transport->xmit.offset = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -0400956 return 0;
957 }
958 status = -EAGAIN;
959 }
960
961 switch (status) {
Trond Myklebust3601c4a2014-06-30 13:42:19 -0400962 case -ENOBUFS:
Trond Myklebustb5872f02015-07-03 09:32:23 -0400963 break;
Chuck Lever176e21e2011-05-09 15:22:44 -0400964 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -0400965 status = xs_nospace(req);
Chuck Lever176e21e2011-05-09 15:22:44 -0400966 break;
967 default:
968 dprintk("RPC: sendmsg returned unrecognized error %d\n",
969 -status);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -0500970 /* fall through */
Chuck Lever176e21e2011-05-09 15:22:44 -0400971 case -EPIPE:
972 xs_close(xprt);
973 status = -ENOTCONN;
974 }
975
976 return status;
977}
978
979/**
Chuck Lever262965f2005-08-11 16:25:56 -0400980 * xs_udp_send_request - write an RPC request to a UDP socket
Trond Myklebust50f484e2018-08-30 13:27:29 -0400981 * @req: pointer to RPC request
Chuck Lever262965f2005-08-11 16:25:56 -0400982 *
983 * Return values:
984 * 0: The request has been sent
985 * EAGAIN: The socket was blocked, please call again later to
986 * complete the request
987 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300988 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -0400989 */
Trond Myklebustadfa7142018-09-03 23:58:59 -0400990static int xs_udp_send_request(struct rpc_rqst *req)
Chuck Lever262965f2005-08-11 16:25:56 -0400991{
Chuck Lever262965f2005-08-11 16:25:56 -0400992 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500993 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400994 struct xdr_buf *xdr = &req->rq_snd_buf;
Jason Baronf279cd02014-09-24 18:08:00 +0000995 int sent = 0;
Chuck Lever262965f2005-08-11 16:25:56 -0400996 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400997
Chuck Lever9903cd12005-08-11 16:25:26 -0400998 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400999 req->rq_svec->iov_base,
1000 req->rq_svec->iov_len);
1001
Trond Myklebust01d37c42009-03-11 14:09:39 -04001002 if (!xprt_bound(xprt))
1003 return -ENOTCONN;
Trond Myklebust75891f52018-09-03 17:37:36 -04001004
1005 if (!xprt_request_get_cong(xprt, req))
1006 return -EBADSLT;
1007
Chuck Lever78215752018-03-05 15:13:07 -05001008 req->rq_xtime = ktime_get();
Jason Baronf279cd02014-09-24 18:08:00 +00001009 status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen,
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001010 xdr, 0, true, &sent);
Chuck Levera246b012005-08-11 16:25:23 -04001011
Chuck Lever46121cf2007-01-31 12:14:08 -05001012 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001013 xdr->len, status);
Chuck Levera246b012005-08-11 16:25:23 -04001014
Jason Baron3dedbb52014-09-24 18:08:04 +00001015 /* firewall is blocking us, don't return -EAGAIN or we end up looping */
1016 if (status == -EPERM)
1017 goto process_status;
1018
NeilBrown743c69e2015-07-27 10:55:35 +10001019 if (status == -EAGAIN && sock_writeable(transport->inet))
1020 status = -ENOBUFS;
1021
Jason Baronf279cd02014-09-24 18:08:00 +00001022 if (sent > 0 || status == 0) {
1023 req->rq_xmit_bytes_sent += sent;
1024 if (sent >= req->rq_slen)
Trond Myklebust21997002007-10-01 11:43:37 -04001025 return 0;
1026 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -04001027 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -04001028 }
Chuck Levera246b012005-08-11 16:25:23 -04001029
Jason Baron3dedbb52014-09-24 18:08:04 +00001030process_status:
Chuck Lever262965f2005-08-11 16:25:56 -04001031 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -04001032 case -ENOTSOCK:
1033 status = -ENOTCONN;
1034 /* Should we call xs_close() here? */
1035 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -04001036 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -04001037 status = xs_nospace(req);
Trond Myklebustb6ddf642008-04-17 18:52:19 -04001038 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001039 case -ENETUNREACH:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001040 case -ENOBUFS:
Chuck Lever262965f2005-08-11 16:25:56 -04001041 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -04001042 case -ECONNREFUSED:
Jason Baron3dedbb52014-09-24 18:08:04 +00001043 case -EPERM:
Chuck Levera246b012005-08-11 16:25:23 -04001044 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -04001045 * prompts ECONNREFUSED. */
Trond Myklebust13331a52016-01-06 08:57:06 -05001046 break;
1047 default:
1048 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1049 -status);
Chuck Levera246b012005-08-11 16:25:23 -04001050 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +08001051
Chuck Lever262965f2005-08-11 16:25:56 -04001052 return status;
Chuck Levera246b012005-08-11 16:25:23 -04001053}
1054
Trond Myklebuste06799f2007-11-05 15:44:12 -05001055/**
Chuck Lever262965f2005-08-11 16:25:56 -04001056 * xs_tcp_send_request - write an RPC request to a TCP socket
Trond Myklebust50f484e2018-08-30 13:27:29 -04001057 * @req: pointer to RPC request
Chuck Lever9903cd12005-08-11 16:25:26 -04001058 *
1059 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -04001060 * 0: The request has been sent
1061 * EAGAIN: The socket was blocked, please call again later to
1062 * complete the request
1063 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001064 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -04001065 *
1066 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -04001067 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -04001068 */
Trond Myklebustadfa7142018-09-03 23:58:59 -04001069static int xs_tcp_send_request(struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001070{
Chuck Levera246b012005-08-11 16:25:23 -04001071 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001072 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -04001073 struct xdr_buf *xdr = &req->rq_snd_buf;
Trond Myklebusta6b31d12013-11-08 16:03:50 -05001074 bool zerocopy = true;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001075 bool vm_wait = false;
Chuck Leverb595bb12007-08-06 11:56:42 -04001076 int status;
Jason Baronf279cd02014-09-24 18:08:00 +00001077 int sent;
Chuck Levera246b012005-08-11 16:25:23 -04001078
Trond Myklebust4cd34e72018-08-31 10:00:02 -04001079 /* Close the stream if the previous transmission was incomplete */
1080 if (xs_send_request_was_aborted(transport, req)) {
1081 if (transport->sock != NULL)
1082 kernel_sock_shutdown(transport->sock, SHUT_RDWR);
1083 return -ENOTCONN;
1084 }
1085
Chuck Lever262965f2005-08-11 16:25:56 -04001086 xs_pktdump("packet data:",
1087 req->rq_svec->iov_base,
1088 req->rq_svec->iov_len);
Trond Myklebusta6b31d12013-11-08 16:03:50 -05001089 /* Don't use zero copy if this is a resend. If the RPC call
1090 * completes while the socket holds a reference to the pages,
1091 * then we may end up resending corrupted data.
1092 */
Trond Myklebust50f484e2018-08-30 13:27:29 -04001093 if (req->rq_task->tk_flags & RPC_TASK_SENT)
Trond Myklebusta6b31d12013-11-08 16:03:50 -05001094 zerocopy = false;
Chuck Levera246b012005-08-11 16:25:23 -04001095
Trond Myklebust7196dbb2017-02-08 11:17:54 -05001096 if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
1097 xs_tcp_set_socket_timeouts(xprt, transport->sock);
1098
Chuck Levera246b012005-08-11 16:25:23 -04001099 /* Continue transmitting the packet/record. We must be careful
1100 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -04001101 * called sendmsg(). */
Chuck Lever78215752018-03-05 15:13:07 -05001102 req->rq_xtime = ktime_get();
Chuck Levera246b012005-08-11 16:25:23 -04001103 while (1) {
Jason Baronf279cd02014-09-24 18:08:00 +00001104 sent = 0;
1105 status = xs_sendpages(transport->sock, NULL, 0, xdr,
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001106 transport->xmit.offset,
1107 zerocopy, &sent);
Chuck Levera246b012005-08-11 16:25:23 -04001108
Chuck Lever46121cf2007-01-31 12:14:08 -05001109 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001110 xdr->len - transport->xmit.offset, status);
Chuck Lever262965f2005-08-11 16:25:56 -04001111
Chuck Lever262965f2005-08-11 16:25:56 -04001112 /* If we've sent the entire packet, immediately
1113 * reset the count of bytes sent. */
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001114 transport->xmit.offset += sent;
1115 req->rq_bytes_sent = transport->xmit.offset;
Chuck Lever262965f2005-08-11 16:25:56 -04001116 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001117 req->rq_xmit_bytes_sent += transport->xmit.offset;
Chuck Lever262965f2005-08-11 16:25:56 -04001118 req->rq_bytes_sent = 0;
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001119 transport->xmit.offset = 0;
Chuck Lever262965f2005-08-11 16:25:56 -04001120 return 0;
Chuck Levera246b012005-08-11 16:25:23 -04001121 }
1122
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001123 WARN_ON_ONCE(sent == 0 && status == 0);
1124
1125 if (status == -EAGAIN ) {
1126 /*
1127 * Return EAGAIN if we're sure we're hitting the
1128 * socket send buffer limits.
1129 */
1130 if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
1131 break;
1132 /*
1133 * Did we hit a memory allocation failure?
1134 */
1135 if (sent == 0) {
1136 status = -ENOBUFS;
1137 if (vm_wait)
1138 break;
1139 /* Retry, knowing now that we're below the
1140 * socket send buffer limit
1141 */
1142 vm_wait = true;
1143 }
1144 continue;
1145 }
Trond Myklebustf580dd02015-07-11 17:48:52 +02001146 if (status < 0)
1147 break;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001148 vm_wait = false;
Chuck Levera246b012005-08-11 16:25:23 -04001149 }
1150
Chuck Lever262965f2005-08-11 16:25:56 -04001151 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -04001152 case -ENOTSOCK:
1153 status = -ENOTCONN;
1154 /* Should we call xs_close() here? */
1155 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001156 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -04001157 status = xs_nospace(req);
Chuck Lever262965f2005-08-11 16:25:56 -04001158 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001159 case -ECONNRESET:
Chuck Lever262965f2005-08-11 16:25:56 -04001160 case -ECONNREFUSED:
1161 case -ENOTCONN:
Trond Myklebust3913c782015-02-08 21:44:04 -05001162 case -EADDRINUSE:
Trond Myklebustb5872f02015-07-03 09:32:23 -04001163 case -ENOBUFS:
Trond Myklebustb9d2bb22012-10-23 11:40:02 -04001164 case -EPIPE:
Trond Myklebust13331a52016-01-06 08:57:06 -05001165 break;
1166 default:
1167 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1168 -status);
Chuck Levera246b012005-08-11 16:25:23 -04001169 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +08001170
Chuck Levera246b012005-08-11 16:25:23 -04001171 return status;
1172}
1173
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001174static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1175{
1176 transport->old_data_ready = sk->sk_data_ready;
1177 transport->old_state_change = sk->sk_state_change;
1178 transport->old_write_space = sk->sk_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -05001179 transport->old_error_report = sk->sk_error_report;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001180}
1181
1182static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1183{
1184 sk->sk_data_ready = transport->old_data_ready;
1185 sk->sk_state_change = transport->old_state_change;
1186 sk->sk_write_space = transport->old_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -05001187 sk->sk_error_report = transport->old_error_report;
1188}
1189
Trond Myklebust42d42a52016-05-23 09:24:55 -04001190static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
1191{
1192 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1193
1194 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
1195}
1196
Trond Myklebustb70ae912015-02-09 09:41:32 -05001197static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1198{
1199 smp_mb__before_atomic();
1200 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1201 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001202 xs_sock_reset_state_flags(xprt);
Trond Myklebustb70ae912015-02-09 09:41:32 -05001203 smp_mb__after_atomic();
1204}
1205
Trond Myklebust21180712013-12-31 13:22:59 -05001206/**
1207 * xs_error_report - callback to handle TCP socket state errors
1208 * @sk: socket
1209 *
1210 * Note: we don't call sock_error() since there may be a rpc_task
1211 * using the socket, and so we don't want to clear sk->sk_err.
1212 */
1213static void xs_error_report(struct sock *sk)
1214{
1215 struct rpc_xprt *xprt;
1216 int err;
1217
1218 read_lock_bh(&sk->sk_callback_lock);
1219 if (!(xprt = xprt_from_sock(sk)))
1220 goto out;
1221
1222 err = -sk->sk_err;
1223 if (err == 0)
1224 goto out;
1225 dprintk("RPC: xs_error_report client %p, error=%d...\n",
1226 xprt, -err);
Trond Myklebuste8353c762013-12-31 13:39:22 -05001227 trace_rpc_socket_error(xprt, sk->sk_socket, err);
Trond Myklebust21180712013-12-31 13:22:59 -05001228 xprt_wake_pending_tasks(xprt, err);
1229 out:
1230 read_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001231}
1232
Chuck Leverfe315e762009-03-11 14:10:21 -04001233static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001234{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001235 struct socket *sock = transport->sock;
1236 struct sock *sk = transport->inet;
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001237 struct rpc_xprt *xprt = &transport->xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001238
Chuck Leverfe315e762009-03-11 14:10:21 -04001239 if (sk == NULL)
1240 return;
Chuck Lever9903cd12005-08-11 16:25:26 -04001241
Jeff Layton264d1df2015-06-03 16:14:27 -04001242 if (atomic_read(&transport->xprt.swapper))
1243 sk_clear_memalloc(sk);
1244
Trond Myklebust09939202015-08-29 19:11:21 -07001245 kernel_sock_shutdown(sock, SHUT_RDWR);
1246
Trond Myklebustedc1b012015-10-05 10:53:49 -04001247 mutex_lock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -04001248 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001249 transport->inet = NULL;
1250 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -04001251
Chuck Lever9903cd12005-08-11 16:25:26 -04001252 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001253
1254 xs_restore_old_callbacks(transport, sk);
Trond Myklebust0c787892015-08-29 13:36:30 -07001255 xprt_clear_connected(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001256 write_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001257 xs_sock_reset_connection_flags(xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04001258 mutex_unlock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -04001259
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001260 trace_rpc_socket_close(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001261 sock_release(sock);
Trond Myklebust0445f922018-12-17 13:34:59 -05001262
1263 xprt_disconnect_done(xprt);
Chuck Leverfe315e762009-03-11 14:10:21 -04001264}
1265
1266/**
1267 * xs_close - close a socket
1268 * @xprt: transport
1269 *
1270 * This is used when all requests are complete; ie, no DRC state remains
1271 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -04001272 *
1273 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1274 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e762009-03-11 14:10:21 -04001275 */
1276static void xs_close(struct rpc_xprt *xprt)
1277{
1278 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1279
1280 dprintk("RPC: xs_close xprt %p\n", xprt);
1281
1282 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -04001283 xprt->reestablish_timeout = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001284}
1285
Chuck Lever4a068252015-05-11 14:02:25 -04001286static void xs_inject_disconnect(struct rpc_xprt *xprt)
1287{
1288 dprintk("RPC: injecting transport disconnect on xprt=%p\n",
1289 xprt);
1290 xprt_disconnect_done(xprt);
1291}
1292
Kinglong Mee315f3812014-03-24 11:07:22 +08001293static void xs_xprt_free(struct rpc_xprt *xprt)
1294{
1295 xs_free_peer_addresses(xprt);
1296 xprt_free(xprt);
1297}
1298
Chuck Lever9903cd12005-08-11 16:25:26 -04001299/**
1300 * xs_destroy - prepare to shutdown a transport
1301 * @xprt: doomed transport
1302 *
1303 */
1304static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001305{
Trond Myklebust03c788272015-09-17 10:42:27 -04001306 struct sock_xprt *transport = container_of(xprt,
1307 struct sock_xprt, xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05001308 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001309
Trond Myklebust03c788272015-09-17 10:42:27 -04001310 cancel_delayed_work_sync(&transport->connect_worker);
Trond Myklebusta1311d82013-10-31 09:18:49 -04001311 xs_close(xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04001312 cancel_work_sync(&transport->recv_worker);
Kinglong Mee315f3812014-03-24 11:07:22 +08001313 xs_xprt_free(xprt);
Trond Myklebusta1311d82013-10-31 09:18:49 -04001314 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -04001315}
1316
Chuck Lever9903cd12005-08-11 16:25:26 -04001317/**
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001318 * xs_udp_data_read_skb - receive callback for UDP sockets
1319 * @xprt: transport
1320 * @sk: socket
1321 * @skb: skbuff
Chuck Lever9903cd12005-08-11 16:25:26 -04001322 *
1323 */
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001324static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1325 struct sock *sk,
1326 struct sk_buff *skb)
Chuck Lever9903cd12005-08-11 16:25:26 -04001327{
1328 struct rpc_task *task;
Chuck Levera246b012005-08-11 16:25:23 -04001329 struct rpc_rqst *rovr;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001330 int repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001331 u32 _xid;
1332 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -04001333
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001334 repsize = skb->len;
Chuck Levera246b012005-08-11 16:25:23 -04001335 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001336 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001337 return;
Chuck Levera246b012005-08-11 16:25:23 -04001338 }
1339
1340 /* Copy the XID from the skb... */
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001341 xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
Chuck Levera246b012005-08-11 16:25:23 -04001342 if (xp == NULL)
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001343 return;
Chuck Levera246b012005-08-11 16:25:23 -04001344
1345 /* Look up and lock the request corresponding to the given XID */
Trond Myklebust75c84152018-08-31 10:21:00 -04001346 spin_lock(&xprt->queue_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001347 rovr = xprt_lookup_rqst(xprt, *xp);
1348 if (!rovr)
1349 goto out_unlock;
Trond Myklebust729749b2017-08-13 10:03:59 -04001350 xprt_pin_rqst(rovr);
Chuck Leverecd465e2018-03-05 15:12:57 -05001351 xprt_update_rtt(rovr->rq_task);
Trond Myklebust75c84152018-08-31 10:21:00 -04001352 spin_unlock(&xprt->queue_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001353 task = rovr->rq_task;
1354
Chuck Levera246b012005-08-11 16:25:23 -04001355 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1356 copied = repsize;
1357
1358 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001359 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
Trond Myklebust75c84152018-08-31 10:21:00 -04001360 spin_lock(&xprt->queue_lock);
Trond Myklebust0afa6b42018-02-09 09:39:42 -05001361 __UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
Trond Myklebust729749b2017-08-13 10:03:59 -04001362 goto out_unpin;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001363 }
1364
Chuck Levera246b012005-08-11 16:25:23 -04001365
Trond Myklebust729749b2017-08-13 10:03:59 -04001366 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001367 xprt_adjust_cwnd(xprt, task, copied);
Trond Myklebustce7c2522017-08-16 15:30:35 -04001368 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust75c84152018-08-31 10:21:00 -04001369 spin_lock(&xprt->queue_lock);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001370 xprt_complete_rqst(task, copied);
Trond Myklebust0afa6b42018-02-09 09:39:42 -05001371 __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
Trond Myklebust729749b2017-08-13 10:03:59 -04001372out_unpin:
1373 xprt_unpin_rqst(rovr);
Chuck Levera246b012005-08-11 16:25:23 -04001374 out_unlock:
Trond Myklebust75c84152018-08-31 10:21:00 -04001375 spin_unlock(&xprt->queue_lock);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001376}
1377
1378static void xs_udp_data_receive(struct sock_xprt *transport)
1379{
1380 struct sk_buff *skb;
1381 struct sock *sk;
1382 int err;
1383
1384 mutex_lock(&transport->recv_mutex);
Trond Myklebustdfcf0382018-12-04 07:50:06 -05001385 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001386 sk = transport->inet;
1387 if (sk == NULL)
1388 goto out;
1389 for (;;) {
Paolo Abeni7c13f972016-11-04 11:28:59 +01001390 skb = skb_recv_udp(sk, 0, 1, &err);
Trond Myklebust4f546142018-09-14 17:45:23 -04001391 if (skb == NULL)
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001392 break;
Trond Myklebust4f546142018-09-14 17:45:23 -04001393 xs_udp_data_read_skb(&transport->xprt, sk, skb);
1394 consume_skb(skb);
1395 cond_resched();
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001396 }
1397out:
1398 mutex_unlock(&transport->recv_mutex);
1399}
1400
1401static void xs_udp_data_receive_workfn(struct work_struct *work)
1402{
1403 struct sock_xprt *transport =
1404 container_of(work, struct sock_xprt, recv_worker);
1405 xs_udp_data_receive(transport);
1406}
1407
1408/**
1409 * xs_data_ready - "data ready" callback for UDP sockets
1410 * @sk: socket with data to read
1411 *
1412 */
1413static void xs_data_ready(struct sock *sk)
1414{
1415 struct rpc_xprt *xprt;
1416
1417 read_lock_bh(&sk->sk_callback_lock);
1418 dprintk("RPC: xs_data_ready...\n");
1419 xprt = xprt_from_sock(sk);
1420 if (xprt != NULL) {
1421 struct sock_xprt *transport = container_of(xprt,
1422 struct sock_xprt, xprt);
Trond Myklebust5157b952016-05-29 10:13:24 -04001423 transport->old_data_ready(sk);
1424 /* Any data means we had a useful conversation, so
1425 * then we don't need to delay the next reconnect
1426 */
1427 if (xprt->reestablish_timeout)
1428 xprt->reestablish_timeout = 0;
Trond Myklebust42d42a52016-05-23 09:24:55 -04001429 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebust40a5f1b2016-05-27 10:39:50 -04001430 queue_work(xprtiod_workqueue, &transport->recv_worker);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001431 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001432 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001433}
1434
Trond Myklebusta519fc72012-09-12 16:49:15 -04001435/*
1436 * Helper function to force a TCP close if the server is sending
1437 * junk and/or it has put us in CLOSE_WAIT
1438 */
1439static void xs_tcp_force_close(struct rpc_xprt *xprt)
1440{
Trond Myklebusta519fc72012-09-12 16:49:15 -04001441 xprt_force_disconnect(xprt);
1442}
1443
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001444#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Chuck Lever6b26cc82016-05-02 14:40:40 -04001445static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
1446{
1447 return PAGE_SIZE;
1448}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001449#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001450
Chuck Lever9903cd12005-08-11 16:25:26 -04001451/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001452 * xs_tcp_state_change - callback to handle TCP socket state changes
1453 * @sk: socket whose state has changed
1454 *
1455 */
1456static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001457{
Chuck Lever9903cd12005-08-11 16:25:26 -04001458 struct rpc_xprt *xprt;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001459 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001460
Eric Dumazetf064af12010-09-22 12:43:39 +00001461 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001462 if (!(xprt = xprt_from_sock(sk)))
1463 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001464 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001465 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001466 sk->sk_state, xprt_connected(xprt),
1467 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001468 sock_flag(sk, SOCK_ZAPPED),
1469 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001470
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001471 transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001472 trace_rpc_socket_state_change(xprt, sk->sk_socket);
Chuck Levera246b012005-08-11 16:25:23 -04001473 switch (sk->sk_state) {
1474 case TCP_ESTABLISHED:
Eric Dumazetf064af12010-09-22 12:43:39 +00001475 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001476 if (!xprt_test_and_set_connected(xprt)) {
Trond Myklebust8b717982013-09-26 10:18:04 -04001477 xprt->connect_cookie++;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001478 clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
1479 xprt_clear_connecting(xprt);
Chuck Lever51971132006-12-05 16:35:19 -05001480
Chuck Lever3968a8a2018-10-01 14:25:36 -04001481 xprt->stat.connect_count++;
1482 xprt->stat.connect_time += (long)jiffies -
1483 xprt->stat.connect_start;
Trond Myklebust2a491992009-03-11 14:38:00 -04001484 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001485 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001486 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001487 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001488 case TCP_FIN_WAIT1:
1489 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001490 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001491 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001492 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001493 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001494 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001495 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001496 smp_mb__after_atomic();
Chuck Levera246b012005-08-11 16:25:23 -04001497 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001498 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001499 /* The server initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001500 xprt->connect_cookie++;
Trond Myklebustd0bea452012-10-23 11:35:47 -04001501 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebusta519fc72012-09-12 16:49:15 -04001502 xs_tcp_force_close(xprt);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001503 /* fall through */
Trond Myklebust663b8852008-01-01 18:42:12 -05001504 case TCP_CLOSING:
1505 /*
1506 * If the server closed down the connection, make sure that
1507 * we back off before reconnecting
1508 */
1509 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1510 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001511 break;
1512 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001513 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001514 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001515 clear_bit(XPRT_CONNECTED, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001516 smp_mb__after_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001517 break;
1518 case TCP_CLOSE:
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001519 if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
1520 &transport->sock_state))
1521 xprt_clear_connecting(xprt);
Trond Myklebust9b308892018-02-05 10:20:06 -05001522 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust9b308892018-02-05 10:20:06 -05001523 /* Trigger the socket release */
1524 xs_tcp_force_close(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001525 }
1526 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001527 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001528}
1529
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001530static void xs_write_space(struct sock *sk)
1531{
Trond Myklebust13331a52016-01-06 08:57:06 -05001532 struct socket_wq *wq;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001533 struct rpc_xprt *xprt;
1534
Trond Myklebust13331a52016-01-06 08:57:06 -05001535 if (!sk->sk_socket)
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001536 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001537 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001538
1539 if (unlikely(!(xprt = xprt_from_sock(sk))))
1540 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001541 rcu_read_lock();
1542 wq = rcu_dereference(sk->sk_wq);
1543 if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0)
1544 goto out;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001545
Trond Myklebustc5445772018-09-03 23:39:27 -04001546 if (xprt_write_space(xprt))
1547 sk->sk_write_pending--;
Trond Myklebust13331a52016-01-06 08:57:06 -05001548out:
1549 rcu_read_unlock();
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001550}
1551
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001552/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001553 * xs_udp_write_space - callback invoked when socket buffer space
1554 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001555 * @sk: socket whose state has changed
1556 *
Chuck Levera246b012005-08-11 16:25:23 -04001557 * Called when more output buffer space is available for this socket.
1558 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001559 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001560 * with a bunch of small requests.
1561 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001562static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001563{
Eric Dumazetf064af12010-09-22 12:43:39 +00001564 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001565
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001566 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001567 if (sock_writeable(sk))
1568 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001569
Eric Dumazetf064af12010-09-22 12:43:39 +00001570 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001571}
Chuck Levera246b012005-08-11 16:25:23 -04001572
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001573/**
1574 * xs_tcp_write_space - callback invoked when socket buffer space
1575 * becomes available
1576 * @sk: socket whose state has changed
1577 *
1578 * Called when more output buffer space is available for this socket.
1579 * We try not to wake our writers until they can make "significant"
1580 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1581 * with a bunch of small requests.
1582 */
1583static void xs_tcp_write_space(struct sock *sk)
1584{
Eric Dumazetf064af12010-09-22 12:43:39 +00001585 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001586
1587 /* from net/core/stream.c:sk_stream_write_space */
Eric Dumazet64dc6132013-07-22 20:26:31 -07001588 if (sk_stream_is_writeable(sk))
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001589 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001590
Eric Dumazetf064af12010-09-22 12:43:39 +00001591 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001592}
1593
Chuck Lever470056c2005-08-25 16:25:56 -07001594static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001595{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001596 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1597 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001598
Chuck Lever7c6e0662006-12-05 16:35:30 -05001599 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001600 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001601 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001602 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001603 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001604 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001605 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001606 sk->sk_write_space(sk);
1607 }
1608}
1609
Chuck Lever43118c22005-08-25 16:25:49 -07001610/**
Chuck Lever470056c2005-08-25 16:25:56 -07001611 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001612 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001613 * @sndsize: requested size of send buffer, in bytes
1614 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001615 *
Chuck Lever470056c2005-08-25 16:25:56 -07001616 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001617 */
Chuck Lever470056c2005-08-25 16:25:56 -07001618static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001619{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001620 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1621
1622 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001623 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001624 transport->sndsize = sndsize + 1024;
1625 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001626 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001627 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001628
1629 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001630}
1631
Chuck Lever46c0ee82005-08-25 16:25:52 -07001632/**
1633 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
Chuck Leveracf0a392018-12-19 11:00:22 -05001634 * @xprt: controlling transport
Chuck Lever46c0ee82005-08-25 16:25:52 -07001635 * @task: task that timed out
1636 *
1637 * Adjust the congestion window after a retransmit timeout has occurred.
1638 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001639static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever46c0ee82005-08-25 16:25:52 -07001640{
Chuck Leverb977b642017-02-08 17:00:51 -05001641 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001642 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
Chuck Leverb977b642017-02-08 17:00:51 -05001643 spin_unlock_bh(&xprt->transport_lock);
Chuck Lever46c0ee82005-08-25 16:25:52 -07001644}
1645
J. Bruce Fields826799e2018-10-18 15:27:02 -04001646static int xs_get_random_port(void)
Chuck Leverb85d8802006-05-25 01:40:49 -04001647{
J. Bruce Fields826799e2018-10-18 15:27:02 -04001648 unsigned short min = xprt_min_resvport, max = xprt_max_resvport;
1649 unsigned short range;
1650 unsigned short rand;
1651
1652 if (max < min)
1653 return -EADDRINUSE;
1654 range = max - min + 1;
1655 rand = (unsigned short) prandom_u32() % range;
1656 return rand + min;
Chuck Leverb85d8802006-05-25 01:40:49 -04001657}
1658
Chuck Lever92200412006-01-03 09:55:51 +01001659/**
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001660 * xs_set_reuseaddr_port - set the socket's port and address reuse options
1661 * @sock: socket
1662 *
1663 * Note that this function has to be called on all sockets that share the
1664 * same port, and it must be called before binding.
1665 */
1666static void xs_sock_set_reuseport(struct socket *sock)
1667{
Trond Myklebust402e23b2015-02-09 17:20:14 -05001668 int opt = 1;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001669
Trond Myklebust402e23b2015-02-09 17:20:14 -05001670 kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
1671 (char *)&opt, sizeof(opt));
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001672}
1673
1674static unsigned short xs_sock_getport(struct socket *sock)
1675{
1676 struct sockaddr_storage buf;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001677 unsigned short port = 0;
1678
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001679 if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001680 goto out;
1681 switch (buf.ss_family) {
1682 case AF_INET6:
1683 port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
1684 break;
1685 case AF_INET:
1686 port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
1687 }
1688out:
1689 return port;
1690}
1691
1692/**
Chuck Lever92200412006-01-03 09:55:51 +01001693 * xs_set_port - reset the port number in the remote endpoint address
1694 * @xprt: generic transport
1695 * @port: new port number
1696 *
1697 */
1698static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1699{
Chuck Lever46121cf2007-01-31 12:14:08 -05001700 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001701
Chuck Lever9dc3b092009-08-09 15:09:46 -04001702 rpc_set_port(xs_addr(xprt), port);
1703 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001704}
1705
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001706static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
1707{
1708 if (transport->srcport == 0)
1709 transport->srcport = xs_sock_getport(sock);
1710}
1711
J. Bruce Fields826799e2018-10-18 15:27:02 -04001712static int xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001713{
J. Bruce Fields826799e2018-10-18 15:27:02 -04001714 int port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001715
1716 if (port == 0 && transport->xprt.resvport)
1717 port = xs_get_random_port();
1718 return port;
1719}
1720
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001721static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001722{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001723 if (transport->srcport != 0)
1724 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001725 if (!transport->xprt.resvport)
1726 return 0;
1727 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1728 return xprt_max_resvport;
1729 return --port;
1730}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001731static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001732{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001733 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001734 int err, nloop = 0;
J. Bruce Fields826799e2018-10-18 15:27:02 -04001735 int port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001736 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001737
Chris Perl0f7a6222014-09-05 15:40:21 -04001738 /*
1739 * If we are asking for any ephemeral port (i.e. port == 0 &&
1740 * transport->xprt.resvport == 0), don't bind. Let the local
1741 * port selection happen implicitly when the socket is used
1742 * (for example at connect time).
1743 *
1744 * This ensures that we can continue to establish TCP
1745 * connections even when all local ephemeral ports are already
1746 * a part of some TCP connection. This makes no difference
1747 * for UDP sockets, but also doens't harm them.
1748 *
1749 * If we're asking for any reserved port (i.e. port == 0 &&
1750 * transport->xprt.resvport == 1) xs_get_srcport above will
1751 * ensure that port is non-zero and we will bind as needed.
1752 */
J. Bruce Fields826799e2018-10-18 15:27:02 -04001753 if (port <= 0)
1754 return port;
Chris Perl0f7a6222014-09-05 15:40:21 -04001755
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001756 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001757 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001758 rpc_set_port((struct sockaddr *)&myaddr, port);
1759 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1760 transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001761 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001762 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001763 break;
Chuck Levera246b012005-08-11 16:25:23 -04001764 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001765 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001766 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001767 if (port > last)
1768 nloop++;
1769 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001770
Chuck Lever4232e862010-10-20 11:52:51 -04001771 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001772 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1773 &((struct sockaddr_in *)&myaddr)->sin_addr,
1774 port, err ? "failed" : "ok", err);
1775 else
1776 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1777 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1778 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001779 return err;
1780}
1781
Chuck Lever176e21e2011-05-09 15:22:44 -04001782/*
1783 * We don't support autobind on AF_LOCAL sockets
1784 */
1785static void xs_local_rpcbind(struct rpc_task *task)
1786{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001787 xprt_set_bound(task->tk_xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04001788}
1789
1790static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1791{
1792}
Chuck Levera246b012005-08-11 16:25:23 -04001793
Peter Zijlstraed075362006-12-06 20:35:24 -08001794#ifdef CONFIG_DEBUG_LOCK_ALLOC
1795static struct lock_class_key xs_key[2];
1796static struct lock_class_key xs_slock_key[2];
1797
Chuck Lever176e21e2011-05-09 15:22:44 -04001798static inline void xs_reclassify_socketu(struct socket *sock)
1799{
1800 struct sock *sk = sock->sk;
1801
Chuck Lever176e21e2011-05-09 15:22:44 -04001802 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1803 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1804}
1805
Chuck Lever8945ee52007-08-06 11:58:04 -04001806static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001807{
1808 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001809
Chuck Lever8945ee52007-08-06 11:58:04 -04001810 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1811 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1812}
Peter Zijlstraed075362006-12-06 20:35:24 -08001813
Chuck Lever8945ee52007-08-06 11:58:04 -04001814static inline void xs_reclassify_socket6(struct socket *sock)
1815{
1816 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001817
Chuck Lever8945ee52007-08-06 11:58:04 -04001818 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1819 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001820}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001821
1822static inline void xs_reclassify_socket(int family, struct socket *sock)
1823{
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02001824 if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -04001825 return;
1826
Chuck Lever4232e862010-10-20 11:52:51 -04001827 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001828 case AF_LOCAL:
1829 xs_reclassify_socketu(sock);
1830 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001831 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001832 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001833 break;
1834 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001835 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001836 break;
1837 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001838}
Peter Zijlstraed075362006-12-06 20:35:24 -08001839#else
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001840static inline void xs_reclassify_socket(int family, struct socket *sock)
1841{
1842}
Peter Zijlstraed075362006-12-06 20:35:24 -08001843#endif
1844
NeilBrown93dc41b2013-10-31 16:14:36 +11001845static void xs_dummy_setup_socket(struct work_struct *work)
1846{
1847}
1848
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001849static struct socket *xs_create_sock(struct rpc_xprt *xprt,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001850 struct sock_xprt *transport, int family, int type,
1851 int protocol, bool reuseport)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001852{
1853 struct socket *sock;
1854 int err;
1855
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001856 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001857 if (err < 0) {
1858 dprintk("RPC: can't create %d transport socket (%d).\n",
1859 protocol, -err);
1860 goto out;
1861 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001862 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001863
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001864 if (reuseport)
1865 xs_sock_set_reuseport(sock);
1866
Ben Hutchings4cea2882011-02-22 21:54:34 +00001867 err = xs_bind(transport, sock);
1868 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001869 sock_release(sock);
1870 goto out;
1871 }
1872
1873 return sock;
1874out:
1875 return ERR_PTR(err);
1876}
1877
Chuck Lever176e21e2011-05-09 15:22:44 -04001878static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1879 struct socket *sock)
1880{
1881 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1882 xprt);
1883
1884 if (!transport->inet) {
1885 struct sock *sk = sock->sk;
1886
1887 write_lock_bh(&sk->sk_callback_lock);
1888
1889 xs_save_old_callbacks(transport, sk);
1890
1891 sk->sk_user_data = xprt;
Trond Myklebusta2648092015-10-06 17:03:00 -04001892 sk->sk_data_ready = xs_data_ready;
Chuck Lever176e21e2011-05-09 15:22:44 -04001893 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07001894 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05001895 sk->sk_error_report = xs_error_report;
Trond Myklebustc2126152015-08-19 21:46:15 -05001896 sk->sk_allocation = GFP_NOIO;
Chuck Lever176e21e2011-05-09 15:22:44 -04001897
1898 xprt_clear_connected(xprt);
1899
1900 /* Reset to new socket */
1901 transport->sock = sock;
1902 transport->inet = sk;
1903
1904 write_unlock_bh(&sk->sk_callback_lock);
1905 }
1906
Trond Myklebust550aebf2018-09-14 14:32:45 -04001907 xs_stream_reset_connect(transport);
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001908
Chuck Lever176e21e2011-05-09 15:22:44 -04001909 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1910}
1911
1912/**
1913 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
Chuck Lever176e21e2011-05-09 15:22:44 -04001914 * @transport: socket transport to connect
Chuck Lever176e21e2011-05-09 15:22:44 -04001915 */
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001916static int xs_local_setup_socket(struct sock_xprt *transport)
Chuck Lever176e21e2011-05-09 15:22:44 -04001917{
Chuck Lever176e21e2011-05-09 15:22:44 -04001918 struct rpc_xprt *xprt = &transport->xprt;
1919 struct socket *sock;
1920 int status = -EIO;
1921
Chuck Lever176e21e2011-05-09 15:22:44 -04001922 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1923 SOCK_STREAM, 0, &sock, 1);
1924 if (status < 0) {
1925 dprintk("RPC: can't create AF_LOCAL "
1926 "transport socket (%d).\n", -status);
1927 goto out;
1928 }
Stefan Hajnoczid1358912015-12-02 14:17:52 +08001929 xs_reclassify_socket(AF_LOCAL, sock);
Chuck Lever176e21e2011-05-09 15:22:44 -04001930
1931 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1932 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1933
1934 status = xs_local_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001935 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever176e21e2011-05-09 15:22:44 -04001936 switch (status) {
1937 case 0:
1938 dprintk("RPC: xprt %p connected to %s\n",
1939 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
Chuck Lever3968a8a2018-10-01 14:25:36 -04001940 xprt->stat.connect_count++;
1941 xprt->stat.connect_time += (long)jiffies -
1942 xprt->stat.connect_start;
Chuck Lever176e21e2011-05-09 15:22:44 -04001943 xprt_set_connected(xprt);
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001944 case -ENOBUFS:
Chuck Lever176e21e2011-05-09 15:22:44 -04001945 break;
1946 case -ENOENT:
1947 dprintk("RPC: xprt %p: socket %s does not exist\n",
1948 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1949 break;
Trond Myklebust4a20a982012-12-15 17:02:29 -05001950 case -ECONNREFUSED:
1951 dprintk("RPC: xprt %p: connection refused for %s\n",
1952 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1953 break;
Chuck Lever176e21e2011-05-09 15:22:44 -04001954 default:
1955 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1956 __func__, -status,
1957 xprt->address_strings[RPC_DISPLAY_ADDR]);
1958 }
1959
1960out:
1961 xprt_clear_connecting(xprt);
1962 xprt_wake_pending_tasks(xprt, status);
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001963 return status;
1964}
1965
Linus Torvaldsb6669732013-02-28 18:02:55 -08001966static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001967{
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001968 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1969 int ret;
1970
1971 if (RPC_IS_ASYNC(task)) {
1972 /*
1973 * We want the AF_LOCAL connect to be resolved in the
1974 * filesystem namespace of the process making the rpc
1975 * call. Thus we connect synchronously.
1976 *
1977 * If we want to support asynchronous AF_LOCAL calls,
1978 * we'll need to figure out how to pass a namespace to
1979 * connect.
1980 */
1981 rpc_exit(task, -ENOTCONN);
1982 return;
1983 }
1984 ret = xs_local_setup_socket(transport);
1985 if (ret && !RPC_IS_SOFTCONN(task))
1986 msleep_interruptible(15000);
Chuck Lever176e21e2011-05-09 15:22:44 -04001987}
1988
Jeff Layton3c87ef62015-06-03 16:14:25 -04001989#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Jeff Laytond6e971d2015-06-03 16:14:28 -04001990/*
1991 * Note that this should be called with XPRT_LOCKED held (or when we otherwise
1992 * know that we have exclusive access to the socket), to guard against
1993 * races with xs_reset_transport.
1994 */
Mel Gormana564b8f2012-07-31 16:45:12 -07001995static void xs_set_memalloc(struct rpc_xprt *xprt)
1996{
1997 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1998 xprt);
1999
Jeff Laytond6e971d2015-06-03 16:14:28 -04002000 /*
2001 * If there's no sock, then we have nothing to set. The
2002 * reconnecting process will get it for us.
2003 */
2004 if (!transport->inet)
2005 return;
Jeff Layton8e228132015-06-03 16:14:26 -04002006 if (atomic_read(&xprt->swapper))
Mel Gormana564b8f2012-07-31 16:45:12 -07002007 sk_set_memalloc(transport->inet);
2008}
2009
2010/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002011 * xs_enable_swap - Tag this transport as being used for swap.
Mel Gormana564b8f2012-07-31 16:45:12 -07002012 * @xprt: transport to tag
Mel Gormana564b8f2012-07-31 16:45:12 -07002013 *
Jeff Layton8e228132015-06-03 16:14:26 -04002014 * Take a reference to this transport on behalf of the rpc_clnt, and
2015 * optionally mark it for swapping if it wasn't already.
Mel Gormana564b8f2012-07-31 16:45:12 -07002016 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002017static int
2018xs_enable_swap(struct rpc_xprt *xprt)
Mel Gormana564b8f2012-07-31 16:45:12 -07002019{
Jeff Laytond6e971d2015-06-03 16:14:28 -04002020 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Mel Gormana564b8f2012-07-31 16:45:12 -07002021
Jeff Laytond6e971d2015-06-03 16:14:28 -04002022 if (atomic_inc_return(&xprt->swapper) != 1)
2023 return 0;
2024 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2025 return -ERESTARTSYS;
2026 if (xs->inet)
2027 sk_set_memalloc(xs->inet);
2028 xprt_release_xprt(xprt, NULL);
Jeff Layton8e228132015-06-03 16:14:26 -04002029 return 0;
Mel Gormana564b8f2012-07-31 16:45:12 -07002030}
Jeff Layton8e228132015-06-03 16:14:26 -04002031
2032/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002033 * xs_disable_swap - Untag this transport as being used for swap.
Jeff Layton8e228132015-06-03 16:14:26 -04002034 * @xprt: transport to tag
2035 *
2036 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
2037 * swapper refcount goes to 0, untag the socket as a memalloc socket.
2038 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002039static void
2040xs_disable_swap(struct rpc_xprt *xprt)
Jeff Layton8e228132015-06-03 16:14:26 -04002041{
Jeff Laytond6e971d2015-06-03 16:14:28 -04002042 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Jeff Layton8e228132015-06-03 16:14:26 -04002043
Jeff Laytond6e971d2015-06-03 16:14:28 -04002044 if (!atomic_dec_and_test(&xprt->swapper))
2045 return;
2046 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2047 return;
2048 if (xs->inet)
2049 sk_clear_memalloc(xs->inet);
2050 xprt_release_xprt(xprt, NULL);
Mel Gormana564b8f2012-07-31 16:45:12 -07002051}
Mel Gormana564b8f2012-07-31 16:45:12 -07002052#else
2053static void xs_set_memalloc(struct rpc_xprt *xprt)
2054{
2055}
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002056
2057static int
2058xs_enable_swap(struct rpc_xprt *xprt)
2059{
2060 return -EINVAL;
2061}
2062
2063static void
2064xs_disable_swap(struct rpc_xprt *xprt)
2065{
2066}
Mel Gormana564b8f2012-07-31 16:45:12 -07002067#endif
2068
Chuck Lever16be2d22007-08-06 11:57:38 -04002069static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04002070{
Chuck Lever16be2d22007-08-06 11:57:38 -04002071 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04002072
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002073 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002074 struct sock *sk = sock->sk;
2075
2076 write_lock_bh(&sk->sk_callback_lock);
2077
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002078 xs_save_old_callbacks(transport, sk);
2079
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002080 sk->sk_user_data = xprt;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002081 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002082 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002083 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebustc2126152015-08-19 21:46:15 -05002084 sk->sk_allocation = GFP_NOIO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002085
2086 xprt_set_connected(xprt);
2087
2088 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002089 transport->sock = sock;
2090 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002091
Mel Gormana564b8f2012-07-31 16:45:12 -07002092 xs_set_memalloc(xprt);
2093
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002094 write_unlock_bh(&sk->sk_callback_lock);
2095 }
Chuck Lever470056c2005-08-25 16:25:56 -07002096 xs_udp_do_set_buffer_size(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002097
2098 xprt->stat.connect_start = jiffies;
Chuck Lever16be2d22007-08-06 11:57:38 -04002099}
2100
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002101static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04002102{
2103 struct sock_xprt *transport =
2104 container_of(work, struct sock_xprt, connect_worker.work);
2105 struct rpc_xprt *xprt = &transport->xprt;
Colin Ian Kingd099b8a2017-09-18 12:21:14 +01002106 struct socket *sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002107 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04002108
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002109 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002110 xs_addr(xprt)->sa_family, SOCK_DGRAM,
2111 IPPROTO_UDP, false);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002112 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04002113 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04002114
Chuck Leverc740eff2009-08-09 15:09:46 -04002115 dprintk("RPC: worker connecting xprt %p via %s to "
2116 "%s (port %s)\n", xprt,
2117 xprt->address_strings[RPC_DISPLAY_PROTO],
2118 xprt->address_strings[RPC_DISPLAY_ADDR],
2119 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04002120
2121 xs_udp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002122 trace_rpc_socket_connect(xprt, sock, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002123 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002124out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002125 xprt_clear_connecting(xprt);
Trond Myklebustcf767852018-12-17 17:38:51 -05002126 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002127 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002128}
2129
Trond Myklebust4876cc72015-06-19 16:17:57 -04002130/**
2131 * xs_tcp_shutdown - gracefully shut down a TCP socket
2132 * @xprt: transport
2133 *
2134 * Initiates a graceful shutdown of the TCP socket by calling the
2135 * equivalent of shutdown(SHUT_RDWR);
2136 */
2137static void xs_tcp_shutdown(struct rpc_xprt *xprt)
2138{
2139 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2140 struct socket *sock = transport->sock;
Trond Myklebust9b308892018-02-05 10:20:06 -05002141 int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
Trond Myklebust4876cc72015-06-19 16:17:57 -04002142
2143 if (sock == NULL)
2144 return;
Trond Myklebust9b308892018-02-05 10:20:06 -05002145 switch (skst) {
2146 default:
Trond Myklebust4876cc72015-06-19 16:17:57 -04002147 kernel_sock_shutdown(sock, SHUT_RDWR);
2148 trace_rpc_socket_shutdown(xprt, sock);
Trond Myklebust9b308892018-02-05 10:20:06 -05002149 break;
2150 case TCP_CLOSE:
2151 case TCP_TIME_WAIT:
Trond Myklebust4876cc72015-06-19 16:17:57 -04002152 xs_reset_transport(transport);
Trond Myklebust9b308892018-02-05 10:20:06 -05002153 }
Trond Myklebust4876cc72015-06-19 16:17:57 -04002154}
2155
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002156static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
2157 struct socket *sock)
2158{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002159 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2160 unsigned int keepidle;
2161 unsigned int keepcnt;
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002162 unsigned int opt_on = 1;
2163 unsigned int timeo;
2164
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002165 spin_lock_bh(&xprt->transport_lock);
2166 keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
2167 keepcnt = xprt->timeout->to_retries + 1;
2168 timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
2169 (xprt->timeout->to_retries + 1);
2170 clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2171 spin_unlock_bh(&xprt->transport_lock);
2172
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002173 /* TCP Keepalive options */
2174 kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
2175 (char *)&opt_on, sizeof(opt_on));
2176 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
2177 (char *)&keepidle, sizeof(keepidle));
2178 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
2179 (char *)&keepidle, sizeof(keepidle));
2180 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
2181 (char *)&keepcnt, sizeof(keepcnt));
2182
2183 /* TCP user timeout (see RFC5482) */
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002184 kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT,
2185 (char *)&timeo, sizeof(timeo));
2186}
2187
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002188static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
2189 unsigned long connect_timeout,
2190 unsigned long reconnect_timeout)
2191{
2192 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2193 struct rpc_timeout to;
2194 unsigned long initval;
2195
2196 spin_lock_bh(&xprt->transport_lock);
2197 if (reconnect_timeout < xprt->max_reconnect_timeout)
2198 xprt->max_reconnect_timeout = reconnect_timeout;
2199 if (connect_timeout < xprt->connect_timeout) {
2200 memcpy(&to, xprt->timeout, sizeof(to));
2201 initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
2202 /* Arbitrary lower limit */
2203 if (initval < XS_TCP_INIT_REEST_TO << 1)
2204 initval = XS_TCP_INIT_REEST_TO << 1;
2205 to.to_initval = initval;
2206 to.to_maxval = initval;
2207 memcpy(&transport->tcp_timeout, &to,
2208 sizeof(transport->tcp_timeout));
2209 xprt->timeout = &transport->tcp_timeout;
2210 xprt->connect_timeout = connect_timeout;
2211 }
2212 set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2213 spin_unlock_bh(&xprt->transport_lock);
2214}
2215
Chuck Lever16be2d22007-08-06 11:57:38 -04002216static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002217{
Chuck Lever16be2d22007-08-06 11:57:38 -04002218 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002219 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002220
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002221 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002222 struct sock *sk = sock->sk;
NeilBrownd88e4d82016-08-04 16:24:28 +10002223 unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC;
Trond Myklebust7f260e82013-09-24 11:25:22 -04002224
NeilBrownd88e4d82016-08-04 16:24:28 +10002225 /* Avoid temporary address, they are bad for long-lived
2226 * connections such as NFS mounts.
2227 * RFC4941, section 3.6 suggests that:
2228 * Individual applications, which have specific
2229 * knowledge about the normal duration of connections,
2230 * MAY override this as appropriate.
2231 */
2232 kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES,
2233 (char *)&addr_pref, sizeof(addr_pref));
2234
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002235 xs_tcp_set_socket_timeouts(xprt, sock);
Trond Myklebust775f06a2015-06-20 15:31:54 -04002236
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002237 write_lock_bh(&sk->sk_callback_lock);
2238
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002239 xs_save_old_callbacks(transport, sk);
2240
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002241 sk->sk_user_data = xprt;
Trond Myklebust5157b952016-05-29 10:13:24 -04002242 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002243 sk->sk_state_change = xs_tcp_state_change;
2244 sk->sk_write_space = xs_tcp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002245 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05002246 sk->sk_error_report = xs_error_report;
Trond Myklebustc2126152015-08-19 21:46:15 -05002247 sk->sk_allocation = GFP_NOIO;
Chuck Lever3167e122005-08-25 16:25:55 -07002248
2249 /* socket options */
Chuck Lever3167e122005-08-25 16:25:55 -07002250 sock_reset_flag(sk, SOCK_LINGER);
Chuck Lever3167e122005-08-25 16:25:55 -07002251 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002252
2253 xprt_clear_connected(xprt);
2254
2255 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002256 transport->sock = sock;
2257 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002258
2259 write_unlock_bh(&sk->sk_callback_lock);
2260 }
2261
Trond Myklebust01d37c42009-03-11 14:09:39 -04002262 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002263 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002264
Mel Gormana564b8f2012-07-31 16:45:12 -07002265 xs_set_memalloc(xprt);
2266
Trond Myklebuste1806c72018-08-13 16:50:49 -04002267 /* Reset TCP record info */
Trond Myklebust550aebf2018-09-14 14:32:45 -04002268 xs_stream_reset_connect(transport);
Trond Myklebuste1806c72018-08-13 16:50:49 -04002269
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002270 /* Tell the socket layer to start connecting... */
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04002271 set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002272 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2273 switch (ret) {
2274 case 0:
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002275 xs_set_srcport(transport, sock);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002276 /* fall through */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002277 case -EINPROGRESS:
2278 /* SYN_SENT! */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002279 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2280 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust1f4c17a2016-08-01 13:36:08 -04002281 break;
2282 case -EADDRNOTAVAIL:
2283 /* Source port number is unavailable. Try a new one! */
2284 transport->srcport = 0;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002285 }
2286out:
2287 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002288}
2289
2290/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002291 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
Chuck Leveracf0a392018-12-19 11:00:22 -05002292 * @work: queued work item
Chuck Lever16be2d22007-08-06 11:57:38 -04002293 *
2294 * Invoked by a work queue tasklet.
2295 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002296static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002297{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002298 struct sock_xprt *transport =
2299 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002300 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002301 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002302 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002303
Chuck Lever16be2d22007-08-06 11:57:38 -04002304 if (!sock) {
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002305 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002306 xs_addr(xprt)->sa_family, SOCK_STREAM,
2307 IPPROTO_TCP, true);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002308 if (IS_ERR(sock)) {
2309 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002310 goto out;
2311 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002312 }
2313
Chuck Leverc740eff2009-08-09 15:09:46 -04002314 dprintk("RPC: worker connecting xprt %p via %s to "
2315 "%s (port %s)\n", xprt,
2316 xprt->address_strings[RPC_DISPLAY_PROTO],
2317 xprt->address_strings[RPC_DISPLAY_ADDR],
2318 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002319
2320 status = xs_tcp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002321 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever46121cf2007-01-31 12:14:08 -05002322 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2323 xprt, -status, xprt_connected(xprt),
2324 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002325 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002326 default:
2327 printk("%s: connect returned unhandled error %d\n",
2328 __func__, status);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002329 /* fall through */
Trond Myklebustf75e6742009-04-21 17:18:20 -04002330 case -EADDRNOTAVAIL:
2331 /* We're probably in TIME_WAIT. Get rid of existing socket,
2332 * and retry
2333 */
Trond Myklebusta519fc72012-09-12 16:49:15 -04002334 xs_tcp_force_close(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002335 break;
Trond Myklebust2a491992009-03-11 14:38:00 -04002336 case 0:
2337 case -EINPROGRESS:
2338 case -EALREADY:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002339 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002340 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002341 case -EINVAL:
2342 /* Happens, for instance, if the user specified a link
2343 * local IPv6 address without a scope-id.
2344 */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002345 case -ECONNREFUSED:
2346 case -ECONNRESET:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002347 case -ENETDOWN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002348 case -ENETUNREACH:
Trond Myklebust4ba161a2017-11-24 12:00:24 -05002349 case -EHOSTUNREACH:
Trond Myklebust3913c782015-02-08 21:44:04 -05002350 case -EADDRINUSE:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002351 case -ENOBUFS:
NeilBrown6ea44ad2017-05-25 17:00:32 +10002352 /*
2353 * xs_tcp_force_close() wakes tasks with -EIO.
2354 * We need to wake them first to ensure the
2355 * correct error code.
2356 */
2357 xprt_wake_pending_tasks(xprt, status);
Trond Myklebust4efdd922015-02-08 15:34:28 -05002358 xs_tcp_force_close(xprt);
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002359 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002360 }
Trond Myklebust2a491992009-03-11 14:38:00 -04002361 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002362out:
Chuck Lever2226feb2005-08-11 16:25:38 -04002363 xprt_clear_connecting(xprt);
Trond Myklebustcf767852018-12-17 17:38:51 -05002364 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002365 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04002366}
2367
Trond Myklebust02910172016-08-04 00:00:33 -04002368static unsigned long xs_reconnect_delay(const struct rpc_xprt *xprt)
2369{
2370 unsigned long start, now = jiffies;
2371
2372 start = xprt->stat.connect_start + xprt->reestablish_timeout;
2373 if (time_after(start, now))
2374 return start - now;
2375 return 0;
2376}
2377
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002378static void xs_reconnect_backoff(struct rpc_xprt *xprt)
2379{
2380 xprt->reestablish_timeout <<= 1;
2381 if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
2382 xprt->reestablish_timeout = xprt->max_reconnect_timeout;
2383 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2384 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2385}
2386
Chuck Lever68e220b2007-08-06 11:57:48 -04002387/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002388 * xs_connect - connect a socket to a remote endpoint
Trond Myklebust1b092092013-01-08 09:26:49 -05002389 * @xprt: pointer to transport structure
Chuck Lever9903cd12005-08-11 16:25:26 -04002390 * @task: address of RPC task that manages state of connect request
2391 *
2392 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002393 *
2394 * UDP socket connects are synchronous, but we use a work queue anyway
2395 * to guarantee that even unprivileged user processes can set up a
2396 * socket on a privileged port.
2397 *
2398 * If a UDP socket connect fails, the delay behavior here prevents
2399 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002400 */
Trond Myklebust1b092092013-01-08 09:26:49 -05002401static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002402{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002403 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002404 unsigned long delay = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002405
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002406 WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2407
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002408 if (transport->sock != NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002409 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2410 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002411 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002412
2413 /* Start by resetting any existing state */
2414 xs_reset_transport(transport);
2415
Trond Myklebust02910172016-08-04 00:00:33 -04002416 delay = xs_reconnect_delay(xprt);
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002417 xs_reconnect_backoff(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002418
Trond Myklebust02910172016-08-04 00:00:33 -04002419 } else
Chuck Lever46121cf2007-01-31 12:14:08 -05002420 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002421
2422 queue_delayed_work(xprtiod_workqueue,
2423 &transport->connect_worker,
2424 delay);
Chuck Levera246b012005-08-11 16:25:23 -04002425}
2426
Chuck Lever262ca072006-03-20 13:44:16 -05002427/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002428 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2429 * @xprt: rpc_xprt struct containing statistics
2430 * @seq: output file
2431 *
2432 */
2433static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2434{
2435 long idle_time = 0;
2436
2437 if (xprt_connected(xprt))
2438 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2439
2440 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
Andy Adamson15a45202012-02-14 16:19:18 -05002441 "%llu %llu %lu %llu %llu\n",
Chuck Lever176e21e2011-05-09 15:22:44 -04002442 xprt->stat.bind_count,
2443 xprt->stat.connect_count,
Chuck Lever8440a882018-10-01 14:25:41 -04002444 xprt->stat.connect_time / HZ,
Chuck Lever176e21e2011-05-09 15:22:44 -04002445 idle_time,
2446 xprt->stat.sends,
2447 xprt->stat.recvs,
2448 xprt->stat.bad_xids,
2449 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002450 xprt->stat.bklog_u,
2451 xprt->stat.max_slots,
2452 xprt->stat.sending_u,
2453 xprt->stat.pending_u);
Chuck Lever176e21e2011-05-09 15:22:44 -04002454}
2455
2456/**
Chuck Lever262ca072006-03-20 13:44:16 -05002457 * xs_udp_print_stats - display UDP socket-specifc stats
2458 * @xprt: rpc_xprt struct containing statistics
2459 * @seq: output file
2460 *
2461 */
2462static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2463{
Chuck Leverc8475462006-12-05 16:35:26 -05002464 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2465
Andy Adamson15a45202012-02-14 16:19:18 -05002466 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2467 "%lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002468 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002469 xprt->stat.bind_count,
2470 xprt->stat.sends,
2471 xprt->stat.recvs,
2472 xprt->stat.bad_xids,
2473 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002474 xprt->stat.bklog_u,
2475 xprt->stat.max_slots,
2476 xprt->stat.sending_u,
2477 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002478}
2479
2480/**
2481 * xs_tcp_print_stats - display TCP socket-specifc stats
2482 * @xprt: rpc_xprt struct containing statistics
2483 * @seq: output file
2484 *
2485 */
2486static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2487{
Chuck Leverc8475462006-12-05 16:35:26 -05002488 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002489 long idle_time = 0;
2490
2491 if (xprt_connected(xprt))
2492 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2493
Andy Adamson15a45202012-02-14 16:19:18 -05002494 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2495 "%llu %llu %lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002496 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002497 xprt->stat.bind_count,
2498 xprt->stat.connect_count,
Chuck Lever8440a882018-10-01 14:25:41 -04002499 xprt->stat.connect_time / HZ,
Chuck Lever262ca072006-03-20 13:44:16 -05002500 idle_time,
2501 xprt->stat.sends,
2502 xprt->stat.recvs,
2503 xprt->stat.bad_xids,
2504 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002505 xprt->stat.bklog_u,
2506 xprt->stat.max_slots,
2507 xprt->stat.sending_u,
2508 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002509}
2510
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002511/*
2512 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2513 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2514 * to use the server side send routines.
2515 */
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002516static int bc_malloc(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002517{
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002518 struct rpc_rqst *rqst = task->tk_rqstp;
2519 size_t size = rqst->rq_callsize;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002520 struct page *page;
2521 struct rpc_buffer *buf;
2522
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002523 if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
2524 WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
2525 size);
2526 return -EINVAL;
2527 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002528
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002529 page = alloc_page(GFP_KERNEL);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002530 if (!page)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002531 return -ENOMEM;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002532
2533 buf = page_address(page);
2534 buf->len = PAGE_SIZE;
2535
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002536 rqst->rq_buffer = buf->data;
Jeff Layton18e601d2016-10-24 20:33:23 -04002537 rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002538 return 0;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002539}
2540
2541/*
2542 * Free the space allocated in the bc_alloc routine
2543 */
Chuck Lever3435c742016-09-15 10:55:29 -04002544static void bc_free(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002545{
Chuck Lever3435c742016-09-15 10:55:29 -04002546 void *buffer = task->tk_rqstp->rq_buffer;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002547 struct rpc_buffer *buf;
2548
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002549 buf = container_of(buffer, struct rpc_buffer, data);
2550 free_page((unsigned long)buf);
2551}
2552
2553/*
2554 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2555 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2556 */
2557static int bc_sendto(struct rpc_rqst *req)
2558{
2559 int len;
2560 struct xdr_buf *xbufp = &req->rq_snd_buf;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002561 struct sock_xprt *transport =
Chuck Lever067fb112019-02-11 11:24:37 -05002562 container_of(req->rq_xprt, struct sock_xprt, xprt);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002563 unsigned long headoff;
2564 unsigned long tailoff;
Chuck Lever067fb112019-02-11 11:24:37 -05002565 struct page *tailpage;
2566 struct msghdr msg = {
2567 .msg_flags = MSG_MORE
2568 };
2569 rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
2570 (u32)xbufp->len);
2571 struct kvec iov = {
2572 .iov_base = &marker,
2573 .iov_len = sizeof(marker),
2574 };
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002575
Chuck Lever067fb112019-02-11 11:24:37 -05002576 len = kernel_sendmsg(transport->sock, &msg, &iov, 1, iov.iov_len);
2577 if (len != iov.iov_len)
2578 return -EAGAIN;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002579
Chuck Lever067fb112019-02-11 11:24:37 -05002580 tailpage = NULL;
2581 if (xbufp->tail[0].iov_len)
2582 tailpage = virt_to_page(xbufp->tail[0].iov_base);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002583 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2584 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
Chuck Lever067fb112019-02-11 11:24:37 -05002585 len = svc_send_common(transport->sock, xbufp,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002586 virt_to_page(xbufp->head[0].iov_base), headoff,
Chuck Lever067fb112019-02-11 11:24:37 -05002587 tailpage, tailoff);
2588 if (len != xbufp->len)
2589 return -EAGAIN;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002590 return len;
2591}
2592
2593/*
2594 * The send routine. Borrows from svc_send
2595 */
Trond Myklebustadfa7142018-09-03 23:58:59 -04002596static int bc_send_request(struct rpc_rqst *req)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002597{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002598 struct svc_xprt *xprt;
Andrzej Hajda7fc56132015-09-24 16:00:09 +02002599 int len;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002600
2601 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2602 /*
2603 * Get the server socket associated with this callback xprt
2604 */
2605 xprt = req->rq_xprt->bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002606
2607 /*
2608 * Grab the mutex to serialize data as the connection is shared
2609 * with the fore channel
2610 */
Trond Myklebustc5445772018-09-03 23:39:27 -04002611 mutex_lock(&xprt->xpt_mutex);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002612 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2613 len = -ENOTCONN;
2614 else
2615 len = bc_sendto(req);
2616 mutex_unlock(&xprt->xpt_mutex);
2617
2618 if (len > 0)
2619 len = 0;
2620
2621 return len;
2622}
2623
2624/*
2625 * The close routine. Since this is client initiated, we do nothing
2626 */
2627
2628static void bc_close(struct rpc_xprt *xprt)
2629{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002630}
2631
2632/*
2633 * The xprt destroy routine. Again, because this connection is client
2634 * initiated, we do nothing
2635 */
2636
2637static void bc_destroy(struct rpc_xprt *xprt)
2638{
Kinglong Mee47f72ef2014-03-24 11:58:16 +08002639 dprintk("RPC: bc_destroy xprt %p\n", xprt);
2640
2641 xs_xprt_free(xprt);
2642 module_put(THIS_MODULE);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002643}
2644
Chuck Leverd31ae252017-08-01 12:00:39 -04002645static const struct rpc_xprt_ops xs_local_ops = {
Chuck Lever176e21e2011-05-09 15:22:44 -04002646 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebust4cd34e72018-08-31 10:00:02 -04002647 .release_xprt = xprt_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002648 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002649 .free_slot = xprt_free_slot,
Chuck Lever176e21e2011-05-09 15:22:44 -04002650 .rpcbind = xs_local_rpcbind,
2651 .set_port = xs_local_set_port,
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002652 .connect = xs_local_connect,
Chuck Lever176e21e2011-05-09 15:22:44 -04002653 .buf_alloc = rpc_malloc,
2654 .buf_free = rpc_free,
Trond Myklebust550aebf2018-09-14 14:32:45 -04002655 .prepare_request = xs_stream_prepare_request,
Chuck Lever176e21e2011-05-09 15:22:44 -04002656 .send_request = xs_local_send_request,
2657 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2658 .close = xs_close,
Trond Myklebusta1311d82013-10-31 09:18:49 -04002659 .destroy = xs_destroy,
Chuck Lever176e21e2011-05-09 15:22:44 -04002660 .print_stats = xs_local_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002661 .enable_swap = xs_enable_swap,
2662 .disable_swap = xs_disable_swap,
Chuck Lever176e21e2011-05-09 15:22:44 -04002663};
2664
Chuck Leverd31ae252017-08-01 12:00:39 -04002665static const struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002666 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002667 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002668 .release_xprt = xprt_release_xprt_cong,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002669 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002670 .free_slot = xprt_free_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002671 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002672 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002673 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002674 .buf_alloc = rpc_malloc,
2675 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002676 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002677 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002678 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002679 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002680 .close = xs_close,
2681 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002682 .print_stats = xs_udp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002683 .enable_swap = xs_enable_swap,
2684 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002685 .inject_disconnect = xs_inject_disconnect,
Chuck Lever262965f2005-08-11 16:25:56 -04002686};
2687
Chuck Leverd31ae252017-08-01 12:00:39 -04002688static const struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002689 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebust4cd34e72018-08-31 10:00:02 -04002690 .release_xprt = xprt_release_xprt,
Trond Myklebust36bd7de2018-09-03 18:41:32 -04002691 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002692 .free_slot = xprt_free_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002693 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002694 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002695 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002696 .buf_alloc = rpc_malloc,
2697 .buf_free = rpc_free,
Trond Myklebust277e4ab2018-09-14 09:49:06 -04002698 .prepare_request = xs_stream_prepare_request,
Chuck Lever262965f2005-08-11 16:25:56 -04002699 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002700 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustc627d312015-02-10 11:06:04 -05002701 .close = xs_tcp_shutdown,
Chuck Lever9903cd12005-08-11 16:25:26 -04002702 .destroy = xs_destroy,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002703 .set_connect_timeout = xs_tcp_set_connect_timeout,
Chuck Lever262ca072006-03-20 13:44:16 -05002704 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002705 .enable_swap = xs_enable_swap,
2706 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002707 .inject_disconnect = xs_inject_disconnect,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002708#ifdef CONFIG_SUNRPC_BACKCHANNEL
2709 .bc_setup = xprt_setup_bc,
Chuck Lever6b26cc82016-05-02 14:40:40 -04002710 .bc_maxpayload = xs_tcp_bc_maxpayload,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002711 .bc_free_rqst = xprt_free_bc_rqst,
2712 .bc_destroy = xprt_destroy_bc,
2713#endif
Chuck Levera246b012005-08-11 16:25:23 -04002714};
2715
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002716/*
2717 * The rpc_xprt_ops for the server backchannel
2718 */
2719
Chuck Leverd31ae252017-08-01 12:00:39 -04002720static const struct rpc_xprt_ops bc_tcp_ops = {
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002721 .reserve_xprt = xprt_reserve_xprt,
2722 .release_xprt = xprt_release_xprt,
Bryan Schumaker84e28a32012-09-24 13:39:01 -04002723 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002724 .free_slot = xprt_free_slot,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002725 .buf_alloc = bc_malloc,
2726 .buf_free = bc_free,
2727 .send_request = bc_send_request,
2728 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2729 .close = bc_close,
2730 .destroy = bc_destroy,
2731 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002732 .enable_swap = xs_enable_swap,
2733 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002734 .inject_disconnect = xs_inject_disconnect,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002735};
2736
Chuck Lever92476852010-10-20 11:53:01 -04002737static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2738{
2739 static const struct sockaddr_in sin = {
2740 .sin_family = AF_INET,
2741 .sin_addr.s_addr = htonl(INADDR_ANY),
2742 };
2743 static const struct sockaddr_in6 sin6 = {
2744 .sin6_family = AF_INET6,
2745 .sin6_addr = IN6ADDR_ANY_INIT,
2746 };
2747
2748 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002749 case AF_LOCAL:
2750 break;
Chuck Lever92476852010-10-20 11:53:01 -04002751 case AF_INET:
2752 memcpy(sap, &sin, sizeof(sin));
2753 break;
2754 case AF_INET6:
2755 memcpy(sap, &sin6, sizeof(sin6));
2756 break;
2757 default:
2758 dprintk("RPC: %s: Bad address family\n", __func__);
2759 return -EAFNOSUPPORT;
2760 }
2761 return 0;
2762}
2763
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002764static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002765 unsigned int slot_table_size,
2766 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002767{
2768 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002769 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002770
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002771 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002772 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002773 return ERR_PTR(-EBADF);
2774 }
2775
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002776 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2777 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002778 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002779 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2780 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002781 return ERR_PTR(-ENOMEM);
2782 }
2783
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002784 new = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002785 mutex_init(&new->recv_mutex);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002786 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2787 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002788 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002789 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002790 else {
2791 int err;
2792 err = xs_init_anyaddr(args->dstaddr->sa_family,
2793 (struct sockaddr *)&new->srcaddr);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002794 if (err != 0) {
2795 xprt_free(xprt);
Chuck Lever92476852010-10-20 11:53:01 -04002796 return ERR_PTR(err);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002797 }
Chuck Lever92476852010-10-20 11:53:01 -04002798 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002799
2800 return xprt;
2801}
2802
Chuck Lever176e21e2011-05-09 15:22:44 -04002803static const struct rpc_timeout xs_local_default_timeout = {
2804 .to_initval = 10 * HZ,
2805 .to_maxval = 10 * HZ,
2806 .to_retries = 2,
2807};
2808
2809/**
2810 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2811 * @args: rpc transport creation arguments
2812 *
2813 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2814 */
2815static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2816{
2817 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2818 struct sock_xprt *transport;
2819 struct rpc_xprt *xprt;
2820 struct rpc_xprt *ret;
2821
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002822 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2823 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002824 if (IS_ERR(xprt))
2825 return xprt;
2826 transport = container_of(xprt, struct sock_xprt, xprt);
2827
2828 xprt->prot = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -04002829 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2830
2831 xprt->bind_timeout = XS_BIND_TO;
2832 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2833 xprt->idle_timeout = XS_IDLE_DISC_TO;
2834
2835 xprt->ops = &xs_local_ops;
2836 xprt->timeout = &xs_local_default_timeout;
2837
Trond Myklebust550aebf2018-09-14 14:32:45 -04002838 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
2839 INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket);
NeilBrown93dc41b2013-10-31 16:14:36 +11002840
Chuck Lever176e21e2011-05-09 15:22:44 -04002841 switch (sun->sun_family) {
2842 case AF_LOCAL:
2843 if (sun->sun_path[0] != '/') {
2844 dprintk("RPC: bad AF_LOCAL address: %s\n",
2845 sun->sun_path);
2846 ret = ERR_PTR(-EINVAL);
2847 goto out_err;
2848 }
2849 xprt_set_bound(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002850 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
J. Bruce Fields7073ea82013-02-21 10:14:22 -05002851 ret = ERR_PTR(xs_local_setup_socket(transport));
2852 if (ret)
2853 goto out_err;
Chuck Lever176e21e2011-05-09 15:22:44 -04002854 break;
2855 default:
2856 ret = ERR_PTR(-EAFNOSUPPORT);
2857 goto out_err;
2858 }
2859
2860 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2861 xprt->address_strings[RPC_DISPLAY_ADDR]);
2862
2863 if (try_module_get(THIS_MODULE))
2864 return xprt;
2865 ret = ERR_PTR(-EINVAL);
2866out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002867 xs_xprt_free(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002868 return ret;
2869}
2870
Trond Myklebust2881ae72007-12-20 16:03:54 -05002871static const struct rpc_timeout xs_udp_default_timeout = {
2872 .to_initval = 5 * HZ,
2873 .to_maxval = 30 * HZ,
2874 .to_increment = 5 * HZ,
2875 .to_retries = 5,
2876};
2877
Chuck Lever9903cd12005-08-11 16:25:26 -04002878/**
2879 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002880 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002881 *
2882 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002883static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002884{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002885 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002886 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002887 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002888 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002889
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002890 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2891 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002892 if (IS_ERR(xprt))
2893 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002894 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002895
Chuck Leverec739ef2006-08-22 20:06:15 -04002896 xprt->prot = IPPROTO_UDP;
Chuck Levera246b012005-08-11 16:25:23 -04002897 /* XXX: header size can vary due to auth type, IPv6, etc. */
2898 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2899
Chuck Lever03bf4b72005-08-25 16:25:55 -07002900 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002901 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2902 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002903
Chuck Lever262965f2005-08-11 16:25:56 -04002904 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002905
Trond Myklebustba7392b2007-12-20 16:03:55 -05002906 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002907
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002908 INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002909 INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2910
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002911 switch (addr->sa_family) {
2912 case AF_INET:
2913 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2914 xprt_set_bound(xprt);
2915
Chuck Lever9dc3b092009-08-09 15:09:46 -04002916 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002917 break;
2918 case AF_INET6:
2919 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2920 xprt_set_bound(xprt);
2921
Chuck Lever9dc3b092009-08-09 15:09:46 -04002922 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002923 break;
2924 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002925 ret = ERR_PTR(-EAFNOSUPPORT);
2926 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002927 }
2928
Chuck Leverc740eff2009-08-09 15:09:46 -04002929 if (xprt_bound(xprt))
2930 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2931 xprt->address_strings[RPC_DISPLAY_ADDR],
2932 xprt->address_strings[RPC_DISPLAY_PORT],
2933 xprt->address_strings[RPC_DISPLAY_PROTO]);
2934 else
2935 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2936 xprt->address_strings[RPC_DISPLAY_ADDR],
2937 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002938
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002939 if (try_module_get(THIS_MODULE))
2940 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002941 ret = ERR_PTR(-EINVAL);
2942out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002943 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002944 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002945}
2946
Trond Myklebust2881ae72007-12-20 16:03:54 -05002947static const struct rpc_timeout xs_tcp_default_timeout = {
2948 .to_initval = 60 * HZ,
2949 .to_maxval = 60 * HZ,
2950 .to_retries = 2,
2951};
2952
Chuck Lever9903cd12005-08-11 16:25:26 -04002953/**
2954 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002955 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002956 *
2957 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002958static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002959{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002960 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002961 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002962 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002963 struct rpc_xprt *ret;
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002964 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2965
2966 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2967 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Levera246b012005-08-11 16:25:23 -04002968
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002969 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002970 max_slot_table_size);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002971 if (IS_ERR(xprt))
2972 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002973 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002974
Chuck Leverec739ef2006-08-22 20:06:15 -04002975 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002976 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002977
Chuck Lever03bf4b72005-08-25 16:25:55 -07002978 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002979 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2980 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002981
Chuck Lever262965f2005-08-11 16:25:56 -04002982 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002983 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002984
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002985 xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002986 xprt->connect_timeout = xprt->timeout->to_initval *
2987 (xprt->timeout->to_retries + 1);
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002988
Trond Myklebustc50b8ee2018-09-14 14:26:28 -04002989 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002990 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
2991
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002992 switch (addr->sa_family) {
2993 case AF_INET:
2994 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2995 xprt_set_bound(xprt);
2996
Chuck Lever9dc3b092009-08-09 15:09:46 -04002997 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002998 break;
2999 case AF_INET6:
3000 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
3001 xprt_set_bound(xprt);
3002
Chuck Lever9dc3b092009-08-09 15:09:46 -04003003 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003004 break;
3005 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003006 ret = ERR_PTR(-EAFNOSUPPORT);
3007 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003008 }
3009
Chuck Leverc740eff2009-08-09 15:09:46 -04003010 if (xprt_bound(xprt))
3011 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3012 xprt->address_strings[RPC_DISPLAY_ADDR],
3013 xprt->address_strings[RPC_DISPLAY_PORT],
3014 xprt->address_strings[RPC_DISPLAY_PROTO]);
3015 else
3016 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
3017 xprt->address_strings[RPC_DISPLAY_ADDR],
3018 xprt->address_strings[RPC_DISPLAY_PROTO]);
3019
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003020 if (try_module_get(THIS_MODULE))
3021 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003022 ret = ERR_PTR(-EINVAL);
3023out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003024 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003025 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04003026}
Chuck Lever282b32e2006-12-05 16:35:51 -05003027
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003028/**
3029 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3030 * @args: rpc transport creation arguments
3031 *
3032 */
3033static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
3034{
3035 struct sockaddr *addr = args->dstaddr;
3036 struct rpc_xprt *xprt;
3037 struct sock_xprt *transport;
3038 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003039 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003040
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003041 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3042 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003043 if (IS_ERR(xprt))
3044 return xprt;
3045 transport = container_of(xprt, struct sock_xprt, xprt);
3046
3047 xprt->prot = IPPROTO_TCP;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003048 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3049 xprt->timeout = &xs_tcp_default_timeout;
3050
3051 /* backchannel */
3052 xprt_set_bound(xprt);
3053 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003054 xprt->reestablish_timeout = 0;
3055 xprt->idle_timeout = 0;
3056
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003057 xprt->ops = &bc_tcp_ops;
3058
3059 switch (addr->sa_family) {
3060 case AF_INET:
3061 xs_format_peer_addresses(xprt, "tcp",
3062 RPCBIND_NETID_TCP);
3063 break;
3064 case AF_INET6:
3065 xs_format_peer_addresses(xprt, "tcp",
3066 RPCBIND_NETID_TCP6);
3067 break;
3068 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003069 ret = ERR_PTR(-EAFNOSUPPORT);
3070 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003071 }
3072
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04003073 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3074 xprt->address_strings[RPC_DISPLAY_ADDR],
3075 xprt->address_strings[RPC_DISPLAY_PORT],
3076 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003077
3078 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003079 * Once we've associated a backchannel xprt with a connection,
Weng Meiling28303ca2013-11-30 17:56:44 +08003080 * we want to keep it around as long as the connection lasts,
3081 * in case we need to start using it for a backchannel again;
3082 * this reference won't be dropped until bc_xprt is destroyed.
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003083 */
3084 xprt_get(xprt);
3085 args->bc_xprt->xpt_bc_xprt = xprt;
3086 xprt->bc_xprt = args->bc_xprt;
3087 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
3088 transport->sock = bc_sock->sk_sock;
3089 transport->inet = bc_sock->sk_sk;
3090
3091 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003092 * Since we don't want connections for the backchannel, we set
3093 * the xprt status to connected
3094 */
3095 xprt_set_connected(xprt);
3096
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003097 if (try_module_get(THIS_MODULE))
3098 return xprt;
Kinglong Mee642aab52014-03-24 12:00:28 +08003099
3100 args->bc_xprt->xpt_bc_xprt = NULL;
J. Bruce Fields39a9bea2016-05-17 12:38:21 -04003101 args->bc_xprt->xpt_bc_xps = NULL;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003102 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003103 ret = ERR_PTR(-EINVAL);
3104out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003105 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003106 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003107}
3108
Chuck Lever176e21e2011-05-09 15:22:44 -04003109static struct xprt_class xs_local_transport = {
3110 .list = LIST_HEAD_INIT(xs_local_transport.list),
3111 .name = "named UNIX socket",
3112 .owner = THIS_MODULE,
3113 .ident = XPRT_TRANSPORT_LOCAL,
3114 .setup = xs_setup_local,
3115};
3116
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003117static struct xprt_class xs_udp_transport = {
3118 .list = LIST_HEAD_INIT(xs_udp_transport.list),
3119 .name = "udp",
3120 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003121 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003122 .setup = xs_setup_udp,
3123};
3124
3125static struct xprt_class xs_tcp_transport = {
3126 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
3127 .name = "tcp",
3128 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003129 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003130 .setup = xs_setup_tcp,
3131};
3132
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003133static struct xprt_class xs_bc_tcp_transport = {
3134 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3135 .name = "tcp NFSv4.1 backchannel",
3136 .owner = THIS_MODULE,
3137 .ident = XPRT_TRANSPORT_BC_TCP,
3138 .setup = xs_setup_bc_tcp,
3139};
3140
Chuck Lever282b32e2006-12-05 16:35:51 -05003141/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003142 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05003143 *
3144 */
3145int init_socket_xprt(void)
3146{
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08003147 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08003148 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05003149
Chuck Lever176e21e2011-05-09 15:22:44 -04003150 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003151 xprt_register_transport(&xs_udp_transport);
3152 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003153 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003154
Chuck Lever282b32e2006-12-05 16:35:51 -05003155 return 0;
3156}
3157
3158/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003159 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05003160 *
3161 */
3162void cleanup_socket_xprt(void)
3163{
Chuck Leverfbf76682006-12-05 16:35:54 -05003164 if (sunrpc_table_header) {
3165 unregister_sysctl_table(sunrpc_table_header);
3166 sunrpc_table_header = NULL;
3167 }
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003168
Chuck Lever176e21e2011-05-09 15:22:44 -04003169 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003170 xprt_unregister_transport(&xs_udp_transport);
3171 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003172 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05003173}
Trond Myklebustcbf11072009-08-09 15:06:19 -04003174
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003175static int param_set_uint_minmax(const char *val,
3176 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003177 unsigned int min, unsigned int max)
3178{
Daniel Walter00cfaa92014-06-21 13:06:38 +01003179 unsigned int num;
Trond Myklebustcbf11072009-08-09 15:06:19 -04003180 int ret;
3181
3182 if (!val)
3183 return -EINVAL;
Daniel Walter00cfaa92014-06-21 13:06:38 +01003184 ret = kstrtouint(val, 0, &num);
Dan Carpenter9761a242017-02-19 00:34:59 +03003185 if (ret)
3186 return ret;
3187 if (num < min || num > max)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003188 return -EINVAL;
3189 *((unsigned int *)kp->arg) = num;
3190 return 0;
3191}
3192
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003193static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003194{
Frank Sorensonffb6ca32016-07-08 16:35:25 -05003195 return param_set_uint_minmax(val, kp,
J. Bruce Fields826799e2018-10-18 15:27:02 -04003196 RPC_MIN_RESVPORT,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003197 RPC_MAX_RESVPORT);
3198}
3199
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303200static const struct kernel_param_ops param_ops_portnr = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003201 .set = param_set_portnr,
3202 .get = param_get_uint,
3203};
3204
Trond Myklebustcbf11072009-08-09 15:06:19 -04003205#define param_check_portnr(name, p) \
3206 __param_check(name, p, unsigned int);
3207
3208module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3209module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3210
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003211static int param_set_slot_table_size(const char *val,
3212 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003213{
3214 return param_set_uint_minmax(val, kp,
3215 RPC_MIN_SLOT_TABLE,
3216 RPC_MAX_SLOT_TABLE);
3217}
3218
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303219static const struct kernel_param_ops param_ops_slot_table_size = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003220 .set = param_set_slot_table_size,
3221 .get = param_get_uint,
3222};
3223
Trond Myklebustcbf11072009-08-09 15:06:19 -04003224#define param_check_slot_table_size(name, p) \
3225 __param_check(name, p, unsigned int);
3226
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003227static int param_set_max_slot_table_size(const char *val,
3228 const struct kernel_param *kp)
3229{
3230 return param_set_uint_minmax(val, kp,
3231 RPC_MIN_SLOT_TABLE,
3232 RPC_MAX_SLOT_TABLE_LIMIT);
3233}
3234
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303235static const struct kernel_param_ops param_ops_max_slot_table_size = {
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003236 .set = param_set_max_slot_table_size,
3237 .get = param_get_uint,
3238};
3239
3240#define param_check_max_slot_table_size(name, p) \
3241 __param_check(name, p, unsigned int);
3242
Trond Myklebustcbf11072009-08-09 15:06:19 -04003243module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3244 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003245module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3246 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003247module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3248 slot_table_size, 0644);