blob: 90d4c92177b7fad7d5d08c7809085c57e4a0bae1 [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
Trond Myklebust550aebf2018-09-14 14:32:45 -0400673static void
674xs_stream_reset_connect(struct sock_xprt *transport)
675{
676 transport->recv.offset = 0;
677 transport->recv.len = 0;
678 transport->recv.copied = 0;
679 transport->xmit.offset = 0;
680 transport->xprt.stat.connect_count++;
681 transport->xprt.stat.connect_start = jiffies;
682}
683
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400684#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
685
Trond Myklebust24c56842006-10-17 15:06:22 -0400686static 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 -0400687{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400688 struct msghdr msg = {
689 .msg_name = addr,
690 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400691 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
692 };
693 struct kvec iov = {
694 .iov_base = vec->iov_base + base,
695 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400696 };
697
Trond Myklebust24c56842006-10-17 15:06:22 -0400698 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400699 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
700 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
701}
702
Jason Baronf279cd02014-09-24 18:08:00 +0000703static 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 -0400704{
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500705 ssize_t (*do_sendpage)(struct socket *sock, struct page *page,
706 int offset, size_t size, int flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400707 struct page **ppage;
708 unsigned int remainder;
Jason Baronf279cd02014-09-24 18:08:00 +0000709 int err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400710
Trond Myklebust24c56842006-10-17 15:06:22 -0400711 remainder = xdr->page_len - base;
712 base += xdr->page_base;
713 ppage = xdr->pages + (base >> PAGE_SHIFT);
714 base &= ~PAGE_MASK;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500715 do_sendpage = sock->ops->sendpage;
716 if (!zerocopy)
717 do_sendpage = sock_no_sendpage;
Trond Myklebust24c56842006-10-17 15:06:22 -0400718 for(;;) {
719 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
720 int flags = XS_SENDMSG_FLAGS;
721
722 remainder -= len;
Trond Myklebust31303d62015-10-06 15:59:20 -0400723 if (more)
Trond Myklebust24c56842006-10-17 15:06:22 -0400724 flags |= MSG_MORE;
Trond Myklebust31303d62015-10-06 15:59:20 -0400725 if (remainder != 0)
726 flags |= MSG_SENDPAGE_NOTLAST | MSG_MORE;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500727 err = do_sendpage(sock, *ppage, base, len, flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400728 if (remainder == 0 || err != len)
729 break;
Jason Baronf279cd02014-09-24 18:08:00 +0000730 *sent_p += err;
Trond Myklebust24c56842006-10-17 15:06:22 -0400731 ppage++;
732 base = 0;
733 }
Jason Baronf279cd02014-09-24 18:08:00 +0000734 if (err > 0) {
735 *sent_p += err;
736 err = 0;
737 }
738 return err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400739}
740
Chuck Lever9903cd12005-08-11 16:25:26 -0400741/**
742 * xs_sendpages - write pages directly to a socket
743 * @sock: socket to send on
744 * @addr: UDP only -- address of destination
745 * @addrlen: UDP only -- length of destination address
746 * @xdr: buffer containing this request
747 * @base: starting position in the buffer
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500748 * @zerocopy: true if it is safe to use sendpage()
Jason Baronf279cd02014-09-24 18:08:00 +0000749 * @sent_p: return the total number of bytes successfully queued for sending
Chuck Lever9903cd12005-08-11 16:25:26 -0400750 *
Chuck Levera246b012005-08-11 16:25:23 -0400751 */
Jason Baronf279cd02014-09-24 18:08:00 +0000752static 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 -0400753{
Trond Myklebust24c56842006-10-17 15:06:22 -0400754 unsigned int remainder = xdr->len - base;
Jason Baronf279cd02014-09-24 18:08:00 +0000755 int err = 0;
756 int sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400757
Chuck Lever262965f2005-08-11 16:25:56 -0400758 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400759 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400760
Trond Myklebust24c56842006-10-17 15:06:22 -0400761 if (base != 0) {
762 addr = NULL;
763 addrlen = 0;
764 }
Chuck Lever262965f2005-08-11 16:25:56 -0400765
Trond Myklebust24c56842006-10-17 15:06:22 -0400766 if (base < xdr->head[0].iov_len || addr != NULL) {
767 unsigned int len = xdr->head[0].iov_len - base;
768 remainder -= len;
769 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
770 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400771 goto out;
Jason Baronf279cd02014-09-24 18:08:00 +0000772 *sent_p += err;
Chuck Levera246b012005-08-11 16:25:23 -0400773 base = 0;
774 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400775 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400776
Trond Myklebust24c56842006-10-17 15:06:22 -0400777 if (base < xdr->page_len) {
778 unsigned int len = xdr->page_len - base;
779 remainder -= len;
Jason Baronf279cd02014-09-24 18:08:00 +0000780 err = xs_send_pagedata(sock, xdr, base, remainder != 0, zerocopy, &sent);
781 *sent_p += sent;
782 if (remainder == 0 || sent != len)
Chuck Levera246b012005-08-11 16:25:23 -0400783 goto out;
784 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400785 } else
786 base -= xdr->page_len;
787
788 if (base >= xdr->tail[0].iov_len)
Jason Baronf279cd02014-09-24 18:08:00 +0000789 return 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400790 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400791out:
Jason Baronf279cd02014-09-24 18:08:00 +0000792 if (err > 0) {
793 *sent_p += err;
794 err = 0;
795 }
796 return err;
Chuck Levera246b012005-08-11 16:25:23 -0400797}
798
Chuck Lever9903cd12005-08-11 16:25:26 -0400799/**
Trond Myklebustc5445772018-09-03 23:39:27 -0400800 * xs_nospace - handle transmit was incomplete
Trond Myklebust50f484e2018-08-30 13:27:29 -0400801 * @req: pointer to RPC request
Chuck Lever9903cd12005-08-11 16:25:26 -0400802 *
Chuck Levera246b012005-08-11 16:25:23 -0400803 */
Trond Myklebustc5445772018-09-03 23:39:27 -0400804static int xs_nospace(struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -0400805{
Chuck Lever262965f2005-08-11 16:25:56 -0400806 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500807 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500808 struct sock *sk = transport->inet;
Trond Myklebust24ca9a82011-11-22 14:44:28 +0200809 int ret = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400810
Chuck Lever46121cf2007-01-31 12:14:08 -0500811 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Trond Myklebustc5445772018-09-03 23:39:27 -0400812 req->rq_task->tk_pid,
813 req->rq_slen - transport->xmit.offset,
Chuck Lever262965f2005-08-11 16:25:56 -0400814 req->rq_slen);
815
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400816 /* Protect against races with write_space */
817 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400818
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400819 /* Don't race with disconnect */
820 if (xprt_connected(xprt)) {
Trond Myklebust13331a52016-01-06 08:57:06 -0500821 /* wait for more buffer space */
822 sk->sk_write_pending++;
Trond Myklebustc5445772018-09-03 23:39:27 -0400823 xprt_wait_for_buffer_space(xprt);
Trond Myklebust13331a52016-01-06 08:57:06 -0500824 } else
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400825 ret = -ENOTCONN;
Chuck Lever262965f2005-08-11 16:25:56 -0400826
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400827 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500828
829 /* Race breaker in case memory is freed before above code is called */
David Vrabeld48f9ce2016-09-19 13:58:30 +0100830 if (ret == -EAGAIN) {
831 struct socket_wq *wq;
832
833 rcu_read_lock();
834 wq = rcu_dereference(sk->sk_wq);
835 set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags);
836 rcu_read_unlock();
837
838 sk->sk_write_space(sk);
839 }
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400840 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400841}
842
Trond Myklebust277e4ab2018-09-14 09:49:06 -0400843static void
844xs_stream_prepare_request(struct rpc_rqst *req)
845{
846 req->rq_task->tk_status = xdr_alloc_bvec(&req->rq_rcv_buf, GFP_NOIO);
847}
848
Chuck Lever61677ee2011-05-09 15:22:34 -0400849/*
Trond Myklebust4cd34e72018-08-31 10:00:02 -0400850 * Determine if the previous message in the stream was aborted before it
851 * could complete transmission.
852 */
853static bool
854xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
855{
856 return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
857}
858
859/*
Chuck Lever61677ee2011-05-09 15:22:34 -0400860 * Construct a stream transport record marker in @buf.
861 */
862static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
863{
864 u32 reclen = buf->len - sizeof(rpc_fraghdr);
865 rpc_fraghdr *base = buf->head[0].iov_base;
866 *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
867}
868
Chuck Lever262965f2005-08-11 16:25:56 -0400869/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400870 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
Trond Myklebust50f484e2018-08-30 13:27:29 -0400871 * @req: pointer to RPC request
Chuck Lever176e21e2011-05-09 15:22:44 -0400872 *
873 * Return values:
874 * 0: The request has been sent
875 * EAGAIN: The socket was blocked, please call again later to
876 * complete the request
877 * ENOTCONN: Caller needs to invoke connect logic then call again
878 * other: Some other error occured, the request was not sent
879 */
Trond Myklebustadfa7142018-09-03 23:58:59 -0400880static int xs_local_send_request(struct rpc_rqst *req)
Chuck Lever176e21e2011-05-09 15:22:44 -0400881{
Chuck Lever176e21e2011-05-09 15:22:44 -0400882 struct rpc_xprt *xprt = req->rq_xprt;
883 struct sock_xprt *transport =
884 container_of(xprt, struct sock_xprt, xprt);
885 struct xdr_buf *xdr = &req->rq_snd_buf;
886 int status;
Jason Baronf279cd02014-09-24 18:08:00 +0000887 int sent = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -0400888
Trond Myklebust4cd34e72018-08-31 10:00:02 -0400889 /* Close the stream if the previous transmission was incomplete */
890 if (xs_send_request_was_aborted(transport, req)) {
891 xs_close(xprt);
892 return -ENOTCONN;
893 }
894
Chuck Lever176e21e2011-05-09 15:22:44 -0400895 xs_encode_stream_record_marker(&req->rq_snd_buf);
896
897 xs_pktdump("packet data:",
898 req->rq_svec->iov_base, req->rq_svec->iov_len);
899
Chuck Lever78215752018-03-05 15:13:07 -0500900 req->rq_xtime = ktime_get();
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400901 status = xs_sendpages(transport->sock, NULL, 0, xdr,
902 transport->xmit.offset,
Jason Baronf279cd02014-09-24 18:08:00 +0000903 true, &sent);
Chuck Lever176e21e2011-05-09 15:22:44 -0400904 dprintk("RPC: %s(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400905 __func__, xdr->len - transport->xmit.offset, status);
NeilBrown743c69e2015-07-27 10:55:35 +1000906
907 if (status == -EAGAIN && sock_writeable(transport->inet))
908 status = -ENOBUFS;
909
Jason Baronf279cd02014-09-24 18:08:00 +0000910 if (likely(sent > 0) || status == 0) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400911 transport->xmit.offset += sent;
912 req->rq_bytes_sent = transport->xmit.offset;
Chuck Lever176e21e2011-05-09 15:22:44 -0400913 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400914 req->rq_xmit_bytes_sent += transport->xmit.offset;
Chuck Lever176e21e2011-05-09 15:22:44 -0400915 req->rq_bytes_sent = 0;
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400916 transport->xmit.offset = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -0400917 return 0;
918 }
919 status = -EAGAIN;
920 }
921
922 switch (status) {
Trond Myklebust3601c4a2014-06-30 13:42:19 -0400923 case -ENOBUFS:
Trond Myklebustb5872f02015-07-03 09:32:23 -0400924 break;
Chuck Lever176e21e2011-05-09 15:22:44 -0400925 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -0400926 status = xs_nospace(req);
Chuck Lever176e21e2011-05-09 15:22:44 -0400927 break;
928 default:
929 dprintk("RPC: sendmsg returned unrecognized error %d\n",
930 -status);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -0500931 /* fall through */
Chuck Lever176e21e2011-05-09 15:22:44 -0400932 case -EPIPE:
933 xs_close(xprt);
934 status = -ENOTCONN;
935 }
936
937 return status;
938}
939
940/**
Chuck Lever262965f2005-08-11 16:25:56 -0400941 * xs_udp_send_request - write an RPC request to a UDP socket
Trond Myklebust50f484e2018-08-30 13:27:29 -0400942 * @req: pointer to RPC request
Chuck Lever262965f2005-08-11 16:25:56 -0400943 *
944 * Return values:
945 * 0: The request has been sent
946 * EAGAIN: The socket was blocked, please call again later to
947 * complete the request
948 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300949 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -0400950 */
Trond Myklebustadfa7142018-09-03 23:58:59 -0400951static int xs_udp_send_request(struct rpc_rqst *req)
Chuck Lever262965f2005-08-11 16:25:56 -0400952{
Chuck Lever262965f2005-08-11 16:25:56 -0400953 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500954 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400955 struct xdr_buf *xdr = &req->rq_snd_buf;
Jason Baronf279cd02014-09-24 18:08:00 +0000956 int sent = 0;
Chuck Lever262965f2005-08-11 16:25:56 -0400957 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400958
Chuck Lever9903cd12005-08-11 16:25:26 -0400959 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400960 req->rq_svec->iov_base,
961 req->rq_svec->iov_len);
962
Trond Myklebust01d37c42009-03-11 14:09:39 -0400963 if (!xprt_bound(xprt))
964 return -ENOTCONN;
Trond Myklebust75891f52018-09-03 17:37:36 -0400965
966 if (!xprt_request_get_cong(xprt, req))
967 return -EBADSLT;
968
Chuck Lever78215752018-03-05 15:13:07 -0500969 req->rq_xtime = ktime_get();
Jason Baronf279cd02014-09-24 18:08:00 +0000970 status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen,
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400971 xdr, 0, true, &sent);
Chuck Levera246b012005-08-11 16:25:23 -0400972
Chuck Lever46121cf2007-01-31 12:14:08 -0500973 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400974 xdr->len, status);
Chuck Levera246b012005-08-11 16:25:23 -0400975
Jason Baron3dedbb52014-09-24 18:08:04 +0000976 /* firewall is blocking us, don't return -EAGAIN or we end up looping */
977 if (status == -EPERM)
978 goto process_status;
979
NeilBrown743c69e2015-07-27 10:55:35 +1000980 if (status == -EAGAIN && sock_writeable(transport->inet))
981 status = -ENOBUFS;
982
Jason Baronf279cd02014-09-24 18:08:00 +0000983 if (sent > 0 || status == 0) {
984 req->rq_xmit_bytes_sent += sent;
985 if (sent >= req->rq_slen)
Trond Myklebust21997002007-10-01 11:43:37 -0400986 return 0;
987 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400988 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400989 }
Chuck Levera246b012005-08-11 16:25:23 -0400990
Jason Baron3dedbb52014-09-24 18:08:04 +0000991process_status:
Chuck Lever262965f2005-08-11 16:25:56 -0400992 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400993 case -ENOTSOCK:
994 status = -ENOTCONN;
995 /* Should we call xs_close() here? */
996 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400997 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -0400998 status = xs_nospace(req);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400999 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001000 case -ENETUNREACH:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001001 case -ENOBUFS:
Chuck Lever262965f2005-08-11 16:25:56 -04001002 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -04001003 case -ECONNREFUSED:
Jason Baron3dedbb52014-09-24 18:08:04 +00001004 case -EPERM:
Chuck Levera246b012005-08-11 16:25:23 -04001005 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -04001006 * prompts ECONNREFUSED. */
Trond Myklebust13331a52016-01-06 08:57:06 -05001007 break;
1008 default:
1009 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1010 -status);
Chuck Levera246b012005-08-11 16:25:23 -04001011 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +08001012
Chuck Lever262965f2005-08-11 16:25:56 -04001013 return status;
Chuck Levera246b012005-08-11 16:25:23 -04001014}
1015
Trond Myklebuste06799f2007-11-05 15:44:12 -05001016/**
Chuck Lever262965f2005-08-11 16:25:56 -04001017 * xs_tcp_send_request - write an RPC request to a TCP socket
Trond Myklebust50f484e2018-08-30 13:27:29 -04001018 * @req: pointer to RPC request
Chuck Lever9903cd12005-08-11 16:25:26 -04001019 *
1020 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -04001021 * 0: The request has been sent
1022 * EAGAIN: The socket was blocked, please call again later to
1023 * complete the request
1024 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001025 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -04001026 *
1027 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -04001028 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -04001029 */
Trond Myklebustadfa7142018-09-03 23:58:59 -04001030static int xs_tcp_send_request(struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001031{
Chuck Levera246b012005-08-11 16:25:23 -04001032 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001033 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -04001034 struct xdr_buf *xdr = &req->rq_snd_buf;
Trond Myklebusta6b31d12013-11-08 16:03:50 -05001035 bool zerocopy = true;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001036 bool vm_wait = false;
Chuck Leverb595bb12007-08-06 11:56:42 -04001037 int status;
Jason Baronf279cd02014-09-24 18:08:00 +00001038 int sent;
Chuck Levera246b012005-08-11 16:25:23 -04001039
Trond Myklebust4cd34e72018-08-31 10:00:02 -04001040 /* Close the stream if the previous transmission was incomplete */
1041 if (xs_send_request_was_aborted(transport, req)) {
1042 if (transport->sock != NULL)
1043 kernel_sock_shutdown(transport->sock, SHUT_RDWR);
1044 return -ENOTCONN;
1045 }
1046
Chuck Lever61677ee2011-05-09 15:22:34 -04001047 xs_encode_stream_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -04001048
Chuck Lever262965f2005-08-11 16:25:56 -04001049 xs_pktdump("packet data:",
1050 req->rq_svec->iov_base,
1051 req->rq_svec->iov_len);
Trond Myklebusta6b31d12013-11-08 16:03:50 -05001052 /* Don't use zero copy if this is a resend. If the RPC call
1053 * completes while the socket holds a reference to the pages,
1054 * then we may end up resending corrupted data.
1055 */
Trond Myklebust50f484e2018-08-30 13:27:29 -04001056 if (req->rq_task->tk_flags & RPC_TASK_SENT)
Trond Myklebusta6b31d12013-11-08 16:03:50 -05001057 zerocopy = false;
Chuck Levera246b012005-08-11 16:25:23 -04001058
Trond Myklebust7196dbb2017-02-08 11:17:54 -05001059 if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
1060 xs_tcp_set_socket_timeouts(xprt, transport->sock);
1061
Chuck Levera246b012005-08-11 16:25:23 -04001062 /* Continue transmitting the packet/record. We must be careful
1063 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -04001064 * called sendmsg(). */
Chuck Lever78215752018-03-05 15:13:07 -05001065 req->rq_xtime = ktime_get();
Chuck Levera246b012005-08-11 16:25:23 -04001066 while (1) {
Jason Baronf279cd02014-09-24 18:08:00 +00001067 sent = 0;
1068 status = xs_sendpages(transport->sock, NULL, 0, xdr,
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001069 transport->xmit.offset,
1070 zerocopy, &sent);
Chuck Levera246b012005-08-11 16:25:23 -04001071
Chuck Lever46121cf2007-01-31 12:14:08 -05001072 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001073 xdr->len - transport->xmit.offset, status);
Chuck Lever262965f2005-08-11 16:25:56 -04001074
Chuck Lever262965f2005-08-11 16:25:56 -04001075 /* If we've sent the entire packet, immediately
1076 * reset the count of bytes sent. */
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001077 transport->xmit.offset += sent;
1078 req->rq_bytes_sent = transport->xmit.offset;
Chuck Lever262965f2005-08-11 16:25:56 -04001079 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001080 req->rq_xmit_bytes_sent += transport->xmit.offset;
Chuck Lever262965f2005-08-11 16:25:56 -04001081 req->rq_bytes_sent = 0;
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001082 transport->xmit.offset = 0;
Chuck Lever262965f2005-08-11 16:25:56 -04001083 return 0;
Chuck Levera246b012005-08-11 16:25:23 -04001084 }
1085
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001086 WARN_ON_ONCE(sent == 0 && status == 0);
1087
1088 if (status == -EAGAIN ) {
1089 /*
1090 * Return EAGAIN if we're sure we're hitting the
1091 * socket send buffer limits.
1092 */
1093 if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
1094 break;
1095 /*
1096 * Did we hit a memory allocation failure?
1097 */
1098 if (sent == 0) {
1099 status = -ENOBUFS;
1100 if (vm_wait)
1101 break;
1102 /* Retry, knowing now that we're below the
1103 * socket send buffer limit
1104 */
1105 vm_wait = true;
1106 }
1107 continue;
1108 }
Trond Myklebustf580dd02015-07-11 17:48:52 +02001109 if (status < 0)
1110 break;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -04001111 vm_wait = false;
Chuck Levera246b012005-08-11 16:25:23 -04001112 }
1113
Chuck Lever262965f2005-08-11 16:25:56 -04001114 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -04001115 case -ENOTSOCK:
1116 status = -ENOTCONN;
1117 /* Should we call xs_close() here? */
1118 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001119 case -EAGAIN:
Trond Myklebustc5445772018-09-03 23:39:27 -04001120 status = xs_nospace(req);
Chuck Lever262965f2005-08-11 16:25:56 -04001121 break;
Chuck Lever262965f2005-08-11 16:25:56 -04001122 case -ECONNRESET:
Chuck Lever262965f2005-08-11 16:25:56 -04001123 case -ECONNREFUSED:
1124 case -ENOTCONN:
Trond Myklebust3913c782015-02-08 21:44:04 -05001125 case -EADDRINUSE:
Trond Myklebustb5872f02015-07-03 09:32:23 -04001126 case -ENOBUFS:
Trond Myklebustb9d2bb22012-10-23 11:40:02 -04001127 case -EPIPE:
Trond Myklebust13331a52016-01-06 08:57:06 -05001128 break;
1129 default:
1130 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1131 -status);
Chuck Levera246b012005-08-11 16:25:23 -04001132 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +08001133
Chuck Levera246b012005-08-11 16:25:23 -04001134 return status;
1135}
1136
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001137static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1138{
1139 transport->old_data_ready = sk->sk_data_ready;
1140 transport->old_state_change = sk->sk_state_change;
1141 transport->old_write_space = sk->sk_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -05001142 transport->old_error_report = sk->sk_error_report;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001143}
1144
1145static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1146{
1147 sk->sk_data_ready = transport->old_data_ready;
1148 sk->sk_state_change = transport->old_state_change;
1149 sk->sk_write_space = transport->old_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -05001150 sk->sk_error_report = transport->old_error_report;
1151}
1152
Trond Myklebust42d42a52016-05-23 09:24:55 -04001153static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
1154{
1155 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1156
1157 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
1158}
1159
Trond Myklebustb70ae912015-02-09 09:41:32 -05001160static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1161{
1162 smp_mb__before_atomic();
1163 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1164 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001165 xs_sock_reset_state_flags(xprt);
Trond Myklebustb70ae912015-02-09 09:41:32 -05001166 smp_mb__after_atomic();
1167}
1168
Trond Myklebust21180712013-12-31 13:22:59 -05001169/**
1170 * xs_error_report - callback to handle TCP socket state errors
1171 * @sk: socket
1172 *
1173 * Note: we don't call sock_error() since there may be a rpc_task
1174 * using the socket, and so we don't want to clear sk->sk_err.
1175 */
1176static void xs_error_report(struct sock *sk)
1177{
1178 struct rpc_xprt *xprt;
1179 int err;
1180
1181 read_lock_bh(&sk->sk_callback_lock);
1182 if (!(xprt = xprt_from_sock(sk)))
1183 goto out;
1184
1185 err = -sk->sk_err;
1186 if (err == 0)
1187 goto out;
1188 dprintk("RPC: xs_error_report client %p, error=%d...\n",
1189 xprt, -err);
Trond Myklebuste8353c762013-12-31 13:39:22 -05001190 trace_rpc_socket_error(xprt, sk->sk_socket, err);
Trond Myklebust21180712013-12-31 13:22:59 -05001191 xprt_wake_pending_tasks(xprt, err);
1192 out:
1193 read_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001194}
1195
Chuck Leverfe315e762009-03-11 14:10:21 -04001196static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001197{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001198 struct socket *sock = transport->sock;
1199 struct sock *sk = transport->inet;
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001200 struct rpc_xprt *xprt = &transport->xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001201
Chuck Leverfe315e762009-03-11 14:10:21 -04001202 if (sk == NULL)
1203 return;
Chuck Lever9903cd12005-08-11 16:25:26 -04001204
Jeff Layton264d1df2015-06-03 16:14:27 -04001205 if (atomic_read(&transport->xprt.swapper))
1206 sk_clear_memalloc(sk);
1207
Trond Myklebust09939202015-08-29 19:11:21 -07001208 kernel_sock_shutdown(sock, SHUT_RDWR);
1209
Trond Myklebustedc1b012015-10-05 10:53:49 -04001210 mutex_lock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -04001211 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001212 transport->inet = NULL;
1213 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -04001214
Chuck Lever9903cd12005-08-11 16:25:26 -04001215 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001216
1217 xs_restore_old_callbacks(transport, sk);
Trond Myklebust0c787892015-08-29 13:36:30 -07001218 xprt_clear_connected(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001219 write_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001220 xs_sock_reset_connection_flags(xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04001221 mutex_unlock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -04001222
Trond Myklebust6cc7e902015-02-08 18:35:25 -05001223 trace_rpc_socket_close(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001224 sock_release(sock);
Chuck Leverfe315e762009-03-11 14:10:21 -04001225}
1226
1227/**
1228 * xs_close - close a socket
1229 * @xprt: transport
1230 *
1231 * This is used when all requests are complete; ie, no DRC state remains
1232 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -04001233 *
1234 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1235 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e762009-03-11 14:10:21 -04001236 */
1237static void xs_close(struct rpc_xprt *xprt)
1238{
1239 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1240
1241 dprintk("RPC: xs_close xprt %p\n", xprt);
1242
1243 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -04001244 xprt->reestablish_timeout = 0;
Chuck Leverfe315e762009-03-11 14:10:21 -04001245
Trond Myklebust62da3b22007-11-06 18:44:20 -05001246 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001247}
1248
Chuck Lever4a068252015-05-11 14:02:25 -04001249static void xs_inject_disconnect(struct rpc_xprt *xprt)
1250{
1251 dprintk("RPC: injecting transport disconnect on xprt=%p\n",
1252 xprt);
1253 xprt_disconnect_done(xprt);
1254}
1255
Kinglong Mee315f3812014-03-24 11:07:22 +08001256static void xs_xprt_free(struct rpc_xprt *xprt)
1257{
1258 xs_free_peer_addresses(xprt);
1259 xprt_free(xprt);
1260}
1261
Chuck Lever9903cd12005-08-11 16:25:26 -04001262/**
1263 * xs_destroy - prepare to shutdown a transport
1264 * @xprt: doomed transport
1265 *
1266 */
1267static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001268{
Trond Myklebust03c788272015-09-17 10:42:27 -04001269 struct sock_xprt *transport = container_of(xprt,
1270 struct sock_xprt, xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05001271 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001272
Trond Myklebust03c788272015-09-17 10:42:27 -04001273 cancel_delayed_work_sync(&transport->connect_worker);
Trond Myklebusta1311d82013-10-31 09:18:49 -04001274 xs_close(xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04001275 cancel_work_sync(&transport->recv_worker);
Kinglong Mee315f3812014-03-24 11:07:22 +08001276 xs_xprt_free(xprt);
Trond Myklebusta1311d82013-10-31 09:18:49 -04001277 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -04001278}
1279
Chuck Lever9903cd12005-08-11 16:25:26 -04001280/**
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001281 * xs_udp_data_read_skb - receive callback for UDP sockets
1282 * @xprt: transport
1283 * @sk: socket
1284 * @skb: skbuff
Chuck Lever9903cd12005-08-11 16:25:26 -04001285 *
1286 */
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001287static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1288 struct sock *sk,
1289 struct sk_buff *skb)
Chuck Lever9903cd12005-08-11 16:25:26 -04001290{
1291 struct rpc_task *task;
Chuck Levera246b012005-08-11 16:25:23 -04001292 struct rpc_rqst *rovr;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001293 int repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001294 u32 _xid;
1295 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -04001296
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001297 repsize = skb->len;
Chuck Levera246b012005-08-11 16:25:23 -04001298 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001299 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001300 return;
Chuck Levera246b012005-08-11 16:25:23 -04001301 }
1302
1303 /* Copy the XID from the skb... */
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001304 xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
Chuck Levera246b012005-08-11 16:25:23 -04001305 if (xp == NULL)
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001306 return;
Chuck Levera246b012005-08-11 16:25:23 -04001307
1308 /* Look up and lock the request corresponding to the given XID */
Trond Myklebust75c84152018-08-31 10:21:00 -04001309 spin_lock(&xprt->queue_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001310 rovr = xprt_lookup_rqst(xprt, *xp);
1311 if (!rovr)
1312 goto out_unlock;
Trond Myklebust729749b2017-08-13 10:03:59 -04001313 xprt_pin_rqst(rovr);
Chuck Leverecd465e2018-03-05 15:12:57 -05001314 xprt_update_rtt(rovr->rq_task);
Trond Myklebust75c84152018-08-31 10:21:00 -04001315 spin_unlock(&xprt->queue_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001316 task = rovr->rq_task;
1317
Chuck Levera246b012005-08-11 16:25:23 -04001318 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1319 copied = repsize;
1320
1321 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001322 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
Trond Myklebust75c84152018-08-31 10:21:00 -04001323 spin_lock(&xprt->queue_lock);
Trond Myklebust0afa6b42018-02-09 09:39:42 -05001324 __UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
Trond Myklebust729749b2017-08-13 10:03:59 -04001325 goto out_unpin;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001326 }
1327
Chuck Levera246b012005-08-11 16:25:23 -04001328
Trond Myklebust729749b2017-08-13 10:03:59 -04001329 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001330 xprt_adjust_cwnd(xprt, task, copied);
Trond Myklebustce7c2522017-08-16 15:30:35 -04001331 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust75c84152018-08-31 10:21:00 -04001332 spin_lock(&xprt->queue_lock);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001333 xprt_complete_rqst(task, copied);
Trond Myklebust0afa6b42018-02-09 09:39:42 -05001334 __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
Trond Myklebust729749b2017-08-13 10:03:59 -04001335out_unpin:
1336 xprt_unpin_rqst(rovr);
Chuck Levera246b012005-08-11 16:25:23 -04001337 out_unlock:
Trond Myklebust75c84152018-08-31 10:21:00 -04001338 spin_unlock(&xprt->queue_lock);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001339}
1340
1341static void xs_udp_data_receive(struct sock_xprt *transport)
1342{
1343 struct sk_buff *skb;
1344 struct sock *sk;
1345 int err;
1346
Trond Myklebust0af34422018-01-14 15:28:29 -05001347restart:
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001348 mutex_lock(&transport->recv_mutex);
1349 sk = transport->inet;
1350 if (sk == NULL)
1351 goto out;
1352 for (;;) {
Paolo Abeni7c13f972016-11-04 11:28:59 +01001353 skb = skb_recv_udp(sk, 0, 1, &err);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001354 if (skb != NULL) {
1355 xs_udp_data_read_skb(&transport->xprt, sk, skb);
Paolo Abeni850cbad2016-10-21 13:55:47 +02001356 consume_skb(skb);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001357 continue;
1358 }
1359 if (!test_and_clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001360 break;
Trond Myklebust0af34422018-01-14 15:28:29 -05001361 if (need_resched()) {
1362 mutex_unlock(&transport->recv_mutex);
1363 cond_resched();
1364 goto restart;
1365 }
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001366 }
1367out:
1368 mutex_unlock(&transport->recv_mutex);
1369}
1370
1371static void xs_udp_data_receive_workfn(struct work_struct *work)
1372{
1373 struct sock_xprt *transport =
1374 container_of(work, struct sock_xprt, recv_worker);
1375 xs_udp_data_receive(transport);
1376}
1377
1378/**
1379 * xs_data_ready - "data ready" callback for UDP sockets
1380 * @sk: socket with data to read
1381 *
1382 */
1383static void xs_data_ready(struct sock *sk)
1384{
1385 struct rpc_xprt *xprt;
1386
1387 read_lock_bh(&sk->sk_callback_lock);
1388 dprintk("RPC: xs_data_ready...\n");
1389 xprt = xprt_from_sock(sk);
1390 if (xprt != NULL) {
1391 struct sock_xprt *transport = container_of(xprt,
1392 struct sock_xprt, xprt);
Trond Myklebust5157b952016-05-29 10:13:24 -04001393 transport->old_data_ready(sk);
1394 /* Any data means we had a useful conversation, so
1395 * then we don't need to delay the next reconnect
1396 */
1397 if (xprt->reestablish_timeout)
1398 xprt->reestablish_timeout = 0;
Trond Myklebust42d42a52016-05-23 09:24:55 -04001399 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebust40a5f1b2016-05-27 10:39:50 -04001400 queue_work(xprtiod_workqueue, &transport->recv_worker);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001401 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001402 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001403}
1404
Trond Myklebusta519fc72012-09-12 16:49:15 -04001405/*
1406 * Helper function to force a TCP close if the server is sending
1407 * junk and/or it has put us in CLOSE_WAIT
1408 */
1409static void xs_tcp_force_close(struct rpc_xprt *xprt)
1410{
Trond Myklebusta519fc72012-09-12 16:49:15 -04001411 xprt_force_disconnect(xprt);
1412}
1413
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001414#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Chuck Lever76566772015-10-24 17:28:32 -04001415static int xs_tcp_bc_up(struct svc_serv *serv, struct net *net)
1416{
1417 int ret;
1418
1419 ret = svc_create_xprt(serv, "tcp-bc", net, PF_INET, 0,
1420 SVC_SOCK_ANONYMOUS);
1421 if (ret < 0)
1422 return ret;
1423 return 0;
1424}
Chuck Lever6b26cc82016-05-02 14:40:40 -04001425
1426static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
1427{
1428 return PAGE_SIZE;
1429}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001430#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001431
Chuck Lever9903cd12005-08-11 16:25:26 -04001432/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001433 * xs_tcp_state_change - callback to handle TCP socket state changes
1434 * @sk: socket whose state has changed
1435 *
1436 */
1437static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001438{
Chuck Lever9903cd12005-08-11 16:25:26 -04001439 struct rpc_xprt *xprt;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001440 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001441
Eric Dumazetf064af12010-09-22 12:43:39 +00001442 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001443 if (!(xprt = xprt_from_sock(sk)))
1444 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001445 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001446 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001447 sk->sk_state, xprt_connected(xprt),
1448 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001449 sock_flag(sk, SOCK_ZAPPED),
1450 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001451
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001452 transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001453 trace_rpc_socket_state_change(xprt, sk->sk_socket);
Chuck Levera246b012005-08-11 16:25:23 -04001454 switch (sk->sk_state) {
1455 case TCP_ESTABLISHED:
Eric Dumazetf064af12010-09-22 12:43:39 +00001456 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001457 if (!xprt_test_and_set_connected(xprt)) {
Trond Myklebust8b717982013-09-26 10:18:04 -04001458 xprt->connect_cookie++;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001459 clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
1460 xprt_clear_connecting(xprt);
Chuck Lever51971132006-12-05 16:35:19 -05001461
Trond Myklebust2a491992009-03-11 14:38:00 -04001462 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001463 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001464 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001465 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001466 case TCP_FIN_WAIT1:
1467 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001468 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001469 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001470 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001471 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001472 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001473 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001474 smp_mb__after_atomic();
Chuck Levera246b012005-08-11 16:25:23 -04001475 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001476 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001477 /* The server initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001478 xprt->connect_cookie++;
Trond Myklebustd0bea452012-10-23 11:35:47 -04001479 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebusta519fc72012-09-12 16:49:15 -04001480 xs_tcp_force_close(xprt);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001481 /* fall through */
Trond Myklebust663b8852008-01-01 18:42:12 -05001482 case TCP_CLOSING:
1483 /*
1484 * If the server closed down the connection, make sure that
1485 * we back off before reconnecting
1486 */
1487 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1488 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001489 break;
1490 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001491 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001492 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001493 clear_bit(XPRT_CONNECTED, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001494 smp_mb__after_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001495 break;
1496 case TCP_CLOSE:
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001497 if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
1498 &transport->sock_state))
1499 xprt_clear_connecting(xprt);
Trond Myklebust9b308892018-02-05 10:20:06 -05001500 clear_bit(XPRT_CLOSING, &xprt->state);
NeilBrown3ffbc1d2017-07-19 14:05:01 +10001501 if (sk->sk_err)
1502 xprt_wake_pending_tasks(xprt, -sk->sk_err);
Trond Myklebust9b308892018-02-05 10:20:06 -05001503 /* Trigger the socket release */
1504 xs_tcp_force_close(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001505 }
1506 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001507 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001508}
1509
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001510static void xs_write_space(struct sock *sk)
1511{
Trond Myklebust13331a52016-01-06 08:57:06 -05001512 struct socket_wq *wq;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001513 struct rpc_xprt *xprt;
1514
Trond Myklebust13331a52016-01-06 08:57:06 -05001515 if (!sk->sk_socket)
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001516 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001517 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001518
1519 if (unlikely(!(xprt = xprt_from_sock(sk))))
1520 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001521 rcu_read_lock();
1522 wq = rcu_dereference(sk->sk_wq);
1523 if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0)
1524 goto out;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001525
Trond Myklebustc5445772018-09-03 23:39:27 -04001526 if (xprt_write_space(xprt))
1527 sk->sk_write_pending--;
Trond Myklebust13331a52016-01-06 08:57:06 -05001528out:
1529 rcu_read_unlock();
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001530}
1531
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001532/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001533 * xs_udp_write_space - callback invoked when socket buffer space
1534 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001535 * @sk: socket whose state has changed
1536 *
Chuck Levera246b012005-08-11 16:25:23 -04001537 * Called when more output buffer space is available for this socket.
1538 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001539 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001540 * with a bunch of small requests.
1541 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001542static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001543{
Eric Dumazetf064af12010-09-22 12:43:39 +00001544 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001545
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001546 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001547 if (sock_writeable(sk))
1548 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001549
Eric Dumazetf064af12010-09-22 12:43:39 +00001550 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001551}
Chuck Levera246b012005-08-11 16:25:23 -04001552
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001553/**
1554 * xs_tcp_write_space - callback invoked when socket buffer space
1555 * becomes available
1556 * @sk: socket whose state has changed
1557 *
1558 * Called when more output buffer space is available for this socket.
1559 * We try not to wake our writers until they can make "significant"
1560 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1561 * with a bunch of small requests.
1562 */
1563static void xs_tcp_write_space(struct sock *sk)
1564{
Eric Dumazetf064af12010-09-22 12:43:39 +00001565 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001566
1567 /* from net/core/stream.c:sk_stream_write_space */
Eric Dumazet64dc6132013-07-22 20:26:31 -07001568 if (sk_stream_is_writeable(sk))
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001569 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001570
Eric Dumazetf064af12010-09-22 12:43:39 +00001571 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001572}
1573
Chuck Lever470056c2005-08-25 16:25:56 -07001574static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001575{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001576 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1577 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001578
Chuck Lever7c6e0662006-12-05 16:35:30 -05001579 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001580 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001581 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001582 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001583 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001584 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001585 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001586 sk->sk_write_space(sk);
1587 }
1588}
1589
Chuck Lever43118c22005-08-25 16:25:49 -07001590/**
Chuck Lever470056c2005-08-25 16:25:56 -07001591 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001592 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001593 * @sndsize: requested size of send buffer, in bytes
1594 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001595 *
Chuck Lever470056c2005-08-25 16:25:56 -07001596 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001597 */
Chuck Lever470056c2005-08-25 16:25:56 -07001598static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001599{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001600 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1601
1602 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001603 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001604 transport->sndsize = sndsize + 1024;
1605 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001606 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001607 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001608
1609 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001610}
1611
Chuck Lever46c0ee82005-08-25 16:25:52 -07001612/**
1613 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1614 * @task: task that timed out
1615 *
1616 * Adjust the congestion window after a retransmit timeout has occurred.
1617 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001618static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever46c0ee82005-08-25 16:25:52 -07001619{
Chuck Leverb977b642017-02-08 17:00:51 -05001620 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001621 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
Chuck Leverb977b642017-02-08 17:00:51 -05001622 spin_unlock_bh(&xprt->transport_lock);
Chuck Lever46c0ee82005-08-25 16:25:52 -07001623}
1624
Chuck Leverb85d8802006-05-25 01:40:49 -04001625static unsigned short xs_get_random_port(void)
1626{
Frank Sorenson5d718992016-07-08 16:35:23 -05001627 unsigned short range = xprt_max_resvport - xprt_min_resvport + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001628 unsigned short rand = (unsigned short) prandom_u32() % range;
Chuck Leverb85d8802006-05-25 01:40:49 -04001629 return rand + xprt_min_resvport;
1630}
1631
Chuck Lever92200412006-01-03 09:55:51 +01001632/**
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001633 * xs_set_reuseaddr_port - set the socket's port and address reuse options
1634 * @sock: socket
1635 *
1636 * Note that this function has to be called on all sockets that share the
1637 * same port, and it must be called before binding.
1638 */
1639static void xs_sock_set_reuseport(struct socket *sock)
1640{
Trond Myklebust402e23b2015-02-09 17:20:14 -05001641 int opt = 1;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001642
Trond Myklebust402e23b2015-02-09 17:20:14 -05001643 kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
1644 (char *)&opt, sizeof(opt));
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001645}
1646
1647static unsigned short xs_sock_getport(struct socket *sock)
1648{
1649 struct sockaddr_storage buf;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001650 unsigned short port = 0;
1651
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001652 if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001653 goto out;
1654 switch (buf.ss_family) {
1655 case AF_INET6:
1656 port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
1657 break;
1658 case AF_INET:
1659 port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
1660 }
1661out:
1662 return port;
1663}
1664
1665/**
Chuck Lever92200412006-01-03 09:55:51 +01001666 * xs_set_port - reset the port number in the remote endpoint address
1667 * @xprt: generic transport
1668 * @port: new port number
1669 *
1670 */
1671static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1672{
Chuck Lever46121cf2007-01-31 12:14:08 -05001673 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001674
Chuck Lever9dc3b092009-08-09 15:09:46 -04001675 rpc_set_port(xs_addr(xprt), port);
1676 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001677}
1678
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001679static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
1680{
1681 if (transport->srcport == 0)
1682 transport->srcport = xs_sock_getport(sock);
1683}
1684
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001685static unsigned short xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001686{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001687 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001688
1689 if (port == 0 && transport->xprt.resvport)
1690 port = xs_get_random_port();
1691 return port;
1692}
1693
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001694static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001695{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001696 if (transport->srcport != 0)
1697 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001698 if (!transport->xprt.resvport)
1699 return 0;
1700 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1701 return xprt_max_resvport;
1702 return --port;
1703}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001704static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001705{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001706 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001707 int err, nloop = 0;
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001708 unsigned short port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001709 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001710
Chris Perl0f7a6222014-09-05 15:40:21 -04001711 /*
1712 * If we are asking for any ephemeral port (i.e. port == 0 &&
1713 * transport->xprt.resvport == 0), don't bind. Let the local
1714 * port selection happen implicitly when the socket is used
1715 * (for example at connect time).
1716 *
1717 * This ensures that we can continue to establish TCP
1718 * connections even when all local ephemeral ports are already
1719 * a part of some TCP connection. This makes no difference
1720 * for UDP sockets, but also doens't harm them.
1721 *
1722 * If we're asking for any reserved port (i.e. port == 0 &&
1723 * transport->xprt.resvport == 1) xs_get_srcport above will
1724 * ensure that port is non-zero and we will bind as needed.
1725 */
1726 if (port == 0)
1727 return 0;
1728
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001729 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001730 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001731 rpc_set_port((struct sockaddr *)&myaddr, port);
1732 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1733 transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001734 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001735 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001736 break;
Chuck Levera246b012005-08-11 16:25:23 -04001737 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001738 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001739 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001740 if (port > last)
1741 nloop++;
1742 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001743
Chuck Lever4232e862010-10-20 11:52:51 -04001744 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001745 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1746 &((struct sockaddr_in *)&myaddr)->sin_addr,
1747 port, err ? "failed" : "ok", err);
1748 else
1749 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1750 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1751 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001752 return err;
1753}
1754
Chuck Lever176e21e2011-05-09 15:22:44 -04001755/*
1756 * We don't support autobind on AF_LOCAL sockets
1757 */
1758static void xs_local_rpcbind(struct rpc_task *task)
1759{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001760 xprt_set_bound(task->tk_xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04001761}
1762
1763static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1764{
1765}
Chuck Levera246b012005-08-11 16:25:23 -04001766
Peter Zijlstraed075362006-12-06 20:35:24 -08001767#ifdef CONFIG_DEBUG_LOCK_ALLOC
1768static struct lock_class_key xs_key[2];
1769static struct lock_class_key xs_slock_key[2];
1770
Chuck Lever176e21e2011-05-09 15:22:44 -04001771static inline void xs_reclassify_socketu(struct socket *sock)
1772{
1773 struct sock *sk = sock->sk;
1774
Chuck Lever176e21e2011-05-09 15:22:44 -04001775 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1776 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1777}
1778
Chuck Lever8945ee52007-08-06 11:58:04 -04001779static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001780{
1781 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001782
Chuck Lever8945ee52007-08-06 11:58:04 -04001783 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1784 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1785}
Peter Zijlstraed075362006-12-06 20:35:24 -08001786
Chuck Lever8945ee52007-08-06 11:58:04 -04001787static inline void xs_reclassify_socket6(struct socket *sock)
1788{
1789 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001790
Chuck Lever8945ee52007-08-06 11:58:04 -04001791 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1792 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001793}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001794
1795static inline void xs_reclassify_socket(int family, struct socket *sock)
1796{
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02001797 if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -04001798 return;
1799
Chuck Lever4232e862010-10-20 11:52:51 -04001800 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001801 case AF_LOCAL:
1802 xs_reclassify_socketu(sock);
1803 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001804 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001805 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001806 break;
1807 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001808 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001809 break;
1810 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001811}
Peter Zijlstraed075362006-12-06 20:35:24 -08001812#else
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001813static inline void xs_reclassify_socket(int family, struct socket *sock)
1814{
1815}
Peter Zijlstraed075362006-12-06 20:35:24 -08001816#endif
1817
NeilBrown93dc41b2013-10-31 16:14:36 +11001818static void xs_dummy_setup_socket(struct work_struct *work)
1819{
1820}
1821
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001822static struct socket *xs_create_sock(struct rpc_xprt *xprt,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001823 struct sock_xprt *transport, int family, int type,
1824 int protocol, bool reuseport)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001825{
1826 struct socket *sock;
1827 int err;
1828
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001829 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001830 if (err < 0) {
1831 dprintk("RPC: can't create %d transport socket (%d).\n",
1832 protocol, -err);
1833 goto out;
1834 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001835 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001836
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001837 if (reuseport)
1838 xs_sock_set_reuseport(sock);
1839
Ben Hutchings4cea2882011-02-22 21:54:34 +00001840 err = xs_bind(transport, sock);
1841 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001842 sock_release(sock);
1843 goto out;
1844 }
1845
1846 return sock;
1847out:
1848 return ERR_PTR(err);
1849}
1850
Chuck Lever176e21e2011-05-09 15:22:44 -04001851static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1852 struct socket *sock)
1853{
1854 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1855 xprt);
1856
1857 if (!transport->inet) {
1858 struct sock *sk = sock->sk;
1859
1860 write_lock_bh(&sk->sk_callback_lock);
1861
1862 xs_save_old_callbacks(transport, sk);
1863
1864 sk->sk_user_data = xprt;
Trond Myklebusta2648092015-10-06 17:03:00 -04001865 sk->sk_data_ready = xs_data_ready;
Chuck Lever176e21e2011-05-09 15:22:44 -04001866 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07001867 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05001868 sk->sk_error_report = xs_error_report;
Trond Myklebustc2126152015-08-19 21:46:15 -05001869 sk->sk_allocation = GFP_NOIO;
Chuck Lever176e21e2011-05-09 15:22:44 -04001870
1871 xprt_clear_connected(xprt);
1872
1873 /* Reset to new socket */
1874 transport->sock = sock;
1875 transport->inet = sk;
1876
1877 write_unlock_bh(&sk->sk_callback_lock);
1878 }
1879
Trond Myklebust550aebf2018-09-14 14:32:45 -04001880 xs_stream_reset_connect(transport);
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04001881
Chuck Lever176e21e2011-05-09 15:22:44 -04001882 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1883}
1884
1885/**
1886 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
Chuck Lever176e21e2011-05-09 15:22:44 -04001887 * @transport: socket transport to connect
Chuck Lever176e21e2011-05-09 15:22:44 -04001888 */
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001889static int xs_local_setup_socket(struct sock_xprt *transport)
Chuck Lever176e21e2011-05-09 15:22:44 -04001890{
Chuck Lever176e21e2011-05-09 15:22:44 -04001891 struct rpc_xprt *xprt = &transport->xprt;
1892 struct socket *sock;
1893 int status = -EIO;
1894
Chuck Lever176e21e2011-05-09 15:22:44 -04001895 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1896 SOCK_STREAM, 0, &sock, 1);
1897 if (status < 0) {
1898 dprintk("RPC: can't create AF_LOCAL "
1899 "transport socket (%d).\n", -status);
1900 goto out;
1901 }
Stefan Hajnoczid1358912015-12-02 14:17:52 +08001902 xs_reclassify_socket(AF_LOCAL, sock);
Chuck Lever176e21e2011-05-09 15:22:44 -04001903
1904 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1905 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1906
1907 status = xs_local_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001908 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever176e21e2011-05-09 15:22:44 -04001909 switch (status) {
1910 case 0:
1911 dprintk("RPC: xprt %p connected to %s\n",
1912 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1913 xprt_set_connected(xprt);
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001914 case -ENOBUFS:
Chuck Lever176e21e2011-05-09 15:22:44 -04001915 break;
1916 case -ENOENT:
1917 dprintk("RPC: xprt %p: socket %s does not exist\n",
1918 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1919 break;
Trond Myklebust4a20a982012-12-15 17:02:29 -05001920 case -ECONNREFUSED:
1921 dprintk("RPC: xprt %p: connection refused for %s\n",
1922 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1923 break;
Chuck Lever176e21e2011-05-09 15:22:44 -04001924 default:
1925 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1926 __func__, -status,
1927 xprt->address_strings[RPC_DISPLAY_ADDR]);
1928 }
1929
1930out:
1931 xprt_clear_connecting(xprt);
1932 xprt_wake_pending_tasks(xprt, status);
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001933 return status;
1934}
1935
Linus Torvaldsb6669732013-02-28 18:02:55 -08001936static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001937{
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05001938 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1939 int ret;
1940
1941 if (RPC_IS_ASYNC(task)) {
1942 /*
1943 * We want the AF_LOCAL connect to be resolved in the
1944 * filesystem namespace of the process making the rpc
1945 * call. Thus we connect synchronously.
1946 *
1947 * If we want to support asynchronous AF_LOCAL calls,
1948 * we'll need to figure out how to pass a namespace to
1949 * connect.
1950 */
1951 rpc_exit(task, -ENOTCONN);
1952 return;
1953 }
1954 ret = xs_local_setup_socket(transport);
1955 if (ret && !RPC_IS_SOFTCONN(task))
1956 msleep_interruptible(15000);
Chuck Lever176e21e2011-05-09 15:22:44 -04001957}
1958
Jeff Layton3c87ef62015-06-03 16:14:25 -04001959#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Jeff Laytond6e971d2015-06-03 16:14:28 -04001960/*
1961 * Note that this should be called with XPRT_LOCKED held (or when we otherwise
1962 * know that we have exclusive access to the socket), to guard against
1963 * races with xs_reset_transport.
1964 */
Mel Gormana564b8f2012-07-31 16:45:12 -07001965static void xs_set_memalloc(struct rpc_xprt *xprt)
1966{
1967 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1968 xprt);
1969
Jeff Laytond6e971d2015-06-03 16:14:28 -04001970 /*
1971 * If there's no sock, then we have nothing to set. The
1972 * reconnecting process will get it for us.
1973 */
1974 if (!transport->inet)
1975 return;
Jeff Layton8e228132015-06-03 16:14:26 -04001976 if (atomic_read(&xprt->swapper))
Mel Gormana564b8f2012-07-31 16:45:12 -07001977 sk_set_memalloc(transport->inet);
1978}
1979
1980/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04001981 * xs_enable_swap - Tag this transport as being used for swap.
Mel Gormana564b8f2012-07-31 16:45:12 -07001982 * @xprt: transport to tag
Mel Gormana564b8f2012-07-31 16:45:12 -07001983 *
Jeff Layton8e228132015-06-03 16:14:26 -04001984 * Take a reference to this transport on behalf of the rpc_clnt, and
1985 * optionally mark it for swapping if it wasn't already.
Mel Gormana564b8f2012-07-31 16:45:12 -07001986 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04001987static int
1988xs_enable_swap(struct rpc_xprt *xprt)
Mel Gormana564b8f2012-07-31 16:45:12 -07001989{
Jeff Laytond6e971d2015-06-03 16:14:28 -04001990 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Mel Gormana564b8f2012-07-31 16:45:12 -07001991
Jeff Laytond6e971d2015-06-03 16:14:28 -04001992 if (atomic_inc_return(&xprt->swapper) != 1)
1993 return 0;
1994 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
1995 return -ERESTARTSYS;
1996 if (xs->inet)
1997 sk_set_memalloc(xs->inet);
1998 xprt_release_xprt(xprt, NULL);
Jeff Layton8e228132015-06-03 16:14:26 -04001999 return 0;
Mel Gormana564b8f2012-07-31 16:45:12 -07002000}
Jeff Layton8e228132015-06-03 16:14:26 -04002001
2002/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002003 * xs_disable_swap - Untag this transport as being used for swap.
Jeff Layton8e228132015-06-03 16:14:26 -04002004 * @xprt: transport to tag
2005 *
2006 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
2007 * swapper refcount goes to 0, untag the socket as a memalloc socket.
2008 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002009static void
2010xs_disable_swap(struct rpc_xprt *xprt)
Jeff Layton8e228132015-06-03 16:14:26 -04002011{
Jeff Laytond6e971d2015-06-03 16:14:28 -04002012 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Jeff Layton8e228132015-06-03 16:14:26 -04002013
Jeff Laytond6e971d2015-06-03 16:14:28 -04002014 if (!atomic_dec_and_test(&xprt->swapper))
2015 return;
2016 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2017 return;
2018 if (xs->inet)
2019 sk_clear_memalloc(xs->inet);
2020 xprt_release_xprt(xprt, NULL);
Mel Gormana564b8f2012-07-31 16:45:12 -07002021}
Mel Gormana564b8f2012-07-31 16:45:12 -07002022#else
2023static void xs_set_memalloc(struct rpc_xprt *xprt)
2024{
2025}
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002026
2027static int
2028xs_enable_swap(struct rpc_xprt *xprt)
2029{
2030 return -EINVAL;
2031}
2032
2033static void
2034xs_disable_swap(struct rpc_xprt *xprt)
2035{
2036}
Mel Gormana564b8f2012-07-31 16:45:12 -07002037#endif
2038
Chuck Lever16be2d22007-08-06 11:57:38 -04002039static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04002040{
Chuck Lever16be2d22007-08-06 11:57:38 -04002041 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04002042
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002043 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002044 struct sock *sk = sock->sk;
2045
2046 write_lock_bh(&sk->sk_callback_lock);
2047
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002048 xs_save_old_callbacks(transport, sk);
2049
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002050 sk->sk_user_data = xprt;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002051 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002052 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002053 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebustc2126152015-08-19 21:46:15 -05002054 sk->sk_allocation = GFP_NOIO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002055
2056 xprt_set_connected(xprt);
2057
2058 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002059 transport->sock = sock;
2060 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002061
Mel Gormana564b8f2012-07-31 16:45:12 -07002062 xs_set_memalloc(xprt);
2063
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002064 write_unlock_bh(&sk->sk_callback_lock);
2065 }
Chuck Lever470056c2005-08-25 16:25:56 -07002066 xs_udp_do_set_buffer_size(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002067
2068 xprt->stat.connect_start = jiffies;
Chuck Lever16be2d22007-08-06 11:57:38 -04002069}
2070
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002071static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04002072{
2073 struct sock_xprt *transport =
2074 container_of(work, struct sock_xprt, connect_worker.work);
2075 struct rpc_xprt *xprt = &transport->xprt;
Colin Ian Kingd099b8a2017-09-18 12:21:14 +01002076 struct socket *sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002077 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04002078
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002079 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002080 xs_addr(xprt)->sa_family, SOCK_DGRAM,
2081 IPPROTO_UDP, false);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002082 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04002083 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04002084
Chuck Leverc740eff2009-08-09 15:09:46 -04002085 dprintk("RPC: worker connecting xprt %p via %s to "
2086 "%s (port %s)\n", xprt,
2087 xprt->address_strings[RPC_DISPLAY_PROTO],
2088 xprt->address_strings[RPC_DISPLAY_ADDR],
2089 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04002090
2091 xs_udp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002092 trace_rpc_socket_connect(xprt, sock, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002093 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002094out:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002095 xprt_unlock_connect(xprt, transport);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002096 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002097 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002098}
2099
Trond Myklebust4876cc72015-06-19 16:17:57 -04002100/**
2101 * xs_tcp_shutdown - gracefully shut down a TCP socket
2102 * @xprt: transport
2103 *
2104 * Initiates a graceful shutdown of the TCP socket by calling the
2105 * equivalent of shutdown(SHUT_RDWR);
2106 */
2107static void xs_tcp_shutdown(struct rpc_xprt *xprt)
2108{
2109 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2110 struct socket *sock = transport->sock;
Trond Myklebust9b308892018-02-05 10:20:06 -05002111 int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
Trond Myklebust4876cc72015-06-19 16:17:57 -04002112
2113 if (sock == NULL)
2114 return;
Trond Myklebust9b308892018-02-05 10:20:06 -05002115 switch (skst) {
2116 default:
Trond Myklebust4876cc72015-06-19 16:17:57 -04002117 kernel_sock_shutdown(sock, SHUT_RDWR);
2118 trace_rpc_socket_shutdown(xprt, sock);
Trond Myklebust9b308892018-02-05 10:20:06 -05002119 break;
2120 case TCP_CLOSE:
2121 case TCP_TIME_WAIT:
Trond Myklebust4876cc72015-06-19 16:17:57 -04002122 xs_reset_transport(transport);
Trond Myklebust9b308892018-02-05 10:20:06 -05002123 }
Trond Myklebust4876cc72015-06-19 16:17:57 -04002124}
2125
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002126static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
2127 struct socket *sock)
2128{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002129 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2130 unsigned int keepidle;
2131 unsigned int keepcnt;
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002132 unsigned int opt_on = 1;
2133 unsigned int timeo;
2134
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002135 spin_lock_bh(&xprt->transport_lock);
2136 keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
2137 keepcnt = xprt->timeout->to_retries + 1;
2138 timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
2139 (xprt->timeout->to_retries + 1);
2140 clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2141 spin_unlock_bh(&xprt->transport_lock);
2142
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002143 /* TCP Keepalive options */
2144 kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
2145 (char *)&opt_on, sizeof(opt_on));
2146 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
2147 (char *)&keepidle, sizeof(keepidle));
2148 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
2149 (char *)&keepidle, sizeof(keepidle));
2150 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
2151 (char *)&keepcnt, sizeof(keepcnt));
2152
2153 /* TCP user timeout (see RFC5482) */
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002154 kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT,
2155 (char *)&timeo, sizeof(timeo));
2156}
2157
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002158static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
2159 unsigned long connect_timeout,
2160 unsigned long reconnect_timeout)
2161{
2162 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2163 struct rpc_timeout to;
2164 unsigned long initval;
2165
2166 spin_lock_bh(&xprt->transport_lock);
2167 if (reconnect_timeout < xprt->max_reconnect_timeout)
2168 xprt->max_reconnect_timeout = reconnect_timeout;
2169 if (connect_timeout < xprt->connect_timeout) {
2170 memcpy(&to, xprt->timeout, sizeof(to));
2171 initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
2172 /* Arbitrary lower limit */
2173 if (initval < XS_TCP_INIT_REEST_TO << 1)
2174 initval = XS_TCP_INIT_REEST_TO << 1;
2175 to.to_initval = initval;
2176 to.to_maxval = initval;
2177 memcpy(&transport->tcp_timeout, &to,
2178 sizeof(transport->tcp_timeout));
2179 xprt->timeout = &transport->tcp_timeout;
2180 xprt->connect_timeout = connect_timeout;
2181 }
2182 set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2183 spin_unlock_bh(&xprt->transport_lock);
2184}
2185
Chuck Lever16be2d22007-08-06 11:57:38 -04002186static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002187{
Chuck Lever16be2d22007-08-06 11:57:38 -04002188 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002189 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002190
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002191 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002192 struct sock *sk = sock->sk;
NeilBrownd88e4d82016-08-04 16:24:28 +10002193 unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC;
Trond Myklebust7f260e82013-09-24 11:25:22 -04002194
NeilBrownd88e4d82016-08-04 16:24:28 +10002195 /* Avoid temporary address, they are bad for long-lived
2196 * connections such as NFS mounts.
2197 * RFC4941, section 3.6 suggests that:
2198 * Individual applications, which have specific
2199 * knowledge about the normal duration of connections,
2200 * MAY override this as appropriate.
2201 */
2202 kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES,
2203 (char *)&addr_pref, sizeof(addr_pref));
2204
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002205 xs_tcp_set_socket_timeouts(xprt, sock);
Trond Myklebust775f06a2015-06-20 15:31:54 -04002206
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002207 write_lock_bh(&sk->sk_callback_lock);
2208
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002209 xs_save_old_callbacks(transport, sk);
2210
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002211 sk->sk_user_data = xprt;
Trond Myklebust5157b952016-05-29 10:13:24 -04002212 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002213 sk->sk_state_change = xs_tcp_state_change;
2214 sk->sk_write_space = xs_tcp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002215 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05002216 sk->sk_error_report = xs_error_report;
Trond Myklebustc2126152015-08-19 21:46:15 -05002217 sk->sk_allocation = GFP_NOIO;
Chuck Lever3167e122005-08-25 16:25:55 -07002218
2219 /* socket options */
Chuck Lever3167e122005-08-25 16:25:55 -07002220 sock_reset_flag(sk, SOCK_LINGER);
Chuck Lever3167e122005-08-25 16:25:55 -07002221 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002222
2223 xprt_clear_connected(xprt);
2224
2225 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002226 transport->sock = sock;
2227 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002228
2229 write_unlock_bh(&sk->sk_callback_lock);
2230 }
2231
Trond Myklebust01d37c42009-03-11 14:09:39 -04002232 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002233 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002234
Mel Gormana564b8f2012-07-31 16:45:12 -07002235 xs_set_memalloc(xprt);
2236
Trond Myklebuste1806c72018-08-13 16:50:49 -04002237 /* Reset TCP record info */
Trond Myklebust550aebf2018-09-14 14:32:45 -04002238 xs_stream_reset_connect(transport);
Trond Myklebuste1806c72018-08-13 16:50:49 -04002239
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002240 /* Tell the socket layer to start connecting... */
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04002241 set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002242 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2243 switch (ret) {
2244 case 0:
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002245 xs_set_srcport(transport, sock);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002246 /* fall through */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002247 case -EINPROGRESS:
2248 /* SYN_SENT! */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002249 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2250 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust1f4c17a2016-08-01 13:36:08 -04002251 break;
2252 case -EADDRNOTAVAIL:
2253 /* Source port number is unavailable. Try a new one! */
2254 transport->srcport = 0;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002255 }
2256out:
2257 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002258}
2259
2260/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002261 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
Chuck Lever16be2d22007-08-06 11:57:38 -04002262 *
2263 * Invoked by a work queue tasklet.
2264 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002265static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002266{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002267 struct sock_xprt *transport =
2268 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002269 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002270 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002271 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002272
Chuck Lever16be2d22007-08-06 11:57:38 -04002273 if (!sock) {
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002274 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002275 xs_addr(xprt)->sa_family, SOCK_STREAM,
2276 IPPROTO_TCP, true);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002277 if (IS_ERR(sock)) {
2278 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002279 goto out;
2280 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002281 }
2282
Chuck Leverc740eff2009-08-09 15:09:46 -04002283 dprintk("RPC: worker connecting xprt %p via %s to "
2284 "%s (port %s)\n", xprt,
2285 xprt->address_strings[RPC_DISPLAY_PROTO],
2286 xprt->address_strings[RPC_DISPLAY_ADDR],
2287 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002288
2289 status = xs_tcp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002290 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever46121cf2007-01-31 12:14:08 -05002291 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2292 xprt, -status, xprt_connected(xprt),
2293 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002294 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002295 default:
2296 printk("%s: connect returned unhandled error %d\n",
2297 __func__, status);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002298 /* fall through */
Trond Myklebustf75e6742009-04-21 17:18:20 -04002299 case -EADDRNOTAVAIL:
2300 /* We're probably in TIME_WAIT. Get rid of existing socket,
2301 * and retry
2302 */
Trond Myklebusta519fc72012-09-12 16:49:15 -04002303 xs_tcp_force_close(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002304 break;
Trond Myklebust2a491992009-03-11 14:38:00 -04002305 case 0:
2306 case -EINPROGRESS:
2307 case -EALREADY:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002308 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002309 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002310 case -EINVAL:
2311 /* Happens, for instance, if the user specified a link
2312 * local IPv6 address without a scope-id.
2313 */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002314 case -ECONNREFUSED:
2315 case -ECONNRESET:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002316 case -ENETDOWN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002317 case -ENETUNREACH:
Trond Myklebust4ba161a2017-11-24 12:00:24 -05002318 case -EHOSTUNREACH:
Trond Myklebust3913c782015-02-08 21:44:04 -05002319 case -EADDRINUSE:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002320 case -ENOBUFS:
NeilBrown6ea44ad2017-05-25 17:00:32 +10002321 /*
2322 * xs_tcp_force_close() wakes tasks with -EIO.
2323 * We need to wake them first to ensure the
2324 * correct error code.
2325 */
2326 xprt_wake_pending_tasks(xprt, status);
Trond Myklebust4efdd922015-02-08 15:34:28 -05002327 xs_tcp_force_close(xprt);
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002328 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002329 }
Trond Myklebust2a491992009-03-11 14:38:00 -04002330 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002331out:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002332 xprt_unlock_connect(xprt, transport);
Chuck Lever2226feb2005-08-11 16:25:38 -04002333 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002334 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04002335}
2336
Trond Myklebust02910172016-08-04 00:00:33 -04002337static unsigned long xs_reconnect_delay(const struct rpc_xprt *xprt)
2338{
2339 unsigned long start, now = jiffies;
2340
2341 start = xprt->stat.connect_start + xprt->reestablish_timeout;
2342 if (time_after(start, now))
2343 return start - now;
2344 return 0;
2345}
2346
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002347static void xs_reconnect_backoff(struct rpc_xprt *xprt)
2348{
2349 xprt->reestablish_timeout <<= 1;
2350 if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
2351 xprt->reestablish_timeout = xprt->max_reconnect_timeout;
2352 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2353 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2354}
2355
Chuck Lever68e220b2007-08-06 11:57:48 -04002356/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002357 * xs_connect - connect a socket to a remote endpoint
Trond Myklebust1b092092013-01-08 09:26:49 -05002358 * @xprt: pointer to transport structure
Chuck Lever9903cd12005-08-11 16:25:26 -04002359 * @task: address of RPC task that manages state of connect request
2360 *
2361 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002362 *
2363 * UDP socket connects are synchronous, but we use a work queue anyway
2364 * to guarantee that even unprivileged user processes can set up a
2365 * socket on a privileged port.
2366 *
2367 * If a UDP socket connect fails, the delay behavior here prevents
2368 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002369 */
Trond Myklebust1b092092013-01-08 09:26:49 -05002370static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002371{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002372 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002373 unsigned long delay = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002374
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002375 WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2376
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002377 if (transport->sock != NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002378 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2379 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002380 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002381
2382 /* Start by resetting any existing state */
2383 xs_reset_transport(transport);
2384
Trond Myklebust02910172016-08-04 00:00:33 -04002385 delay = xs_reconnect_delay(xprt);
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002386 xs_reconnect_backoff(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002387
Trond Myklebust02910172016-08-04 00:00:33 -04002388 } else
Chuck Lever46121cf2007-01-31 12:14:08 -05002389 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002390
2391 queue_delayed_work(xprtiod_workqueue,
2392 &transport->connect_worker,
2393 delay);
Chuck Levera246b012005-08-11 16:25:23 -04002394}
2395
Chuck Lever262ca072006-03-20 13:44:16 -05002396/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002397 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2398 * @xprt: rpc_xprt struct containing statistics
2399 * @seq: output file
2400 *
2401 */
2402static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2403{
2404 long idle_time = 0;
2405
2406 if (xprt_connected(xprt))
2407 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2408
2409 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
Andy Adamson15a45202012-02-14 16:19:18 -05002410 "%llu %llu %lu %llu %llu\n",
Chuck Lever176e21e2011-05-09 15:22:44 -04002411 xprt->stat.bind_count,
2412 xprt->stat.connect_count,
2413 xprt->stat.connect_time,
2414 idle_time,
2415 xprt->stat.sends,
2416 xprt->stat.recvs,
2417 xprt->stat.bad_xids,
2418 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002419 xprt->stat.bklog_u,
2420 xprt->stat.max_slots,
2421 xprt->stat.sending_u,
2422 xprt->stat.pending_u);
Chuck Lever176e21e2011-05-09 15:22:44 -04002423}
2424
2425/**
Chuck Lever262ca072006-03-20 13:44:16 -05002426 * xs_udp_print_stats - display UDP socket-specifc stats
2427 * @xprt: rpc_xprt struct containing statistics
2428 * @seq: output file
2429 *
2430 */
2431static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2432{
Chuck Leverc8475462006-12-05 16:35:26 -05002433 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2434
Andy Adamson15a45202012-02-14 16:19:18 -05002435 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2436 "%lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002437 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002438 xprt->stat.bind_count,
2439 xprt->stat.sends,
2440 xprt->stat.recvs,
2441 xprt->stat.bad_xids,
2442 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002443 xprt->stat.bklog_u,
2444 xprt->stat.max_slots,
2445 xprt->stat.sending_u,
2446 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002447}
2448
2449/**
2450 * xs_tcp_print_stats - display TCP socket-specifc stats
2451 * @xprt: rpc_xprt struct containing statistics
2452 * @seq: output file
2453 *
2454 */
2455static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2456{
Chuck Leverc8475462006-12-05 16:35:26 -05002457 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002458 long idle_time = 0;
2459
2460 if (xprt_connected(xprt))
2461 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2462
Andy Adamson15a45202012-02-14 16:19:18 -05002463 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2464 "%llu %llu %lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002465 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002466 xprt->stat.bind_count,
2467 xprt->stat.connect_count,
2468 xprt->stat.connect_time,
2469 idle_time,
2470 xprt->stat.sends,
2471 xprt->stat.recvs,
2472 xprt->stat.bad_xids,
2473 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002474 xprt->stat.bklog_u,
2475 xprt->stat.max_slots,
2476 xprt->stat.sending_u,
2477 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002478}
2479
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002480/*
2481 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2482 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2483 * to use the server side send routines.
2484 */
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002485static int bc_malloc(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002486{
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002487 struct rpc_rqst *rqst = task->tk_rqstp;
2488 size_t size = rqst->rq_callsize;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002489 struct page *page;
2490 struct rpc_buffer *buf;
2491
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002492 if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
2493 WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
2494 size);
2495 return -EINVAL;
2496 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002497
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002498 page = alloc_page(GFP_KERNEL);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002499 if (!page)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002500 return -ENOMEM;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002501
2502 buf = page_address(page);
2503 buf->len = PAGE_SIZE;
2504
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002505 rqst->rq_buffer = buf->data;
Jeff Layton18e601d2016-10-24 20:33:23 -04002506 rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002507 return 0;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002508}
2509
2510/*
2511 * Free the space allocated in the bc_alloc routine
2512 */
Chuck Lever3435c742016-09-15 10:55:29 -04002513static void bc_free(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002514{
Chuck Lever3435c742016-09-15 10:55:29 -04002515 void *buffer = task->tk_rqstp->rq_buffer;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002516 struct rpc_buffer *buf;
2517
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002518 buf = container_of(buffer, struct rpc_buffer, data);
2519 free_page((unsigned long)buf);
2520}
2521
2522/*
2523 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2524 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2525 */
2526static int bc_sendto(struct rpc_rqst *req)
2527{
2528 int len;
2529 struct xdr_buf *xbufp = &req->rq_snd_buf;
2530 struct rpc_xprt *xprt = req->rq_xprt;
2531 struct sock_xprt *transport =
2532 container_of(xprt, struct sock_xprt, xprt);
2533 struct socket *sock = transport->sock;
2534 unsigned long headoff;
2535 unsigned long tailoff;
2536
Chuck Lever61677ee2011-05-09 15:22:34 -04002537 xs_encode_stream_record_marker(xbufp);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002538
2539 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2540 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2541 len = svc_send_common(sock, xbufp,
2542 virt_to_page(xbufp->head[0].iov_base), headoff,
2543 xbufp->tail[0].iov_base, tailoff);
2544
2545 if (len != xbufp->len) {
2546 printk(KERN_NOTICE "Error sending entire callback!\n");
2547 len = -EAGAIN;
2548 }
2549
2550 return len;
2551}
2552
2553/*
2554 * The send routine. Borrows from svc_send
2555 */
Trond Myklebustadfa7142018-09-03 23:58:59 -04002556static int bc_send_request(struct rpc_rqst *req)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002557{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002558 struct svc_xprt *xprt;
Andrzej Hajda7fc56132015-09-24 16:00:09 +02002559 int len;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002560
2561 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2562 /*
2563 * Get the server socket associated with this callback xprt
2564 */
2565 xprt = req->rq_xprt->bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002566
2567 /*
2568 * Grab the mutex to serialize data as the connection is shared
2569 * with the fore channel
2570 */
Trond Myklebustc5445772018-09-03 23:39:27 -04002571 mutex_lock(&xprt->xpt_mutex);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002572 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2573 len = -ENOTCONN;
2574 else
2575 len = bc_sendto(req);
2576 mutex_unlock(&xprt->xpt_mutex);
2577
2578 if (len > 0)
2579 len = 0;
2580
2581 return len;
2582}
2583
2584/*
2585 * The close routine. Since this is client initiated, we do nothing
2586 */
2587
2588static void bc_close(struct rpc_xprt *xprt)
2589{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002590}
2591
2592/*
2593 * The xprt destroy routine. Again, because this connection is client
2594 * initiated, we do nothing
2595 */
2596
2597static void bc_destroy(struct rpc_xprt *xprt)
2598{
Kinglong Mee47f72ef2014-03-24 11:58:16 +08002599 dprintk("RPC: bc_destroy xprt %p\n", xprt);
2600
2601 xs_xprt_free(xprt);
2602 module_put(THIS_MODULE);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002603}
2604
Chuck Leverd31ae252017-08-01 12:00:39 -04002605static const struct rpc_xprt_ops xs_local_ops = {
Chuck Lever176e21e2011-05-09 15:22:44 -04002606 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebust4cd34e72018-08-31 10:00:02 -04002607 .release_xprt = xprt_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002608 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002609 .free_slot = xprt_free_slot,
Chuck Lever176e21e2011-05-09 15:22:44 -04002610 .rpcbind = xs_local_rpcbind,
2611 .set_port = xs_local_set_port,
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002612 .connect = xs_local_connect,
Chuck Lever176e21e2011-05-09 15:22:44 -04002613 .buf_alloc = rpc_malloc,
2614 .buf_free = rpc_free,
Trond Myklebust550aebf2018-09-14 14:32:45 -04002615 .prepare_request = xs_stream_prepare_request,
Chuck Lever176e21e2011-05-09 15:22:44 -04002616 .send_request = xs_local_send_request,
2617 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2618 .close = xs_close,
Trond Myklebusta1311d82013-10-31 09:18:49 -04002619 .destroy = xs_destroy,
Chuck Lever176e21e2011-05-09 15:22:44 -04002620 .print_stats = xs_local_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002621 .enable_swap = xs_enable_swap,
2622 .disable_swap = xs_disable_swap,
Chuck Lever176e21e2011-05-09 15:22:44 -04002623};
2624
Chuck Leverd31ae252017-08-01 12:00:39 -04002625static const struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002626 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002627 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002628 .release_xprt = xprt_release_xprt_cong,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002629 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002630 .free_slot = xprt_free_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002631 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002632 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002633 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002634 .buf_alloc = rpc_malloc,
2635 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002636 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002637 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002638 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002639 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002640 .close = xs_close,
2641 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002642 .print_stats = xs_udp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002643 .enable_swap = xs_enable_swap,
2644 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002645 .inject_disconnect = xs_inject_disconnect,
Chuck Lever262965f2005-08-11 16:25:56 -04002646};
2647
Chuck Leverd31ae252017-08-01 12:00:39 -04002648static const struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002649 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebust4cd34e72018-08-31 10:00:02 -04002650 .release_xprt = xprt_release_xprt,
Trond Myklebust36bd7de2018-09-03 18:41:32 -04002651 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002652 .free_slot = xprt_free_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002653 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002654 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002655 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002656 .buf_alloc = rpc_malloc,
2657 .buf_free = rpc_free,
Trond Myklebust277e4ab2018-09-14 09:49:06 -04002658 .prepare_request = xs_stream_prepare_request,
Chuck Lever262965f2005-08-11 16:25:56 -04002659 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002660 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustc627d312015-02-10 11:06:04 -05002661 .close = xs_tcp_shutdown,
Chuck Lever9903cd12005-08-11 16:25:26 -04002662 .destroy = xs_destroy,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002663 .set_connect_timeout = xs_tcp_set_connect_timeout,
Chuck Lever262ca072006-03-20 13:44:16 -05002664 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002665 .enable_swap = xs_enable_swap,
2666 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002667 .inject_disconnect = xs_inject_disconnect,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002668#ifdef CONFIG_SUNRPC_BACKCHANNEL
2669 .bc_setup = xprt_setup_bc,
Chuck Lever76566772015-10-24 17:28:32 -04002670 .bc_up = xs_tcp_bc_up,
Chuck Lever6b26cc82016-05-02 14:40:40 -04002671 .bc_maxpayload = xs_tcp_bc_maxpayload,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002672 .bc_free_rqst = xprt_free_bc_rqst,
2673 .bc_destroy = xprt_destroy_bc,
2674#endif
Chuck Levera246b012005-08-11 16:25:23 -04002675};
2676
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002677/*
2678 * The rpc_xprt_ops for the server backchannel
2679 */
2680
Chuck Leverd31ae252017-08-01 12:00:39 -04002681static const struct rpc_xprt_ops bc_tcp_ops = {
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002682 .reserve_xprt = xprt_reserve_xprt,
2683 .release_xprt = xprt_release_xprt,
Bryan Schumaker84e28a32012-09-24 13:39:01 -04002684 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002685 .free_slot = xprt_free_slot,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002686 .buf_alloc = bc_malloc,
2687 .buf_free = bc_free,
2688 .send_request = bc_send_request,
2689 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2690 .close = bc_close,
2691 .destroy = bc_destroy,
2692 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002693 .enable_swap = xs_enable_swap,
2694 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002695 .inject_disconnect = xs_inject_disconnect,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002696};
2697
Chuck Lever92476852010-10-20 11:53:01 -04002698static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2699{
2700 static const struct sockaddr_in sin = {
2701 .sin_family = AF_INET,
2702 .sin_addr.s_addr = htonl(INADDR_ANY),
2703 };
2704 static const struct sockaddr_in6 sin6 = {
2705 .sin6_family = AF_INET6,
2706 .sin6_addr = IN6ADDR_ANY_INIT,
2707 };
2708
2709 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002710 case AF_LOCAL:
2711 break;
Chuck Lever92476852010-10-20 11:53:01 -04002712 case AF_INET:
2713 memcpy(sap, &sin, sizeof(sin));
2714 break;
2715 case AF_INET6:
2716 memcpy(sap, &sin6, sizeof(sin6));
2717 break;
2718 default:
2719 dprintk("RPC: %s: Bad address family\n", __func__);
2720 return -EAFNOSUPPORT;
2721 }
2722 return 0;
2723}
2724
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002725static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002726 unsigned int slot_table_size,
2727 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002728{
2729 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002730 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002731
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002732 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002733 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002734 return ERR_PTR(-EBADF);
2735 }
2736
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002737 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2738 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002739 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002740 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2741 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002742 return ERR_PTR(-ENOMEM);
2743 }
2744
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002745 new = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002746 mutex_init(&new->recv_mutex);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002747 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2748 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002749 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002750 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002751 else {
2752 int err;
2753 err = xs_init_anyaddr(args->dstaddr->sa_family,
2754 (struct sockaddr *)&new->srcaddr);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002755 if (err != 0) {
2756 xprt_free(xprt);
Chuck Lever92476852010-10-20 11:53:01 -04002757 return ERR_PTR(err);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002758 }
Chuck Lever92476852010-10-20 11:53:01 -04002759 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002760
2761 return xprt;
2762}
2763
Chuck Lever176e21e2011-05-09 15:22:44 -04002764static const struct rpc_timeout xs_local_default_timeout = {
2765 .to_initval = 10 * HZ,
2766 .to_maxval = 10 * HZ,
2767 .to_retries = 2,
2768};
2769
2770/**
2771 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2772 * @args: rpc transport creation arguments
2773 *
2774 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2775 */
2776static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2777{
2778 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2779 struct sock_xprt *transport;
2780 struct rpc_xprt *xprt;
2781 struct rpc_xprt *ret;
2782
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002783 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2784 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002785 if (IS_ERR(xprt))
2786 return xprt;
2787 transport = container_of(xprt, struct sock_xprt, xprt);
2788
2789 xprt->prot = 0;
2790 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2791 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2792
2793 xprt->bind_timeout = XS_BIND_TO;
2794 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2795 xprt->idle_timeout = XS_IDLE_DISC_TO;
2796
2797 xprt->ops = &xs_local_ops;
2798 xprt->timeout = &xs_local_default_timeout;
2799
Trond Myklebust550aebf2018-09-14 14:32:45 -04002800 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
2801 INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket);
NeilBrown93dc41b2013-10-31 16:14:36 +11002802
Chuck Lever176e21e2011-05-09 15:22:44 -04002803 switch (sun->sun_family) {
2804 case AF_LOCAL:
2805 if (sun->sun_path[0] != '/') {
2806 dprintk("RPC: bad AF_LOCAL address: %s\n",
2807 sun->sun_path);
2808 ret = ERR_PTR(-EINVAL);
2809 goto out_err;
2810 }
2811 xprt_set_bound(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002812 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
J. Bruce Fields7073ea82013-02-21 10:14:22 -05002813 ret = ERR_PTR(xs_local_setup_socket(transport));
2814 if (ret)
2815 goto out_err;
Chuck Lever176e21e2011-05-09 15:22:44 -04002816 break;
2817 default:
2818 ret = ERR_PTR(-EAFNOSUPPORT);
2819 goto out_err;
2820 }
2821
2822 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2823 xprt->address_strings[RPC_DISPLAY_ADDR]);
2824
2825 if (try_module_get(THIS_MODULE))
2826 return xprt;
2827 ret = ERR_PTR(-EINVAL);
2828out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002829 xs_xprt_free(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002830 return ret;
2831}
2832
Trond Myklebust2881ae72007-12-20 16:03:54 -05002833static const struct rpc_timeout xs_udp_default_timeout = {
2834 .to_initval = 5 * HZ,
2835 .to_maxval = 30 * HZ,
2836 .to_increment = 5 * HZ,
2837 .to_retries = 5,
2838};
2839
Chuck Lever9903cd12005-08-11 16:25:26 -04002840/**
2841 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002842 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002843 *
2844 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002845static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002846{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002847 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002848 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002849 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002850 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002851
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002852 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2853 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002854 if (IS_ERR(xprt))
2855 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002856 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002857
Chuck Leverec739ef2006-08-22 20:06:15 -04002858 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002859 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002860 /* XXX: header size can vary due to auth type, IPv6, etc. */
2861 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2862
Chuck Lever03bf4b72005-08-25 16:25:55 -07002863 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002864 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2865 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002866
Chuck Lever262965f2005-08-11 16:25:56 -04002867 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002868
Trond Myklebustba7392b2007-12-20 16:03:55 -05002869 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002870
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002871 INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002872 INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2873
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002874 switch (addr->sa_family) {
2875 case AF_INET:
2876 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2877 xprt_set_bound(xprt);
2878
Chuck Lever9dc3b092009-08-09 15:09:46 -04002879 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002880 break;
2881 case AF_INET6:
2882 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2883 xprt_set_bound(xprt);
2884
Chuck Lever9dc3b092009-08-09 15:09:46 -04002885 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002886 break;
2887 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002888 ret = ERR_PTR(-EAFNOSUPPORT);
2889 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002890 }
2891
Chuck Leverc740eff2009-08-09 15:09:46 -04002892 if (xprt_bound(xprt))
2893 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2894 xprt->address_strings[RPC_DISPLAY_ADDR],
2895 xprt->address_strings[RPC_DISPLAY_PORT],
2896 xprt->address_strings[RPC_DISPLAY_PROTO]);
2897 else
2898 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2899 xprt->address_strings[RPC_DISPLAY_ADDR],
2900 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002901
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002902 if (try_module_get(THIS_MODULE))
2903 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002904 ret = ERR_PTR(-EINVAL);
2905out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002906 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002907 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002908}
2909
Trond Myklebust2881ae72007-12-20 16:03:54 -05002910static const struct rpc_timeout xs_tcp_default_timeout = {
2911 .to_initval = 60 * HZ,
2912 .to_maxval = 60 * HZ,
2913 .to_retries = 2,
2914};
2915
Chuck Lever9903cd12005-08-11 16:25:26 -04002916/**
2917 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002918 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002919 *
2920 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002921static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002922{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002923 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002924 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002925 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002926 struct rpc_xprt *ret;
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002927 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2928
2929 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2930 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Levera246b012005-08-11 16:25:23 -04002931
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002932 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
Trond Myklebustb7993ce2013-04-14 11:42:00 -04002933 max_slot_table_size);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002934 if (IS_ERR(xprt))
2935 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002936 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002937
Chuck Leverec739ef2006-08-22 20:06:15 -04002938 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002939 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002940 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002941
Chuck Lever03bf4b72005-08-25 16:25:55 -07002942 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002943 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2944 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002945
Chuck Lever262965f2005-08-11 16:25:56 -04002946 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002947 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002948
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002949 xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002950 xprt->connect_timeout = xprt->timeout->to_initval *
2951 (xprt->timeout->to_retries + 1);
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002952
Trond Myklebustc50b8ee2018-09-14 14:26:28 -04002953 INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002954 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
2955
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002956 switch (addr->sa_family) {
2957 case AF_INET:
2958 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2959 xprt_set_bound(xprt);
2960
Chuck Lever9dc3b092009-08-09 15:09:46 -04002961 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002962 break;
2963 case AF_INET6:
2964 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2965 xprt_set_bound(xprt);
2966
Chuck Lever9dc3b092009-08-09 15:09:46 -04002967 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002968 break;
2969 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002970 ret = ERR_PTR(-EAFNOSUPPORT);
2971 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002972 }
2973
Chuck Leverc740eff2009-08-09 15:09:46 -04002974 if (xprt_bound(xprt))
2975 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2976 xprt->address_strings[RPC_DISPLAY_ADDR],
2977 xprt->address_strings[RPC_DISPLAY_PORT],
2978 xprt->address_strings[RPC_DISPLAY_PROTO]);
2979 else
2980 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2981 xprt->address_strings[RPC_DISPLAY_ADDR],
2982 xprt->address_strings[RPC_DISPLAY_PROTO]);
2983
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002984 if (try_module_get(THIS_MODULE))
2985 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002986 ret = ERR_PTR(-EINVAL);
2987out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002988 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002989 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002990}
Chuck Lever282b32e2006-12-05 16:35:51 -05002991
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002992/**
2993 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2994 * @args: rpc transport creation arguments
2995 *
2996 */
2997static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2998{
2999 struct sockaddr *addr = args->dstaddr;
3000 struct rpc_xprt *xprt;
3001 struct sock_xprt *transport;
3002 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003003 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003004
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003005 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3006 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003007 if (IS_ERR(xprt))
3008 return xprt;
3009 transport = container_of(xprt, struct sock_xprt, xprt);
3010
3011 xprt->prot = IPPROTO_TCP;
3012 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
3013 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3014 xprt->timeout = &xs_tcp_default_timeout;
3015
3016 /* backchannel */
3017 xprt_set_bound(xprt);
3018 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003019 xprt->reestablish_timeout = 0;
3020 xprt->idle_timeout = 0;
3021
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003022 xprt->ops = &bc_tcp_ops;
3023
3024 switch (addr->sa_family) {
3025 case AF_INET:
3026 xs_format_peer_addresses(xprt, "tcp",
3027 RPCBIND_NETID_TCP);
3028 break;
3029 case AF_INET6:
3030 xs_format_peer_addresses(xprt, "tcp",
3031 RPCBIND_NETID_TCP6);
3032 break;
3033 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003034 ret = ERR_PTR(-EAFNOSUPPORT);
3035 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003036 }
3037
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04003038 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3039 xprt->address_strings[RPC_DISPLAY_ADDR],
3040 xprt->address_strings[RPC_DISPLAY_PORT],
3041 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003042
3043 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003044 * Once we've associated a backchannel xprt with a connection,
Weng Meiling28303ca2013-11-30 17:56:44 +08003045 * we want to keep it around as long as the connection lasts,
3046 * in case we need to start using it for a backchannel again;
3047 * this reference won't be dropped until bc_xprt is destroyed.
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003048 */
3049 xprt_get(xprt);
3050 args->bc_xprt->xpt_bc_xprt = xprt;
3051 xprt->bc_xprt = args->bc_xprt;
3052 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
3053 transport->sock = bc_sock->sk_sock;
3054 transport->inet = bc_sock->sk_sk;
3055
3056 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003057 * Since we don't want connections for the backchannel, we set
3058 * the xprt status to connected
3059 */
3060 xprt_set_connected(xprt);
3061
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003062 if (try_module_get(THIS_MODULE))
3063 return xprt;
Kinglong Mee642aab52014-03-24 12:00:28 +08003064
3065 args->bc_xprt->xpt_bc_xprt = NULL;
J. Bruce Fields39a9bea2016-05-17 12:38:21 -04003066 args->bc_xprt->xpt_bc_xps = NULL;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003067 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003068 ret = ERR_PTR(-EINVAL);
3069out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003070 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003071 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003072}
3073
Chuck Lever176e21e2011-05-09 15:22:44 -04003074static struct xprt_class xs_local_transport = {
3075 .list = LIST_HEAD_INIT(xs_local_transport.list),
3076 .name = "named UNIX socket",
3077 .owner = THIS_MODULE,
3078 .ident = XPRT_TRANSPORT_LOCAL,
3079 .setup = xs_setup_local,
3080};
3081
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003082static struct xprt_class xs_udp_transport = {
3083 .list = LIST_HEAD_INIT(xs_udp_transport.list),
3084 .name = "udp",
3085 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003086 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003087 .setup = xs_setup_udp,
3088};
3089
3090static struct xprt_class xs_tcp_transport = {
3091 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
3092 .name = "tcp",
3093 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003094 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003095 .setup = xs_setup_tcp,
3096};
3097
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003098static struct xprt_class xs_bc_tcp_transport = {
3099 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3100 .name = "tcp NFSv4.1 backchannel",
3101 .owner = THIS_MODULE,
3102 .ident = XPRT_TRANSPORT_BC_TCP,
3103 .setup = xs_setup_bc_tcp,
3104};
3105
Chuck Lever282b32e2006-12-05 16:35:51 -05003106/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003107 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05003108 *
3109 */
3110int init_socket_xprt(void)
3111{
Jeff Laytonf895b252014-11-17 16:58:04 -05003112#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08003113 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08003114 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05003115#endif
3116
Chuck Lever176e21e2011-05-09 15:22:44 -04003117 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003118 xprt_register_transport(&xs_udp_transport);
3119 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003120 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003121
Chuck Lever282b32e2006-12-05 16:35:51 -05003122 return 0;
3123}
3124
3125/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003126 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05003127 *
3128 */
3129void cleanup_socket_xprt(void)
3130{
Jeff Laytonf895b252014-11-17 16:58:04 -05003131#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Leverfbf76682006-12-05 16:35:54 -05003132 if (sunrpc_table_header) {
3133 unregister_sysctl_table(sunrpc_table_header);
3134 sunrpc_table_header = NULL;
3135 }
3136#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003137
Chuck Lever176e21e2011-05-09 15:22:44 -04003138 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003139 xprt_unregister_transport(&xs_udp_transport);
3140 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003141 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05003142}
Trond Myklebustcbf11072009-08-09 15:06:19 -04003143
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003144static int param_set_uint_minmax(const char *val,
3145 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003146 unsigned int min, unsigned int max)
3147{
Daniel Walter00cfaa92014-06-21 13:06:38 +01003148 unsigned int num;
Trond Myklebustcbf11072009-08-09 15:06:19 -04003149 int ret;
3150
3151 if (!val)
3152 return -EINVAL;
Daniel Walter00cfaa92014-06-21 13:06:38 +01003153 ret = kstrtouint(val, 0, &num);
Dan Carpenter9761a242017-02-19 00:34:59 +03003154 if (ret)
3155 return ret;
3156 if (num < min || num > max)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003157 return -EINVAL;
3158 *((unsigned int *)kp->arg) = num;
3159 return 0;
3160}
3161
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003162static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003163{
Frank Sorensonffb6ca32016-07-08 16:35:25 -05003164 if (kp->arg == &xprt_min_resvport)
3165 return param_set_uint_minmax(val, kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003166 RPC_MIN_RESVPORT,
Frank Sorensonffb6ca32016-07-08 16:35:25 -05003167 xprt_max_resvport);
3168 return param_set_uint_minmax(val, kp,
3169 xprt_min_resvport,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003170 RPC_MAX_RESVPORT);
3171}
3172
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303173static const struct kernel_param_ops param_ops_portnr = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003174 .set = param_set_portnr,
3175 .get = param_get_uint,
3176};
3177
Trond Myklebustcbf11072009-08-09 15:06:19 -04003178#define param_check_portnr(name, p) \
3179 __param_check(name, p, unsigned int);
3180
3181module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3182module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3183
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003184static int param_set_slot_table_size(const char *val,
3185 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003186{
3187 return param_set_uint_minmax(val, kp,
3188 RPC_MIN_SLOT_TABLE,
3189 RPC_MAX_SLOT_TABLE);
3190}
3191
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303192static const struct kernel_param_ops param_ops_slot_table_size = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003193 .set = param_set_slot_table_size,
3194 .get = param_get_uint,
3195};
3196
Trond Myklebustcbf11072009-08-09 15:06:19 -04003197#define param_check_slot_table_size(name, p) \
3198 __param_check(name, p, unsigned int);
3199
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003200static int param_set_max_slot_table_size(const char *val,
3201 const struct kernel_param *kp)
3202{
3203 return param_set_uint_minmax(val, kp,
3204 RPC_MIN_SLOT_TABLE,
3205 RPC_MAX_SLOT_TABLE_LIMIT);
3206}
3207
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303208static const struct kernel_param_ops param_ops_max_slot_table_size = {
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003209 .set = param_set_max_slot_table_size,
3210 .get = param_get_uint,
3211};
3212
3213#define param_check_max_slot_table_size(name, p) \
3214 __param_check(name, p, unsigned int);
3215
Trond Myklebustcbf11072009-08-09 15:06:19 -04003216module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3217 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003218module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3219 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003220module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3221 slot_table_size, 0644);