blob: 8a5e823e0b339b1998ff21b9cac814de213a2b23 [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>
51#include <linux/uio.h>
Chuck Levera246b012005-08-11 16:25:23 -040052
Trond Myklebust40b5ea02013-09-04 12:16:23 -040053#include <trace/events/sunrpc.h>
54
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030055#include "sunrpc.h"
Chuck Lever176e21e2011-05-09 15:22:44 -040056
57static void xs_close(struct rpc_xprt *xprt);
Trond Myklebust7196dbb2017-02-08 11:17:54 -050058static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
59 struct socket *sock);
Chuck Lever176e21e2011-05-09 15:22:44 -040060
Chuck Lever9903cd12005-08-11 16:25:26 -040061/*
Chuck Leverc556b752005-11-01 12:24:48 -050062 * xprtsock tunables
63 */
Trond Myklebust09acfea2012-03-11 15:22:54 -040064static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
65static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
66static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
Chuck Leverc556b752005-11-01 12:24:48 -050067
Trond Myklebust09acfea2012-03-11 15:22:54 -040068static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
69static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
Chuck Leverc556b752005-11-01 12:24:48 -050070
Trond Myklebust54c09872015-02-09 11:01:02 -050071#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
72
Trond Myklebust7d1e8252009-03-11 14:38:03 -040073#define XS_TCP_LINGER_TO (15U * HZ)
Trond Myklebust25fe6142009-03-11 14:38:03 -040074static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
Trond Myklebust7d1e8252009-03-11 14:38:03 -040075
Chuck Leverc556b752005-11-01 12:24:48 -050076/*
Chuck Leverfbf76682006-12-05 16:35:54 -050077 * We can register our own files under /proc/sys/sunrpc by
78 * calling register_sysctl_table() again. The files in that
79 * directory become the union of all files registered there.
80 *
81 * We simply need to make sure that we don't collide with
82 * someone else's file names!
83 */
84
Chuck Leverfbf76682006-12-05 16:35:54 -050085static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
86static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
Trond Myklebustd9ba1312011-07-17 18:11:30 -040087static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Leverfbf76682006-12-05 16:35:54 -050088static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
89static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
90
91static struct ctl_table_header *sunrpc_table_header;
92
93/*
94 * FIXME: changing the UDP slot table size should also resize the UDP
95 * socket buffers for existing UDP transports
96 */
Joe Perchesfe2c6332013-06-11 23:04:25 -070097static struct ctl_table xs_tunables_table[] = {
Chuck Leverfbf76682006-12-05 16:35:54 -050098 {
Chuck Leverfbf76682006-12-05 16:35:54 -050099 .procname = "udp_slot_table_entries",
100 .data = &xprt_udp_slot_table_entries,
101 .maxlen = sizeof(unsigned int),
102 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800103 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500104 .extra1 = &min_slot_table_size,
105 .extra2 = &max_slot_table_size
106 },
107 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500108 .procname = "tcp_slot_table_entries",
109 .data = &xprt_tcp_slot_table_entries,
110 .maxlen = sizeof(unsigned int),
111 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800112 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500113 .extra1 = &min_slot_table_size,
114 .extra2 = &max_slot_table_size
115 },
116 {
Trond Myklebustd9ba1312011-07-17 18:11:30 -0400117 .procname = "tcp_max_slot_table_entries",
118 .data = &xprt_max_tcp_slot_table_entries,
119 .maxlen = sizeof(unsigned int),
120 .mode = 0644,
121 .proc_handler = proc_dointvec_minmax,
122 .extra1 = &min_slot_table_size,
123 .extra2 = &max_tcp_slot_table_limit
124 },
125 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500126 .procname = "min_resvport",
127 .data = &xprt_min_resvport,
128 .maxlen = sizeof(unsigned int),
129 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800130 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500131 .extra1 = &xprt_min_resvport_limit,
J. Bruce Fields826799e2018-10-18 15:27:02 -0400132 .extra2 = &xprt_max_resvport_limit
Chuck Leverfbf76682006-12-05 16:35:54 -0500133 },
134 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500135 .procname = "max_resvport",
136 .data = &xprt_max_resvport,
137 .maxlen = sizeof(unsigned int),
138 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800139 .proc_handler = proc_dointvec_minmax,
J. Bruce Fields826799e2018-10-18 15:27:02 -0400140 .extra1 = &xprt_min_resvport_limit,
Chuck Leverfbf76682006-12-05 16:35:54 -0500141 .extra2 = &xprt_max_resvport_limit
142 },
143 {
Trond Myklebust25fe6142009-03-11 14:38:03 -0400144 .procname = "tcp_fin_timeout",
145 .data = &xs_tcp_fin_timeout,
146 .maxlen = sizeof(xs_tcp_fin_timeout),
147 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800148 .proc_handler = proc_dointvec_jiffies,
Trond Myklebust25fe6142009-03-11 14:38:03 -0400149 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800150 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500151};
152
Joe Perchesfe2c6332013-06-11 23:04:25 -0700153static struct ctl_table sunrpc_table[] = {
Chuck Leverfbf76682006-12-05 16:35:54 -0500154 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500155 .procname = "sunrpc",
156 .mode = 0555,
157 .child = xs_tunables_table
158 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800159 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500160};
161
162#endif
163
164/*
Chuck Lever03bf4b72005-08-25 16:25:55 -0700165 * Wait duration for a reply from the RPC portmapper.
166 */
167#define XS_BIND_TO (60U * HZ)
168
169/*
170 * Delay if a UDP socket connect error occurs. This is most likely some
171 * kind of resource problem on the local host.
172 */
173#define XS_UDP_REEST_TO (2U * HZ)
174
175/*
176 * The reestablish timeout allows clients to delay for a bit before attempting
177 * to reconnect to a server that just dropped our connection.
178 *
179 * We implement an exponential backoff when trying to reestablish a TCP
180 * transport connection with the server. Some servers like to drop a TCP
181 * connection when they are overworked, so we start with a short timeout and
182 * increase over time if the server is down or not responding.
183 */
184#define XS_TCP_INIT_REEST_TO (3U * HZ)
Chuck Lever03bf4b72005-08-25 16:25:55 -0700185
186/*
187 * TCP idle timeout; client drops the transport socket if it is idle
188 * for this long. Note that we also timeout UDP sockets to prevent
189 * holding port numbers when there is no RPC traffic.
190 */
191#define XS_IDLE_DISC_TO (5U * 60 * HZ)
192
Jeff Laytonf895b252014-11-17 16:58:04 -0500193#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Levera246b012005-08-11 16:25:23 -0400194# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400195# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400196#endif
197
Chuck Levera246b012005-08-11 16:25:23 -0400198#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400199static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400200{
Chuck Lever9903cd12005-08-11 16:25:26 -0400201 u8 *buf = (u8 *) packet;
202 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400203
Chuck Lever46121cf2007-01-31 12:14:08 -0500204 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400205 for (j = 0; j < count && j < 128; j += 4) {
206 if (!(j & 31)) {
207 if (j)
208 dprintk("\n");
209 dprintk("0x%04x ", j);
210 }
211 dprintk("%02x%02x%02x%02x ",
212 buf[j], buf[j+1], buf[j+2], buf[j+3]);
213 }
214 dprintk("\n");
215}
216#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400217static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400218{
219 /* NOP */
220}
221#endif
222
Trond Myklebust21180712013-12-31 13:22:59 -0500223static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
224{
225 return (struct rpc_xprt *) sk->sk_user_data;
226}
227
Chuck Lever95392c52007-08-06 11:57:58 -0400228static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400229{
Chuck Lever95392c52007-08-06 11:57:58 -0400230 return (struct sockaddr *) &xprt->addr;
231}
232
Chuck Lever176e21e2011-05-09 15:22:44 -0400233static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
234{
235 return (struct sockaddr_un *) &xprt->addr;
236}
237
Chuck Lever95392c52007-08-06 11:57:58 -0400238static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
239{
240 return (struct sockaddr_in *) &xprt->addr;
241}
242
243static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
244{
245 return (struct sockaddr_in6 *) &xprt->addr;
246}
247
Chuck Leverc877b842009-08-09 15:09:36 -0400248static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400249{
Chuck Leverc877b842009-08-09 15:09:36 -0400250 struct sockaddr *sap = xs_addr(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400251 struct sockaddr_in6 *sin6;
252 struct sockaddr_in *sin;
Chuck Lever176e21e2011-05-09 15:22:44 -0400253 struct sockaddr_un *sun;
Chuck Leverc877b842009-08-09 15:09:36 -0400254 char buf[128];
Chuck Leveredb267a2006-08-22 20:06:18 -0400255
Chuck Lever9dc3b092009-08-09 15:09:46 -0400256 switch (sap->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400257 case AF_LOCAL:
258 sun = xs_addr_un(xprt);
259 strlcpy(buf, sun->sun_path, sizeof(buf));
260 xprt->address_strings[RPC_DISPLAY_ADDR] =
261 kstrdup(buf, GFP_KERNEL);
262 break;
Chuck Lever9dc3b092009-08-09 15:09:46 -0400263 case AF_INET:
Chuck Lever176e21e2011-05-09 15:22:44 -0400264 (void)rpc_ntop(sap, buf, sizeof(buf));
265 xprt->address_strings[RPC_DISPLAY_ADDR] =
266 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400267 sin = xs_addr_in(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800268 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
Chuck Lever9dc3b092009-08-09 15:09:46 -0400269 break;
270 case AF_INET6:
Chuck Lever176e21e2011-05-09 15:22:44 -0400271 (void)rpc_ntop(sap, buf, sizeof(buf));
272 xprt->address_strings[RPC_DISPLAY_ADDR] =
273 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400274 sin6 = xs_addr_in6(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800275 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400276 break;
277 default:
278 BUG();
Chuck Leveredb267a2006-08-22 20:06:18 -0400279 }
Chuck Lever176e21e2011-05-09 15:22:44 -0400280
Chuck Lever9dc3b092009-08-09 15:09:46 -0400281 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
Chuck Leveredb267a2006-08-22 20:06:18 -0400282}
283
Chuck Lever9dc3b092009-08-09 15:09:46 -0400284static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400285{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400286 struct sockaddr *sap = xs_addr(xprt);
287 char buf[128];
Chuck Lever4b6473f2007-08-06 11:57:12 -0400288
Joe Perches81160e662010-03-08 12:15:59 -0800289 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400290 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400291
Joe Perches81160e662010-03-08 12:15:59 -0800292 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400293 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
294}
Chuck Lever4b6473f2007-08-06 11:57:12 -0400295
Chuck Lever9dc3b092009-08-09 15:09:46 -0400296static void xs_format_peer_addresses(struct rpc_xprt *xprt,
297 const char *protocol,
298 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400299{
Chuck Leverb454ae92008-01-07 18:34:48 -0500300 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leverb454ae92008-01-07 18:34:48 -0500301 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leverc877b842009-08-09 15:09:36 -0400302 xs_format_common_peer_addresses(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400303 xs_format_common_peer_ports(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400304}
305
Chuck Lever9dc3b092009-08-09 15:09:46 -0400306static void xs_update_peer_port(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400307{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400308 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
309 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400310
Chuck Lever9dc3b092009-08-09 15:09:46 -0400311 xs_format_common_peer_ports(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400312}
313
314static void xs_free_peer_addresses(struct rpc_xprt *xprt)
315{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500316 unsigned int i;
317
318 for (i = 0; i < RPC_DISPLAY_MAX; i++)
319 switch (i) {
320 case RPC_DISPLAY_PROTO:
321 case RPC_DISPLAY_NETID:
322 continue;
323 default:
324 kfree(xprt->address_strings[i]);
325 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400326}
327
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400328static size_t
329xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp)
330{
331 size_t i,n;
332
Trond Myklebust16e5e902018-12-02 15:22:48 -0500333 if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES))
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400334 return want;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400335 n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT;
336 for (i = 0; i < n; i++) {
337 if (buf->pages[i])
338 continue;
339 buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp);
340 if (!buf->pages[i]) {
Trond Myklebust16e5e902018-12-02 15:22:48 -0500341 i *= PAGE_SIZE;
342 return i > buf->page_base ? i - buf->page_base : 0;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400343 }
344 }
345 return want;
346}
347
348static ssize_t
349xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek)
350{
351 ssize_t ret;
352 if (seek != 0)
353 iov_iter_advance(&msg->msg_iter, seek);
354 ret = sock_recvmsg(sock, msg, flags);
355 return ret > 0 ? ret + seek : ret;
356}
357
358static ssize_t
359xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags,
360 struct kvec *kvec, size_t count, size_t seek)
361{
Al Viro0e9b4a82018-11-01 18:19:03 -0400362 iov_iter_kvec(&msg->msg_iter, READ, kvec, 1, count);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400363 return xs_sock_recvmsg(sock, msg, flags, seek);
364}
365
366static ssize_t
367xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags,
368 struct bio_vec *bvec, unsigned long nr, size_t count,
369 size_t seek)
370{
Al Viro0e9b4a82018-11-01 18:19:03 -0400371 iov_iter_bvec(&msg->msg_iter, READ, bvec, nr, count);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400372 return xs_sock_recvmsg(sock, msg, flags, seek);
373}
374
375static ssize_t
376xs_read_discard(struct socket *sock, struct msghdr *msg, int flags,
377 size_t count)
378{
Trond Myklebustb76a5af2018-12-03 15:58:58 -0500379 iov_iter_discard(&msg->msg_iter, READ, count);
380 return sock_recvmsg(sock, msg, flags);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400381}
382
383static ssize_t
384xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags,
385 struct xdr_buf *buf, size_t count, size_t seek, size_t *read)
386{
387 size_t want, seek_init = seek, offset = 0;
388 ssize_t ret;
389
390 if (seek < buf->head[0].iov_len) {
391 want = min_t(size_t, count, buf->head[0].iov_len);
392 ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek);
393 if (ret <= 0)
394 goto sock_err;
395 offset += ret;
396 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
397 goto out;
398 if (ret != want)
Trond Myklebustc4433052018-12-04 07:52:11 -0500399 goto out;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400400 seek = 0;
401 } else {
402 seek -= buf->head[0].iov_len;
403 offset += buf->head[0].iov_len;
404 }
Trond Myklebust16e5e902018-12-02 15:22:48 -0500405
406 want = xs_alloc_sparse_pages(buf,
407 min_t(size_t, count - offset, buf->page_len),
408 GFP_NOWAIT);
409 if (seek < want) {
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400410 ret = xs_read_bvec(sock, msg, flags, buf->bvec,
411 xdr_buf_pagecount(buf),
412 want + buf->page_base,
413 seek + buf->page_base);
414 if (ret <= 0)
415 goto sock_err;
416 offset += ret - buf->page_base;
417 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
418 goto out;
419 if (ret != want)
Trond Myklebustc4433052018-12-04 07:52:11 -0500420 goto out;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400421 seek = 0;
422 } else {
Trond Myklebust16e5e902018-12-02 15:22:48 -0500423 seek -= want;
424 offset += want;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400425 }
Trond Myklebust16e5e902018-12-02 15:22:48 -0500426
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400427 if (seek < buf->tail[0].iov_len) {
428 want = min_t(size_t, count - offset, buf->tail[0].iov_len);
429 ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek);
430 if (ret <= 0)
431 goto sock_err;
432 offset += ret;
433 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
434 goto out;
435 if (ret != want)
Trond Myklebustc4433052018-12-04 07:52:11 -0500436 goto out;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400437 } else
438 offset += buf->tail[0].iov_len;
439 ret = -EMSGSIZE;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400440out:
441 *read = offset - seek_init;
442 return ret;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400443sock_err:
444 offset += seek;
445 goto out;
446}
447
448static void
449xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf)
450{
451 if (!transport->recv.copied) {
452 if (buf->head[0].iov_len >= transport->recv.offset)
453 memcpy(buf->head[0].iov_base,
454 &transport->recv.xid,
455 transport->recv.offset);
456 transport->recv.copied = transport->recv.offset;
457 }
458}
459
460static bool
461xs_read_stream_request_done(struct sock_xprt *transport)
462{
463 return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT);
464}
465
466static ssize_t
467xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg,
468 int flags, struct rpc_rqst *req)
469{
470 struct xdr_buf *buf = &req->rq_private_buf;
471 size_t want, read;
472 ssize_t ret;
473
474 xs_read_header(transport, buf);
475
476 want = transport->recv.len - transport->recv.offset;
477 ret = xs_read_xdr_buf(transport->sock, msg, flags, buf,
478 transport->recv.copied + want, transport->recv.copied,
479 &read);
480 transport->recv.offset += read;
481 transport->recv.copied += read;
482 if (transport->recv.offset == transport->recv.len) {
483 if (xs_read_stream_request_done(transport))
484 msg->msg_flags |= MSG_EOR;
Trond Myklebustc4433052018-12-04 07:52:11 -0500485 return read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400486 }
487
488 switch (ret) {
Trond Myklebustc4433052018-12-04 07:52:11 -0500489 default:
490 break;
Trond Myklebust26781ea2018-12-03 15:41:38 -0500491 case -EFAULT:
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400492 case -EMSGSIZE:
Trond Myklebust26781ea2018-12-03 15:41:38 -0500493 msg->msg_flags |= MSG_TRUNC;
Trond Myklebustc4433052018-12-04 07:52:11 -0500494 return read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400495 case 0:
496 return -ESHUTDOWN;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400497 }
Trond Myklebustc4433052018-12-04 07:52:11 -0500498 return ret < 0 ? ret : read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400499}
500
501static size_t
502xs_read_stream_headersize(bool isfrag)
503{
504 if (isfrag)
505 return sizeof(__be32);
506 return 3 * sizeof(__be32);
507}
508
509static ssize_t
510xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg,
511 int flags, size_t want, size_t seek)
512{
513 struct kvec kvec = {
514 .iov_base = &transport->recv.fraghdr,
515 .iov_len = want,
516 };
517 return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek);
518}
519
520#if defined(CONFIG_SUNRPC_BACKCHANNEL)
521static ssize_t
522xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
523{
524 struct rpc_xprt *xprt = &transport->xprt;
525 struct rpc_rqst *req;
526 ssize_t ret;
527
528 /* Look up and lock the request corresponding to the given XID */
529 req = xprt_lookup_bc_request(xprt, transport->recv.xid);
530 if (!req) {
531 printk(KERN_WARNING "Callback slot table overflowed\n");
532 return -ESHUTDOWN;
533 }
534
535 ret = xs_read_stream_request(transport, msg, flags, req);
536 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
Trond Myklebustc4433052018-12-04 07:52:11 -0500537 xprt_complete_bc_request(req, transport->recv.copied);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400538
539 return ret;
540}
541#else /* CONFIG_SUNRPC_BACKCHANNEL */
542static ssize_t
543xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
544{
545 return -ESHUTDOWN;
546}
547#endif /* CONFIG_SUNRPC_BACKCHANNEL */
548
549static ssize_t
550xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags)
551{
552 struct rpc_xprt *xprt = &transport->xprt;
553 struct rpc_rqst *req;
554 ssize_t ret = 0;
555
556 /* Look up and lock the request corresponding to the given XID */
557 spin_lock(&xprt->queue_lock);
558 req = xprt_lookup_rqst(xprt, transport->recv.xid);
559 if (!req) {
560 msg->msg_flags |= MSG_TRUNC;
561 goto out;
562 }
563 xprt_pin_rqst(req);
564 spin_unlock(&xprt->queue_lock);
565
566 ret = xs_read_stream_request(transport, msg, flags, req);
567
568 spin_lock(&xprt->queue_lock);
569 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
Trond Myklebustc4433052018-12-04 07:52:11 -0500570 xprt_complete_rqst(req->rq_task, transport->recv.copied);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400571 xprt_unpin_rqst(req);
572out:
573 spin_unlock(&xprt->queue_lock);
574 return ret;
575}
576
577static ssize_t
578xs_read_stream(struct sock_xprt *transport, int flags)
579{
580 struct msghdr msg = { 0 };
581 size_t want, read = 0;
582 ssize_t ret = 0;
583
584 if (transport->recv.len == 0) {
585 want = xs_read_stream_headersize(transport->recv.copied != 0);
586 ret = xs_read_stream_header(transport, &msg, flags, want,
587 transport->recv.offset);
588 if (ret <= 0)
589 goto out_err;
590 transport->recv.offset = ret;
Trond Myklebustc4433052018-12-04 07:52:11 -0500591 if (transport->recv.offset != want)
592 return transport->recv.offset;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400593 transport->recv.len = be32_to_cpu(transport->recv.fraghdr) &
594 RPC_FRAGMENT_SIZE_MASK;
595 transport->recv.offset -= sizeof(transport->recv.fraghdr);
596 read = ret;
597 }
598
599 switch (be32_to_cpu(transport->recv.calldir)) {
Trond Myklebustc4433052018-12-04 07:52:11 -0500600 default:
601 msg.msg_flags |= MSG_TRUNC;
602 break;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400603 case RPC_CALL:
604 ret = xs_read_stream_call(transport, &msg, flags);
605 break;
606 case RPC_REPLY:
607 ret = xs_read_stream_reply(transport, &msg, flags);
608 }
609 if (msg.msg_flags & MSG_TRUNC) {
610 transport->recv.calldir = cpu_to_be32(-1);
611 transport->recv.copied = -1;
612 }
613 if (ret < 0)
614 goto out_err;
615 read += ret;
616 if (transport->recv.offset < transport->recv.len) {
Trond Myklebustc4433052018-12-04 07:52:11 -0500617 if (!(msg.msg_flags & MSG_TRUNC))
618 return read;
Trond Myklebustb76a5af2018-12-03 15:58:58 -0500619 msg.msg_flags = 0;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400620 ret = xs_read_discard(transport->sock, &msg, flags,
621 transport->recv.len - transport->recv.offset);
622 if (ret <= 0)
623 goto out_err;
624 transport->recv.offset += ret;
625 read += ret;
626 if (transport->recv.offset != transport->recv.len)
Trond Myklebustc4433052018-12-04 07:52:11 -0500627 return read;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400628 }
629 if (xs_read_stream_request_done(transport)) {
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400630 trace_xs_stream_read_request(transport);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400631 transport->recv.copied = 0;
632 }
633 transport->recv.offset = 0;
634 transport->recv.len = 0;
635 return read;
636out_err:
Trond Myklebust794628572018-12-03 18:49:00 -0500637 return ret != 0 ? ret : -ESHUTDOWN;
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400638}
639
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400640static void xs_stream_data_receive(struct sock_xprt *transport)
641{
642 size_t read = 0;
643 ssize_t ret = 0;
644
645 mutex_lock(&transport->recv_mutex);
Trond Myklebustdfcf0382018-12-04 07:50:06 -0500646 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400647 if (transport->sock == NULL)
648 goto out;
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400649 for (;;) {
650 ret = xs_read_stream(transport, MSG_DONTWAIT);
Trond Myklebustc4433052018-12-04 07:52:11 -0500651 if (ret < 0)
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400652 break;
653 read += ret;
654 cond_resched();
655 }
656out:
657 mutex_unlock(&transport->recv_mutex);
658 trace_xs_stream_read_data(&transport->xprt, ret, read);
659}
660
661static void xs_stream_data_receive_workfn(struct work_struct *work)
662{
663 struct sock_xprt *transport =
664 container_of(work, struct sock_xprt, recv_worker);
665 xs_stream_data_receive(transport);
666}
667
Trond Myklebust550aebf2018-09-14 14:32:45 -0400668static void
669xs_stream_reset_connect(struct sock_xprt *transport)
670{
671 transport->recv.offset = 0;
672 transport->recv.len = 0;
673 transport->recv.copied = 0;
674 transport->xmit.offset = 0;
675 transport->xprt.stat.connect_count++;
676 transport->xprt.stat.connect_start = jiffies;
677}
678
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400679#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
680
Trond Myklebust24c56842006-10-17 15:06:22 -0400681static 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 -0400682{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400683 struct msghdr msg = {
684 .msg_name = addr,
685 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400686 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
687 };
688 struct kvec iov = {
689 .iov_base = vec->iov_base + base,
690 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400691 };
692
Trond Myklebust24c56842006-10-17 15:06:22 -0400693 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400694 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
695 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
696}
697
Jason Baronf279cd02014-09-24 18:08:00 +0000698static 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 -0400699{
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500700 ssize_t (*do_sendpage)(struct socket *sock, struct page *page,
701 int offset, size_t size, int flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400702 struct page **ppage;
703 unsigned int remainder;
Jason Baronf279cd02014-09-24 18:08:00 +0000704 int err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400705
Trond Myklebust24c56842006-10-17 15:06:22 -0400706 remainder = xdr->page_len - base;
707 base += xdr->page_base;
708 ppage = xdr->pages + (base >> PAGE_SHIFT);
709 base &= ~PAGE_MASK;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500710 do_sendpage = sock->ops->sendpage;
711 if (!zerocopy)
712 do_sendpage = sock_no_sendpage;
Trond Myklebust24c56842006-10-17 15:06:22 -0400713 for(;;) {
714 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
715 int flags = XS_SENDMSG_FLAGS;
716
717 remainder -= len;
Trond Myklebust31303d62015-10-06 15:59:20 -0400718 if (more)
Trond Myklebust24c56842006-10-17 15:06:22 -0400719 flags |= MSG_MORE;
Trond Myklebust31303d62015-10-06 15:59:20 -0400720 if (remainder != 0)
721 flags |= MSG_SENDPAGE_NOTLAST | MSG_MORE;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500722 err = do_sendpage(sock, *ppage, base, len, flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400723 if (remainder == 0 || err != len)
724 break;
Jason Baronf279cd02014-09-24 18:08:00 +0000725 *sent_p += err;
Trond Myklebust24c56842006-10-17 15:06:22 -0400726 ppage++;
727 base = 0;
728 }
Jason Baronf279cd02014-09-24 18:08:00 +0000729 if (err > 0) {
730 *sent_p += err;
731 err = 0;
732 }
733 return err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400734}
735
Chuck Lever9903cd12005-08-11 16:25:26 -0400736/**
737 * xs_sendpages - write pages directly to a socket
738 * @sock: socket to send on
739 * @addr: UDP only -- address of destination
740 * @addrlen: UDP only -- length of destination address
741 * @xdr: buffer containing this request
742 * @base: starting position in the buffer
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500743 * @zerocopy: true if it is safe to use sendpage()
Jason Baronf279cd02014-09-24 18:08:00 +0000744 * @sent_p: return the total number of bytes successfully queued for sending
Chuck Lever9903cd12005-08-11 16:25:26 -0400745 *
Chuck Levera246b012005-08-11 16:25:23 -0400746 */
Jason Baronf279cd02014-09-24 18:08:00 +0000747static 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 -0400748{
Trond Myklebust24c56842006-10-17 15:06:22 -0400749 unsigned int remainder = xdr->len - base;
Jason Baronf279cd02014-09-24 18:08:00 +0000750 int err = 0;
751 int sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400752
Chuck Lever262965f2005-08-11 16:25:56 -0400753 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400754 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400755
Trond Myklebust24c56842006-10-17 15:06:22 -0400756 if (base != 0) {
757 addr = NULL;
758 addrlen = 0;
759 }
Chuck Lever262965f2005-08-11 16:25:56 -0400760
Trond Myklebust24c56842006-10-17 15:06:22 -0400761 if (base < xdr->head[0].iov_len || addr != NULL) {
762 unsigned int len = xdr->head[0].iov_len - base;
763 remainder -= len;
764 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
765 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400766 goto out;
Jason Baronf279cd02014-09-24 18:08:00 +0000767 *sent_p += err;
Chuck Levera246b012005-08-11 16:25:23 -0400768 base = 0;
769 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400770 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400771
Trond Myklebust24c56842006-10-17 15:06:22 -0400772 if (base < xdr->page_len) {
773 unsigned int len = xdr->page_len - base;
774 remainder -= len;
Jason Baronf279cd02014-09-24 18:08:00 +0000775 err = xs_send_pagedata(sock, xdr, base, remainder != 0, zerocopy, &sent);
776 *sent_p += sent;
777 if (remainder == 0 || sent != len)
Chuck Levera246b012005-08-11 16:25:23 -0400778 goto out;
779 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400780 } else
781 base -= xdr->page_len;
782
783 if (base >= xdr->tail[0].iov_len)
Jason Baronf279cd02014-09-24 18:08:00 +0000784 return 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400785 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400786out:
Jason Baronf279cd02014-09-24 18:08:00 +0000787 if (err > 0) {
788 *sent_p += err;
789 err = 0;
790 }
791 return err;
Chuck Levera246b012005-08-11 16:25:23 -0400792}
793
Chuck Lever9903cd12005-08-11 16:25:26 -0400794/**
Trond Myklebustc5445772018-09-03 23:39:27 -0400795 * xs_nospace - handle transmit was incomplete
Trond Myklebust50f484e2018-08-30 13:27:29 -0400796 * @req: pointer to RPC request
Chuck Lever9903cd12005-08-11 16:25:26 -0400797 *
Chuck Levera246b012005-08-11 16:25:23 -0400798 */
Trond Myklebustc5445772018-09-03 23:39:27 -0400799static int xs_nospace(struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -0400800{
Chuck Lever262965f2005-08-11 16:25:56 -0400801 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500802 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500803 struct sock *sk = transport->inet;
Trond Myklebust24ca9a82011-11-22 14:44:28 +0200804 int ret = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400805
Chuck Lever46121cf2007-01-31 12:14:08 -0500806 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Trond Myklebustc5445772018-09-03 23:39:27 -0400807 req->rq_task->tk_pid,
808 req->rq_slen - transport->xmit.offset,
Chuck Lever262965f2005-08-11 16:25:56 -0400809 req->rq_slen);
810
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400811 /* Protect against races with write_space */
812 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400813
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400814 /* Don't race with disconnect */
815 if (xprt_connected(xprt)) {
Trond Myklebust13331a52016-01-06 08:57:06 -0500816 /* wait for more buffer space */
817 sk->sk_write_pending++;
Trond Myklebustc5445772018-09-03 23:39:27 -0400818 xprt_wait_for_buffer_space(xprt);
Trond Myklebust13331a52016-01-06 08:57:06 -0500819 } else
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400820 ret = -ENOTCONN;
Chuck Lever262965f2005-08-11 16:25:56 -0400821
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400822 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500823
824 /* Race breaker in case memory is freed before above code is called */
David Vrabeld48f9ce2016-09-19 13:58:30 +0100825 if (ret == -EAGAIN) {
826 struct socket_wq *wq;
827
828 rcu_read_lock();
829 wq = rcu_dereference(sk->sk_wq);
830 set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags);
831 rcu_read_unlock();
832
833 sk->sk_write_space(sk);
834 }
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400835 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400836}
837
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400838static void
839xs_stream_prepare_request(struct rpc_rqst *req)
840{
841 req->rq_task->tk_status = xdr_alloc_bvec(&req->rq_rcv_buf, GFP_NOIO);
842}
843
Chuck Lever61677ee2011-05-09 15:22:34 -0400844/*
Trond Myklebust4cd34e72018-08-31 10:00:02 -0400845 * Determine if the previous message in the stream was aborted before it
846 * could complete transmission.
847 */
848static bool
849xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
850{
851 return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
852}
853
854/*
Chuck Lever61677ee2011-05-09 15:22:34 -0400855 * Construct a stream transport record marker in @buf.
856 */
857static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
858{
859 u32 reclen = buf->len - sizeof(rpc_fraghdr);
860 rpc_fraghdr *base = buf->head[0].iov_base;
861 *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
862}
863
Chuck Lever262965f2005-08-11 16:25:56 -0400864/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400865 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
Trond Myklebust50f484e2018-08-30 13:27:29 -0400866 * @req: pointer to RPC request
Chuck Lever176e21e2011-05-09 15:22:44 -0400867 *
868 * Return values:
869 * 0: The request has been sent
870 * EAGAIN: The socket was blocked, please call again later to
871 * complete the request
872 * ENOTCONN: Caller needs to invoke connect logic then call again
873 * other: Some other error occured, the request was not sent
874 */
Trond Myklebustadfa7142018-09-03 23:58:59 -0400875static int xs_local_send_request(struct rpc_rqst *req)
Chuck Lever176e21e2011-05-09 15:22:44 -0400876{
Chuck Lever176e21e2011-05-09 15:22:44 -0400877 struct rpc_xprt *xprt = req->rq_xprt;
878 struct sock_xprt *transport =
879 container_of(xprt, struct sock_xprt, xprt);
880 struct xdr_buf *xdr = &req->rq_snd_buf;
881 int status;
Jason Baronf279cd02014-09-24 18:08:00 +0000882 int sent = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -0400883
Trond Myklebust4cd34e72018-08-31 10:00:02 -0400884 /* Close the stream if the previous transmission was incomplete */
885 if (xs_send_request_was_aborted(transport, req)) {
886 xs_close(xprt);
887 return -ENOTCONN;
888 }
889
Chuck Lever176e21e2011-05-09 15:22:44 -0400890 xs_encode_stream_record_marker(&req->rq_snd_buf);
891
892 xs_pktdump("packet data:",
893 req->rq_svec->iov_base, req->rq_svec->iov_len);
894
Chuck Lever78215752018-03-05 15:13:07 -0500895 req->rq_xtime = ktime_get();
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400896 status = xs_sendpages(transport->sock, NULL, 0, xdr,
897 transport->xmit.offset,
Jason Baronf279cd02014-09-24 18:08:00 +0000898 true, &sent);
Chuck Lever176e21e2011-05-09 15:22:44 -0400899 dprintk("RPC: %s(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400900 __func__, xdr->len - transport->xmit.offset, status);
NeilBrown743c69e2015-07-27 10:55:35 +1000901
902 if (status == -EAGAIN && sock_writeable(transport->inet))
903 status = -ENOBUFS;
904
Jason Baronf279cd02014-09-24 18:08:00 +0000905 if (likely(sent > 0) || status == 0) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400906 transport->xmit.offset += sent;
907 req->rq_bytes_sent = transport->xmit.offset;
Chuck Lever176e21e2011-05-09 15:22:44 -0400908 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400909 req->rq_xmit_bytes_sent += transport->xmit.offset;
Chuck Lever176e21e2011-05-09 15:22:44 -0400910 req->rq_bytes_sent = 0;
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400911 transport->xmit.offset = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -0400912 return 0;
913 }
914 status = -EAGAIN;
915 }
916
917 switch (status) {
Trond Myklebust3601c4a2014-06-30 13:42:19 -0400918 case -ENOBUFS:
Trond Myklebustb5872f02015-07-03 09:32:23 -0400919 break;
Chuck Lever176e21e2011-05-09 15:22:44 -0400920 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -0400921 status = xs_nospace(req);
Chuck Lever176e21e2011-05-09 15:22:44 -0400922 break;
923 default:
924 dprintk("RPC: sendmsg returned unrecognized error %d\n",
925 -status);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -0500926 /* fall through */
Chuck Lever176e21e2011-05-09 15:22:44 -0400927 case -EPIPE:
928 xs_close(xprt);
929 status = -ENOTCONN;
930 }
931
932 return status;
933}
934
935/**
Chuck Lever262965f2005-08-11 16:25:56 -0400936 * xs_udp_send_request - write an RPC request to a UDP socket
Trond Myklebust50f484e2018-08-30 13:27:29 -0400937 * @req: pointer to RPC request
Chuck Lever262965f2005-08-11 16:25:56 -0400938 *
939 * Return values:
940 * 0: The request has been sent
941 * EAGAIN: The socket was blocked, please call again later to
942 * complete the request
943 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300944 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -0400945 */
Trond Myklebustadfa7142018-09-03 23:58:59 -0400946static int xs_udp_send_request(struct rpc_rqst *req)
Chuck Lever262965f2005-08-11 16:25:56 -0400947{
Chuck Lever262965f2005-08-11 16:25:56 -0400948 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500949 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400950 struct xdr_buf *xdr = &req->rq_snd_buf;
Jason Baronf279cd02014-09-24 18:08:00 +0000951 int sent = 0;
Chuck Lever262965f2005-08-11 16:25:56 -0400952 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400953
Chuck Lever9903cd12005-08-11 16:25:26 -0400954 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400955 req->rq_svec->iov_base,
956 req->rq_svec->iov_len);
957
Trond Myklebust01d37c42009-03-11 14:09:39 -0400958 if (!xprt_bound(xprt))
959 return -ENOTCONN;
Trond Myklebust75891f52018-09-03 17:37:36 -0400960
961 if (!xprt_request_get_cong(xprt, req))
962 return -EBADSLT;
963
Chuck Lever78215752018-03-05 15:13:07 -0500964 req->rq_xtime = ktime_get();
Jason Baronf279cd02014-09-24 18:08:00 +0000965 status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen,
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400966 xdr, 0, true, &sent);
Chuck Levera246b012005-08-11 16:25:23 -0400967
Chuck Lever46121cf2007-01-31 12:14:08 -0500968 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400969 xdr->len, status);
Chuck Levera246b012005-08-11 16:25:23 -0400970
Jason Baron3dedbb52014-09-24 18:08:04 +0000971 /* firewall is blocking us, don't return -EAGAIN or we end up looping */
972 if (status == -EPERM)
973 goto process_status;
974
NeilBrown743c69e2015-07-27 10:55:35 +1000975 if (status == -EAGAIN && sock_writeable(transport->inet))
976 status = -ENOBUFS;
977
Jason Baronf279cd02014-09-24 18:08:00 +0000978 if (sent > 0 || status == 0) {
979 req->rq_xmit_bytes_sent += sent;
980 if (sent >= req->rq_slen)
Trond Myklebust21997002007-10-01 11:43:37 -0400981 return 0;
982 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400983 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400984 }
Chuck Levera246b012005-08-11 16:25:23 -0400985
Jason Baron3dedbb52014-09-24 18:08:04 +0000986process_status:
Chuck Lever262965f2005-08-11 16:25:56 -0400987 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400988 case -ENOTSOCK:
989 status = -ENOTCONN;
990 /* Should we call xs_close() here? */
991 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400992 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -0400993 status = xs_nospace(req);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400994 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400995 case -ENETUNREACH:
Trond Myklebust3601c4a2014-06-30 13:42:19 -0400996 case -ENOBUFS:
Chuck Lever262965f2005-08-11 16:25:56 -0400997 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400998 case -ECONNREFUSED:
Jason Baron3dedbb52014-09-24 18:08:04 +0000999 case -EPERM:
Chuck Levera246b012005-08-11 16:25:23 -04001000 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -04001001 * prompts ECONNREFUSED. */
Trond Myklebust13331a52016-01-06 08:57:06 -05001002 break;
1003 default:
1004 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1005 -status);
Chuck Levera246b012005-08-11 16:25:23 -04001006 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +08001007
Chuck Lever262965f2005-08-11 16:25:56 -04001008 return status;
Chuck Levera246b012005-08-11 16:25:23 -04001009}
1010
Trond Myklebuste06799f2007-11-05 15:44:12 -05001011/**
Chuck Lever262965f2005-08-11 16:25:56 -04001012 * xs_tcp_send_request - write an RPC request to a TCP socket
Trond Myklebust50f484e2018-08-30 13:27:29 -04001013 * @req: pointer to RPC request
Chuck Lever9903cd12005-08-11 16:25:26 -04001014 *
1015 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -04001016 * 0: The request has been sent
1017 * EAGAIN: The socket was blocked, please call again later to
1018 * complete the request
1019 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001020 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -04001021 *
1022 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -04001023 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -04001024 */
Trond Myklebustadfa7142018-09-03 23:58:59 -04001025static int xs_tcp_send_request(struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001026{
Chuck Levera246b012005-08-11 16:25:23 -04001027 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001028 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -04001029 struct xdr_buf *xdr = &req->rq_snd_buf;
Trond Myklebusta6b31d12013-11-08 16:03:50 -05001030 bool zerocopy = true;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001031 bool vm_wait = false;
Chuck Leverb595bb12007-08-06 11:56:42 -04001032 int status;
Jason Baronf279cd02014-09-24 18:08:00 +00001033 int sent;
Chuck Levera246b012005-08-11 16:25:23 -04001034
Trond Myklebust4cd34e72018-08-31 10:00:02 -04001035 /* Close the stream if the previous transmission was incomplete */
1036 if (xs_send_request_was_aborted(transport, req)) {
1037 if (transport->sock != NULL)
1038 kernel_sock_shutdown(transport->sock, SHUT_RDWR);
1039 return -ENOTCONN;
1040 }
1041
Chuck Lever61677ee2011-05-09 15:22:34 -04001042 xs_encode_stream_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -04001043
Chuck Lever262965f2005-08-11 16:25:56 -04001044 xs_pktdump("packet data:",
1045 req->rq_svec->iov_base,
1046 req->rq_svec->iov_len);
Trond Myklebusta6b31d12013-11-08 16:03:50 -05001047 /* Don't use zero copy if this is a resend. If the RPC call
1048 * completes while the socket holds a reference to the pages,
1049 * then we may end up resending corrupted data.
1050 */
Trond Myklebust50f484e2018-08-30 13:27:29 -04001051 if (req->rq_task->tk_flags & RPC_TASK_SENT)
Trond Myklebusta6b31d12013-11-08 16:03:50 -05001052 zerocopy = false;
Chuck Levera246b012005-08-11 16:25:23 -04001053
Trond Myklebust7196dbb2017-02-08 11:17:54 -05001054 if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
1055 xs_tcp_set_socket_timeouts(xprt, transport->sock);
1056
Chuck Levera246b012005-08-11 16:25:23 -04001057 /* Continue transmitting the packet/record. We must be careful
1058 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -04001059 * called sendmsg(). */
Chuck Lever78215752018-03-05 15:13:07 -05001060 req->rq_xtime = ktime_get();
Chuck Levera246b012005-08-11 16:25:23 -04001061 while (1) {
Jason Baronf279cd02014-09-24 18:08:00 +00001062 sent = 0;
1063 status = xs_sendpages(transport->sock, NULL, 0, xdr,
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001064 transport->xmit.offset,
1065 zerocopy, &sent);
Chuck Levera246b012005-08-11 16:25:23 -04001066
Chuck Lever46121cf2007-01-31 12:14:08 -05001067 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001068 xdr->len - transport->xmit.offset, status);
Chuck Lever262965f2005-08-11 16:25:56 -04001069
Chuck Lever262965f2005-08-11 16:25:56 -04001070 /* If we've sent the entire packet, immediately
1071 * reset the count of bytes sent. */
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001072 transport->xmit.offset += sent;
1073 req->rq_bytes_sent = transport->xmit.offset;
Chuck Lever262965f2005-08-11 16:25:56 -04001074 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001075 req->rq_xmit_bytes_sent += transport->xmit.offset;
Chuck Lever262965f2005-08-11 16:25:56 -04001076 req->rq_bytes_sent = 0;
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001077 transport->xmit.offset = 0;
Chuck Lever262965f2005-08-11 16:25:56 -04001078 return 0;
Chuck Levera246b012005-08-11 16:25:23 -04001079 }
1080
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001081 WARN_ON_ONCE(sent == 0 && status == 0);
1082
1083 if (status == -EAGAIN ) {
1084 /*
1085 * Return EAGAIN if we're sure we're hitting the
1086 * socket send buffer limits.
1087 */
1088 if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
1089 break;
1090 /*
1091 * Did we hit a memory allocation failure?
1092 */
1093 if (sent == 0) {
1094 status = -ENOBUFS;
1095 if (vm_wait)
1096 break;
1097 /* Retry, knowing now that we're below the
1098 * socket send buffer limit
1099 */
1100 vm_wait = true;
1101 }
1102 continue;
1103 }
Trond Myklebustf580dd02015-07-11 17:48:52 +02001104 if (status < 0)
1105 break;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001106 vm_wait = false;
Chuck Levera246b012005-08-11 16:25:23 -04001107 }
1108
Chuck Lever262965f2005-08-11 16:25:56 -04001109 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -04001110 case -ENOTSOCK:
1111 status = -ENOTCONN;
1112 /* Should we call xs_close() here? */
1113 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001114 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -04001115 status = xs_nospace(req);
Chuck Lever262965f2005-08-11 16:25:56 -04001116 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001117 case -ECONNRESET:
Chuck Lever262965f2005-08-11 16:25:56 -04001118 case -ECONNREFUSED:
1119 case -ENOTCONN:
Trond Myklebust3913c782015-02-08 21:44:04 -05001120 case -EADDRINUSE:
Trond Myklebustb5872f02015-07-03 09:32:23 -04001121 case -ENOBUFS:
Trond Myklebustb9d2bb22012-10-23 11:40:02 -04001122 case -EPIPE:
Trond Myklebust13331a52016-01-06 08:57:06 -05001123 break;
1124 default:
1125 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1126 -status);
Chuck Levera246b012005-08-11 16:25:23 -04001127 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +08001128
Chuck Levera246b012005-08-11 16:25:23 -04001129 return status;
1130}
1131
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001132static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1133{
1134 transport->old_data_ready = sk->sk_data_ready;
1135 transport->old_state_change = sk->sk_state_change;
1136 transport->old_write_space = sk->sk_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -05001137 transport->old_error_report = sk->sk_error_report;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001138}
1139
1140static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1141{
1142 sk->sk_data_ready = transport->old_data_ready;
1143 sk->sk_state_change = transport->old_state_change;
1144 sk->sk_write_space = transport->old_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -05001145 sk->sk_error_report = transport->old_error_report;
1146}
1147
Trond Myklebust42d42a52016-05-23 09:24:55 -04001148static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
1149{
1150 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1151
1152 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
1153}
1154
Trond Myklebustb70ae912015-02-09 09:41:32 -05001155static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1156{
1157 smp_mb__before_atomic();
1158 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1159 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001160 xs_sock_reset_state_flags(xprt);
Trond Myklebustb70ae912015-02-09 09:41:32 -05001161 smp_mb__after_atomic();
1162}
1163
Trond Myklebust21180712013-12-31 13:22:59 -05001164/**
1165 * xs_error_report - callback to handle TCP socket state errors
1166 * @sk: socket
1167 *
1168 * Note: we don't call sock_error() since there may be a rpc_task
1169 * using the socket, and so we don't want to clear sk->sk_err.
1170 */
1171static void xs_error_report(struct sock *sk)
1172{
1173 struct rpc_xprt *xprt;
1174 int err;
1175
1176 read_lock_bh(&sk->sk_callback_lock);
1177 if (!(xprt = xprt_from_sock(sk)))
1178 goto out;
1179
1180 err = -sk->sk_err;
1181 if (err == 0)
1182 goto out;
1183 dprintk("RPC: xs_error_report client %p, error=%d...\n",
1184 xprt, -err);
Trond Myklebuste8353c762013-12-31 13:39:22 -05001185 trace_rpc_socket_error(xprt, sk->sk_socket, err);
Trond Myklebust21180712013-12-31 13:22:59 -05001186 xprt_wake_pending_tasks(xprt, err);
1187 out:
1188 read_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001189}
1190
Chuck Leverfe315e762009-03-11 14:10:21 -04001191static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001192{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001193 struct socket *sock = transport->sock;
1194 struct sock *sk = transport->inet;
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001195 struct rpc_xprt *xprt = &transport->xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001196
Chuck Leverfe315e762009-03-11 14:10:21 -04001197 if (sk == NULL)
1198 return;
Chuck Lever9903cd12005-08-11 16:25:26 -04001199
Jeff Layton264d1df2015-06-03 16:14:27 -04001200 if (atomic_read(&transport->xprt.swapper))
1201 sk_clear_memalloc(sk);
1202
Trond Myklebust09939202015-08-29 19:11:21 -07001203 kernel_sock_shutdown(sock, SHUT_RDWR);
1204
Trond Myklebustedc1b012015-10-05 10:53:49 -04001205 mutex_lock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -04001206 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001207 transport->inet = NULL;
1208 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -04001209
Chuck Lever9903cd12005-08-11 16:25:26 -04001210 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001211
1212 xs_restore_old_callbacks(transport, sk);
Trond Myklebust0c787892015-08-29 13:36:30 -07001213 xprt_clear_connected(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001214 write_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001215 xs_sock_reset_connection_flags(xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04001216 mutex_unlock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -04001217
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001218 trace_rpc_socket_close(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001219 sock_release(sock);
Chuck Leverfe315e762009-03-11 14:10:21 -04001220}
1221
1222/**
1223 * xs_close - close a socket
1224 * @xprt: transport
1225 *
1226 * This is used when all requests are complete; ie, no DRC state remains
1227 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -04001228 *
1229 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1230 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e762009-03-11 14:10:21 -04001231 */
1232static void xs_close(struct rpc_xprt *xprt)
1233{
1234 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1235
1236 dprintk("RPC: xs_close xprt %p\n", xprt);
1237
1238 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -04001239 xprt->reestablish_timeout = 0;
Chuck Leverfe315e762009-03-11 14:10:21 -04001240
Trond Myklebust62da3b22007-11-06 18:44:20 -05001241 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001242}
1243
Chuck Lever4a068252015-05-11 14:02:25 -04001244static void xs_inject_disconnect(struct rpc_xprt *xprt)
1245{
1246 dprintk("RPC: injecting transport disconnect on xprt=%p\n",
1247 xprt);
1248 xprt_disconnect_done(xprt);
1249}
1250
Kinglong Mee315f3812014-03-24 11:07:22 +08001251static void xs_xprt_free(struct rpc_xprt *xprt)
1252{
1253 xs_free_peer_addresses(xprt);
1254 xprt_free(xprt);
1255}
1256
Chuck Lever9903cd12005-08-11 16:25:26 -04001257/**
1258 * xs_destroy - prepare to shutdown a transport
1259 * @xprt: doomed transport
1260 *
1261 */
1262static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001263{
Trond Myklebust03c788272015-09-17 10:42:27 -04001264 struct sock_xprt *transport = container_of(xprt,
1265 struct sock_xprt, xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05001266 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001267
Trond Myklebust03c788272015-09-17 10:42:27 -04001268 cancel_delayed_work_sync(&transport->connect_worker);
Trond Myklebusta1311d82013-10-31 09:18:49 -04001269 xs_close(xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04001270 cancel_work_sync(&transport->recv_worker);
Kinglong Mee315f3812014-03-24 11:07:22 +08001271 xs_xprt_free(xprt);
Trond Myklebusta1311d82013-10-31 09:18:49 -04001272 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -04001273}
1274
Chuck Lever9903cd12005-08-11 16:25:26 -04001275/**
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001276 * xs_udp_data_read_skb - receive callback for UDP sockets
1277 * @xprt: transport
1278 * @sk: socket
1279 * @skb: skbuff
Chuck Lever9903cd12005-08-11 16:25:26 -04001280 *
1281 */
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001282static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1283 struct sock *sk,
1284 struct sk_buff *skb)
Chuck Lever9903cd12005-08-11 16:25:26 -04001285{
1286 struct rpc_task *task;
Chuck Levera246b012005-08-11 16:25:23 -04001287 struct rpc_rqst *rovr;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001288 int repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001289 u32 _xid;
1290 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -04001291
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001292 repsize = skb->len;
Chuck Levera246b012005-08-11 16:25:23 -04001293 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001294 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001295 return;
Chuck Levera246b012005-08-11 16:25:23 -04001296 }
1297
1298 /* Copy the XID from the skb... */
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001299 xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
Chuck Levera246b012005-08-11 16:25:23 -04001300 if (xp == NULL)
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001301 return;
Chuck Levera246b012005-08-11 16:25:23 -04001302
1303 /* Look up and lock the request corresponding to the given XID */
Trond Myklebust75c84152018-08-31 10:21:00 -04001304 spin_lock(&xprt->queue_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001305 rovr = xprt_lookup_rqst(xprt, *xp);
1306 if (!rovr)
1307 goto out_unlock;
Trond Myklebust729749b2017-08-13 10:03:59 -04001308 xprt_pin_rqst(rovr);
Chuck Leverecd465e2018-03-05 15:12:57 -05001309 xprt_update_rtt(rovr->rq_task);
Trond Myklebust75c84152018-08-31 10:21:00 -04001310 spin_unlock(&xprt->queue_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001311 task = rovr->rq_task;
1312
Chuck Levera246b012005-08-11 16:25:23 -04001313 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1314 copied = repsize;
1315
1316 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001317 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
Trond Myklebust75c84152018-08-31 10:21:00 -04001318 spin_lock(&xprt->queue_lock);
Trond Myklebust0afa6b42018-02-09 09:39:42 -05001319 __UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
Trond Myklebust729749b2017-08-13 10:03:59 -04001320 goto out_unpin;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001321 }
1322
Chuck Levera246b012005-08-11 16:25:23 -04001323
Trond Myklebust729749b2017-08-13 10:03:59 -04001324 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001325 xprt_adjust_cwnd(xprt, task, copied);
Trond Myklebustce7c2522017-08-16 15:30:35 -04001326 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust75c84152018-08-31 10:21:00 -04001327 spin_lock(&xprt->queue_lock);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001328 xprt_complete_rqst(task, copied);
Trond Myklebust0afa6b42018-02-09 09:39:42 -05001329 __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
Trond Myklebust729749b2017-08-13 10:03:59 -04001330out_unpin:
1331 xprt_unpin_rqst(rovr);
Chuck Levera246b012005-08-11 16:25:23 -04001332 out_unlock:
Trond Myklebust75c84152018-08-31 10:21:00 -04001333 spin_unlock(&xprt->queue_lock);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001334}
1335
1336static void xs_udp_data_receive(struct sock_xprt *transport)
1337{
1338 struct sk_buff *skb;
1339 struct sock *sk;
1340 int err;
1341
1342 mutex_lock(&transport->recv_mutex);
Trond Myklebustdfcf0382018-12-04 07:50:06 -05001343 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001344 sk = transport->inet;
1345 if (sk == NULL)
1346 goto out;
1347 for (;;) {
Paolo Abeni7c13f972016-11-04 11:28:59 +01001348 skb = skb_recv_udp(sk, 0, 1, &err);
Trond Myklebust4f546142018-09-14 17:45:23 -04001349 if (skb == NULL)
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001350 break;
Trond Myklebust4f546142018-09-14 17:45:23 -04001351 xs_udp_data_read_skb(&transport->xprt, sk, skb);
1352 consume_skb(skb);
1353 cond_resched();
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001354 }
1355out:
1356 mutex_unlock(&transport->recv_mutex);
1357}
1358
1359static void xs_udp_data_receive_workfn(struct work_struct *work)
1360{
1361 struct sock_xprt *transport =
1362 container_of(work, struct sock_xprt, recv_worker);
1363 xs_udp_data_receive(transport);
1364}
1365
1366/**
1367 * xs_data_ready - "data ready" callback for UDP sockets
1368 * @sk: socket with data to read
1369 *
1370 */
1371static void xs_data_ready(struct sock *sk)
1372{
1373 struct rpc_xprt *xprt;
1374
1375 read_lock_bh(&sk->sk_callback_lock);
1376 dprintk("RPC: xs_data_ready...\n");
1377 xprt = xprt_from_sock(sk);
1378 if (xprt != NULL) {
1379 struct sock_xprt *transport = container_of(xprt,
1380 struct sock_xprt, xprt);
Trond Myklebust5157b952016-05-29 10:13:24 -04001381 transport->old_data_ready(sk);
1382 /* Any data means we had a useful conversation, so
1383 * then we don't need to delay the next reconnect
1384 */
1385 if (xprt->reestablish_timeout)
1386 xprt->reestablish_timeout = 0;
Trond Myklebust42d42a52016-05-23 09:24:55 -04001387 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebust40a5f1b2016-05-27 10:39:50 -04001388 queue_work(xprtiod_workqueue, &transport->recv_worker);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001389 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001390 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001391}
1392
Trond Myklebusta519fc72012-09-12 16:49:15 -04001393/*
1394 * Helper function to force a TCP close if the server is sending
1395 * junk and/or it has put us in CLOSE_WAIT
1396 */
1397static void xs_tcp_force_close(struct rpc_xprt *xprt)
1398{
Trond Myklebusta519fc72012-09-12 16:49:15 -04001399 xprt_force_disconnect(xprt);
1400}
1401
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001402#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Chuck Lever76566772015-10-24 17:28:32 -04001403static int xs_tcp_bc_up(struct svc_serv *serv, struct net *net)
1404{
1405 int ret;
1406
1407 ret = svc_create_xprt(serv, "tcp-bc", net, PF_INET, 0,
1408 SVC_SOCK_ANONYMOUS);
1409 if (ret < 0)
1410 return ret;
1411 return 0;
1412}
Chuck Lever6b26cc82016-05-02 14:40:40 -04001413
1414static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
1415{
1416 return PAGE_SIZE;
1417}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001418#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001419
Chuck Lever9903cd12005-08-11 16:25:26 -04001420/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001421 * xs_tcp_state_change - callback to handle TCP socket state changes
1422 * @sk: socket whose state has changed
1423 *
1424 */
1425static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001426{
Chuck Lever9903cd12005-08-11 16:25:26 -04001427 struct rpc_xprt *xprt;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001428 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001429
Eric Dumazetf064af12010-09-22 12:43:39 +00001430 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001431 if (!(xprt = xprt_from_sock(sk)))
1432 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001433 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001434 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001435 sk->sk_state, xprt_connected(xprt),
1436 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001437 sock_flag(sk, SOCK_ZAPPED),
1438 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001439
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001440 transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001441 trace_rpc_socket_state_change(xprt, sk->sk_socket);
Chuck Levera246b012005-08-11 16:25:23 -04001442 switch (sk->sk_state) {
1443 case TCP_ESTABLISHED:
Eric Dumazetf064af12010-09-22 12:43:39 +00001444 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001445 if (!xprt_test_and_set_connected(xprt)) {
Trond Myklebust8b717982013-09-26 10:18:04 -04001446 xprt->connect_cookie++;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001447 clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
1448 xprt_clear_connecting(xprt);
Chuck Lever51971132006-12-05 16:35:19 -05001449
Chuck Lever3968a8a2018-10-01 14:25:36 -04001450 xprt->stat.connect_count++;
1451 xprt->stat.connect_time += (long)jiffies -
1452 xprt->stat.connect_start;
Trond Myklebust2a491992009-03-11 14:38:00 -04001453 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001454 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001455 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001456 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001457 case TCP_FIN_WAIT1:
1458 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001459 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001460 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001461 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001462 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001463 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001464 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001465 smp_mb__after_atomic();
Chuck Levera246b012005-08-11 16:25:23 -04001466 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001467 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001468 /* The server initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001469 xprt->connect_cookie++;
Trond Myklebustd0bea452012-10-23 11:35:47 -04001470 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebusta519fc72012-09-12 16:49:15 -04001471 xs_tcp_force_close(xprt);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001472 /* fall through */
Trond Myklebust663b8852008-01-01 18:42:12 -05001473 case TCP_CLOSING:
1474 /*
1475 * If the server closed down the connection, make sure that
1476 * we back off before reconnecting
1477 */
1478 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1479 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001480 break;
1481 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001482 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001483 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001484 clear_bit(XPRT_CONNECTED, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001485 smp_mb__after_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001486 break;
1487 case TCP_CLOSE:
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001488 if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
1489 &transport->sock_state))
1490 xprt_clear_connecting(xprt);
Trond Myklebust9b308892018-02-05 10:20:06 -05001491 clear_bit(XPRT_CLOSING, &xprt->state);
NeilBrown3ffbc1d2017-07-19 14:05:01 +10001492 if (sk->sk_err)
1493 xprt_wake_pending_tasks(xprt, -sk->sk_err);
Trond Myklebust9b308892018-02-05 10:20:06 -05001494 /* Trigger the socket release */
1495 xs_tcp_force_close(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001496 }
1497 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001498 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001499}
1500
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001501static void xs_write_space(struct sock *sk)
1502{
Trond Myklebust13331a52016-01-06 08:57:06 -05001503 struct socket_wq *wq;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001504 struct rpc_xprt *xprt;
1505
Trond Myklebust13331a52016-01-06 08:57:06 -05001506 if (!sk->sk_socket)
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001507 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001508 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001509
1510 if (unlikely(!(xprt = xprt_from_sock(sk))))
1511 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001512 rcu_read_lock();
1513 wq = rcu_dereference(sk->sk_wq);
1514 if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0)
1515 goto out;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001516
Trond Myklebustc5445772018-09-03 23:39:27 -04001517 if (xprt_write_space(xprt))
1518 sk->sk_write_pending--;
Trond Myklebust13331a52016-01-06 08:57:06 -05001519out:
1520 rcu_read_unlock();
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001521}
1522
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001523/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001524 * xs_udp_write_space - callback invoked when socket buffer space
1525 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001526 * @sk: socket whose state has changed
1527 *
Chuck Levera246b012005-08-11 16:25:23 -04001528 * Called when more output buffer space is available for this socket.
1529 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001530 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001531 * with a bunch of small requests.
1532 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001533static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001534{
Eric Dumazetf064af12010-09-22 12:43:39 +00001535 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001536
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001537 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001538 if (sock_writeable(sk))
1539 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001540
Eric Dumazetf064af12010-09-22 12:43:39 +00001541 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001542}
Chuck Levera246b012005-08-11 16:25:23 -04001543
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001544/**
1545 * xs_tcp_write_space - callback invoked when socket buffer space
1546 * becomes available
1547 * @sk: socket whose state has changed
1548 *
1549 * Called when more output buffer space is available for this socket.
1550 * We try not to wake our writers until they can make "significant"
1551 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1552 * with a bunch of small requests.
1553 */
1554static void xs_tcp_write_space(struct sock *sk)
1555{
Eric Dumazetf064af12010-09-22 12:43:39 +00001556 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001557
1558 /* from net/core/stream.c:sk_stream_write_space */
Eric Dumazet64dc6132013-07-22 20:26:31 -07001559 if (sk_stream_is_writeable(sk))
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001560 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001561
Eric Dumazetf064af12010-09-22 12:43:39 +00001562 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001563}
1564
Chuck Lever470056c2005-08-25 16:25:56 -07001565static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001566{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001567 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1568 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001569
Chuck Lever7c6e0662006-12-05 16:35:30 -05001570 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001571 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001572 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001573 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001574 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001575 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001576 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001577 sk->sk_write_space(sk);
1578 }
1579}
1580
Chuck Lever43118c22005-08-25 16:25:49 -07001581/**
Chuck Lever470056c2005-08-25 16:25:56 -07001582 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001583 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001584 * @sndsize: requested size of send buffer, in bytes
1585 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001586 *
Chuck Lever470056c2005-08-25 16:25:56 -07001587 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001588 */
Chuck Lever470056c2005-08-25 16:25:56 -07001589static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001590{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001591 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1592
1593 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001594 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001595 transport->sndsize = sndsize + 1024;
1596 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001597 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001598 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001599
1600 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001601}
1602
Chuck Lever46c0ee82005-08-25 16:25:52 -07001603/**
1604 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1605 * @task: task that timed out
1606 *
1607 * Adjust the congestion window after a retransmit timeout has occurred.
1608 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001609static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever46c0ee82005-08-25 16:25:52 -07001610{
Chuck Leverb977b642017-02-08 17:00:51 -05001611 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001612 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
Chuck Leverb977b642017-02-08 17:00:51 -05001613 spin_unlock_bh(&xprt->transport_lock);
Chuck Lever46c0ee82005-08-25 16:25:52 -07001614}
1615
J. Bruce Fields826799e2018-10-18 15:27:02 -04001616static int xs_get_random_port(void)
Chuck Leverb85d8802006-05-25 01:40:49 -04001617{
J. Bruce Fields826799e2018-10-18 15:27:02 -04001618 unsigned short min = xprt_min_resvport, max = xprt_max_resvport;
1619 unsigned short range;
1620 unsigned short rand;
1621
1622 if (max < min)
1623 return -EADDRINUSE;
1624 range = max - min + 1;
1625 rand = (unsigned short) prandom_u32() % range;
1626 return rand + min;
Chuck Leverb85d8802006-05-25 01:40:49 -04001627}
1628
Chuck Lever92200412006-01-03 09:55:51 +01001629/**
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001630 * xs_set_reuseaddr_port - set the socket's port and address reuse options
1631 * @sock: socket
1632 *
1633 * Note that this function has to be called on all sockets that share the
1634 * same port, and it must be called before binding.
1635 */
1636static void xs_sock_set_reuseport(struct socket *sock)
1637{
Trond Myklebust402e23b2015-02-09 17:20:14 -05001638 int opt = 1;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001639
Trond Myklebust402e23b2015-02-09 17:20:14 -05001640 kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
1641 (char *)&opt, sizeof(opt));
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001642}
1643
1644static unsigned short xs_sock_getport(struct socket *sock)
1645{
1646 struct sockaddr_storage buf;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001647 unsigned short port = 0;
1648
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001649 if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001650 goto out;
1651 switch (buf.ss_family) {
1652 case AF_INET6:
1653 port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
1654 break;
1655 case AF_INET:
1656 port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
1657 }
1658out:
1659 return port;
1660}
1661
1662/**
Chuck Lever92200412006-01-03 09:55:51 +01001663 * xs_set_port - reset the port number in the remote endpoint address
1664 * @xprt: generic transport
1665 * @port: new port number
1666 *
1667 */
1668static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1669{
Chuck Lever46121cf2007-01-31 12:14:08 -05001670 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001671
Chuck Lever9dc3b092009-08-09 15:09:46 -04001672 rpc_set_port(xs_addr(xprt), port);
1673 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001674}
1675
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001676static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
1677{
1678 if (transport->srcport == 0)
1679 transport->srcport = xs_sock_getport(sock);
1680}
1681
J. Bruce Fields826799e2018-10-18 15:27:02 -04001682static int xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001683{
J. Bruce Fields826799e2018-10-18 15:27:02 -04001684 int port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001685
1686 if (port == 0 && transport->xprt.resvport)
1687 port = xs_get_random_port();
1688 return port;
1689}
1690
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001691static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001692{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001693 if (transport->srcport != 0)
1694 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001695 if (!transport->xprt.resvport)
1696 return 0;
1697 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1698 return xprt_max_resvport;
1699 return --port;
1700}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001701static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001702{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001703 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001704 int err, nloop = 0;
J. Bruce Fields826799e2018-10-18 15:27:02 -04001705 int port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001706 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001707
Chris Perl0f7a6222014-09-05 15:40:21 -04001708 /*
1709 * If we are asking for any ephemeral port (i.e. port == 0 &&
1710 * transport->xprt.resvport == 0), don't bind. Let the local
1711 * port selection happen implicitly when the socket is used
1712 * (for example at connect time).
1713 *
1714 * This ensures that we can continue to establish TCP
1715 * connections even when all local ephemeral ports are already
1716 * a part of some TCP connection. This makes no difference
1717 * for UDP sockets, but also doens't harm them.
1718 *
1719 * If we're asking for any reserved port (i.e. port == 0 &&
1720 * transport->xprt.resvport == 1) xs_get_srcport above will
1721 * ensure that port is non-zero and we will bind as needed.
1722 */
J. Bruce Fields826799e2018-10-18 15:27:02 -04001723 if (port <= 0)
1724 return port;
Chris Perl0f7a6222014-09-05 15:40:21 -04001725
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001726 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001727 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001728 rpc_set_port((struct sockaddr *)&myaddr, port);
1729 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1730 transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001731 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001732 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001733 break;
Chuck Levera246b012005-08-11 16:25:23 -04001734 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001735 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001736 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001737 if (port > last)
1738 nloop++;
1739 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001740
Chuck Lever4232e862010-10-20 11:52:51 -04001741 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001742 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1743 &((struct sockaddr_in *)&myaddr)->sin_addr,
1744 port, err ? "failed" : "ok", err);
1745 else
1746 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1747 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1748 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001749 return err;
1750}
1751
Chuck Lever176e21e2011-05-09 15:22:44 -04001752/*
1753 * We don't support autobind on AF_LOCAL sockets
1754 */
1755static void xs_local_rpcbind(struct rpc_task *task)
1756{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001757 xprt_set_bound(task->tk_xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04001758}
1759
1760static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1761{
1762}
Chuck Levera246b012005-08-11 16:25:23 -04001763
Peter Zijlstraed075362006-12-06 20:35:24 -08001764#ifdef CONFIG_DEBUG_LOCK_ALLOC
1765static struct lock_class_key xs_key[2];
1766static struct lock_class_key xs_slock_key[2];
1767
Chuck Lever176e21e2011-05-09 15:22:44 -04001768static inline void xs_reclassify_socketu(struct socket *sock)
1769{
1770 struct sock *sk = sock->sk;
1771
Chuck Lever176e21e2011-05-09 15:22:44 -04001772 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1773 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1774}
1775
Chuck Lever8945ee52007-08-06 11:58:04 -04001776static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001777{
1778 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001779
Chuck Lever8945ee52007-08-06 11:58:04 -04001780 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1781 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1782}
Peter Zijlstraed075362006-12-06 20:35:24 -08001783
Chuck Lever8945ee52007-08-06 11:58:04 -04001784static inline void xs_reclassify_socket6(struct socket *sock)
1785{
1786 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001787
Chuck Lever8945ee52007-08-06 11:58:04 -04001788 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1789 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001790}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001791
1792static inline void xs_reclassify_socket(int family, struct socket *sock)
1793{
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02001794 if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -04001795 return;
1796
Chuck Lever4232e862010-10-20 11:52:51 -04001797 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001798 case AF_LOCAL:
1799 xs_reclassify_socketu(sock);
1800 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001801 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001802 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001803 break;
1804 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001805 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001806 break;
1807 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001808}
Peter Zijlstraed075362006-12-06 20:35:24 -08001809#else
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001810static inline void xs_reclassify_socket(int family, struct socket *sock)
1811{
1812}
Peter Zijlstraed075362006-12-06 20:35:24 -08001813#endif
1814
NeilBrown93dc41b2013-10-31 16:14:36 +11001815static void xs_dummy_setup_socket(struct work_struct *work)
1816{
1817}
1818
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001819static struct socket *xs_create_sock(struct rpc_xprt *xprt,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001820 struct sock_xprt *transport, int family, int type,
1821 int protocol, bool reuseport)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001822{
1823 struct socket *sock;
1824 int err;
1825
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001826 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001827 if (err < 0) {
1828 dprintk("RPC: can't create %d transport socket (%d).\n",
1829 protocol, -err);
1830 goto out;
1831 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001832 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001833
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001834 if (reuseport)
1835 xs_sock_set_reuseport(sock);
1836
Ben Hutchings4cea2882011-02-22 21:54:34 +00001837 err = xs_bind(transport, sock);
1838 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001839 sock_release(sock);
1840 goto out;
1841 }
1842
1843 return sock;
1844out:
1845 return ERR_PTR(err);
1846}
1847
Chuck Lever176e21e2011-05-09 15:22:44 -04001848static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1849 struct socket *sock)
1850{
1851 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1852 xprt);
1853
1854 if (!transport->inet) {
1855 struct sock *sk = sock->sk;
1856
1857 write_lock_bh(&sk->sk_callback_lock);
1858
1859 xs_save_old_callbacks(transport, sk);
1860
1861 sk->sk_user_data = xprt;
Trond Myklebusta2648092015-10-06 17:03:00 -04001862 sk->sk_data_ready = xs_data_ready;
Chuck Lever176e21e2011-05-09 15:22:44 -04001863 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07001864 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05001865 sk->sk_error_report = xs_error_report;
Trond Myklebustc2126152015-08-19 21:46:15 -05001866 sk->sk_allocation = GFP_NOIO;
Chuck Lever176e21e2011-05-09 15:22:44 -04001867
1868 xprt_clear_connected(xprt);
1869
1870 /* Reset to new socket */
1871 transport->sock = sock;
1872 transport->inet = sk;
1873
1874 write_unlock_bh(&sk->sk_callback_lock);
1875 }
1876
Trond Myklebust550aebf2018-09-14 14:32:45 -04001877 xs_stream_reset_connect(transport);
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001878
Chuck Lever176e21e2011-05-09 15:22:44 -04001879 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1880}
1881
1882/**
1883 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
Chuck Lever176e21e2011-05-09 15:22:44 -04001884 * @transport: socket transport to connect
Chuck Lever176e21e2011-05-09 15:22:44 -04001885 */
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001886static int xs_local_setup_socket(struct sock_xprt *transport)
Chuck Lever176e21e2011-05-09 15:22:44 -04001887{
Chuck Lever176e21e2011-05-09 15:22:44 -04001888 struct rpc_xprt *xprt = &transport->xprt;
1889 struct socket *sock;
1890 int status = -EIO;
1891
Chuck Lever176e21e2011-05-09 15:22:44 -04001892 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1893 SOCK_STREAM, 0, &sock, 1);
1894 if (status < 0) {
1895 dprintk("RPC: can't create AF_LOCAL "
1896 "transport socket (%d).\n", -status);
1897 goto out;
1898 }
Stefan Hajnoczid1358912015-12-02 14:17:52 +08001899 xs_reclassify_socket(AF_LOCAL, sock);
Chuck Lever176e21e2011-05-09 15:22:44 -04001900
1901 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1902 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1903
1904 status = xs_local_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001905 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever176e21e2011-05-09 15:22:44 -04001906 switch (status) {
1907 case 0:
1908 dprintk("RPC: xprt %p connected to %s\n",
1909 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
Chuck Lever3968a8a2018-10-01 14:25:36 -04001910 xprt->stat.connect_count++;
1911 xprt->stat.connect_time += (long)jiffies -
1912 xprt->stat.connect_start;
Chuck Lever176e21e2011-05-09 15:22:44 -04001913 xprt_set_connected(xprt);
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001914 case -ENOBUFS:
Chuck Lever176e21e2011-05-09 15:22:44 -04001915 break;
1916 case -ENOENT:
1917 dprintk("RPC: xprt %p: socket %s does not exist\n",
1918 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1919 break;
Trond Myklebust4a20a982012-12-15 17:02:29 -05001920 case -ECONNREFUSED:
1921 dprintk("RPC: xprt %p: connection refused for %s\n",
1922 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1923 break;
Chuck Lever176e21e2011-05-09 15:22:44 -04001924 default:
1925 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1926 __func__, -status,
1927 xprt->address_strings[RPC_DISPLAY_ADDR]);
1928 }
1929
1930out:
1931 xprt_clear_connecting(xprt);
1932 xprt_wake_pending_tasks(xprt, status);
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001933 return status;
1934}
1935
Linus Torvaldsb6669732013-02-28 18:02:55 -08001936static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001937{
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001938 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1939 int ret;
1940
1941 if (RPC_IS_ASYNC(task)) {
1942 /*
1943 * We want the AF_LOCAL connect to be resolved in the
1944 * filesystem namespace of the process making the rpc
1945 * call. Thus we connect synchronously.
1946 *
1947 * If we want to support asynchronous AF_LOCAL calls,
1948 * we'll need to figure out how to pass a namespace to
1949 * connect.
1950 */
1951 rpc_exit(task, -ENOTCONN);
1952 return;
1953 }
1954 ret = xs_local_setup_socket(transport);
1955 if (ret && !RPC_IS_SOFTCONN(task))
1956 msleep_interruptible(15000);
Chuck Lever176e21e2011-05-09 15:22:44 -04001957}
1958
Jeff Layton3c87ef62015-06-03 16:14:25 -04001959#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Jeff Laytond6e971d2015-06-03 16:14:28 -04001960/*
1961 * Note that this should be called with XPRT_LOCKED held (or when we otherwise
1962 * know that we have exclusive access to the socket), to guard against
1963 * races with xs_reset_transport.
1964 */
Mel Gormana564b8f2012-07-31 16:45:12 -07001965static void xs_set_memalloc(struct rpc_xprt *xprt)
1966{
1967 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1968 xprt);
1969
Jeff Laytond6e971d2015-06-03 16:14:28 -04001970 /*
1971 * If there's no sock, then we have nothing to set. The
1972 * reconnecting process will get it for us.
1973 */
1974 if (!transport->inet)
1975 return;
Jeff Layton8e228132015-06-03 16:14:26 -04001976 if (atomic_read(&xprt->swapper))
Mel Gormana564b8f2012-07-31 16:45:12 -07001977 sk_set_memalloc(transport->inet);
1978}
1979
1980/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04001981 * xs_enable_swap - Tag this transport as being used for swap.
Mel Gormana564b8f2012-07-31 16:45:12 -07001982 * @xprt: transport to tag
Mel Gormana564b8f2012-07-31 16:45:12 -07001983 *
Jeff Layton8e228132015-06-03 16:14:26 -04001984 * Take a reference to this transport on behalf of the rpc_clnt, and
1985 * optionally mark it for swapping if it wasn't already.
Mel Gormana564b8f2012-07-31 16:45:12 -07001986 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04001987static int
1988xs_enable_swap(struct rpc_xprt *xprt)
Mel Gormana564b8f2012-07-31 16:45:12 -07001989{
Jeff Laytond6e971d2015-06-03 16:14:28 -04001990 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Mel Gormana564b8f2012-07-31 16:45:12 -07001991
Jeff Laytond6e971d2015-06-03 16:14:28 -04001992 if (atomic_inc_return(&xprt->swapper) != 1)
1993 return 0;
1994 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
1995 return -ERESTARTSYS;
1996 if (xs->inet)
1997 sk_set_memalloc(xs->inet);
1998 xprt_release_xprt(xprt, NULL);
Jeff Layton8e228132015-06-03 16:14:26 -04001999 return 0;
Mel Gormana564b8f2012-07-31 16:45:12 -07002000}
Jeff Layton8e228132015-06-03 16:14:26 -04002001
2002/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002003 * xs_disable_swap - Untag this transport as being used for swap.
Jeff Layton8e228132015-06-03 16:14:26 -04002004 * @xprt: transport to tag
2005 *
2006 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
2007 * swapper refcount goes to 0, untag the socket as a memalloc socket.
2008 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002009static void
2010xs_disable_swap(struct rpc_xprt *xprt)
Jeff Layton8e228132015-06-03 16:14:26 -04002011{
Jeff Laytond6e971d2015-06-03 16:14:28 -04002012 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Jeff Layton8e228132015-06-03 16:14:26 -04002013
Jeff Laytond6e971d2015-06-03 16:14:28 -04002014 if (!atomic_dec_and_test(&xprt->swapper))
2015 return;
2016 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2017 return;
2018 if (xs->inet)
2019 sk_clear_memalloc(xs->inet);
2020 xprt_release_xprt(xprt, NULL);
Mel Gormana564b8f2012-07-31 16:45:12 -07002021}
Mel Gormana564b8f2012-07-31 16:45:12 -07002022#else
2023static void xs_set_memalloc(struct rpc_xprt *xprt)
2024{
2025}
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002026
2027static int
2028xs_enable_swap(struct rpc_xprt *xprt)
2029{
2030 return -EINVAL;
2031}
2032
2033static void
2034xs_disable_swap(struct rpc_xprt *xprt)
2035{
2036}
Mel Gormana564b8f2012-07-31 16:45:12 -07002037#endif
2038
Chuck Lever16be2d22007-08-06 11:57:38 -04002039static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04002040{
Chuck Lever16be2d22007-08-06 11:57:38 -04002041 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04002042
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002043 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002044 struct sock *sk = sock->sk;
2045
2046 write_lock_bh(&sk->sk_callback_lock);
2047
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002048 xs_save_old_callbacks(transport, sk);
2049
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002050 sk->sk_user_data = xprt;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002051 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002052 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002053 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebustc2126152015-08-19 21:46:15 -05002054 sk->sk_allocation = GFP_NOIO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002055
2056 xprt_set_connected(xprt);
2057
2058 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002059 transport->sock = sock;
2060 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002061
Mel Gormana564b8f2012-07-31 16:45:12 -07002062 xs_set_memalloc(xprt);
2063
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002064 write_unlock_bh(&sk->sk_callback_lock);
2065 }
Chuck Lever470056c2005-08-25 16:25:56 -07002066 xs_udp_do_set_buffer_size(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002067
2068 xprt->stat.connect_start = jiffies;
Chuck Lever16be2d22007-08-06 11:57:38 -04002069}
2070
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002071static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04002072{
2073 struct sock_xprt *transport =
2074 container_of(work, struct sock_xprt, connect_worker.work);
2075 struct rpc_xprt *xprt = &transport->xprt;
Colin Ian Kingd099b8a2017-09-18 12:21:14 +01002076 struct socket *sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002077 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04002078
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002079 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002080 xs_addr(xprt)->sa_family, SOCK_DGRAM,
2081 IPPROTO_UDP, false);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002082 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04002083 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04002084
Chuck Leverc740eff2009-08-09 15:09:46 -04002085 dprintk("RPC: worker connecting xprt %p via %s to "
2086 "%s (port %s)\n", xprt,
2087 xprt->address_strings[RPC_DISPLAY_PROTO],
2088 xprt->address_strings[RPC_DISPLAY_ADDR],
2089 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04002090
2091 xs_udp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002092 trace_rpc_socket_connect(xprt, sock, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002093 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002094out:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002095 xprt_unlock_connect(xprt, transport);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002096 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002097 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002098}
2099
Trond Myklebust4876cc72015-06-19 16:17:57 -04002100/**
2101 * xs_tcp_shutdown - gracefully shut down a TCP socket
2102 * @xprt: transport
2103 *
2104 * Initiates a graceful shutdown of the TCP socket by calling the
2105 * equivalent of shutdown(SHUT_RDWR);
2106 */
2107static void xs_tcp_shutdown(struct rpc_xprt *xprt)
2108{
2109 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2110 struct socket *sock = transport->sock;
Trond Myklebust9b308892018-02-05 10:20:06 -05002111 int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
Trond Myklebust4876cc72015-06-19 16:17:57 -04002112
2113 if (sock == NULL)
2114 return;
Trond Myklebust9b308892018-02-05 10:20:06 -05002115 switch (skst) {
2116 default:
Trond Myklebust4876cc72015-06-19 16:17:57 -04002117 kernel_sock_shutdown(sock, SHUT_RDWR);
2118 trace_rpc_socket_shutdown(xprt, sock);
Trond Myklebust9b308892018-02-05 10:20:06 -05002119 break;
2120 case TCP_CLOSE:
2121 case TCP_TIME_WAIT:
Trond Myklebust4876cc72015-06-19 16:17:57 -04002122 xs_reset_transport(transport);
Trond Myklebust9b308892018-02-05 10:20:06 -05002123 }
Trond Myklebust4876cc72015-06-19 16:17:57 -04002124}
2125
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002126static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
2127 struct socket *sock)
2128{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002129 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2130 unsigned int keepidle;
2131 unsigned int keepcnt;
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002132 unsigned int opt_on = 1;
2133 unsigned int timeo;
2134
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002135 spin_lock_bh(&xprt->transport_lock);
2136 keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
2137 keepcnt = xprt->timeout->to_retries + 1;
2138 timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
2139 (xprt->timeout->to_retries + 1);
2140 clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2141 spin_unlock_bh(&xprt->transport_lock);
2142
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002143 /* TCP Keepalive options */
2144 kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
2145 (char *)&opt_on, sizeof(opt_on));
2146 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
2147 (char *)&keepidle, sizeof(keepidle));
2148 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
2149 (char *)&keepidle, sizeof(keepidle));
2150 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
2151 (char *)&keepcnt, sizeof(keepcnt));
2152
2153 /* TCP user timeout (see RFC5482) */
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002154 kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT,
2155 (char *)&timeo, sizeof(timeo));
2156}
2157
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002158static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
2159 unsigned long connect_timeout,
2160 unsigned long reconnect_timeout)
2161{
2162 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2163 struct rpc_timeout to;
2164 unsigned long initval;
2165
2166 spin_lock_bh(&xprt->transport_lock);
2167 if (reconnect_timeout < xprt->max_reconnect_timeout)
2168 xprt->max_reconnect_timeout = reconnect_timeout;
2169 if (connect_timeout < xprt->connect_timeout) {
2170 memcpy(&to, xprt->timeout, sizeof(to));
2171 initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
2172 /* Arbitrary lower limit */
2173 if (initval < XS_TCP_INIT_REEST_TO << 1)
2174 initval = XS_TCP_INIT_REEST_TO << 1;
2175 to.to_initval = initval;
2176 to.to_maxval = initval;
2177 memcpy(&transport->tcp_timeout, &to,
2178 sizeof(transport->tcp_timeout));
2179 xprt->timeout = &transport->tcp_timeout;
2180 xprt->connect_timeout = connect_timeout;
2181 }
2182 set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2183 spin_unlock_bh(&xprt->transport_lock);
2184}
2185
Chuck Lever16be2d22007-08-06 11:57:38 -04002186static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002187{
Chuck Lever16be2d22007-08-06 11:57:38 -04002188 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002189 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002190
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002191 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002192 struct sock *sk = sock->sk;
NeilBrownd88e4d82016-08-04 16:24:28 +10002193 unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC;
Trond Myklebust7f260e82013-09-24 11:25:22 -04002194
NeilBrownd88e4d82016-08-04 16:24:28 +10002195 /* Avoid temporary address, they are bad for long-lived
2196 * connections such as NFS mounts.
2197 * RFC4941, section 3.6 suggests that:
2198 * Individual applications, which have specific
2199 * knowledge about the normal duration of connections,
2200 * MAY override this as appropriate.
2201 */
2202 kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES,
2203 (char *)&addr_pref, sizeof(addr_pref));
2204
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002205 xs_tcp_set_socket_timeouts(xprt, sock);
Trond Myklebust775f06a2015-06-20 15:31:54 -04002206
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002207 write_lock_bh(&sk->sk_callback_lock);
2208
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002209 xs_save_old_callbacks(transport, sk);
2210
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002211 sk->sk_user_data = xprt;
Trond Myklebust5157b952016-05-29 10:13:24 -04002212 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002213 sk->sk_state_change = xs_tcp_state_change;
2214 sk->sk_write_space = xs_tcp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002215 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05002216 sk->sk_error_report = xs_error_report;
Trond Myklebustc2126152015-08-19 21:46:15 -05002217 sk->sk_allocation = GFP_NOIO;
Chuck Lever3167e122005-08-25 16:25:55 -07002218
2219 /* socket options */
Chuck Lever3167e122005-08-25 16:25:55 -07002220 sock_reset_flag(sk, SOCK_LINGER);
Chuck Lever3167e122005-08-25 16:25:55 -07002221 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002222
2223 xprt_clear_connected(xprt);
2224
2225 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002226 transport->sock = sock;
2227 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002228
2229 write_unlock_bh(&sk->sk_callback_lock);
2230 }
2231
Trond Myklebust01d37c42009-03-11 14:09:39 -04002232 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002233 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002234
Mel Gormana564b8f2012-07-31 16:45:12 -07002235 xs_set_memalloc(xprt);
2236
Trond Myklebuste1806c72018-08-13 16:50:49 -04002237 /* Reset TCP record info */
Trond Myklebust550aebf2018-09-14 14:32:45 -04002238 xs_stream_reset_connect(transport);
Trond Myklebuste1806c72018-08-13 16:50:49 -04002239
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002240 /* Tell the socket layer to start connecting... */
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04002241 set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002242 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2243 switch (ret) {
2244 case 0:
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002245 xs_set_srcport(transport, sock);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002246 /* fall through */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002247 case -EINPROGRESS:
2248 /* SYN_SENT! */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002249 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2250 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust1f4c17a2016-08-01 13:36:08 -04002251 break;
2252 case -EADDRNOTAVAIL:
2253 /* Source port number is unavailable. Try a new one! */
2254 transport->srcport = 0;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002255 }
2256out:
2257 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002258}
2259
2260/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002261 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
Chuck Lever16be2d22007-08-06 11:57:38 -04002262 *
2263 * Invoked by a work queue tasklet.
2264 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002265static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002266{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002267 struct sock_xprt *transport =
2268 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002269 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002270 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002271 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002272
Chuck Lever16be2d22007-08-06 11:57:38 -04002273 if (!sock) {
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002274 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002275 xs_addr(xprt)->sa_family, SOCK_STREAM,
2276 IPPROTO_TCP, true);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002277 if (IS_ERR(sock)) {
2278 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002279 goto out;
2280 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002281 }
2282
Chuck Leverc740eff2009-08-09 15:09:46 -04002283 dprintk("RPC: worker connecting xprt %p via %s to "
2284 "%s (port %s)\n", xprt,
2285 xprt->address_strings[RPC_DISPLAY_PROTO],
2286 xprt->address_strings[RPC_DISPLAY_ADDR],
2287 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002288
2289 status = xs_tcp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002290 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever46121cf2007-01-31 12:14:08 -05002291 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2292 xprt, -status, xprt_connected(xprt),
2293 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002294 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002295 default:
2296 printk("%s: connect returned unhandled error %d\n",
2297 __func__, status);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002298 /* fall through */
Trond Myklebustf75e6742009-04-21 17:18:20 -04002299 case -EADDRNOTAVAIL:
2300 /* We're probably in TIME_WAIT. Get rid of existing socket,
2301 * and retry
2302 */
Trond Myklebusta519fc72012-09-12 16:49:15 -04002303 xs_tcp_force_close(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002304 break;
Trond Myklebust2a491992009-03-11 14:38:00 -04002305 case 0:
2306 case -EINPROGRESS:
2307 case -EALREADY:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002308 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002309 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002310 case -EINVAL:
2311 /* Happens, for instance, if the user specified a link
2312 * local IPv6 address without a scope-id.
2313 */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002314 case -ECONNREFUSED:
2315 case -ECONNRESET:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002316 case -ENETDOWN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002317 case -ENETUNREACH:
Trond Myklebust4ba161a2017-11-24 12:00:24 -05002318 case -EHOSTUNREACH:
Trond Myklebust3913c782015-02-08 21:44:04 -05002319 case -EADDRINUSE:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002320 case -ENOBUFS:
NeilBrown6ea44ad2017-05-25 17:00:32 +10002321 /*
2322 * xs_tcp_force_close() wakes tasks with -EIO.
2323 * We need to wake them first to ensure the
2324 * correct error code.
2325 */
2326 xprt_wake_pending_tasks(xprt, status);
Trond Myklebust4efdd922015-02-08 15:34:28 -05002327 xs_tcp_force_close(xprt);
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002328 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002329 }
Trond Myklebust2a491992009-03-11 14:38:00 -04002330 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002331out:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002332 xprt_unlock_connect(xprt, transport);
Chuck Lever2226feb2005-08-11 16:25:38 -04002333 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002334 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04002335}
2336
Trond Myklebust02910172016-08-04 00:00:33 -04002337static unsigned long xs_reconnect_delay(const struct rpc_xprt *xprt)
2338{
2339 unsigned long start, now = jiffies;
2340
2341 start = xprt->stat.connect_start + xprt->reestablish_timeout;
2342 if (time_after(start, now))
2343 return start - now;
2344 return 0;
2345}
2346
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002347static void xs_reconnect_backoff(struct rpc_xprt *xprt)
2348{
2349 xprt->reestablish_timeout <<= 1;
2350 if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
2351 xprt->reestablish_timeout = xprt->max_reconnect_timeout;
2352 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2353 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2354}
2355
Chuck Lever68e220b2007-08-06 11:57:48 -04002356/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002357 * xs_connect - connect a socket to a remote endpoint
Trond Myklebust1b092092013-01-08 09:26:49 -05002358 * @xprt: pointer to transport structure
Chuck Lever9903cd12005-08-11 16:25:26 -04002359 * @task: address of RPC task that manages state of connect request
2360 *
2361 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002362 *
2363 * UDP socket connects are synchronous, but we use a work queue anyway
2364 * to guarantee that even unprivileged user processes can set up a
2365 * socket on a privileged port.
2366 *
2367 * If a UDP socket connect fails, the delay behavior here prevents
2368 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002369 */
Trond Myklebust1b092092013-01-08 09:26:49 -05002370static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002371{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002372 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002373 unsigned long delay = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002374
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002375 WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2376
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002377 if (transport->sock != NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002378 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2379 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002380 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002381
2382 /* Start by resetting any existing state */
2383 xs_reset_transport(transport);
2384
Trond Myklebust02910172016-08-04 00:00:33 -04002385 delay = xs_reconnect_delay(xprt);
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002386 xs_reconnect_backoff(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002387
Trond Myklebust02910172016-08-04 00:00:33 -04002388 } else
Chuck Lever46121cf2007-01-31 12:14:08 -05002389 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002390
2391 queue_delayed_work(xprtiod_workqueue,
2392 &transport->connect_worker,
2393 delay);
Chuck Levera246b012005-08-11 16:25:23 -04002394}
2395
Chuck Lever262ca072006-03-20 13:44:16 -05002396/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002397 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2398 * @xprt: rpc_xprt struct containing statistics
2399 * @seq: output file
2400 *
2401 */
2402static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2403{
2404 long idle_time = 0;
2405
2406 if (xprt_connected(xprt))
2407 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2408
2409 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
Andy Adamson15a45202012-02-14 16:19:18 -05002410 "%llu %llu %lu %llu %llu\n",
Chuck Lever176e21e2011-05-09 15:22:44 -04002411 xprt->stat.bind_count,
2412 xprt->stat.connect_count,
Chuck Lever8440a882018-10-01 14:25:41 -04002413 xprt->stat.connect_time / HZ,
Chuck Lever176e21e2011-05-09 15:22:44 -04002414 idle_time,
2415 xprt->stat.sends,
2416 xprt->stat.recvs,
2417 xprt->stat.bad_xids,
2418 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002419 xprt->stat.bklog_u,
2420 xprt->stat.max_slots,
2421 xprt->stat.sending_u,
2422 xprt->stat.pending_u);
Chuck Lever176e21e2011-05-09 15:22:44 -04002423}
2424
2425/**
Chuck Lever262ca072006-03-20 13:44:16 -05002426 * xs_udp_print_stats - display UDP socket-specifc stats
2427 * @xprt: rpc_xprt struct containing statistics
2428 * @seq: output file
2429 *
2430 */
2431static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2432{
Chuck Leverc8475462006-12-05 16:35:26 -05002433 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2434
Andy Adamson15a45202012-02-14 16:19:18 -05002435 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2436 "%lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002437 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002438 xprt->stat.bind_count,
2439 xprt->stat.sends,
2440 xprt->stat.recvs,
2441 xprt->stat.bad_xids,
2442 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002443 xprt->stat.bklog_u,
2444 xprt->stat.max_slots,
2445 xprt->stat.sending_u,
2446 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002447}
2448
2449/**
2450 * xs_tcp_print_stats - display TCP socket-specifc stats
2451 * @xprt: rpc_xprt struct containing statistics
2452 * @seq: output file
2453 *
2454 */
2455static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2456{
Chuck Leverc8475462006-12-05 16:35:26 -05002457 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002458 long idle_time = 0;
2459
2460 if (xprt_connected(xprt))
2461 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2462
Andy Adamson15a45202012-02-14 16:19:18 -05002463 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2464 "%llu %llu %lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002465 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002466 xprt->stat.bind_count,
2467 xprt->stat.connect_count,
Chuck Lever8440a882018-10-01 14:25:41 -04002468 xprt->stat.connect_time / HZ,
Chuck Lever262ca072006-03-20 13:44:16 -05002469 idle_time,
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
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002480/*
2481 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2482 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2483 * to use the server side send routines.
2484 */
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002485static int bc_malloc(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002486{
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002487 struct rpc_rqst *rqst = task->tk_rqstp;
2488 size_t size = rqst->rq_callsize;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002489 struct page *page;
2490 struct rpc_buffer *buf;
2491
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002492 if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
2493 WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
2494 size);
2495 return -EINVAL;
2496 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002497
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002498 page = alloc_page(GFP_KERNEL);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002499 if (!page)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002500 return -ENOMEM;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002501
2502 buf = page_address(page);
2503 buf->len = PAGE_SIZE;
2504
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002505 rqst->rq_buffer = buf->data;
Jeff Layton18e601d2016-10-24 20:33:23 -04002506 rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002507 return 0;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002508}
2509
2510/*
2511 * Free the space allocated in the bc_alloc routine
2512 */
Chuck Lever3435c742016-09-15 10:55:29 -04002513static void bc_free(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002514{
Chuck Lever3435c742016-09-15 10:55:29 -04002515 void *buffer = task->tk_rqstp->rq_buffer;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002516 struct rpc_buffer *buf;
2517
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002518 buf = container_of(buffer, struct rpc_buffer, data);
2519 free_page((unsigned long)buf);
2520}
2521
2522/*
2523 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2524 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2525 */
2526static int bc_sendto(struct rpc_rqst *req)
2527{
2528 int len;
2529 struct xdr_buf *xbufp = &req->rq_snd_buf;
2530 struct rpc_xprt *xprt = req->rq_xprt;
2531 struct sock_xprt *transport =
2532 container_of(xprt, struct sock_xprt, xprt);
2533 struct socket *sock = transport->sock;
2534 unsigned long headoff;
2535 unsigned long tailoff;
2536
Chuck Lever61677ee2011-05-09 15:22:34 -04002537 xs_encode_stream_record_marker(xbufp);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002538
2539 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2540 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2541 len = svc_send_common(sock, xbufp,
2542 virt_to_page(xbufp->head[0].iov_base), headoff,
2543 xbufp->tail[0].iov_base, tailoff);
2544
2545 if (len != xbufp->len) {
2546 printk(KERN_NOTICE "Error sending entire callback!\n");
2547 len = -EAGAIN;
2548 }
2549
2550 return len;
2551}
2552
2553/*
2554 * The send routine. Borrows from svc_send
2555 */
Trond Myklebustadfa7142018-09-03 23:58:59 -04002556static int bc_send_request(struct rpc_rqst *req)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002557{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002558 struct svc_xprt *xprt;
Andrzej Hajda7fc56132015-09-24 16:00:09 +02002559 int len;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002560
2561 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2562 /*
2563 * Get the server socket associated with this callback xprt
2564 */
2565 xprt = req->rq_xprt->bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002566
2567 /*
2568 * Grab the mutex to serialize data as the connection is shared
2569 * with the fore channel
2570 */
Trond Myklebustc5445772018-09-03 23:39:27 -04002571 mutex_lock(&xprt->xpt_mutex);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002572 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2573 len = -ENOTCONN;
2574 else
2575 len = bc_sendto(req);
2576 mutex_unlock(&xprt->xpt_mutex);
2577
2578 if (len > 0)
2579 len = 0;
2580
2581 return len;
2582}
2583
2584/*
2585 * The close routine. Since this is client initiated, we do nothing
2586 */
2587
2588static void bc_close(struct rpc_xprt *xprt)
2589{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002590}
2591
2592/*
2593 * The xprt destroy routine. Again, because this connection is client
2594 * initiated, we do nothing
2595 */
2596
2597static void bc_destroy(struct rpc_xprt *xprt)
2598{
Kinglong Mee47f72ef2014-03-24 11:58:16 +08002599 dprintk("RPC: bc_destroy xprt %p\n", xprt);
2600
2601 xs_xprt_free(xprt);
2602 module_put(THIS_MODULE);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002603}
2604
Chuck Leverd31ae252017-08-01 12:00:39 -04002605static const struct rpc_xprt_ops xs_local_ops = {
Chuck Lever176e21e2011-05-09 15:22:44 -04002606 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebust4cd34e72018-08-31 10:00:02 -04002607 .release_xprt = xprt_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002608 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002609 .free_slot = xprt_free_slot,
Chuck Lever176e21e2011-05-09 15:22:44 -04002610 .rpcbind = xs_local_rpcbind,
2611 .set_port = xs_local_set_port,
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002612 .connect = xs_local_connect,
Chuck Lever176e21e2011-05-09 15:22:44 -04002613 .buf_alloc = rpc_malloc,
2614 .buf_free = rpc_free,
Trond Myklebust550aebf2018-09-14 14:32:45 -04002615 .prepare_request = xs_stream_prepare_request,
Chuck Lever176e21e2011-05-09 15:22:44 -04002616 .send_request = xs_local_send_request,
2617 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2618 .close = xs_close,
Trond Myklebusta1311d82013-10-31 09:18:49 -04002619 .destroy = xs_destroy,
Chuck Lever176e21e2011-05-09 15:22:44 -04002620 .print_stats = xs_local_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002621 .enable_swap = xs_enable_swap,
2622 .disable_swap = xs_disable_swap,
Chuck Lever176e21e2011-05-09 15:22:44 -04002623};
2624
Chuck Leverd31ae252017-08-01 12:00:39 -04002625static const struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002626 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002627 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002628 .release_xprt = xprt_release_xprt_cong,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002629 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002630 .free_slot = xprt_free_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002631 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002632 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002633 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002634 .buf_alloc = rpc_malloc,
2635 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002636 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002637 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002638 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002639 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002640 .close = xs_close,
2641 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002642 .print_stats = xs_udp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002643 .enable_swap = xs_enable_swap,
2644 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002645 .inject_disconnect = xs_inject_disconnect,
Chuck Lever262965f2005-08-11 16:25:56 -04002646};
2647
Chuck Leverd31ae252017-08-01 12:00:39 -04002648static const struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002649 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebust4cd34e72018-08-31 10:00:02 -04002650 .release_xprt = xprt_release_xprt,
Trond Myklebust36bd7de2018-09-03 18:41:32 -04002651 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002652 .free_slot = xprt_free_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002653 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002654 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002655 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002656 .buf_alloc = rpc_malloc,
2657 .buf_free = rpc_free,
Trond Myklebust277e4ab2018-09-14 09:49:06 -04002658 .prepare_request = xs_stream_prepare_request,
Chuck Lever262965f2005-08-11 16:25:56 -04002659 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002660 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustc627d312015-02-10 11:06:04 -05002661 .close = xs_tcp_shutdown,
Chuck Lever9903cd12005-08-11 16:25:26 -04002662 .destroy = xs_destroy,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002663 .set_connect_timeout = xs_tcp_set_connect_timeout,
Chuck Lever262ca072006-03-20 13:44:16 -05002664 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002665 .enable_swap = xs_enable_swap,
2666 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002667 .inject_disconnect = xs_inject_disconnect,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002668#ifdef CONFIG_SUNRPC_BACKCHANNEL
2669 .bc_setup = xprt_setup_bc,
Chuck Lever76566772015-10-24 17:28:32 -04002670 .bc_up = xs_tcp_bc_up,
Chuck Lever6b26cc82016-05-02 14:40:40 -04002671 .bc_maxpayload = xs_tcp_bc_maxpayload,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002672 .bc_free_rqst = xprt_free_bc_rqst,
2673 .bc_destroy = xprt_destroy_bc,
2674#endif
Chuck Levera246b012005-08-11 16:25:23 -04002675};
2676
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002677/*
2678 * The rpc_xprt_ops for the server backchannel
2679 */
2680
Chuck Leverd31ae252017-08-01 12:00:39 -04002681static const struct rpc_xprt_ops bc_tcp_ops = {
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002682 .reserve_xprt = xprt_reserve_xprt,
2683 .release_xprt = xprt_release_xprt,
Bryan Schumaker84e28a32012-09-24 13:39:01 -04002684 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002685 .free_slot = xprt_free_slot,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002686 .buf_alloc = bc_malloc,
2687 .buf_free = bc_free,
2688 .send_request = bc_send_request,
2689 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2690 .close = bc_close,
2691 .destroy = bc_destroy,
2692 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002693 .enable_swap = xs_enable_swap,
2694 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002695 .inject_disconnect = xs_inject_disconnect,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002696};
2697
Chuck Lever92476852010-10-20 11:53:01 -04002698static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2699{
2700 static const struct sockaddr_in sin = {
2701 .sin_family = AF_INET,
2702 .sin_addr.s_addr = htonl(INADDR_ANY),
2703 };
2704 static const struct sockaddr_in6 sin6 = {
2705 .sin6_family = AF_INET6,
2706 .sin6_addr = IN6ADDR_ANY_INIT,
2707 };
2708
2709 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002710 case AF_LOCAL:
2711 break;
Chuck Lever92476852010-10-20 11:53:01 -04002712 case AF_INET:
2713 memcpy(sap, &sin, sizeof(sin));
2714 break;
2715 case AF_INET6:
2716 memcpy(sap, &sin6, sizeof(sin6));
2717 break;
2718 default:
2719 dprintk("RPC: %s: Bad address family\n", __func__);
2720 return -EAFNOSUPPORT;
2721 }
2722 return 0;
2723}
2724
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002725static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002726 unsigned int slot_table_size,
2727 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002728{
2729 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002730 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002731
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002732 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002733 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002734 return ERR_PTR(-EBADF);
2735 }
2736
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002737 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2738 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002739 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002740 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2741 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002742 return ERR_PTR(-ENOMEM);
2743 }
2744
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002745 new = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002746 mutex_init(&new->recv_mutex);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002747 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2748 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002749 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002750 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002751 else {
2752 int err;
2753 err = xs_init_anyaddr(args->dstaddr->sa_family,
2754 (struct sockaddr *)&new->srcaddr);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002755 if (err != 0) {
2756 xprt_free(xprt);
Chuck Lever92476852010-10-20 11:53:01 -04002757 return ERR_PTR(err);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002758 }
Chuck Lever92476852010-10-20 11:53:01 -04002759 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002760
2761 return xprt;
2762}
2763
Chuck Lever176e21e2011-05-09 15:22:44 -04002764static const struct rpc_timeout xs_local_default_timeout = {
2765 .to_initval = 10 * HZ,
2766 .to_maxval = 10 * HZ,
2767 .to_retries = 2,
2768};
2769
2770/**
2771 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2772 * @args: rpc transport creation arguments
2773 *
2774 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2775 */
2776static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2777{
2778 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2779 struct sock_xprt *transport;
2780 struct rpc_xprt *xprt;
2781 struct rpc_xprt *ret;
2782
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002783 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2784 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002785 if (IS_ERR(xprt))
2786 return xprt;
2787 transport = container_of(xprt, struct sock_xprt, xprt);
2788
2789 xprt->prot = 0;
2790 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2791 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2792
2793 xprt->bind_timeout = XS_BIND_TO;
2794 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2795 xprt->idle_timeout = XS_IDLE_DISC_TO;
2796
2797 xprt->ops = &xs_local_ops;
2798 xprt->timeout = &xs_local_default_timeout;
2799
Trond Myklebust550aebf2018-09-14 14:32:45 -04002800 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
2801 INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket);
NeilBrown93dc41b2013-10-31 16:14:36 +11002802
Chuck Lever176e21e2011-05-09 15:22:44 -04002803 switch (sun->sun_family) {
2804 case AF_LOCAL:
2805 if (sun->sun_path[0] != '/') {
2806 dprintk("RPC: bad AF_LOCAL address: %s\n",
2807 sun->sun_path);
2808 ret = ERR_PTR(-EINVAL);
2809 goto out_err;
2810 }
2811 xprt_set_bound(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002812 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
J. Bruce Fields7073ea82013-02-21 10:14:22 -05002813 ret = ERR_PTR(xs_local_setup_socket(transport));
2814 if (ret)
2815 goto out_err;
Chuck Lever176e21e2011-05-09 15:22:44 -04002816 break;
2817 default:
2818 ret = ERR_PTR(-EAFNOSUPPORT);
2819 goto out_err;
2820 }
2821
2822 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2823 xprt->address_strings[RPC_DISPLAY_ADDR]);
2824
2825 if (try_module_get(THIS_MODULE))
2826 return xprt;
2827 ret = ERR_PTR(-EINVAL);
2828out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002829 xs_xprt_free(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002830 return ret;
2831}
2832
Trond Myklebust2881ae72007-12-20 16:03:54 -05002833static const struct rpc_timeout xs_udp_default_timeout = {
2834 .to_initval = 5 * HZ,
2835 .to_maxval = 30 * HZ,
2836 .to_increment = 5 * HZ,
2837 .to_retries = 5,
2838};
2839
Chuck Lever9903cd12005-08-11 16:25:26 -04002840/**
2841 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002842 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002843 *
2844 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002845static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002846{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002847 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002848 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002849 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002850 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002851
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002852 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2853 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002854 if (IS_ERR(xprt))
2855 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002856 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002857
Chuck Leverec739ef2006-08-22 20:06:15 -04002858 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002859 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002860 /* XXX: header size can vary due to auth type, IPv6, etc. */
2861 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2862
Chuck Lever03bf4b72005-08-25 16:25:55 -07002863 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002864 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2865 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002866
Chuck Lever262965f2005-08-11 16:25:56 -04002867 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002868
Trond Myklebustba7392b2007-12-20 16:03:55 -05002869 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002870
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002871 INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002872 INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2873
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002874 switch (addr->sa_family) {
2875 case AF_INET:
2876 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2877 xprt_set_bound(xprt);
2878
Chuck Lever9dc3b092009-08-09 15:09:46 -04002879 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002880 break;
2881 case AF_INET6:
2882 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2883 xprt_set_bound(xprt);
2884
Chuck Lever9dc3b092009-08-09 15:09:46 -04002885 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002886 break;
2887 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002888 ret = ERR_PTR(-EAFNOSUPPORT);
2889 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002890 }
2891
Chuck Leverc740eff2009-08-09 15:09:46 -04002892 if (xprt_bound(xprt))
2893 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2894 xprt->address_strings[RPC_DISPLAY_ADDR],
2895 xprt->address_strings[RPC_DISPLAY_PORT],
2896 xprt->address_strings[RPC_DISPLAY_PROTO]);
2897 else
2898 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2899 xprt->address_strings[RPC_DISPLAY_ADDR],
2900 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002901
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002902 if (try_module_get(THIS_MODULE))
2903 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002904 ret = ERR_PTR(-EINVAL);
2905out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002906 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002907 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002908}
2909
Trond Myklebust2881ae72007-12-20 16:03:54 -05002910static const struct rpc_timeout xs_tcp_default_timeout = {
2911 .to_initval = 60 * HZ,
2912 .to_maxval = 60 * HZ,
2913 .to_retries = 2,
2914};
2915
Chuck Lever9903cd12005-08-11 16:25:26 -04002916/**
2917 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002918 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002919 *
2920 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002921static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002922{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002923 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002924 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002925 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002926 struct rpc_xprt *ret;
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002927 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2928
2929 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2930 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Levera246b012005-08-11 16:25:23 -04002931
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002932 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002933 max_slot_table_size);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002934 if (IS_ERR(xprt))
2935 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002936 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002937
Chuck Leverec739ef2006-08-22 20:06:15 -04002938 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002939 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002940 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002941
Chuck Lever03bf4b72005-08-25 16:25:55 -07002942 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002943 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2944 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002945
Chuck Lever262965f2005-08-11 16:25:56 -04002946 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002947 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002948
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002949 xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002950 xprt->connect_timeout = xprt->timeout->to_initval *
2951 (xprt->timeout->to_retries + 1);
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002952
Trond Myklebustc50b8ee2018-09-14 14:26:28 -04002953 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002954 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
2955
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002956 switch (addr->sa_family) {
2957 case AF_INET:
2958 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2959 xprt_set_bound(xprt);
2960
Chuck Lever9dc3b092009-08-09 15:09:46 -04002961 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002962 break;
2963 case AF_INET6:
2964 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2965 xprt_set_bound(xprt);
2966
Chuck Lever9dc3b092009-08-09 15:09:46 -04002967 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002968 break;
2969 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002970 ret = ERR_PTR(-EAFNOSUPPORT);
2971 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002972 }
2973
Chuck Leverc740eff2009-08-09 15:09:46 -04002974 if (xprt_bound(xprt))
2975 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2976 xprt->address_strings[RPC_DISPLAY_ADDR],
2977 xprt->address_strings[RPC_DISPLAY_PORT],
2978 xprt->address_strings[RPC_DISPLAY_PROTO]);
2979 else
2980 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2981 xprt->address_strings[RPC_DISPLAY_ADDR],
2982 xprt->address_strings[RPC_DISPLAY_PROTO]);
2983
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002984 if (try_module_get(THIS_MODULE))
2985 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002986 ret = ERR_PTR(-EINVAL);
2987out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002988 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002989 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002990}
Chuck Lever282b32e2006-12-05 16:35:51 -05002991
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002992/**
2993 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2994 * @args: rpc transport creation arguments
2995 *
2996 */
2997static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2998{
2999 struct sockaddr *addr = args->dstaddr;
3000 struct rpc_xprt *xprt;
3001 struct sock_xprt *transport;
3002 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003003 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003004
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003005 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3006 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003007 if (IS_ERR(xprt))
3008 return xprt;
3009 transport = container_of(xprt, struct sock_xprt, xprt);
3010
3011 xprt->prot = IPPROTO_TCP;
3012 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
3013 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3014 xprt->timeout = &xs_tcp_default_timeout;
3015
3016 /* backchannel */
3017 xprt_set_bound(xprt);
3018 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003019 xprt->reestablish_timeout = 0;
3020 xprt->idle_timeout = 0;
3021
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003022 xprt->ops = &bc_tcp_ops;
3023
3024 switch (addr->sa_family) {
3025 case AF_INET:
3026 xs_format_peer_addresses(xprt, "tcp",
3027 RPCBIND_NETID_TCP);
3028 break;
3029 case AF_INET6:
3030 xs_format_peer_addresses(xprt, "tcp",
3031 RPCBIND_NETID_TCP6);
3032 break;
3033 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003034 ret = ERR_PTR(-EAFNOSUPPORT);
3035 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003036 }
3037
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04003038 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3039 xprt->address_strings[RPC_DISPLAY_ADDR],
3040 xprt->address_strings[RPC_DISPLAY_PORT],
3041 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003042
3043 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003044 * Once we've associated a backchannel xprt with a connection,
Weng Meiling28303ca2013-11-30 17:56:44 +08003045 * we want to keep it around as long as the connection lasts,
3046 * in case we need to start using it for a backchannel again;
3047 * this reference won't be dropped until bc_xprt is destroyed.
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003048 */
3049 xprt_get(xprt);
3050 args->bc_xprt->xpt_bc_xprt = xprt;
3051 xprt->bc_xprt = args->bc_xprt;
3052 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
3053 transport->sock = bc_sock->sk_sock;
3054 transport->inet = bc_sock->sk_sk;
3055
3056 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003057 * Since we don't want connections for the backchannel, we set
3058 * the xprt status to connected
3059 */
3060 xprt_set_connected(xprt);
3061
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003062 if (try_module_get(THIS_MODULE))
3063 return xprt;
Kinglong Mee642aab52014-03-24 12:00:28 +08003064
3065 args->bc_xprt->xpt_bc_xprt = NULL;
J. Bruce Fields39a9bea2016-05-17 12:38:21 -04003066 args->bc_xprt->xpt_bc_xps = NULL;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003067 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003068 ret = ERR_PTR(-EINVAL);
3069out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003070 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003071 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003072}
3073
Chuck Lever176e21e2011-05-09 15:22:44 -04003074static struct xprt_class xs_local_transport = {
3075 .list = LIST_HEAD_INIT(xs_local_transport.list),
3076 .name = "named UNIX socket",
3077 .owner = THIS_MODULE,
3078 .ident = XPRT_TRANSPORT_LOCAL,
3079 .setup = xs_setup_local,
3080};
3081
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003082static struct xprt_class xs_udp_transport = {
3083 .list = LIST_HEAD_INIT(xs_udp_transport.list),
3084 .name = "udp",
3085 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003086 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003087 .setup = xs_setup_udp,
3088};
3089
3090static struct xprt_class xs_tcp_transport = {
3091 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
3092 .name = "tcp",
3093 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003094 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003095 .setup = xs_setup_tcp,
3096};
3097
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003098static struct xprt_class xs_bc_tcp_transport = {
3099 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3100 .name = "tcp NFSv4.1 backchannel",
3101 .owner = THIS_MODULE,
3102 .ident = XPRT_TRANSPORT_BC_TCP,
3103 .setup = xs_setup_bc_tcp,
3104};
3105
Chuck Lever282b32e2006-12-05 16:35:51 -05003106/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003107 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05003108 *
3109 */
3110int init_socket_xprt(void)
3111{
Jeff Laytonf895b252014-11-17 16:58:04 -05003112#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08003113 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08003114 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05003115#endif
3116
Chuck Lever176e21e2011-05-09 15:22:44 -04003117 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003118 xprt_register_transport(&xs_udp_transport);
3119 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003120 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003121
Chuck Lever282b32e2006-12-05 16:35:51 -05003122 return 0;
3123}
3124
3125/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003126 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05003127 *
3128 */
3129void cleanup_socket_xprt(void)
3130{
Jeff Laytonf895b252014-11-17 16:58:04 -05003131#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Leverfbf76682006-12-05 16:35:54 -05003132 if (sunrpc_table_header) {
3133 unregister_sysctl_table(sunrpc_table_header);
3134 sunrpc_table_header = NULL;
3135 }
3136#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003137
Chuck Lever176e21e2011-05-09 15:22:44 -04003138 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003139 xprt_unregister_transport(&xs_udp_transport);
3140 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003141 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05003142}
Trond Myklebustcbf11072009-08-09 15:06:19 -04003143
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003144static int param_set_uint_minmax(const char *val,
3145 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003146 unsigned int min, unsigned int max)
3147{
Daniel Walter00cfaa92014-06-21 13:06:38 +01003148 unsigned int num;
Trond Myklebustcbf11072009-08-09 15:06:19 -04003149 int ret;
3150
3151 if (!val)
3152 return -EINVAL;
Daniel Walter00cfaa92014-06-21 13:06:38 +01003153 ret = kstrtouint(val, 0, &num);
Dan Carpenter9761a242017-02-19 00:34:59 +03003154 if (ret)
3155 return ret;
3156 if (num < min || num > max)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003157 return -EINVAL;
3158 *((unsigned int *)kp->arg) = num;
3159 return 0;
3160}
3161
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003162static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003163{
Frank Sorensonffb6ca32016-07-08 16:35:25 -05003164 return param_set_uint_minmax(val, kp,
J. Bruce Fields826799e2018-10-18 15:27:02 -04003165 RPC_MIN_RESVPORT,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003166 RPC_MAX_RESVPORT);
3167}
3168
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303169static const struct kernel_param_ops param_ops_portnr = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003170 .set = param_set_portnr,
3171 .get = param_get_uint,
3172};
3173
Trond Myklebustcbf11072009-08-09 15:06:19 -04003174#define param_check_portnr(name, p) \
3175 __param_check(name, p, unsigned int);
3176
3177module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3178module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3179
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003180static int param_set_slot_table_size(const char *val,
3181 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003182{
3183 return param_set_uint_minmax(val, kp,
3184 RPC_MIN_SLOT_TABLE,
3185 RPC_MAX_SLOT_TABLE);
3186}
3187
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303188static const struct kernel_param_ops param_ops_slot_table_size = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003189 .set = param_set_slot_table_size,
3190 .get = param_get_uint,
3191};
3192
Trond Myklebustcbf11072009-08-09 15:06:19 -04003193#define param_check_slot_table_size(name, p) \
3194 __param_check(name, p, unsigned int);
3195
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003196static int param_set_max_slot_table_size(const char *val,
3197 const struct kernel_param *kp)
3198{
3199 return param_set_uint_minmax(val, kp,
3200 RPC_MIN_SLOT_TABLE,
3201 RPC_MAX_SLOT_TABLE_LIMIT);
3202}
3203
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303204static const struct kernel_param_ops param_ops_max_slot_table_size = {
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003205 .set = param_set_max_slot_table_size,
3206 .get = param_get_uint,
3207};
3208
3209#define param_check_max_slot_table_size(name, p) \
3210 __param_check(name, p, unsigned int);
3211
Trond Myklebustcbf11072009-08-09 15:06:19 -04003212module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3213 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003214module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3215 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003216module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3217 slot_table_size, 0644);