blob: 55df1fadab27741ba4ff332c318f0bd69b2de0e6 [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,
Frank Sorensone08ea3a2016-07-08 16:35:24 -0500132 .extra2 = &xprt_max_resvport
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,
Frank Sorensone08ea3a2016-07-08 16:35:24 -0500140 .extra1 = &xprt_min_resvport,
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
333 if (!(buf->flags & XDRBUF_SPARSE_PAGES))
334 return want;
335 if (want > buf->page_len)
336 want = buf->page_len;
337 n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT;
338 for (i = 0; i < n; i++) {
339 if (buf->pages[i])
340 continue;
341 buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp);
342 if (!buf->pages[i]) {
343 buf->page_len = (i * PAGE_SIZE) - buf->page_base;
344 return buf->page_len;
345 }
346 }
347 return want;
348}
349
350static ssize_t
351xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek)
352{
353 ssize_t ret;
354 if (seek != 0)
355 iov_iter_advance(&msg->msg_iter, seek);
356 ret = sock_recvmsg(sock, msg, flags);
357 return ret > 0 ? ret + seek : ret;
358}
359
360static ssize_t
361xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags,
362 struct kvec *kvec, size_t count, size_t seek)
363{
364 iov_iter_kvec(&msg->msg_iter, READ | ITER_KVEC, kvec, 1, count);
365 return xs_sock_recvmsg(sock, msg, flags, seek);
366}
367
368static ssize_t
369xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags,
370 struct bio_vec *bvec, unsigned long nr, size_t count,
371 size_t seek)
372{
373 iov_iter_bvec(&msg->msg_iter, READ | ITER_BVEC, bvec, nr, count);
374 return xs_sock_recvmsg(sock, msg, flags, seek);
375}
376
377static ssize_t
378xs_read_discard(struct socket *sock, struct msghdr *msg, int flags,
379 size_t count)
380{
381 struct kvec kvec = { 0 };
382 return xs_read_kvec(sock, msg, flags | MSG_TRUNC, &kvec, count, 0);
383}
384
385static ssize_t
386xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags,
387 struct xdr_buf *buf, size_t count, size_t seek, size_t *read)
388{
389 size_t want, seek_init = seek, offset = 0;
390 ssize_t ret;
391
392 if (seek < buf->head[0].iov_len) {
393 want = min_t(size_t, count, buf->head[0].iov_len);
394 ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek);
395 if (ret <= 0)
396 goto sock_err;
397 offset += ret;
398 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
399 goto out;
400 if (ret != want)
401 goto eagain;
402 seek = 0;
403 } else {
404 seek -= buf->head[0].iov_len;
405 offset += buf->head[0].iov_len;
406 }
407 if (seek < buf->page_len) {
408 want = xs_alloc_sparse_pages(buf,
409 min_t(size_t, count - offset, buf->page_len),
410 GFP_NOWAIT);
411 ret = xs_read_bvec(sock, msg, flags, buf->bvec,
412 xdr_buf_pagecount(buf),
413 want + buf->page_base,
414 seek + buf->page_base);
415 if (ret <= 0)
416 goto sock_err;
417 offset += ret - buf->page_base;
418 if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
419 goto out;
420 if (ret != want)
421 goto eagain;
422 seek = 0;
423 } else {
424 seek -= buf->page_len;
425 offset += buf->page_len;
426 }
427 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)
436 goto eagain;
437 } else
438 offset += buf->tail[0].iov_len;
439 ret = -EMSGSIZE;
440 msg->msg_flags |= MSG_TRUNC;
441out:
442 *read = offset - seek_init;
443 return ret;
444eagain:
445 ret = -EAGAIN;
446 goto out;
447sock_err:
448 offset += seek;
449 goto out;
450}
451
452static void
453xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf)
454{
455 if (!transport->recv.copied) {
456 if (buf->head[0].iov_len >= transport->recv.offset)
457 memcpy(buf->head[0].iov_base,
458 &transport->recv.xid,
459 transport->recv.offset);
460 transport->recv.copied = transport->recv.offset;
461 }
462}
463
464static bool
465xs_read_stream_request_done(struct sock_xprt *transport)
466{
467 return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT);
468}
469
470static ssize_t
471xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg,
472 int flags, struct rpc_rqst *req)
473{
474 struct xdr_buf *buf = &req->rq_private_buf;
475 size_t want, read;
476 ssize_t ret;
477
478 xs_read_header(transport, buf);
479
480 want = transport->recv.len - transport->recv.offset;
481 ret = xs_read_xdr_buf(transport->sock, msg, flags, buf,
482 transport->recv.copied + want, transport->recv.copied,
483 &read);
484 transport->recv.offset += read;
485 transport->recv.copied += read;
486 if (transport->recv.offset == transport->recv.len) {
487 if (xs_read_stream_request_done(transport))
488 msg->msg_flags |= MSG_EOR;
489 return transport->recv.copied;
490 }
491
492 switch (ret) {
493 case -EMSGSIZE:
494 return transport->recv.copied;
495 case 0:
496 return -ESHUTDOWN;
497 default:
498 if (ret < 0)
499 return ret;
500 }
501 return -EAGAIN;
502}
503
504static size_t
505xs_read_stream_headersize(bool isfrag)
506{
507 if (isfrag)
508 return sizeof(__be32);
509 return 3 * sizeof(__be32);
510}
511
512static ssize_t
513xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg,
514 int flags, size_t want, size_t seek)
515{
516 struct kvec kvec = {
517 .iov_base = &transport->recv.fraghdr,
518 .iov_len = want,
519 };
520 return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek);
521}
522
523#if defined(CONFIG_SUNRPC_BACKCHANNEL)
524static ssize_t
525xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
526{
527 struct rpc_xprt *xprt = &transport->xprt;
528 struct rpc_rqst *req;
529 ssize_t ret;
530
531 /* Look up and lock the request corresponding to the given XID */
532 req = xprt_lookup_bc_request(xprt, transport->recv.xid);
533 if (!req) {
534 printk(KERN_WARNING "Callback slot table overflowed\n");
535 return -ESHUTDOWN;
536 }
537
538 ret = xs_read_stream_request(transport, msg, flags, req);
539 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
540 xprt_complete_bc_request(req, ret);
541
542 return ret;
543}
544#else /* CONFIG_SUNRPC_BACKCHANNEL */
545static ssize_t
546xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
547{
548 return -ESHUTDOWN;
549}
550#endif /* CONFIG_SUNRPC_BACKCHANNEL */
551
552static ssize_t
553xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags)
554{
555 struct rpc_xprt *xprt = &transport->xprt;
556 struct rpc_rqst *req;
557 ssize_t ret = 0;
558
559 /* Look up and lock the request corresponding to the given XID */
560 spin_lock(&xprt->queue_lock);
561 req = xprt_lookup_rqst(xprt, transport->recv.xid);
562 if (!req) {
563 msg->msg_flags |= MSG_TRUNC;
564 goto out;
565 }
566 xprt_pin_rqst(req);
567 spin_unlock(&xprt->queue_lock);
568
569 ret = xs_read_stream_request(transport, msg, flags, req);
570
571 spin_lock(&xprt->queue_lock);
572 if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
573 xprt_complete_rqst(req->rq_task, ret);
574 xprt_unpin_rqst(req);
575out:
576 spin_unlock(&xprt->queue_lock);
577 return ret;
578}
579
580static ssize_t
581xs_read_stream(struct sock_xprt *transport, int flags)
582{
583 struct msghdr msg = { 0 };
584 size_t want, read = 0;
585 ssize_t ret = 0;
586
587 if (transport->recv.len == 0) {
588 want = xs_read_stream_headersize(transport->recv.copied != 0);
589 ret = xs_read_stream_header(transport, &msg, flags, want,
590 transport->recv.offset);
591 if (ret <= 0)
592 goto out_err;
593 transport->recv.offset = ret;
594 if (ret != want) {
595 ret = -EAGAIN;
596 goto out_err;
597 }
598 transport->recv.len = be32_to_cpu(transport->recv.fraghdr) &
599 RPC_FRAGMENT_SIZE_MASK;
600 transport->recv.offset -= sizeof(transport->recv.fraghdr);
601 read = ret;
602 }
603
604 switch (be32_to_cpu(transport->recv.calldir)) {
605 case RPC_CALL:
606 ret = xs_read_stream_call(transport, &msg, flags);
607 break;
608 case RPC_REPLY:
609 ret = xs_read_stream_reply(transport, &msg, flags);
610 }
611 if (msg.msg_flags & MSG_TRUNC) {
612 transport->recv.calldir = cpu_to_be32(-1);
613 transport->recv.copied = -1;
614 }
615 if (ret < 0)
616 goto out_err;
617 read += ret;
618 if (transport->recv.offset < transport->recv.len) {
619 ret = xs_read_discard(transport->sock, &msg, flags,
620 transport->recv.len - transport->recv.offset);
621 if (ret <= 0)
622 goto out_err;
623 transport->recv.offset += ret;
624 read += ret;
625 if (transport->recv.offset != transport->recv.len)
626 return -EAGAIN;
627 }
628 if (xs_read_stream_request_done(transport)) {
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400629 trace_xs_stream_read_request(transport);
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400630 transport->recv.copied = 0;
631 }
632 transport->recv.offset = 0;
633 transport->recv.len = 0;
634 return read;
635out_err:
636 switch (ret) {
637 case 0:
638 case -ESHUTDOWN:
639 xprt_force_disconnect(&transport->xprt);
640 return -ESHUTDOWN;
641 }
642 return ret;
643}
644
Trond Myklebustc50b8ee2018-09-14 14:26:28 -0400645static void xs_stream_data_receive(struct sock_xprt *transport)
646{
647 size_t read = 0;
648 ssize_t ret = 0;
649
650 mutex_lock(&transport->recv_mutex);
651 if (transport->sock == NULL)
652 goto out;
653 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
654 for (;;) {
655 ret = xs_read_stream(transport, MSG_DONTWAIT);
656 if (ret <= 0)
657 break;
658 read += ret;
659 cond_resched();
660 }
661out:
662 mutex_unlock(&transport->recv_mutex);
663 trace_xs_stream_read_data(&transport->xprt, ret, read);
664}
665
666static void xs_stream_data_receive_workfn(struct work_struct *work)
667{
668 struct sock_xprt *transport =
669 container_of(work, struct sock_xprt, recv_worker);
670 xs_stream_data_receive(transport);
671}
672
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400673#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
674
Trond Myklebust24c56842006-10-17 15:06:22 -0400675static 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 -0400676{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400677 struct msghdr msg = {
678 .msg_name = addr,
679 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400680 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
681 };
682 struct kvec iov = {
683 .iov_base = vec->iov_base + base,
684 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400685 };
686
Trond Myklebust24c56842006-10-17 15:06:22 -0400687 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400688 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
689 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
690}
691
Jason Baronf279cd02014-09-24 18:08:00 +0000692static 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 -0400693{
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500694 ssize_t (*do_sendpage)(struct socket *sock, struct page *page,
695 int offset, size_t size, int flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400696 struct page **ppage;
697 unsigned int remainder;
Jason Baronf279cd02014-09-24 18:08:00 +0000698 int err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400699
Trond Myklebust24c56842006-10-17 15:06:22 -0400700 remainder = xdr->page_len - base;
701 base += xdr->page_base;
702 ppage = xdr->pages + (base >> PAGE_SHIFT);
703 base &= ~PAGE_MASK;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500704 do_sendpage = sock->ops->sendpage;
705 if (!zerocopy)
706 do_sendpage = sock_no_sendpage;
Trond Myklebust24c56842006-10-17 15:06:22 -0400707 for(;;) {
708 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
709 int flags = XS_SENDMSG_FLAGS;
710
711 remainder -= len;
Trond Myklebust31303d62015-10-06 15:59:20 -0400712 if (more)
Trond Myklebust24c56842006-10-17 15:06:22 -0400713 flags |= MSG_MORE;
Trond Myklebust31303d62015-10-06 15:59:20 -0400714 if (remainder != 0)
715 flags |= MSG_SENDPAGE_NOTLAST | MSG_MORE;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500716 err = do_sendpage(sock, *ppage, base, len, flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400717 if (remainder == 0 || err != len)
718 break;
Jason Baronf279cd02014-09-24 18:08:00 +0000719 *sent_p += err;
Trond Myklebust24c56842006-10-17 15:06:22 -0400720 ppage++;
721 base = 0;
722 }
Jason Baronf279cd02014-09-24 18:08:00 +0000723 if (err > 0) {
724 *sent_p += err;
725 err = 0;
726 }
727 return err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400728}
729
Chuck Lever9903cd12005-08-11 16:25:26 -0400730/**
731 * xs_sendpages - write pages directly to a socket
732 * @sock: socket to send on
733 * @addr: UDP only -- address of destination
734 * @addrlen: UDP only -- length of destination address
735 * @xdr: buffer containing this request
736 * @base: starting position in the buffer
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500737 * @zerocopy: true if it is safe to use sendpage()
Jason Baronf279cd02014-09-24 18:08:00 +0000738 * @sent_p: return the total number of bytes successfully queued for sending
Chuck Lever9903cd12005-08-11 16:25:26 -0400739 *
Chuck Levera246b012005-08-11 16:25:23 -0400740 */
Jason Baronf279cd02014-09-24 18:08:00 +0000741static 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 -0400742{
Trond Myklebust24c56842006-10-17 15:06:22 -0400743 unsigned int remainder = xdr->len - base;
Jason Baronf279cd02014-09-24 18:08:00 +0000744 int err = 0;
745 int sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400746
Chuck Lever262965f2005-08-11 16:25:56 -0400747 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400748 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400749
Trond Myklebust24c56842006-10-17 15:06:22 -0400750 if (base != 0) {
751 addr = NULL;
752 addrlen = 0;
753 }
Chuck Lever262965f2005-08-11 16:25:56 -0400754
Trond Myklebust24c56842006-10-17 15:06:22 -0400755 if (base < xdr->head[0].iov_len || addr != NULL) {
756 unsigned int len = xdr->head[0].iov_len - base;
757 remainder -= len;
758 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
759 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400760 goto out;
Jason Baronf279cd02014-09-24 18:08:00 +0000761 *sent_p += err;
Chuck Levera246b012005-08-11 16:25:23 -0400762 base = 0;
763 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400764 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400765
Trond Myklebust24c56842006-10-17 15:06:22 -0400766 if (base < xdr->page_len) {
767 unsigned int len = xdr->page_len - base;
768 remainder -= len;
Jason Baronf279cd02014-09-24 18:08:00 +0000769 err = xs_send_pagedata(sock, xdr, base, remainder != 0, zerocopy, &sent);
770 *sent_p += sent;
771 if (remainder == 0 || sent != len)
Chuck Levera246b012005-08-11 16:25:23 -0400772 goto out;
773 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400774 } else
775 base -= xdr->page_len;
776
777 if (base >= xdr->tail[0].iov_len)
Jason Baronf279cd02014-09-24 18:08:00 +0000778 return 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400779 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400780out:
Jason Baronf279cd02014-09-24 18:08:00 +0000781 if (err > 0) {
782 *sent_p += err;
783 err = 0;
784 }
785 return err;
Chuck Levera246b012005-08-11 16:25:23 -0400786}
787
Chuck Lever9903cd12005-08-11 16:25:26 -0400788/**
Trond Myklebustc5445772018-09-03 23:39:27 -0400789 * xs_nospace - handle transmit was incomplete
Trond Myklebust50f484e2018-08-30 13:27:29 -0400790 * @req: pointer to RPC request
Chuck Lever9903cd12005-08-11 16:25:26 -0400791 *
Chuck Levera246b012005-08-11 16:25:23 -0400792 */
Trond Myklebustc5445772018-09-03 23:39:27 -0400793static int xs_nospace(struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -0400794{
Chuck Lever262965f2005-08-11 16:25:56 -0400795 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500796 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500797 struct sock *sk = transport->inet;
Trond Myklebust24ca9a82011-11-22 14:44:28 +0200798 int ret = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400799
Chuck Lever46121cf2007-01-31 12:14:08 -0500800 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Trond Myklebustc5445772018-09-03 23:39:27 -0400801 req->rq_task->tk_pid,
802 req->rq_slen - transport->xmit.offset,
Chuck Lever262965f2005-08-11 16:25:56 -0400803 req->rq_slen);
804
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400805 /* Protect against races with write_space */
806 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400807
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400808 /* Don't race with disconnect */
809 if (xprt_connected(xprt)) {
Trond Myklebust13331a52016-01-06 08:57:06 -0500810 /* wait for more buffer space */
811 sk->sk_write_pending++;
Trond Myklebustc5445772018-09-03 23:39:27 -0400812 xprt_wait_for_buffer_space(xprt);
Trond Myklebust13331a52016-01-06 08:57:06 -0500813 } else
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400814 ret = -ENOTCONN;
Chuck Lever262965f2005-08-11 16:25:56 -0400815
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400816 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500817
818 /* Race breaker in case memory is freed before above code is called */
David Vrabeld48f9ce2016-09-19 13:58:30 +0100819 if (ret == -EAGAIN) {
820 struct socket_wq *wq;
821
822 rcu_read_lock();
823 wq = rcu_dereference(sk->sk_wq);
824 set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags);
825 rcu_read_unlock();
826
827 sk->sk_write_space(sk);
828 }
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400829 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400830}
831
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400832static void
833xs_stream_prepare_request(struct rpc_rqst *req)
834{
835 req->rq_task->tk_status = xdr_alloc_bvec(&req->rq_rcv_buf, GFP_NOIO);
836}
837
Chuck Lever61677ee2011-05-09 15:22:34 -0400838/*
Trond Myklebust4cd34e72018-08-31 10:00:02 -0400839 * Determine if the previous message in the stream was aborted before it
840 * could complete transmission.
841 */
842static bool
843xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
844{
845 return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
846}
847
848/*
Chuck Lever61677ee2011-05-09 15:22:34 -0400849 * Construct a stream transport record marker in @buf.
850 */
851static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
852{
853 u32 reclen = buf->len - sizeof(rpc_fraghdr);
854 rpc_fraghdr *base = buf->head[0].iov_base;
855 *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
856}
857
Chuck Lever262965f2005-08-11 16:25:56 -0400858/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400859 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
Trond Myklebust50f484e2018-08-30 13:27:29 -0400860 * @req: pointer to RPC request
Chuck Lever176e21e2011-05-09 15:22:44 -0400861 *
862 * Return values:
863 * 0: The request has been sent
864 * EAGAIN: The socket was blocked, please call again later to
865 * complete the request
866 * ENOTCONN: Caller needs to invoke connect logic then call again
867 * other: Some other error occured, the request was not sent
868 */
Trond Myklebustadfa7142018-09-03 23:58:59 -0400869static int xs_local_send_request(struct rpc_rqst *req)
Chuck Lever176e21e2011-05-09 15:22:44 -0400870{
Chuck Lever176e21e2011-05-09 15:22:44 -0400871 struct rpc_xprt *xprt = req->rq_xprt;
872 struct sock_xprt *transport =
873 container_of(xprt, struct sock_xprt, xprt);
874 struct xdr_buf *xdr = &req->rq_snd_buf;
875 int status;
Jason Baronf279cd02014-09-24 18:08:00 +0000876 int sent = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -0400877
Trond Myklebust4cd34e72018-08-31 10:00:02 -0400878 /* Close the stream if the previous transmission was incomplete */
879 if (xs_send_request_was_aborted(transport, req)) {
880 xs_close(xprt);
881 return -ENOTCONN;
882 }
883
Chuck Lever176e21e2011-05-09 15:22:44 -0400884 xs_encode_stream_record_marker(&req->rq_snd_buf);
885
886 xs_pktdump("packet data:",
887 req->rq_svec->iov_base, req->rq_svec->iov_len);
888
Chuck Lever78215752018-03-05 15:13:07 -0500889 req->rq_xtime = ktime_get();
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400890 status = xs_sendpages(transport->sock, NULL, 0, xdr,
891 transport->xmit.offset,
Jason Baronf279cd02014-09-24 18:08:00 +0000892 true, &sent);
Chuck Lever176e21e2011-05-09 15:22:44 -0400893 dprintk("RPC: %s(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400894 __func__, xdr->len - transport->xmit.offset, status);
NeilBrown743c69e2015-07-27 10:55:35 +1000895
896 if (status == -EAGAIN && sock_writeable(transport->inet))
897 status = -ENOBUFS;
898
Jason Baronf279cd02014-09-24 18:08:00 +0000899 if (likely(sent > 0) || status == 0) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400900 transport->xmit.offset += sent;
901 req->rq_bytes_sent = transport->xmit.offset;
Chuck Lever176e21e2011-05-09 15:22:44 -0400902 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400903 req->rq_xmit_bytes_sent += transport->xmit.offset;
Chuck Lever176e21e2011-05-09 15:22:44 -0400904 req->rq_bytes_sent = 0;
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400905 transport->xmit.offset = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -0400906 return 0;
907 }
908 status = -EAGAIN;
909 }
910
911 switch (status) {
Trond Myklebust3601c4a2014-06-30 13:42:19 -0400912 case -ENOBUFS:
Trond Myklebustb5872f02015-07-03 09:32:23 -0400913 break;
Chuck Lever176e21e2011-05-09 15:22:44 -0400914 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -0400915 status = xs_nospace(req);
Chuck Lever176e21e2011-05-09 15:22:44 -0400916 break;
917 default:
918 dprintk("RPC: sendmsg returned unrecognized error %d\n",
919 -status);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -0500920 /* fall through */
Chuck Lever176e21e2011-05-09 15:22:44 -0400921 case -EPIPE:
922 xs_close(xprt);
923 status = -ENOTCONN;
924 }
925
926 return status;
927}
928
929/**
Chuck Lever262965f2005-08-11 16:25:56 -0400930 * xs_udp_send_request - write an RPC request to a UDP socket
Trond Myklebust50f484e2018-08-30 13:27:29 -0400931 * @req: pointer to RPC request
Chuck Lever262965f2005-08-11 16:25:56 -0400932 *
933 * Return values:
934 * 0: The request has been sent
935 * EAGAIN: The socket was blocked, please call again later to
936 * complete the request
937 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300938 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -0400939 */
Trond Myklebustadfa7142018-09-03 23:58:59 -0400940static int xs_udp_send_request(struct rpc_rqst *req)
Chuck Lever262965f2005-08-11 16:25:56 -0400941{
Chuck Lever262965f2005-08-11 16:25:56 -0400942 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500943 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400944 struct xdr_buf *xdr = &req->rq_snd_buf;
Jason Baronf279cd02014-09-24 18:08:00 +0000945 int sent = 0;
Chuck Lever262965f2005-08-11 16:25:56 -0400946 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400947
Chuck Lever9903cd12005-08-11 16:25:26 -0400948 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400949 req->rq_svec->iov_base,
950 req->rq_svec->iov_len);
951
Trond Myklebust01d37c42009-03-11 14:09:39 -0400952 if (!xprt_bound(xprt))
953 return -ENOTCONN;
Trond Myklebust75891f52018-09-03 17:37:36 -0400954
955 if (!xprt_request_get_cong(xprt, req))
956 return -EBADSLT;
957
Chuck Lever78215752018-03-05 15:13:07 -0500958 req->rq_xtime = ktime_get();
Jason Baronf279cd02014-09-24 18:08:00 +0000959 status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen,
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400960 xdr, 0, true, &sent);
Chuck Levera246b012005-08-11 16:25:23 -0400961
Chuck Lever46121cf2007-01-31 12:14:08 -0500962 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400963 xdr->len, status);
Chuck Levera246b012005-08-11 16:25:23 -0400964
Jason Baron3dedbb52014-09-24 18:08:04 +0000965 /* firewall is blocking us, don't return -EAGAIN or we end up looping */
966 if (status == -EPERM)
967 goto process_status;
968
NeilBrown743c69e2015-07-27 10:55:35 +1000969 if (status == -EAGAIN && sock_writeable(transport->inet))
970 status = -ENOBUFS;
971
Jason Baronf279cd02014-09-24 18:08:00 +0000972 if (sent > 0 || status == 0) {
973 req->rq_xmit_bytes_sent += sent;
974 if (sent >= req->rq_slen)
Trond Myklebust21997002007-10-01 11:43:37 -0400975 return 0;
976 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400977 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400978 }
Chuck Levera246b012005-08-11 16:25:23 -0400979
Jason Baron3dedbb52014-09-24 18:08:04 +0000980process_status:
Chuck Lever262965f2005-08-11 16:25:56 -0400981 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400982 case -ENOTSOCK:
983 status = -ENOTCONN;
984 /* Should we call xs_close() here? */
985 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400986 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -0400987 status = xs_nospace(req);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400988 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400989 case -ENETUNREACH:
Trond Myklebust3601c4a2014-06-30 13:42:19 -0400990 case -ENOBUFS:
Chuck Lever262965f2005-08-11 16:25:56 -0400991 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400992 case -ECONNREFUSED:
Jason Baron3dedbb52014-09-24 18:08:04 +0000993 case -EPERM:
Chuck Levera246b012005-08-11 16:25:23 -0400994 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400995 * prompts ECONNREFUSED. */
Trond Myklebust13331a52016-01-06 08:57:06 -0500996 break;
997 default:
998 dprintk("RPC: sendmsg returned unrecognized error %d\n",
999 -status);
Chuck Levera246b012005-08-11 16:25:23 -04001000 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +08001001
Chuck Lever262965f2005-08-11 16:25:56 -04001002 return status;
Chuck Levera246b012005-08-11 16:25:23 -04001003}
1004
Trond Myklebuste06799f2007-11-05 15:44:12 -05001005/**
Chuck Lever262965f2005-08-11 16:25:56 -04001006 * xs_tcp_send_request - write an RPC request to a TCP socket
Trond Myklebust50f484e2018-08-30 13:27:29 -04001007 * @req: pointer to RPC request
Chuck Lever9903cd12005-08-11 16:25:26 -04001008 *
1009 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -04001010 * 0: The request has been sent
1011 * EAGAIN: The socket was blocked, please call again later to
1012 * complete the request
1013 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001014 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -04001015 *
1016 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -04001017 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -04001018 */
Trond Myklebustadfa7142018-09-03 23:58:59 -04001019static int xs_tcp_send_request(struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001020{
Chuck Levera246b012005-08-11 16:25:23 -04001021 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001022 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -04001023 struct xdr_buf *xdr = &req->rq_snd_buf;
Trond Myklebusta6b31d12013-11-08 16:03:50 -05001024 bool zerocopy = true;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001025 bool vm_wait = false;
Chuck Leverb595bb12007-08-06 11:56:42 -04001026 int status;
Jason Baronf279cd02014-09-24 18:08:00 +00001027 int sent;
Chuck Levera246b012005-08-11 16:25:23 -04001028
Trond Myklebust4cd34e72018-08-31 10:00:02 -04001029 /* Close the stream if the previous transmission was incomplete */
1030 if (xs_send_request_was_aborted(transport, req)) {
1031 if (transport->sock != NULL)
1032 kernel_sock_shutdown(transport->sock, SHUT_RDWR);
1033 return -ENOTCONN;
1034 }
1035
Chuck Lever61677ee2011-05-09 15:22:34 -04001036 xs_encode_stream_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -04001037
Chuck Lever262965f2005-08-11 16:25:56 -04001038 xs_pktdump("packet data:",
1039 req->rq_svec->iov_base,
1040 req->rq_svec->iov_len);
Trond Myklebusta6b31d12013-11-08 16:03:50 -05001041 /* Don't use zero copy if this is a resend. If the RPC call
1042 * completes while the socket holds a reference to the pages,
1043 * then we may end up resending corrupted data.
1044 */
Trond Myklebust50f484e2018-08-30 13:27:29 -04001045 if (req->rq_task->tk_flags & RPC_TASK_SENT)
Trond Myklebusta6b31d12013-11-08 16:03:50 -05001046 zerocopy = false;
Chuck Levera246b012005-08-11 16:25:23 -04001047
Trond Myklebust7196dbb2017-02-08 11:17:54 -05001048 if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
1049 xs_tcp_set_socket_timeouts(xprt, transport->sock);
1050
Chuck Levera246b012005-08-11 16:25:23 -04001051 /* Continue transmitting the packet/record. We must be careful
1052 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -04001053 * called sendmsg(). */
Chuck Lever78215752018-03-05 15:13:07 -05001054 req->rq_xtime = ktime_get();
Chuck Levera246b012005-08-11 16:25:23 -04001055 while (1) {
Jason Baronf279cd02014-09-24 18:08:00 +00001056 sent = 0;
1057 status = xs_sendpages(transport->sock, NULL, 0, xdr,
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001058 transport->xmit.offset,
1059 zerocopy, &sent);
Chuck Levera246b012005-08-11 16:25:23 -04001060
Chuck Lever46121cf2007-01-31 12:14:08 -05001061 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001062 xdr->len - transport->xmit.offset, status);
Chuck Lever262965f2005-08-11 16:25:56 -04001063
Chuck Lever262965f2005-08-11 16:25:56 -04001064 /* If we've sent the entire packet, immediately
1065 * reset the count of bytes sent. */
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001066 transport->xmit.offset += sent;
1067 req->rq_bytes_sent = transport->xmit.offset;
Chuck Lever262965f2005-08-11 16:25:56 -04001068 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001069 req->rq_xmit_bytes_sent += transport->xmit.offset;
Chuck Lever262965f2005-08-11 16:25:56 -04001070 req->rq_bytes_sent = 0;
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001071 transport->xmit.offset = 0;
Chuck Lever262965f2005-08-11 16:25:56 -04001072 return 0;
Chuck Levera246b012005-08-11 16:25:23 -04001073 }
1074
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001075 WARN_ON_ONCE(sent == 0 && status == 0);
1076
1077 if (status == -EAGAIN ) {
1078 /*
1079 * Return EAGAIN if we're sure we're hitting the
1080 * socket send buffer limits.
1081 */
1082 if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
1083 break;
1084 /*
1085 * Did we hit a memory allocation failure?
1086 */
1087 if (sent == 0) {
1088 status = -ENOBUFS;
1089 if (vm_wait)
1090 break;
1091 /* Retry, knowing now that we're below the
1092 * socket send buffer limit
1093 */
1094 vm_wait = true;
1095 }
1096 continue;
1097 }
Trond Myklebustf580dd02015-07-11 17:48:52 +02001098 if (status < 0)
1099 break;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001100 vm_wait = false;
Chuck Levera246b012005-08-11 16:25:23 -04001101 }
1102
Chuck Lever262965f2005-08-11 16:25:56 -04001103 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -04001104 case -ENOTSOCK:
1105 status = -ENOTCONN;
1106 /* Should we call xs_close() here? */
1107 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001108 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -04001109 status = xs_nospace(req);
Chuck Lever262965f2005-08-11 16:25:56 -04001110 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001111 case -ECONNRESET:
Chuck Lever262965f2005-08-11 16:25:56 -04001112 case -ECONNREFUSED:
1113 case -ENOTCONN:
Trond Myklebust3913c782015-02-08 21:44:04 -05001114 case -EADDRINUSE:
Trond Myklebustb5872f02015-07-03 09:32:23 -04001115 case -ENOBUFS:
Trond Myklebustb9d2bb22012-10-23 11:40:02 -04001116 case -EPIPE:
Trond Myklebust13331a52016-01-06 08:57:06 -05001117 break;
1118 default:
1119 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1120 -status);
Chuck Levera246b012005-08-11 16:25:23 -04001121 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +08001122
Chuck Levera246b012005-08-11 16:25:23 -04001123 return status;
1124}
1125
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001126static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1127{
1128 transport->old_data_ready = sk->sk_data_ready;
1129 transport->old_state_change = sk->sk_state_change;
1130 transport->old_write_space = sk->sk_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -05001131 transport->old_error_report = sk->sk_error_report;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001132}
1133
1134static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1135{
1136 sk->sk_data_ready = transport->old_data_ready;
1137 sk->sk_state_change = transport->old_state_change;
1138 sk->sk_write_space = transport->old_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -05001139 sk->sk_error_report = transport->old_error_report;
1140}
1141
Trond Myklebust42d42a52016-05-23 09:24:55 -04001142static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
1143{
1144 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1145
1146 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
1147}
1148
Trond Myklebustb70ae912015-02-09 09:41:32 -05001149static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1150{
1151 smp_mb__before_atomic();
1152 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1153 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001154 xs_sock_reset_state_flags(xprt);
Trond Myklebustb70ae912015-02-09 09:41:32 -05001155 smp_mb__after_atomic();
1156}
1157
Trond Myklebust21180712013-12-31 13:22:59 -05001158/**
1159 * xs_error_report - callback to handle TCP socket state errors
1160 * @sk: socket
1161 *
1162 * Note: we don't call sock_error() since there may be a rpc_task
1163 * using the socket, and so we don't want to clear sk->sk_err.
1164 */
1165static void xs_error_report(struct sock *sk)
1166{
1167 struct rpc_xprt *xprt;
1168 int err;
1169
1170 read_lock_bh(&sk->sk_callback_lock);
1171 if (!(xprt = xprt_from_sock(sk)))
1172 goto out;
1173
1174 err = -sk->sk_err;
1175 if (err == 0)
1176 goto out;
1177 dprintk("RPC: xs_error_report client %p, error=%d...\n",
1178 xprt, -err);
Trond Myklebuste8353c762013-12-31 13:39:22 -05001179 trace_rpc_socket_error(xprt, sk->sk_socket, err);
Trond Myklebust21180712013-12-31 13:22:59 -05001180 xprt_wake_pending_tasks(xprt, err);
1181 out:
1182 read_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001183}
1184
Chuck Leverfe315e72009-03-11 14:10:21 -04001185static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001186{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001187 struct socket *sock = transport->sock;
1188 struct sock *sk = transport->inet;
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001189 struct rpc_xprt *xprt = &transport->xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001190
Chuck Leverfe315e72009-03-11 14:10:21 -04001191 if (sk == NULL)
1192 return;
Chuck Lever9903cd12005-08-11 16:25:26 -04001193
Jeff Layton264d1df2015-06-03 16:14:27 -04001194 if (atomic_read(&transport->xprt.swapper))
1195 sk_clear_memalloc(sk);
1196
Trond Myklebust09939202015-08-29 19:11:21 -07001197 kernel_sock_shutdown(sock, SHUT_RDWR);
1198
Trond Myklebustedc1b012015-10-05 10:53:49 -04001199 mutex_lock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -04001200 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001201 transport->inet = NULL;
1202 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -04001203
Chuck Lever9903cd12005-08-11 16:25:26 -04001204 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001205
1206 xs_restore_old_callbacks(transport, sk);
Trond Myklebust0c787892015-08-29 13:36:30 -07001207 xprt_clear_connected(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001208 write_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001209 xs_sock_reset_connection_flags(xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04001210 mutex_unlock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -04001211
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001212 trace_rpc_socket_close(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001213 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -04001214}
1215
1216/**
1217 * xs_close - close a socket
1218 * @xprt: transport
1219 *
1220 * This is used when all requests are complete; ie, no DRC state remains
1221 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -04001222 *
1223 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1224 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e72009-03-11 14:10:21 -04001225 */
1226static void xs_close(struct rpc_xprt *xprt)
1227{
1228 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1229
1230 dprintk("RPC: xs_close xprt %p\n", xprt);
1231
1232 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -04001233 xprt->reestablish_timeout = 0;
Chuck Leverfe315e72009-03-11 14:10:21 -04001234
Trond Myklebust62da3b22007-11-06 18:44:20 -05001235 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001236}
1237
Chuck Lever4a068252015-05-11 14:02:25 -04001238static void xs_inject_disconnect(struct rpc_xprt *xprt)
1239{
1240 dprintk("RPC: injecting transport disconnect on xprt=%p\n",
1241 xprt);
1242 xprt_disconnect_done(xprt);
1243}
1244
Kinglong Mee315f3812014-03-24 11:07:22 +08001245static void xs_xprt_free(struct rpc_xprt *xprt)
1246{
1247 xs_free_peer_addresses(xprt);
1248 xprt_free(xprt);
1249}
1250
Chuck Lever9903cd12005-08-11 16:25:26 -04001251/**
1252 * xs_destroy - prepare to shutdown a transport
1253 * @xprt: doomed transport
1254 *
1255 */
1256static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001257{
Trond Myklebust03c788272015-09-17 10:42:27 -04001258 struct sock_xprt *transport = container_of(xprt,
1259 struct sock_xprt, xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05001260 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001261
Trond Myklebust03c788272015-09-17 10:42:27 -04001262 cancel_delayed_work_sync(&transport->connect_worker);
Trond Myklebusta1311d82013-10-31 09:18:49 -04001263 xs_close(xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04001264 cancel_work_sync(&transport->recv_worker);
Kinglong Mee315f3812014-03-24 11:07:22 +08001265 xs_xprt_free(xprt);
Trond Myklebusta1311d82013-10-31 09:18:49 -04001266 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -04001267}
1268
Chuck Lever176e21e2011-05-09 15:22:44 -04001269static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
1270{
1271 struct xdr_skb_reader desc = {
1272 .skb = skb,
1273 .offset = sizeof(rpc_fraghdr),
1274 .count = skb->len - sizeof(rpc_fraghdr),
1275 };
1276
1277 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
1278 return -1;
1279 if (desc.count)
1280 return -1;
1281 return 0;
1282}
1283
1284/**
Trond Myklebusta2648092015-10-06 17:03:00 -04001285 * xs_local_data_read_skb
1286 * @xprt: transport
1287 * @sk: socket
1288 * @skb: skbuff
Chuck Lever176e21e2011-05-09 15:22:44 -04001289 *
1290 * Currently this assumes we can read the whole reply in a single gulp.
1291 */
Trond Myklebusta2648092015-10-06 17:03:00 -04001292static void xs_local_data_read_skb(struct rpc_xprt *xprt,
1293 struct sock *sk,
1294 struct sk_buff *skb)
Chuck Lever176e21e2011-05-09 15:22:44 -04001295{
1296 struct rpc_task *task;
Chuck Lever176e21e2011-05-09 15:22:44 -04001297 struct rpc_rqst *rovr;
Trond Myklebusta2648092015-10-06 17:03:00 -04001298 int repsize, copied;
Chuck Lever176e21e2011-05-09 15:22:44 -04001299 u32 _xid;
1300 __be32 *xp;
1301
Chuck Lever176e21e2011-05-09 15:22:44 -04001302 repsize = skb->len - sizeof(rpc_fraghdr);
1303 if (repsize < 4) {
1304 dprintk("RPC: impossible RPC reply size %d\n", repsize);
Trond Myklebusta2648092015-10-06 17:03:00 -04001305 return;
Chuck Lever176e21e2011-05-09 15:22:44 -04001306 }
1307
1308 /* Copy the XID from the skb... */
1309 xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
1310 if (xp == NULL)
Trond Myklebusta2648092015-10-06 17:03:00 -04001311 return;
Chuck Lever176e21e2011-05-09 15:22:44 -04001312
1313 /* Look up and lock the request corresponding to the given XID */
Trond Myklebust75c84152018-08-31 10:21:00 -04001314 spin_lock(&xprt->queue_lock);
Chuck Lever176e21e2011-05-09 15:22:44 -04001315 rovr = xprt_lookup_rqst(xprt, *xp);
1316 if (!rovr)
1317 goto out_unlock;
Trond Myklebust729749b2017-08-13 10:03:59 -04001318 xprt_pin_rqst(rovr);
Trond Myklebust75c84152018-08-31 10:21:00 -04001319 spin_unlock(&xprt->queue_lock);
Chuck Lever176e21e2011-05-09 15:22:44 -04001320 task = rovr->rq_task;
1321
1322 copied = rovr->rq_private_buf.buflen;
1323 if (copied > repsize)
1324 copied = repsize;
1325
1326 if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
1327 dprintk("RPC: sk_buff copy failed\n");
Trond Myklebust75c84152018-08-31 10:21:00 -04001328 spin_lock(&xprt->queue_lock);
Trond Myklebust729749b2017-08-13 10:03:59 -04001329 goto out_unpin;
Chuck Lever176e21e2011-05-09 15:22:44 -04001330 }
1331
Trond Myklebust75c84152018-08-31 10:21:00 -04001332 spin_lock(&xprt->queue_lock);
Chuck Lever176e21e2011-05-09 15:22:44 -04001333 xprt_complete_rqst(task, copied);
Trond Myklebust729749b2017-08-13 10:03:59 -04001334out_unpin:
1335 xprt_unpin_rqst(rovr);
Chuck Lever176e21e2011-05-09 15:22:44 -04001336 out_unlock:
Trond Myklebust75c84152018-08-31 10:21:00 -04001337 spin_unlock(&xprt->queue_lock);
Trond Myklebusta2648092015-10-06 17:03:00 -04001338}
1339
1340static void xs_local_data_receive(struct sock_xprt *transport)
1341{
1342 struct sk_buff *skb;
1343 struct sock *sk;
1344 int err;
1345
Trond Myklebust0af34422018-01-14 15:28:29 -05001346restart:
Trond Myklebusta2648092015-10-06 17:03:00 -04001347 mutex_lock(&transport->recv_mutex);
1348 sk = transport->inet;
1349 if (sk == NULL)
1350 goto out;
1351 for (;;) {
1352 skb = skb_recv_datagram(sk, 0, 1, &err);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001353 if (skb != NULL) {
1354 xs_local_data_read_skb(&transport->xprt, sk, skb);
1355 skb_free_datagram(sk, skb);
1356 continue;
1357 }
1358 if (!test_and_clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebusta2648092015-10-06 17:03:00 -04001359 break;
Trond Myklebust0af34422018-01-14 15:28:29 -05001360 if (need_resched()) {
1361 mutex_unlock(&transport->recv_mutex);
1362 cond_resched();
1363 goto restart;
1364 }
Trond Myklebusta2648092015-10-06 17:03:00 -04001365 }
1366out:
1367 mutex_unlock(&transport->recv_mutex);
1368}
1369
1370static void xs_local_data_receive_workfn(struct work_struct *work)
1371{
1372 struct sock_xprt *transport =
1373 container_of(work, struct sock_xprt, recv_worker);
1374 xs_local_data_receive(transport);
Chuck Lever176e21e2011-05-09 15:22:44 -04001375}
1376
Chuck Lever9903cd12005-08-11 16:25:26 -04001377/**
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001378 * xs_udp_data_read_skb - receive callback for UDP sockets
1379 * @xprt: transport
1380 * @sk: socket
1381 * @skb: skbuff
Chuck Lever9903cd12005-08-11 16:25:26 -04001382 *
1383 */
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001384static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1385 struct sock *sk,
1386 struct sk_buff *skb)
Chuck Lever9903cd12005-08-11 16:25:26 -04001387{
1388 struct rpc_task *task;
Chuck Levera246b012005-08-11 16:25:23 -04001389 struct rpc_rqst *rovr;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001390 int repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001391 u32 _xid;
1392 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -04001393
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001394 repsize = skb->len;
Chuck Levera246b012005-08-11 16:25:23 -04001395 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001396 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001397 return;
Chuck Levera246b012005-08-11 16:25:23 -04001398 }
1399
1400 /* Copy the XID from the skb... */
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001401 xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
Chuck Levera246b012005-08-11 16:25:23 -04001402 if (xp == NULL)
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001403 return;
Chuck Levera246b012005-08-11 16:25:23 -04001404
1405 /* Look up and lock the request corresponding to the given XID */
Trond Myklebust75c84152018-08-31 10:21:00 -04001406 spin_lock(&xprt->queue_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001407 rovr = xprt_lookup_rqst(xprt, *xp);
1408 if (!rovr)
1409 goto out_unlock;
Trond Myklebust729749b2017-08-13 10:03:59 -04001410 xprt_pin_rqst(rovr);
Chuck Leverecd465e2018-03-05 15:12:57 -05001411 xprt_update_rtt(rovr->rq_task);
Trond Myklebust75c84152018-08-31 10:21:00 -04001412 spin_unlock(&xprt->queue_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001413 task = rovr->rq_task;
1414
Chuck Levera246b012005-08-11 16:25:23 -04001415 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1416 copied = repsize;
1417
1418 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001419 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
Trond Myklebust75c84152018-08-31 10:21:00 -04001420 spin_lock(&xprt->queue_lock);
Trond Myklebust0afa6b42018-02-09 09:39:42 -05001421 __UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
Trond Myklebust729749b2017-08-13 10:03:59 -04001422 goto out_unpin;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001423 }
1424
Chuck Levera246b012005-08-11 16:25:23 -04001425
Trond Myklebust729749b2017-08-13 10:03:59 -04001426 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001427 xprt_adjust_cwnd(xprt, task, copied);
Trond Myklebustce7c2522017-08-16 15:30:35 -04001428 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust75c84152018-08-31 10:21:00 -04001429 spin_lock(&xprt->queue_lock);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001430 xprt_complete_rqst(task, copied);
Trond Myklebust0afa6b42018-02-09 09:39:42 -05001431 __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
Trond Myklebust729749b2017-08-13 10:03:59 -04001432out_unpin:
1433 xprt_unpin_rqst(rovr);
Chuck Levera246b012005-08-11 16:25:23 -04001434 out_unlock:
Trond Myklebust75c84152018-08-31 10:21:00 -04001435 spin_unlock(&xprt->queue_lock);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001436}
1437
1438static void xs_udp_data_receive(struct sock_xprt *transport)
1439{
1440 struct sk_buff *skb;
1441 struct sock *sk;
1442 int err;
1443
Trond Myklebust0af34422018-01-14 15:28:29 -05001444restart:
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001445 mutex_lock(&transport->recv_mutex);
1446 sk = transport->inet;
1447 if (sk == NULL)
1448 goto out;
1449 for (;;) {
Paolo Abeni7c13f972016-11-04 11:28:59 +01001450 skb = skb_recv_udp(sk, 0, 1, &err);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001451 if (skb != NULL) {
1452 xs_udp_data_read_skb(&transport->xprt, sk, skb);
Paolo Abeni850cbad2016-10-21 13:55:47 +02001453 consume_skb(skb);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001454 continue;
1455 }
1456 if (!test_and_clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001457 break;
Trond Myklebust0af34422018-01-14 15:28:29 -05001458 if (need_resched()) {
1459 mutex_unlock(&transport->recv_mutex);
1460 cond_resched();
1461 goto restart;
1462 }
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001463 }
1464out:
1465 mutex_unlock(&transport->recv_mutex);
1466}
1467
1468static void xs_udp_data_receive_workfn(struct work_struct *work)
1469{
1470 struct sock_xprt *transport =
1471 container_of(work, struct sock_xprt, recv_worker);
1472 xs_udp_data_receive(transport);
1473}
1474
1475/**
1476 * xs_data_ready - "data ready" callback for UDP sockets
1477 * @sk: socket with data to read
1478 *
1479 */
1480static void xs_data_ready(struct sock *sk)
1481{
1482 struct rpc_xprt *xprt;
1483
1484 read_lock_bh(&sk->sk_callback_lock);
1485 dprintk("RPC: xs_data_ready...\n");
1486 xprt = xprt_from_sock(sk);
1487 if (xprt != NULL) {
1488 struct sock_xprt *transport = container_of(xprt,
1489 struct sock_xprt, xprt);
Trond Myklebust5157b952016-05-29 10:13:24 -04001490 transport->old_data_ready(sk);
1491 /* Any data means we had a useful conversation, so
1492 * then we don't need to delay the next reconnect
1493 */
1494 if (xprt->reestablish_timeout)
1495 xprt->reestablish_timeout = 0;
Trond Myklebust42d42a52016-05-23 09:24:55 -04001496 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebust40a5f1b2016-05-27 10:39:50 -04001497 queue_work(xprtiod_workqueue, &transport->recv_worker);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001498 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001499 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001500}
1501
Trond Myklebusta519fc72012-09-12 16:49:15 -04001502/*
1503 * Helper function to force a TCP close if the server is sending
1504 * junk and/or it has put us in CLOSE_WAIT
1505 */
1506static void xs_tcp_force_close(struct rpc_xprt *xprt)
1507{
Trond Myklebusta519fc72012-09-12 16:49:15 -04001508 xprt_force_disconnect(xprt);
1509}
1510
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001511#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Chuck Lever76566772015-10-24 17:28:32 -04001512static int xs_tcp_bc_up(struct svc_serv *serv, struct net *net)
1513{
1514 int ret;
1515
1516 ret = svc_create_xprt(serv, "tcp-bc", net, PF_INET, 0,
1517 SVC_SOCK_ANONYMOUS);
1518 if (ret < 0)
1519 return ret;
1520 return 0;
1521}
Chuck Lever6b26cc82016-05-02 14:40:40 -04001522
1523static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
1524{
1525 return PAGE_SIZE;
1526}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001527#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001528
Chuck Lever9903cd12005-08-11 16:25:26 -04001529/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001530 * xs_tcp_state_change - callback to handle TCP socket state changes
1531 * @sk: socket whose state has changed
1532 *
1533 */
1534static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001535{
Chuck Lever9903cd12005-08-11 16:25:26 -04001536 struct rpc_xprt *xprt;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001537 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001538
Eric Dumazetf064af12010-09-22 12:43:39 +00001539 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001540 if (!(xprt = xprt_from_sock(sk)))
1541 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001542 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001543 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001544 sk->sk_state, xprt_connected(xprt),
1545 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001546 sock_flag(sk, SOCK_ZAPPED),
1547 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001548
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001549 transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001550 trace_rpc_socket_state_change(xprt, sk->sk_socket);
Chuck Levera246b012005-08-11 16:25:23 -04001551 switch (sk->sk_state) {
1552 case TCP_ESTABLISHED:
Eric Dumazetf064af12010-09-22 12:43:39 +00001553 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001554 if (!xprt_test_and_set_connected(xprt)) {
Trond Myklebust8b717982013-09-26 10:18:04 -04001555 xprt->connect_cookie++;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001556 clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
1557 xprt_clear_connecting(xprt);
Chuck Lever51971132006-12-05 16:35:19 -05001558
Trond Myklebust2a491992009-03-11 14:38:00 -04001559 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001560 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001561 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001562 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001563 case TCP_FIN_WAIT1:
1564 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001565 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001566 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001567 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001568 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001569 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001570 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001571 smp_mb__after_atomic();
Chuck Levera246b012005-08-11 16:25:23 -04001572 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001573 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001574 /* The server initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001575 xprt->connect_cookie++;
Trond Myklebustd0bea452012-10-23 11:35:47 -04001576 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebusta519fc72012-09-12 16:49:15 -04001577 xs_tcp_force_close(xprt);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001578 /* fall through */
Trond Myklebust663b8852008-01-01 18:42:12 -05001579 case TCP_CLOSING:
1580 /*
1581 * If the server closed down the connection, make sure that
1582 * we back off before reconnecting
1583 */
1584 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1585 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001586 break;
1587 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001588 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001589 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001590 clear_bit(XPRT_CONNECTED, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001591 smp_mb__after_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001592 break;
1593 case TCP_CLOSE:
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001594 if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
1595 &transport->sock_state))
1596 xprt_clear_connecting(xprt);
Trond Myklebust9b308892018-02-05 10:20:06 -05001597 clear_bit(XPRT_CLOSING, &xprt->state);
NeilBrown3ffbc1d2017-07-19 14:05:01 +10001598 if (sk->sk_err)
1599 xprt_wake_pending_tasks(xprt, -sk->sk_err);
Trond Myklebust9b308892018-02-05 10:20:06 -05001600 /* Trigger the socket release */
1601 xs_tcp_force_close(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001602 }
1603 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001604 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001605}
1606
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001607static void xs_write_space(struct sock *sk)
1608{
Trond Myklebust13331a52016-01-06 08:57:06 -05001609 struct socket_wq *wq;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001610 struct rpc_xprt *xprt;
1611
Trond Myklebust13331a52016-01-06 08:57:06 -05001612 if (!sk->sk_socket)
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001613 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001614 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001615
1616 if (unlikely(!(xprt = xprt_from_sock(sk))))
1617 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001618 rcu_read_lock();
1619 wq = rcu_dereference(sk->sk_wq);
1620 if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0)
1621 goto out;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001622
Trond Myklebustc5445772018-09-03 23:39:27 -04001623 if (xprt_write_space(xprt))
1624 sk->sk_write_pending--;
Trond Myklebust13331a52016-01-06 08:57:06 -05001625out:
1626 rcu_read_unlock();
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001627}
1628
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001629/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001630 * xs_udp_write_space - callback invoked when socket buffer space
1631 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001632 * @sk: socket whose state has changed
1633 *
Chuck Levera246b012005-08-11 16:25:23 -04001634 * Called when more output buffer space is available for this socket.
1635 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001636 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001637 * with a bunch of small requests.
1638 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001639static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001640{
Eric Dumazetf064af12010-09-22 12:43:39 +00001641 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001642
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001643 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001644 if (sock_writeable(sk))
1645 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001646
Eric Dumazetf064af12010-09-22 12:43:39 +00001647 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001648}
Chuck Levera246b012005-08-11 16:25:23 -04001649
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001650/**
1651 * xs_tcp_write_space - callback invoked when socket buffer space
1652 * becomes available
1653 * @sk: socket whose state has changed
1654 *
1655 * Called when more output buffer space is available for this socket.
1656 * We try not to wake our writers until they can make "significant"
1657 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1658 * with a bunch of small requests.
1659 */
1660static void xs_tcp_write_space(struct sock *sk)
1661{
Eric Dumazetf064af12010-09-22 12:43:39 +00001662 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001663
1664 /* from net/core/stream.c:sk_stream_write_space */
Eric Dumazet64dc6132013-07-22 20:26:31 -07001665 if (sk_stream_is_writeable(sk))
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001666 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001667
Eric Dumazetf064af12010-09-22 12:43:39 +00001668 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001669}
1670
Chuck Lever470056c2005-08-25 16:25:56 -07001671static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001672{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001673 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1674 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001675
Chuck Lever7c6e0662006-12-05 16:35:30 -05001676 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001677 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001678 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001679 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001680 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001681 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001682 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001683 sk->sk_write_space(sk);
1684 }
1685}
1686
Chuck Lever43118c22005-08-25 16:25:49 -07001687/**
Chuck Lever470056c2005-08-25 16:25:56 -07001688 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001689 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001690 * @sndsize: requested size of send buffer, in bytes
1691 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001692 *
Chuck Lever470056c2005-08-25 16:25:56 -07001693 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001694 */
Chuck Lever470056c2005-08-25 16:25:56 -07001695static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001696{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001697 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1698
1699 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001700 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001701 transport->sndsize = sndsize + 1024;
1702 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001703 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001704 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001705
1706 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001707}
1708
Chuck Lever46c0ee82005-08-25 16:25:52 -07001709/**
1710 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1711 * @task: task that timed out
1712 *
1713 * Adjust the congestion window after a retransmit timeout has occurred.
1714 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001715static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever46c0ee82005-08-25 16:25:52 -07001716{
Chuck Leverb977b642017-02-08 17:00:51 -05001717 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001718 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
Chuck Leverb977b642017-02-08 17:00:51 -05001719 spin_unlock_bh(&xprt->transport_lock);
Chuck Lever46c0ee82005-08-25 16:25:52 -07001720}
1721
Chuck Leverb85d8802006-05-25 01:40:49 -04001722static unsigned short xs_get_random_port(void)
1723{
Frank Sorenson5d718992016-07-08 16:35:23 -05001724 unsigned short range = xprt_max_resvport - xprt_min_resvport + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001725 unsigned short rand = (unsigned short) prandom_u32() % range;
Chuck Leverb85d8802006-05-25 01:40:49 -04001726 return rand + xprt_min_resvport;
1727}
1728
Chuck Lever92200412006-01-03 09:55:51 +01001729/**
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001730 * xs_set_reuseaddr_port - set the socket's port and address reuse options
1731 * @sock: socket
1732 *
1733 * Note that this function has to be called on all sockets that share the
1734 * same port, and it must be called before binding.
1735 */
1736static void xs_sock_set_reuseport(struct socket *sock)
1737{
Trond Myklebust402e23b2015-02-09 17:20:14 -05001738 int opt = 1;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001739
Trond Myklebust402e23b2015-02-09 17:20:14 -05001740 kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
1741 (char *)&opt, sizeof(opt));
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001742}
1743
1744static unsigned short xs_sock_getport(struct socket *sock)
1745{
1746 struct sockaddr_storage buf;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001747 unsigned short port = 0;
1748
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001749 if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001750 goto out;
1751 switch (buf.ss_family) {
1752 case AF_INET6:
1753 port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
1754 break;
1755 case AF_INET:
1756 port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
1757 }
1758out:
1759 return port;
1760}
1761
1762/**
Chuck Lever92200412006-01-03 09:55:51 +01001763 * xs_set_port - reset the port number in the remote endpoint address
1764 * @xprt: generic transport
1765 * @port: new port number
1766 *
1767 */
1768static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1769{
Chuck Lever46121cf2007-01-31 12:14:08 -05001770 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001771
Chuck Lever9dc3b092009-08-09 15:09:46 -04001772 rpc_set_port(xs_addr(xprt), port);
1773 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001774}
1775
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001776static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
1777{
1778 if (transport->srcport == 0)
1779 transport->srcport = xs_sock_getport(sock);
1780}
1781
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001782static unsigned short xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001783{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001784 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001785
1786 if (port == 0 && transport->xprt.resvport)
1787 port = xs_get_random_port();
1788 return port;
1789}
1790
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001791static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001792{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001793 if (transport->srcport != 0)
1794 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001795 if (!transport->xprt.resvport)
1796 return 0;
1797 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1798 return xprt_max_resvport;
1799 return --port;
1800}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001801static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001802{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001803 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001804 int err, nloop = 0;
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001805 unsigned short port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001806 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001807
Chris Perl0f7a6222014-09-05 15:40:21 -04001808 /*
1809 * If we are asking for any ephemeral port (i.e. port == 0 &&
1810 * transport->xprt.resvport == 0), don't bind. Let the local
1811 * port selection happen implicitly when the socket is used
1812 * (for example at connect time).
1813 *
1814 * This ensures that we can continue to establish TCP
1815 * connections even when all local ephemeral ports are already
1816 * a part of some TCP connection. This makes no difference
1817 * for UDP sockets, but also doens't harm them.
1818 *
1819 * If we're asking for any reserved port (i.e. port == 0 &&
1820 * transport->xprt.resvport == 1) xs_get_srcport above will
1821 * ensure that port is non-zero and we will bind as needed.
1822 */
1823 if (port == 0)
1824 return 0;
1825
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001826 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001827 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001828 rpc_set_port((struct sockaddr *)&myaddr, port);
1829 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1830 transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001831 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001832 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001833 break;
Chuck Levera246b012005-08-11 16:25:23 -04001834 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001835 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001836 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001837 if (port > last)
1838 nloop++;
1839 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001840
Chuck Lever4232e862010-10-20 11:52:51 -04001841 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001842 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1843 &((struct sockaddr_in *)&myaddr)->sin_addr,
1844 port, err ? "failed" : "ok", err);
1845 else
1846 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1847 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1848 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001849 return err;
1850}
1851
Chuck Lever176e21e2011-05-09 15:22:44 -04001852/*
1853 * We don't support autobind on AF_LOCAL sockets
1854 */
1855static void xs_local_rpcbind(struct rpc_task *task)
1856{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001857 xprt_set_bound(task->tk_xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04001858}
1859
1860static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1861{
1862}
Chuck Levera246b012005-08-11 16:25:23 -04001863
Peter Zijlstraed075362006-12-06 20:35:24 -08001864#ifdef CONFIG_DEBUG_LOCK_ALLOC
1865static struct lock_class_key xs_key[2];
1866static struct lock_class_key xs_slock_key[2];
1867
Chuck Lever176e21e2011-05-09 15:22:44 -04001868static inline void xs_reclassify_socketu(struct socket *sock)
1869{
1870 struct sock *sk = sock->sk;
1871
Chuck Lever176e21e2011-05-09 15:22:44 -04001872 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1873 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1874}
1875
Chuck Lever8945ee52007-08-06 11:58:04 -04001876static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001877{
1878 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001879
Chuck Lever8945ee52007-08-06 11:58:04 -04001880 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1881 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1882}
Peter Zijlstraed075362006-12-06 20:35:24 -08001883
Chuck Lever8945ee52007-08-06 11:58:04 -04001884static inline void xs_reclassify_socket6(struct socket *sock)
1885{
1886 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001887
Chuck Lever8945ee52007-08-06 11:58:04 -04001888 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1889 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001890}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001891
1892static inline void xs_reclassify_socket(int family, struct socket *sock)
1893{
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02001894 if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -04001895 return;
1896
Chuck Lever4232e862010-10-20 11:52:51 -04001897 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001898 case AF_LOCAL:
1899 xs_reclassify_socketu(sock);
1900 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001901 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001902 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001903 break;
1904 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001905 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001906 break;
1907 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001908}
Peter Zijlstraed075362006-12-06 20:35:24 -08001909#else
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001910static inline void xs_reclassify_socket(int family, struct socket *sock)
1911{
1912}
Peter Zijlstraed075362006-12-06 20:35:24 -08001913#endif
1914
NeilBrown93dc41b2013-10-31 16:14:36 +11001915static void xs_dummy_setup_socket(struct work_struct *work)
1916{
1917}
1918
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001919static struct socket *xs_create_sock(struct rpc_xprt *xprt,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001920 struct sock_xprt *transport, int family, int type,
1921 int protocol, bool reuseport)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001922{
1923 struct socket *sock;
1924 int err;
1925
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001926 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001927 if (err < 0) {
1928 dprintk("RPC: can't create %d transport socket (%d).\n",
1929 protocol, -err);
1930 goto out;
1931 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001932 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001933
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001934 if (reuseport)
1935 xs_sock_set_reuseport(sock);
1936
Ben Hutchings4cea2882011-02-22 21:54:34 +00001937 err = xs_bind(transport, sock);
1938 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001939 sock_release(sock);
1940 goto out;
1941 }
1942
1943 return sock;
1944out:
1945 return ERR_PTR(err);
1946}
1947
Chuck Lever176e21e2011-05-09 15:22:44 -04001948static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1949 struct socket *sock)
1950{
1951 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1952 xprt);
1953
1954 if (!transport->inet) {
1955 struct sock *sk = sock->sk;
1956
1957 write_lock_bh(&sk->sk_callback_lock);
1958
1959 xs_save_old_callbacks(transport, sk);
1960
1961 sk->sk_user_data = xprt;
Trond Myklebusta2648092015-10-06 17:03:00 -04001962 sk->sk_data_ready = xs_data_ready;
Chuck Lever176e21e2011-05-09 15:22:44 -04001963 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07001964 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05001965 sk->sk_error_report = xs_error_report;
Trond Myklebustc2126152015-08-19 21:46:15 -05001966 sk->sk_allocation = GFP_NOIO;
Chuck Lever176e21e2011-05-09 15:22:44 -04001967
1968 xprt_clear_connected(xprt);
1969
1970 /* Reset to new socket */
1971 transport->sock = sock;
1972 transport->inet = sk;
1973
1974 write_unlock_bh(&sk->sk_callback_lock);
1975 }
1976
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001977 transport->xmit.offset = 0;
1978
Chuck Lever176e21e2011-05-09 15:22:44 -04001979 /* Tell the socket layer to start connecting... */
1980 xprt->stat.connect_count++;
1981 xprt->stat.connect_start = jiffies;
1982 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1983}
1984
1985/**
1986 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
Chuck Lever176e21e2011-05-09 15:22:44 -04001987 * @transport: socket transport to connect
Chuck Lever176e21e2011-05-09 15:22:44 -04001988 */
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001989static int xs_local_setup_socket(struct sock_xprt *transport)
Chuck Lever176e21e2011-05-09 15:22:44 -04001990{
Chuck Lever176e21e2011-05-09 15:22:44 -04001991 struct rpc_xprt *xprt = &transport->xprt;
1992 struct socket *sock;
1993 int status = -EIO;
1994
Chuck Lever176e21e2011-05-09 15:22:44 -04001995 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1996 SOCK_STREAM, 0, &sock, 1);
1997 if (status < 0) {
1998 dprintk("RPC: can't create AF_LOCAL "
1999 "transport socket (%d).\n", -status);
2000 goto out;
2001 }
Stefan Hajnoczid1358912015-12-02 14:17:52 +08002002 xs_reclassify_socket(AF_LOCAL, sock);
Chuck Lever176e21e2011-05-09 15:22:44 -04002003
2004 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
2005 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2006
2007 status = xs_local_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002008 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever176e21e2011-05-09 15:22:44 -04002009 switch (status) {
2010 case 0:
2011 dprintk("RPC: xprt %p connected to %s\n",
2012 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2013 xprt_set_connected(xprt);
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002014 case -ENOBUFS:
Chuck Lever176e21e2011-05-09 15:22:44 -04002015 break;
2016 case -ENOENT:
2017 dprintk("RPC: xprt %p: socket %s does not exist\n",
2018 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2019 break;
Trond Myklebust4a20a982012-12-15 17:02:29 -05002020 case -ECONNREFUSED:
2021 dprintk("RPC: xprt %p: connection refused for %s\n",
2022 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2023 break;
Chuck Lever176e21e2011-05-09 15:22:44 -04002024 default:
2025 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
2026 __func__, -status,
2027 xprt->address_strings[RPC_DISPLAY_ADDR]);
2028 }
2029
2030out:
2031 xprt_clear_connecting(xprt);
2032 xprt_wake_pending_tasks(xprt, status);
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002033 return status;
2034}
2035
Linus Torvaldsb6669732013-02-28 18:02:55 -08002036static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002037{
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002038 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2039 int ret;
2040
2041 if (RPC_IS_ASYNC(task)) {
2042 /*
2043 * We want the AF_LOCAL connect to be resolved in the
2044 * filesystem namespace of the process making the rpc
2045 * call. Thus we connect synchronously.
2046 *
2047 * If we want to support asynchronous AF_LOCAL calls,
2048 * we'll need to figure out how to pass a namespace to
2049 * connect.
2050 */
2051 rpc_exit(task, -ENOTCONN);
2052 return;
2053 }
2054 ret = xs_local_setup_socket(transport);
2055 if (ret && !RPC_IS_SOFTCONN(task))
2056 msleep_interruptible(15000);
Chuck Lever176e21e2011-05-09 15:22:44 -04002057}
2058
Jeff Layton3c87ef62015-06-03 16:14:25 -04002059#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Jeff Laytond6e971d2015-06-03 16:14:28 -04002060/*
2061 * Note that this should be called with XPRT_LOCKED held (or when we otherwise
2062 * know that we have exclusive access to the socket), to guard against
2063 * races with xs_reset_transport.
2064 */
Mel Gormana564b8f2012-07-31 16:45:12 -07002065static void xs_set_memalloc(struct rpc_xprt *xprt)
2066{
2067 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2068 xprt);
2069
Jeff Laytond6e971d2015-06-03 16:14:28 -04002070 /*
2071 * If there's no sock, then we have nothing to set. The
2072 * reconnecting process will get it for us.
2073 */
2074 if (!transport->inet)
2075 return;
Jeff Layton8e228132015-06-03 16:14:26 -04002076 if (atomic_read(&xprt->swapper))
Mel Gormana564b8f2012-07-31 16:45:12 -07002077 sk_set_memalloc(transport->inet);
2078}
2079
2080/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002081 * xs_enable_swap - Tag this transport as being used for swap.
Mel Gormana564b8f2012-07-31 16:45:12 -07002082 * @xprt: transport to tag
Mel Gormana564b8f2012-07-31 16:45:12 -07002083 *
Jeff Layton8e228132015-06-03 16:14:26 -04002084 * Take a reference to this transport on behalf of the rpc_clnt, and
2085 * optionally mark it for swapping if it wasn't already.
Mel Gormana564b8f2012-07-31 16:45:12 -07002086 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002087static int
2088xs_enable_swap(struct rpc_xprt *xprt)
Mel Gormana564b8f2012-07-31 16:45:12 -07002089{
Jeff Laytond6e971d2015-06-03 16:14:28 -04002090 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Mel Gormana564b8f2012-07-31 16:45:12 -07002091
Jeff Laytond6e971d2015-06-03 16:14:28 -04002092 if (atomic_inc_return(&xprt->swapper) != 1)
2093 return 0;
2094 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2095 return -ERESTARTSYS;
2096 if (xs->inet)
2097 sk_set_memalloc(xs->inet);
2098 xprt_release_xprt(xprt, NULL);
Jeff Layton8e228132015-06-03 16:14:26 -04002099 return 0;
Mel Gormana564b8f2012-07-31 16:45:12 -07002100}
Jeff Layton8e228132015-06-03 16:14:26 -04002101
2102/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002103 * xs_disable_swap - Untag this transport as being used for swap.
Jeff Layton8e228132015-06-03 16:14:26 -04002104 * @xprt: transport to tag
2105 *
2106 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
2107 * swapper refcount goes to 0, untag the socket as a memalloc socket.
2108 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002109static void
2110xs_disable_swap(struct rpc_xprt *xprt)
Jeff Layton8e228132015-06-03 16:14:26 -04002111{
Jeff Laytond6e971d2015-06-03 16:14:28 -04002112 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Jeff Layton8e228132015-06-03 16:14:26 -04002113
Jeff Laytond6e971d2015-06-03 16:14:28 -04002114 if (!atomic_dec_and_test(&xprt->swapper))
2115 return;
2116 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2117 return;
2118 if (xs->inet)
2119 sk_clear_memalloc(xs->inet);
2120 xprt_release_xprt(xprt, NULL);
Mel Gormana564b8f2012-07-31 16:45:12 -07002121}
Mel Gormana564b8f2012-07-31 16:45:12 -07002122#else
2123static void xs_set_memalloc(struct rpc_xprt *xprt)
2124{
2125}
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002126
2127static int
2128xs_enable_swap(struct rpc_xprt *xprt)
2129{
2130 return -EINVAL;
2131}
2132
2133static void
2134xs_disable_swap(struct rpc_xprt *xprt)
2135{
2136}
Mel Gormana564b8f2012-07-31 16:45:12 -07002137#endif
2138
Chuck Lever16be2d22007-08-06 11:57:38 -04002139static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04002140{
Chuck Lever16be2d22007-08-06 11:57:38 -04002141 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04002142
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002143 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002144 struct sock *sk = sock->sk;
2145
2146 write_lock_bh(&sk->sk_callback_lock);
2147
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002148 xs_save_old_callbacks(transport, sk);
2149
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002150 sk->sk_user_data = xprt;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002151 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002152 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002153 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebustc2126152015-08-19 21:46:15 -05002154 sk->sk_allocation = GFP_NOIO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002155
2156 xprt_set_connected(xprt);
2157
2158 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002159 transport->sock = sock;
2160 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002161
Mel Gormana564b8f2012-07-31 16:45:12 -07002162 xs_set_memalloc(xprt);
2163
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002164 write_unlock_bh(&sk->sk_callback_lock);
2165 }
Chuck Lever470056c2005-08-25 16:25:56 -07002166 xs_udp_do_set_buffer_size(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002167
2168 xprt->stat.connect_start = jiffies;
Chuck Lever16be2d22007-08-06 11:57:38 -04002169}
2170
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002171static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04002172{
2173 struct sock_xprt *transport =
2174 container_of(work, struct sock_xprt, connect_worker.work);
2175 struct rpc_xprt *xprt = &transport->xprt;
Colin Ian Kingd099b8a2017-09-18 12:21:14 +01002176 struct socket *sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002177 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04002178
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002179 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002180 xs_addr(xprt)->sa_family, SOCK_DGRAM,
2181 IPPROTO_UDP, false);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002182 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04002183 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04002184
Chuck Leverc740eff2009-08-09 15:09:46 -04002185 dprintk("RPC: worker connecting xprt %p via %s to "
2186 "%s (port %s)\n", xprt,
2187 xprt->address_strings[RPC_DISPLAY_PROTO],
2188 xprt->address_strings[RPC_DISPLAY_ADDR],
2189 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04002190
2191 xs_udp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002192 trace_rpc_socket_connect(xprt, sock, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002193 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002194out:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002195 xprt_unlock_connect(xprt, transport);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002196 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002197 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002198}
2199
Trond Myklebust4876cc72015-06-19 16:17:57 -04002200/**
2201 * xs_tcp_shutdown - gracefully shut down a TCP socket
2202 * @xprt: transport
2203 *
2204 * Initiates a graceful shutdown of the TCP socket by calling the
2205 * equivalent of shutdown(SHUT_RDWR);
2206 */
2207static void xs_tcp_shutdown(struct rpc_xprt *xprt)
2208{
2209 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2210 struct socket *sock = transport->sock;
Trond Myklebust9b308892018-02-05 10:20:06 -05002211 int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
Trond Myklebust4876cc72015-06-19 16:17:57 -04002212
2213 if (sock == NULL)
2214 return;
Trond Myklebust9b308892018-02-05 10:20:06 -05002215 switch (skst) {
2216 default:
Trond Myklebust4876cc72015-06-19 16:17:57 -04002217 kernel_sock_shutdown(sock, SHUT_RDWR);
2218 trace_rpc_socket_shutdown(xprt, sock);
Trond Myklebust9b308892018-02-05 10:20:06 -05002219 break;
2220 case TCP_CLOSE:
2221 case TCP_TIME_WAIT:
Trond Myklebust4876cc72015-06-19 16:17:57 -04002222 xs_reset_transport(transport);
Trond Myklebust9b308892018-02-05 10:20:06 -05002223 }
Trond Myklebust4876cc72015-06-19 16:17:57 -04002224}
2225
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002226static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
2227 struct socket *sock)
2228{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002229 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2230 unsigned int keepidle;
2231 unsigned int keepcnt;
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002232 unsigned int opt_on = 1;
2233 unsigned int timeo;
2234
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002235 spin_lock_bh(&xprt->transport_lock);
2236 keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
2237 keepcnt = xprt->timeout->to_retries + 1;
2238 timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
2239 (xprt->timeout->to_retries + 1);
2240 clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2241 spin_unlock_bh(&xprt->transport_lock);
2242
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002243 /* TCP Keepalive options */
2244 kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
2245 (char *)&opt_on, sizeof(opt_on));
2246 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
2247 (char *)&keepidle, sizeof(keepidle));
2248 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
2249 (char *)&keepidle, sizeof(keepidle));
2250 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
2251 (char *)&keepcnt, sizeof(keepcnt));
2252
2253 /* TCP user timeout (see RFC5482) */
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002254 kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT,
2255 (char *)&timeo, sizeof(timeo));
2256}
2257
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002258static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
2259 unsigned long connect_timeout,
2260 unsigned long reconnect_timeout)
2261{
2262 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2263 struct rpc_timeout to;
2264 unsigned long initval;
2265
2266 spin_lock_bh(&xprt->transport_lock);
2267 if (reconnect_timeout < xprt->max_reconnect_timeout)
2268 xprt->max_reconnect_timeout = reconnect_timeout;
2269 if (connect_timeout < xprt->connect_timeout) {
2270 memcpy(&to, xprt->timeout, sizeof(to));
2271 initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
2272 /* Arbitrary lower limit */
2273 if (initval < XS_TCP_INIT_REEST_TO << 1)
2274 initval = XS_TCP_INIT_REEST_TO << 1;
2275 to.to_initval = initval;
2276 to.to_maxval = initval;
2277 memcpy(&transport->tcp_timeout, &to,
2278 sizeof(transport->tcp_timeout));
2279 xprt->timeout = &transport->tcp_timeout;
2280 xprt->connect_timeout = connect_timeout;
2281 }
2282 set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2283 spin_unlock_bh(&xprt->transport_lock);
2284}
2285
Chuck Lever16be2d22007-08-06 11:57:38 -04002286static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002287{
Chuck Lever16be2d22007-08-06 11:57:38 -04002288 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002289 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002290
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002291 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002292 struct sock *sk = sock->sk;
NeilBrownd88e4d82016-08-04 16:24:28 +10002293 unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC;
Trond Myklebust7f260e82013-09-24 11:25:22 -04002294
NeilBrownd88e4d82016-08-04 16:24:28 +10002295 /* Avoid temporary address, they are bad for long-lived
2296 * connections such as NFS mounts.
2297 * RFC4941, section 3.6 suggests that:
2298 * Individual applications, which have specific
2299 * knowledge about the normal duration of connections,
2300 * MAY override this as appropriate.
2301 */
2302 kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES,
2303 (char *)&addr_pref, sizeof(addr_pref));
2304
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002305 xs_tcp_set_socket_timeouts(xprt, sock);
Trond Myklebust775f06a2015-06-20 15:31:54 -04002306
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002307 write_lock_bh(&sk->sk_callback_lock);
2308
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002309 xs_save_old_callbacks(transport, sk);
2310
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002311 sk->sk_user_data = xprt;
Trond Myklebust5157b952016-05-29 10:13:24 -04002312 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002313 sk->sk_state_change = xs_tcp_state_change;
2314 sk->sk_write_space = xs_tcp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002315 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05002316 sk->sk_error_report = xs_error_report;
Trond Myklebustc2126152015-08-19 21:46:15 -05002317 sk->sk_allocation = GFP_NOIO;
Chuck Lever3167e122005-08-25 16:25:55 -07002318
2319 /* socket options */
Chuck Lever3167e122005-08-25 16:25:55 -07002320 sock_reset_flag(sk, SOCK_LINGER);
Chuck Lever3167e122005-08-25 16:25:55 -07002321 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002322
2323 xprt_clear_connected(xprt);
2324
2325 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002326 transport->sock = sock;
2327 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002328
2329 write_unlock_bh(&sk->sk_callback_lock);
2330 }
2331
Trond Myklebust01d37c42009-03-11 14:09:39 -04002332 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002333 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002334
Mel Gormana564b8f2012-07-31 16:45:12 -07002335 xs_set_memalloc(xprt);
2336
Trond Myklebuste1806c72018-08-13 16:50:49 -04002337 /* Reset TCP record info */
2338 transport->recv.offset = 0;
2339 transport->recv.len = 0;
2340 transport->recv.copied = 0;
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04002341 transport->xmit.offset = 0;
Trond Myklebuste1806c72018-08-13 16:50:49 -04002342
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002343 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05002344 xprt->stat.connect_count++;
2345 xprt->stat.connect_start = jiffies;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04002346 set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002347 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2348 switch (ret) {
2349 case 0:
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002350 xs_set_srcport(transport, sock);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002351 /* fall through */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002352 case -EINPROGRESS:
2353 /* SYN_SENT! */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002354 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2355 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust1f4c17a2016-08-01 13:36:08 -04002356 break;
2357 case -EADDRNOTAVAIL:
2358 /* Source port number is unavailable. Try a new one! */
2359 transport->srcport = 0;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002360 }
2361out:
2362 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002363}
2364
2365/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002366 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
Chuck Lever16be2d22007-08-06 11:57:38 -04002367 *
2368 * Invoked by a work queue tasklet.
2369 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002370static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002371{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002372 struct sock_xprt *transport =
2373 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002374 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002375 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002376 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002377
Chuck Lever16be2d22007-08-06 11:57:38 -04002378 if (!sock) {
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002379 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002380 xs_addr(xprt)->sa_family, SOCK_STREAM,
2381 IPPROTO_TCP, true);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002382 if (IS_ERR(sock)) {
2383 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002384 goto out;
2385 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002386 }
2387
Chuck Leverc740eff2009-08-09 15:09:46 -04002388 dprintk("RPC: worker connecting xprt %p via %s to "
2389 "%s (port %s)\n", xprt,
2390 xprt->address_strings[RPC_DISPLAY_PROTO],
2391 xprt->address_strings[RPC_DISPLAY_ADDR],
2392 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002393
2394 status = xs_tcp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002395 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever46121cf2007-01-31 12:14:08 -05002396 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2397 xprt, -status, xprt_connected(xprt),
2398 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002399 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002400 default:
2401 printk("%s: connect returned unhandled error %d\n",
2402 __func__, status);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002403 /* fall through */
Trond Myklebustf75e6742009-04-21 17:18:20 -04002404 case -EADDRNOTAVAIL:
2405 /* We're probably in TIME_WAIT. Get rid of existing socket,
2406 * and retry
2407 */
Trond Myklebusta519fc72012-09-12 16:49:15 -04002408 xs_tcp_force_close(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002409 break;
Trond Myklebust2a491992009-03-11 14:38:00 -04002410 case 0:
2411 case -EINPROGRESS:
2412 case -EALREADY:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002413 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002414 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002415 case -EINVAL:
2416 /* Happens, for instance, if the user specified a link
2417 * local IPv6 address without a scope-id.
2418 */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002419 case -ECONNREFUSED:
2420 case -ECONNRESET:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002421 case -ENETDOWN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002422 case -ENETUNREACH:
Trond Myklebust4ba161a2017-11-24 12:00:24 -05002423 case -EHOSTUNREACH:
Trond Myklebust3913c782015-02-08 21:44:04 -05002424 case -EADDRINUSE:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002425 case -ENOBUFS:
NeilBrown6ea44ad2017-05-25 17:00:32 +10002426 /*
2427 * xs_tcp_force_close() wakes tasks with -EIO.
2428 * We need to wake them first to ensure the
2429 * correct error code.
2430 */
2431 xprt_wake_pending_tasks(xprt, status);
Trond Myklebust4efdd922015-02-08 15:34:28 -05002432 xs_tcp_force_close(xprt);
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002433 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002434 }
Trond Myklebust2a491992009-03-11 14:38:00 -04002435 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002436out:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002437 xprt_unlock_connect(xprt, transport);
Chuck Lever2226feb2005-08-11 16:25:38 -04002438 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002439 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04002440}
2441
Trond Myklebust02910172016-08-04 00:00:33 -04002442static unsigned long xs_reconnect_delay(const struct rpc_xprt *xprt)
2443{
2444 unsigned long start, now = jiffies;
2445
2446 start = xprt->stat.connect_start + xprt->reestablish_timeout;
2447 if (time_after(start, now))
2448 return start - now;
2449 return 0;
2450}
2451
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002452static void xs_reconnect_backoff(struct rpc_xprt *xprt)
2453{
2454 xprt->reestablish_timeout <<= 1;
2455 if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
2456 xprt->reestablish_timeout = xprt->max_reconnect_timeout;
2457 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2458 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2459}
2460
Chuck Lever68e220b2007-08-06 11:57:48 -04002461/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002462 * xs_connect - connect a socket to a remote endpoint
Trond Myklebust1b092092013-01-08 09:26:49 -05002463 * @xprt: pointer to transport structure
Chuck Lever9903cd12005-08-11 16:25:26 -04002464 * @task: address of RPC task that manages state of connect request
2465 *
2466 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002467 *
2468 * UDP socket connects are synchronous, but we use a work queue anyway
2469 * to guarantee that even unprivileged user processes can set up a
2470 * socket on a privileged port.
2471 *
2472 * If a UDP socket connect fails, the delay behavior here prevents
2473 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002474 */
Trond Myklebust1b092092013-01-08 09:26:49 -05002475static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002476{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002477 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002478 unsigned long delay = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002479
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002480 WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2481
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002482 if (transport->sock != NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002483 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2484 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002485 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002486
2487 /* Start by resetting any existing state */
2488 xs_reset_transport(transport);
2489
Trond Myklebust02910172016-08-04 00:00:33 -04002490 delay = xs_reconnect_delay(xprt);
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002491 xs_reconnect_backoff(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002492
Trond Myklebust02910172016-08-04 00:00:33 -04002493 } else
Chuck Lever46121cf2007-01-31 12:14:08 -05002494 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002495
2496 queue_delayed_work(xprtiod_workqueue,
2497 &transport->connect_worker,
2498 delay);
Chuck Levera246b012005-08-11 16:25:23 -04002499}
2500
Chuck Lever262ca072006-03-20 13:44:16 -05002501/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002502 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2503 * @xprt: rpc_xprt struct containing statistics
2504 * @seq: output file
2505 *
2506 */
2507static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2508{
2509 long idle_time = 0;
2510
2511 if (xprt_connected(xprt))
2512 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2513
2514 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
Andy Adamson15a45202012-02-14 16:19:18 -05002515 "%llu %llu %lu %llu %llu\n",
Chuck Lever176e21e2011-05-09 15:22:44 -04002516 xprt->stat.bind_count,
2517 xprt->stat.connect_count,
2518 xprt->stat.connect_time,
2519 idle_time,
2520 xprt->stat.sends,
2521 xprt->stat.recvs,
2522 xprt->stat.bad_xids,
2523 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002524 xprt->stat.bklog_u,
2525 xprt->stat.max_slots,
2526 xprt->stat.sending_u,
2527 xprt->stat.pending_u);
Chuck Lever176e21e2011-05-09 15:22:44 -04002528}
2529
2530/**
Chuck Lever262ca072006-03-20 13:44:16 -05002531 * xs_udp_print_stats - display UDP socket-specifc stats
2532 * @xprt: rpc_xprt struct containing statistics
2533 * @seq: output file
2534 *
2535 */
2536static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2537{
Chuck Leverc8475462006-12-05 16:35:26 -05002538 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2539
Andy Adamson15a45202012-02-14 16:19:18 -05002540 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2541 "%lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002542 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002543 xprt->stat.bind_count,
2544 xprt->stat.sends,
2545 xprt->stat.recvs,
2546 xprt->stat.bad_xids,
2547 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002548 xprt->stat.bklog_u,
2549 xprt->stat.max_slots,
2550 xprt->stat.sending_u,
2551 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002552}
2553
2554/**
2555 * xs_tcp_print_stats - display TCP socket-specifc stats
2556 * @xprt: rpc_xprt struct containing statistics
2557 * @seq: output file
2558 *
2559 */
2560static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2561{
Chuck Leverc8475462006-12-05 16:35:26 -05002562 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002563 long idle_time = 0;
2564
2565 if (xprt_connected(xprt))
2566 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2567
Andy Adamson15a45202012-02-14 16:19:18 -05002568 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2569 "%llu %llu %lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002570 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002571 xprt->stat.bind_count,
2572 xprt->stat.connect_count,
2573 xprt->stat.connect_time,
2574 idle_time,
2575 xprt->stat.sends,
2576 xprt->stat.recvs,
2577 xprt->stat.bad_xids,
2578 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002579 xprt->stat.bklog_u,
2580 xprt->stat.max_slots,
2581 xprt->stat.sending_u,
2582 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002583}
2584
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002585/*
2586 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2587 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2588 * to use the server side send routines.
2589 */
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002590static int bc_malloc(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002591{
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002592 struct rpc_rqst *rqst = task->tk_rqstp;
2593 size_t size = rqst->rq_callsize;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002594 struct page *page;
2595 struct rpc_buffer *buf;
2596
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002597 if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
2598 WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
2599 size);
2600 return -EINVAL;
2601 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002602
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002603 page = alloc_page(GFP_KERNEL);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002604 if (!page)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002605 return -ENOMEM;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002606
2607 buf = page_address(page);
2608 buf->len = PAGE_SIZE;
2609
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002610 rqst->rq_buffer = buf->data;
Jeff Layton18e601d2016-10-24 20:33:23 -04002611 rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002612 return 0;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002613}
2614
2615/*
2616 * Free the space allocated in the bc_alloc routine
2617 */
Chuck Lever3435c742016-09-15 10:55:29 -04002618static void bc_free(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002619{
Chuck Lever3435c742016-09-15 10:55:29 -04002620 void *buffer = task->tk_rqstp->rq_buffer;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002621 struct rpc_buffer *buf;
2622
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002623 buf = container_of(buffer, struct rpc_buffer, data);
2624 free_page((unsigned long)buf);
2625}
2626
2627/*
2628 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2629 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2630 */
2631static int bc_sendto(struct rpc_rqst *req)
2632{
2633 int len;
2634 struct xdr_buf *xbufp = &req->rq_snd_buf;
2635 struct rpc_xprt *xprt = req->rq_xprt;
2636 struct sock_xprt *transport =
2637 container_of(xprt, struct sock_xprt, xprt);
2638 struct socket *sock = transport->sock;
2639 unsigned long headoff;
2640 unsigned long tailoff;
2641
Chuck Lever61677ee2011-05-09 15:22:34 -04002642 xs_encode_stream_record_marker(xbufp);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002643
2644 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2645 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2646 len = svc_send_common(sock, xbufp,
2647 virt_to_page(xbufp->head[0].iov_base), headoff,
2648 xbufp->tail[0].iov_base, tailoff);
2649
2650 if (len != xbufp->len) {
2651 printk(KERN_NOTICE "Error sending entire callback!\n");
2652 len = -EAGAIN;
2653 }
2654
2655 return len;
2656}
2657
2658/*
2659 * The send routine. Borrows from svc_send
2660 */
Trond Myklebustadfa7142018-09-03 23:58:59 -04002661static int bc_send_request(struct rpc_rqst *req)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002662{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002663 struct svc_xprt *xprt;
Andrzej Hajda7fc56132015-09-24 16:00:09 +02002664 int len;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002665
2666 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2667 /*
2668 * Get the server socket associated with this callback xprt
2669 */
2670 xprt = req->rq_xprt->bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002671
2672 /*
2673 * Grab the mutex to serialize data as the connection is shared
2674 * with the fore channel
2675 */
Trond Myklebustc5445772018-09-03 23:39:27 -04002676 mutex_lock(&xprt->xpt_mutex);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002677 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2678 len = -ENOTCONN;
2679 else
2680 len = bc_sendto(req);
2681 mutex_unlock(&xprt->xpt_mutex);
2682
2683 if (len > 0)
2684 len = 0;
2685
2686 return len;
2687}
2688
2689/*
2690 * The close routine. Since this is client initiated, we do nothing
2691 */
2692
2693static void bc_close(struct rpc_xprt *xprt)
2694{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002695}
2696
2697/*
2698 * The xprt destroy routine. Again, because this connection is client
2699 * initiated, we do nothing
2700 */
2701
2702static void bc_destroy(struct rpc_xprt *xprt)
2703{
Kinglong Mee47f72ef2014-03-24 11:58:16 +08002704 dprintk("RPC: bc_destroy xprt %p\n", xprt);
2705
2706 xs_xprt_free(xprt);
2707 module_put(THIS_MODULE);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002708}
2709
Chuck Leverd31ae252017-08-01 12:00:39 -04002710static const struct rpc_xprt_ops xs_local_ops = {
Chuck Lever176e21e2011-05-09 15:22:44 -04002711 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebust4cd34e72018-08-31 10:00:02 -04002712 .release_xprt = xprt_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002713 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002714 .free_slot = xprt_free_slot,
Chuck Lever176e21e2011-05-09 15:22:44 -04002715 .rpcbind = xs_local_rpcbind,
2716 .set_port = xs_local_set_port,
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002717 .connect = xs_local_connect,
Chuck Lever176e21e2011-05-09 15:22:44 -04002718 .buf_alloc = rpc_malloc,
2719 .buf_free = rpc_free,
2720 .send_request = xs_local_send_request,
2721 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2722 .close = xs_close,
Trond Myklebusta1311d82013-10-31 09:18:49 -04002723 .destroy = xs_destroy,
Chuck Lever176e21e2011-05-09 15:22:44 -04002724 .print_stats = xs_local_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002725 .enable_swap = xs_enable_swap,
2726 .disable_swap = xs_disable_swap,
Chuck Lever176e21e2011-05-09 15:22:44 -04002727};
2728
Chuck Leverd31ae252017-08-01 12:00:39 -04002729static const struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002730 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002731 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002732 .release_xprt = xprt_release_xprt_cong,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002733 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002734 .free_slot = xprt_free_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002735 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002736 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002737 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002738 .buf_alloc = rpc_malloc,
2739 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002740 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002741 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002742 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002743 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002744 .close = xs_close,
2745 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002746 .print_stats = xs_udp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002747 .enable_swap = xs_enable_swap,
2748 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002749 .inject_disconnect = xs_inject_disconnect,
Chuck Lever262965f2005-08-11 16:25:56 -04002750};
2751
Chuck Leverd31ae252017-08-01 12:00:39 -04002752static const struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002753 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebust4cd34e72018-08-31 10:00:02 -04002754 .release_xprt = xprt_release_xprt,
Trond Myklebust36bd7de2018-09-03 18:41:32 -04002755 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002756 .free_slot = xprt_free_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002757 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002758 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002759 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002760 .buf_alloc = rpc_malloc,
2761 .buf_free = rpc_free,
Trond Myklebust277e4ab2018-09-14 09:49:06 -04002762 .prepare_request = xs_stream_prepare_request,
Chuck Lever262965f2005-08-11 16:25:56 -04002763 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002764 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustc627d312015-02-10 11:06:04 -05002765 .close = xs_tcp_shutdown,
Chuck Lever9903cd12005-08-11 16:25:26 -04002766 .destroy = xs_destroy,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002767 .set_connect_timeout = xs_tcp_set_connect_timeout,
Chuck Lever262ca072006-03-20 13:44:16 -05002768 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002769 .enable_swap = xs_enable_swap,
2770 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002771 .inject_disconnect = xs_inject_disconnect,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002772#ifdef CONFIG_SUNRPC_BACKCHANNEL
2773 .bc_setup = xprt_setup_bc,
Chuck Lever76566772015-10-24 17:28:32 -04002774 .bc_up = xs_tcp_bc_up,
Chuck Lever6b26cc82016-05-02 14:40:40 -04002775 .bc_maxpayload = xs_tcp_bc_maxpayload,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002776 .bc_free_rqst = xprt_free_bc_rqst,
2777 .bc_destroy = xprt_destroy_bc,
2778#endif
Chuck Levera246b012005-08-11 16:25:23 -04002779};
2780
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002781/*
2782 * The rpc_xprt_ops for the server backchannel
2783 */
2784
Chuck Leverd31ae252017-08-01 12:00:39 -04002785static const struct rpc_xprt_ops bc_tcp_ops = {
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002786 .reserve_xprt = xprt_reserve_xprt,
2787 .release_xprt = xprt_release_xprt,
Bryan Schumaker84e28a32012-09-24 13:39:01 -04002788 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002789 .free_slot = xprt_free_slot,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002790 .buf_alloc = bc_malloc,
2791 .buf_free = bc_free,
2792 .send_request = bc_send_request,
2793 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2794 .close = bc_close,
2795 .destroy = bc_destroy,
2796 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002797 .enable_swap = xs_enable_swap,
2798 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002799 .inject_disconnect = xs_inject_disconnect,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002800};
2801
Chuck Lever92476852010-10-20 11:53:01 -04002802static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2803{
2804 static const struct sockaddr_in sin = {
2805 .sin_family = AF_INET,
2806 .sin_addr.s_addr = htonl(INADDR_ANY),
2807 };
2808 static const struct sockaddr_in6 sin6 = {
2809 .sin6_family = AF_INET6,
2810 .sin6_addr = IN6ADDR_ANY_INIT,
2811 };
2812
2813 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002814 case AF_LOCAL:
2815 break;
Chuck Lever92476852010-10-20 11:53:01 -04002816 case AF_INET:
2817 memcpy(sap, &sin, sizeof(sin));
2818 break;
2819 case AF_INET6:
2820 memcpy(sap, &sin6, sizeof(sin6));
2821 break;
2822 default:
2823 dprintk("RPC: %s: Bad address family\n", __func__);
2824 return -EAFNOSUPPORT;
2825 }
2826 return 0;
2827}
2828
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002829static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002830 unsigned int slot_table_size,
2831 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002832{
2833 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002834 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002835
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002836 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002837 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002838 return ERR_PTR(-EBADF);
2839 }
2840
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002841 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2842 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002843 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002844 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2845 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002846 return ERR_PTR(-ENOMEM);
2847 }
2848
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002849 new = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002850 mutex_init(&new->recv_mutex);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002851 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2852 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002853 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002854 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002855 else {
2856 int err;
2857 err = xs_init_anyaddr(args->dstaddr->sa_family,
2858 (struct sockaddr *)&new->srcaddr);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002859 if (err != 0) {
2860 xprt_free(xprt);
Chuck Lever92476852010-10-20 11:53:01 -04002861 return ERR_PTR(err);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002862 }
Chuck Lever92476852010-10-20 11:53:01 -04002863 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002864
2865 return xprt;
2866}
2867
Chuck Lever176e21e2011-05-09 15:22:44 -04002868static const struct rpc_timeout xs_local_default_timeout = {
2869 .to_initval = 10 * HZ,
2870 .to_maxval = 10 * HZ,
2871 .to_retries = 2,
2872};
2873
2874/**
2875 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2876 * @args: rpc transport creation arguments
2877 *
2878 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2879 */
2880static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2881{
2882 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2883 struct sock_xprt *transport;
2884 struct rpc_xprt *xprt;
2885 struct rpc_xprt *ret;
2886
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002887 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2888 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002889 if (IS_ERR(xprt))
2890 return xprt;
2891 transport = container_of(xprt, struct sock_xprt, xprt);
2892
2893 xprt->prot = 0;
2894 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2895 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2896
2897 xprt->bind_timeout = XS_BIND_TO;
2898 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2899 xprt->idle_timeout = XS_IDLE_DISC_TO;
2900
2901 xprt->ops = &xs_local_ops;
2902 xprt->timeout = &xs_local_default_timeout;
2903
Trond Myklebusta2648092015-10-06 17:03:00 -04002904 INIT_WORK(&transport->recv_worker, xs_local_data_receive_workfn);
NeilBrown93dc41b2013-10-31 16:14:36 +11002905 INIT_DELAYED_WORK(&transport->connect_worker,
2906 xs_dummy_setup_socket);
2907
Chuck Lever176e21e2011-05-09 15:22:44 -04002908 switch (sun->sun_family) {
2909 case AF_LOCAL:
2910 if (sun->sun_path[0] != '/') {
2911 dprintk("RPC: bad AF_LOCAL address: %s\n",
2912 sun->sun_path);
2913 ret = ERR_PTR(-EINVAL);
2914 goto out_err;
2915 }
2916 xprt_set_bound(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002917 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
J. Bruce Fields7073ea82013-02-21 10:14:22 -05002918 ret = ERR_PTR(xs_local_setup_socket(transport));
2919 if (ret)
2920 goto out_err;
Chuck Lever176e21e2011-05-09 15:22:44 -04002921 break;
2922 default:
2923 ret = ERR_PTR(-EAFNOSUPPORT);
2924 goto out_err;
2925 }
2926
2927 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2928 xprt->address_strings[RPC_DISPLAY_ADDR]);
2929
2930 if (try_module_get(THIS_MODULE))
2931 return xprt;
2932 ret = ERR_PTR(-EINVAL);
2933out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002934 xs_xprt_free(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002935 return ret;
2936}
2937
Trond Myklebust2881ae72007-12-20 16:03:54 -05002938static const struct rpc_timeout xs_udp_default_timeout = {
2939 .to_initval = 5 * HZ,
2940 .to_maxval = 30 * HZ,
2941 .to_increment = 5 * HZ,
2942 .to_retries = 5,
2943};
2944
Chuck Lever9903cd12005-08-11 16:25:26 -04002945/**
2946 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002947 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002948 *
2949 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002950static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002951{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002952 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002953 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002954 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002955 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002956
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002957 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2958 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002959 if (IS_ERR(xprt))
2960 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002961 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002962
Chuck Leverec739ef2006-08-22 20:06:15 -04002963 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002964 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002965 /* XXX: header size can vary due to auth type, IPv6, etc. */
2966 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2967
Chuck Lever03bf4b72005-08-25 16:25:55 -07002968 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002969 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2970 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002971
Chuck Lever262965f2005-08-11 16:25:56 -04002972 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002973
Trond Myklebustba7392b2007-12-20 16:03:55 -05002974 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002975
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002976 INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002977 INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2978
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002979 switch (addr->sa_family) {
2980 case AF_INET:
2981 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2982 xprt_set_bound(xprt);
2983
Chuck Lever9dc3b092009-08-09 15:09:46 -04002984 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002985 break;
2986 case AF_INET6:
2987 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2988 xprt_set_bound(xprt);
2989
Chuck Lever9dc3b092009-08-09 15:09:46 -04002990 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002991 break;
2992 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002993 ret = ERR_PTR(-EAFNOSUPPORT);
2994 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002995 }
2996
Chuck Leverc740eff2009-08-09 15:09:46 -04002997 if (xprt_bound(xprt))
2998 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2999 xprt->address_strings[RPC_DISPLAY_ADDR],
3000 xprt->address_strings[RPC_DISPLAY_PORT],
3001 xprt->address_strings[RPC_DISPLAY_PROTO]);
3002 else
3003 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
3004 xprt->address_strings[RPC_DISPLAY_ADDR],
3005 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04003006
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003007 if (try_module_get(THIS_MODULE))
3008 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003009 ret = ERR_PTR(-EINVAL);
3010out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003011 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003012 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04003013}
3014
Trond Myklebust2881ae72007-12-20 16:03:54 -05003015static const struct rpc_timeout xs_tcp_default_timeout = {
3016 .to_initval = 60 * HZ,
3017 .to_maxval = 60 * HZ,
3018 .to_retries = 2,
3019};
3020
Chuck Lever9903cd12005-08-11 16:25:26 -04003021/**
3022 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02003023 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04003024 *
3025 */
Adrian Bunk483066d2007-10-24 18:24:02 +02003026static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04003027{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003028 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04003029 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05003030 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003031 struct rpc_xprt *ret;
Trond Myklebustb7993ce2013-04-14 11:42:00 -04003032 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
3033
3034 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
3035 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Levera246b012005-08-11 16:25:23 -04003036
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003037 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
Trond Myklebustb7993ce2013-04-14 11:42:00 -04003038 max_slot_table_size);
Chuck Leverc8541ec2006-10-17 14:44:27 -04003039 if (IS_ERR(xprt))
3040 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05003041 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04003042
Chuck Leverec739ef2006-08-22 20:06:15 -04003043 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07003044 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07003045 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04003046
Chuck Lever03bf4b72005-08-25 16:25:55 -07003047 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07003048 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
3049 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04003050
Chuck Lever262965f2005-08-11 16:25:56 -04003051 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05003052 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04003053
Trond Myklebust3851f1c2016-08-04 00:08:45 -04003054 xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05003055 xprt->connect_timeout = xprt->timeout->to_initval *
3056 (xprt->timeout->to_retries + 1);
Trond Myklebust3851f1c2016-08-04 00:08:45 -04003057
Trond Myklebustc50b8ee2018-09-14 14:26:28 -04003058 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
Trond Myklebustedc1b012015-10-05 10:53:49 -04003059 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
3060
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003061 switch (addr->sa_family) {
3062 case AF_INET:
3063 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
3064 xprt_set_bound(xprt);
3065
Chuck Lever9dc3b092009-08-09 15:09:46 -04003066 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003067 break;
3068 case AF_INET6:
3069 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
3070 xprt_set_bound(xprt);
3071
Chuck Lever9dc3b092009-08-09 15:09:46 -04003072 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003073 break;
3074 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003075 ret = ERR_PTR(-EAFNOSUPPORT);
3076 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003077 }
3078
Chuck Leverc740eff2009-08-09 15:09:46 -04003079 if (xprt_bound(xprt))
3080 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3081 xprt->address_strings[RPC_DISPLAY_ADDR],
3082 xprt->address_strings[RPC_DISPLAY_PORT],
3083 xprt->address_strings[RPC_DISPLAY_PROTO]);
3084 else
3085 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
3086 xprt->address_strings[RPC_DISPLAY_ADDR],
3087 xprt->address_strings[RPC_DISPLAY_PROTO]);
3088
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003089 if (try_module_get(THIS_MODULE))
3090 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003091 ret = ERR_PTR(-EINVAL);
3092out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003093 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003094 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04003095}
Chuck Lever282b32e2006-12-05 16:35:51 -05003096
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003097/**
3098 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3099 * @args: rpc transport creation arguments
3100 *
3101 */
3102static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
3103{
3104 struct sockaddr *addr = args->dstaddr;
3105 struct rpc_xprt *xprt;
3106 struct sock_xprt *transport;
3107 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003108 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003109
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003110 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3111 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003112 if (IS_ERR(xprt))
3113 return xprt;
3114 transport = container_of(xprt, struct sock_xprt, xprt);
3115
3116 xprt->prot = IPPROTO_TCP;
3117 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
3118 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3119 xprt->timeout = &xs_tcp_default_timeout;
3120
3121 /* backchannel */
3122 xprt_set_bound(xprt);
3123 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003124 xprt->reestablish_timeout = 0;
3125 xprt->idle_timeout = 0;
3126
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003127 xprt->ops = &bc_tcp_ops;
3128
3129 switch (addr->sa_family) {
3130 case AF_INET:
3131 xs_format_peer_addresses(xprt, "tcp",
3132 RPCBIND_NETID_TCP);
3133 break;
3134 case AF_INET6:
3135 xs_format_peer_addresses(xprt, "tcp",
3136 RPCBIND_NETID_TCP6);
3137 break;
3138 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003139 ret = ERR_PTR(-EAFNOSUPPORT);
3140 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003141 }
3142
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04003143 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3144 xprt->address_strings[RPC_DISPLAY_ADDR],
3145 xprt->address_strings[RPC_DISPLAY_PORT],
3146 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003147
3148 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003149 * Once we've associated a backchannel xprt with a connection,
Weng Meiling28303ca2013-11-30 17:56:44 +08003150 * we want to keep it around as long as the connection lasts,
3151 * in case we need to start using it for a backchannel again;
3152 * this reference won't be dropped until bc_xprt is destroyed.
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003153 */
3154 xprt_get(xprt);
3155 args->bc_xprt->xpt_bc_xprt = xprt;
3156 xprt->bc_xprt = args->bc_xprt;
3157 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
3158 transport->sock = bc_sock->sk_sock;
3159 transport->inet = bc_sock->sk_sk;
3160
3161 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003162 * Since we don't want connections for the backchannel, we set
3163 * the xprt status to connected
3164 */
3165 xprt_set_connected(xprt);
3166
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003167 if (try_module_get(THIS_MODULE))
3168 return xprt;
Kinglong Mee642aab52014-03-24 12:00:28 +08003169
3170 args->bc_xprt->xpt_bc_xprt = NULL;
J. Bruce Fields39a9bea2016-05-17 12:38:21 -04003171 args->bc_xprt->xpt_bc_xps = NULL;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003172 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003173 ret = ERR_PTR(-EINVAL);
3174out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003175 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003176 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003177}
3178
Chuck Lever176e21e2011-05-09 15:22:44 -04003179static struct xprt_class xs_local_transport = {
3180 .list = LIST_HEAD_INIT(xs_local_transport.list),
3181 .name = "named UNIX socket",
3182 .owner = THIS_MODULE,
3183 .ident = XPRT_TRANSPORT_LOCAL,
3184 .setup = xs_setup_local,
3185};
3186
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003187static struct xprt_class xs_udp_transport = {
3188 .list = LIST_HEAD_INIT(xs_udp_transport.list),
3189 .name = "udp",
3190 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003191 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003192 .setup = xs_setup_udp,
3193};
3194
3195static struct xprt_class xs_tcp_transport = {
3196 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
3197 .name = "tcp",
3198 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003199 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003200 .setup = xs_setup_tcp,
3201};
3202
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003203static struct xprt_class xs_bc_tcp_transport = {
3204 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3205 .name = "tcp NFSv4.1 backchannel",
3206 .owner = THIS_MODULE,
3207 .ident = XPRT_TRANSPORT_BC_TCP,
3208 .setup = xs_setup_bc_tcp,
3209};
3210
Chuck Lever282b32e2006-12-05 16:35:51 -05003211/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003212 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05003213 *
3214 */
3215int init_socket_xprt(void)
3216{
Jeff Laytonf895b252014-11-17 16:58:04 -05003217#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08003218 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08003219 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05003220#endif
3221
Chuck Lever176e21e2011-05-09 15:22:44 -04003222 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003223 xprt_register_transport(&xs_udp_transport);
3224 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003225 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003226
Chuck Lever282b32e2006-12-05 16:35:51 -05003227 return 0;
3228}
3229
3230/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003231 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05003232 *
3233 */
3234void cleanup_socket_xprt(void)
3235{
Jeff Laytonf895b252014-11-17 16:58:04 -05003236#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Leverfbf76682006-12-05 16:35:54 -05003237 if (sunrpc_table_header) {
3238 unregister_sysctl_table(sunrpc_table_header);
3239 sunrpc_table_header = NULL;
3240 }
3241#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003242
Chuck Lever176e21e2011-05-09 15:22:44 -04003243 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003244 xprt_unregister_transport(&xs_udp_transport);
3245 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003246 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05003247}
Trond Myklebustcbf11072009-08-09 15:06:19 -04003248
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003249static int param_set_uint_minmax(const char *val,
3250 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003251 unsigned int min, unsigned int max)
3252{
Daniel Walter00cfaa92014-06-21 13:06:38 +01003253 unsigned int num;
Trond Myklebustcbf11072009-08-09 15:06:19 -04003254 int ret;
3255
3256 if (!val)
3257 return -EINVAL;
Daniel Walter00cfaa92014-06-21 13:06:38 +01003258 ret = kstrtouint(val, 0, &num);
Dan Carpenter9761a242017-02-19 00:34:59 +03003259 if (ret)
3260 return ret;
3261 if (num < min || num > max)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003262 return -EINVAL;
3263 *((unsigned int *)kp->arg) = num;
3264 return 0;
3265}
3266
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003267static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003268{
Frank Sorensonffb6ca32016-07-08 16:35:25 -05003269 if (kp->arg == &xprt_min_resvport)
3270 return param_set_uint_minmax(val, kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003271 RPC_MIN_RESVPORT,
Frank Sorensonffb6ca32016-07-08 16:35:25 -05003272 xprt_max_resvport);
3273 return param_set_uint_minmax(val, kp,
3274 xprt_min_resvport,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003275 RPC_MAX_RESVPORT);
3276}
3277
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303278static const struct kernel_param_ops param_ops_portnr = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003279 .set = param_set_portnr,
3280 .get = param_get_uint,
3281};
3282
Trond Myklebustcbf11072009-08-09 15:06:19 -04003283#define param_check_portnr(name, p) \
3284 __param_check(name, p, unsigned int);
3285
3286module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3287module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3288
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003289static int param_set_slot_table_size(const char *val,
3290 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003291{
3292 return param_set_uint_minmax(val, kp,
3293 RPC_MIN_SLOT_TABLE,
3294 RPC_MAX_SLOT_TABLE);
3295}
3296
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303297static const struct kernel_param_ops param_ops_slot_table_size = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003298 .set = param_set_slot_table_size,
3299 .get = param_get_uint,
3300};
3301
Trond Myklebustcbf11072009-08-09 15:06:19 -04003302#define param_check_slot_table_size(name, p) \
3303 __param_check(name, p, unsigned int);
3304
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003305static int param_set_max_slot_table_size(const char *val,
3306 const struct kernel_param *kp)
3307{
3308 return param_set_uint_minmax(val, kp,
3309 RPC_MIN_SLOT_TABLE,
3310 RPC_MAX_SLOT_TABLE_LIMIT);
3311}
3312
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303313static const struct kernel_param_ops param_ops_max_slot_table_size = {
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003314 .set = param_set_max_slot_table_size,
3315 .get = param_get_uint,
3316};
3317
3318#define param_check_max_slot_table_size(name, p) \
3319 __param_check(name, p, unsigned int);
3320
Trond Myklebustcbf11072009-08-09 15:06:19 -04003321module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3322 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003323module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3324 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003325module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3326 slot_table_size, 0644);