blob: f54e8110f4c6aace62f6a2174259c4527cac67ef [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>
50
Trond Myklebust40b5ea02013-09-04 12:16:23 -040051#include <trace/events/sunrpc.h>
52
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030053#include "sunrpc.h"
Chuck Lever176e21e2011-05-09 15:22:44 -040054
Trond Myklebust3d188802018-01-14 15:47:06 -050055#define RPC_TCP_READ_CHUNK_SZ (3*512*1024)
56
Chuck Lever176e21e2011-05-09 15:22:44 -040057static 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
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400328#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
329
Trond Myklebust24c56842006-10-17 15:06:22 -0400330static 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 -0400331{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400332 struct msghdr msg = {
333 .msg_name = addr,
334 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400335 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
336 };
337 struct kvec iov = {
338 .iov_base = vec->iov_base + base,
339 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400340 };
341
Trond Myklebust24c56842006-10-17 15:06:22 -0400342 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400343 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
344 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
345}
346
Jason Baronf279cd02014-09-24 18:08:00 +0000347static 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 -0400348{
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500349 ssize_t (*do_sendpage)(struct socket *sock, struct page *page,
350 int offset, size_t size, int flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400351 struct page **ppage;
352 unsigned int remainder;
Jason Baronf279cd02014-09-24 18:08:00 +0000353 int err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400354
Trond Myklebust24c56842006-10-17 15:06:22 -0400355 remainder = xdr->page_len - base;
356 base += xdr->page_base;
357 ppage = xdr->pages + (base >> PAGE_SHIFT);
358 base &= ~PAGE_MASK;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500359 do_sendpage = sock->ops->sendpage;
360 if (!zerocopy)
361 do_sendpage = sock_no_sendpage;
Trond Myklebust24c56842006-10-17 15:06:22 -0400362 for(;;) {
363 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
364 int flags = XS_SENDMSG_FLAGS;
365
366 remainder -= len;
Trond Myklebust31303d62015-10-06 15:59:20 -0400367 if (more)
Trond Myklebust24c56842006-10-17 15:06:22 -0400368 flags |= MSG_MORE;
Trond Myklebust31303d62015-10-06 15:59:20 -0400369 if (remainder != 0)
370 flags |= MSG_SENDPAGE_NOTLAST | MSG_MORE;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500371 err = do_sendpage(sock, *ppage, base, len, flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400372 if (remainder == 0 || err != len)
373 break;
Jason Baronf279cd02014-09-24 18:08:00 +0000374 *sent_p += err;
Trond Myklebust24c56842006-10-17 15:06:22 -0400375 ppage++;
376 base = 0;
377 }
Jason Baronf279cd02014-09-24 18:08:00 +0000378 if (err > 0) {
379 *sent_p += err;
380 err = 0;
381 }
382 return err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400383}
384
Chuck Lever9903cd12005-08-11 16:25:26 -0400385/**
386 * xs_sendpages - write pages directly to a socket
387 * @sock: socket to send on
388 * @addr: UDP only -- address of destination
389 * @addrlen: UDP only -- length of destination address
390 * @xdr: buffer containing this request
391 * @base: starting position in the buffer
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500392 * @zerocopy: true if it is safe to use sendpage()
Jason Baronf279cd02014-09-24 18:08:00 +0000393 * @sent_p: return the total number of bytes successfully queued for sending
Chuck Lever9903cd12005-08-11 16:25:26 -0400394 *
Chuck Levera246b012005-08-11 16:25:23 -0400395 */
Jason Baronf279cd02014-09-24 18:08:00 +0000396static 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 -0400397{
Trond Myklebust24c56842006-10-17 15:06:22 -0400398 unsigned int remainder = xdr->len - base;
Jason Baronf279cd02014-09-24 18:08:00 +0000399 int err = 0;
400 int sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400401
Chuck Lever262965f2005-08-11 16:25:56 -0400402 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400403 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400404
Trond Myklebust24c56842006-10-17 15:06:22 -0400405 if (base != 0) {
406 addr = NULL;
407 addrlen = 0;
408 }
Chuck Lever262965f2005-08-11 16:25:56 -0400409
Trond Myklebust24c56842006-10-17 15:06:22 -0400410 if (base < xdr->head[0].iov_len || addr != NULL) {
411 unsigned int len = xdr->head[0].iov_len - base;
412 remainder -= len;
413 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
414 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400415 goto out;
Jason Baronf279cd02014-09-24 18:08:00 +0000416 *sent_p += err;
Chuck Levera246b012005-08-11 16:25:23 -0400417 base = 0;
418 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400419 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400420
Trond Myklebust24c56842006-10-17 15:06:22 -0400421 if (base < xdr->page_len) {
422 unsigned int len = xdr->page_len - base;
423 remainder -= len;
Jason Baronf279cd02014-09-24 18:08:00 +0000424 err = xs_send_pagedata(sock, xdr, base, remainder != 0, zerocopy, &sent);
425 *sent_p += sent;
426 if (remainder == 0 || sent != len)
Chuck Levera246b012005-08-11 16:25:23 -0400427 goto out;
428 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400429 } else
430 base -= xdr->page_len;
431
432 if (base >= xdr->tail[0].iov_len)
Jason Baronf279cd02014-09-24 18:08:00 +0000433 return 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400434 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400435out:
Jason Baronf279cd02014-09-24 18:08:00 +0000436 if (err > 0) {
437 *sent_p += err;
438 err = 0;
439 }
440 return err;
Chuck Levera246b012005-08-11 16:25:23 -0400441}
442
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400443static void xs_nospace_callback(struct rpc_task *task)
444{
445 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
446
447 transport->inet->sk_write_pending--;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400448}
449
Chuck Lever9903cd12005-08-11 16:25:26 -0400450/**
Chuck Lever262965f2005-08-11 16:25:56 -0400451 * xs_nospace - place task on wait queue if transmit was incomplete
Trond Myklebust50f484e2018-08-30 13:27:29 -0400452 * @req: pointer to RPC request
Chuck Lever262965f2005-08-11 16:25:56 -0400453 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400454 *
Chuck Levera246b012005-08-11 16:25:23 -0400455 */
Trond Myklebust50f484e2018-08-30 13:27:29 -0400456static int xs_nospace(struct rpc_rqst *req, struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400457{
Chuck Lever262965f2005-08-11 16:25:56 -0400458 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500459 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500460 struct sock *sk = transport->inet;
Trond Myklebust24ca9a82011-11-22 14:44:28 +0200461 int ret = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400462
Chuck Lever46121cf2007-01-31 12:14:08 -0500463 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400464 task->tk_pid, req->rq_slen - transport->xmit.offset,
Chuck Lever262965f2005-08-11 16:25:56 -0400465 req->rq_slen);
466
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400467 /* Protect against races with write_space */
468 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400469
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400470 /* Don't race with disconnect */
471 if (xprt_connected(xprt)) {
Trond Myklebust13331a52016-01-06 08:57:06 -0500472 /* wait for more buffer space */
473 sk->sk_write_pending++;
474 xprt_wait_for_buffer_space(task, xs_nospace_callback);
475 } else
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400476 ret = -ENOTCONN;
Chuck Lever262965f2005-08-11 16:25:56 -0400477
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400478 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust06ea0bf2014-02-11 09:15:54 -0500479
480 /* Race breaker in case memory is freed before above code is called */
David Vrabeld48f9ce2016-09-19 13:58:30 +0100481 if (ret == -EAGAIN) {
482 struct socket_wq *wq;
483
484 rcu_read_lock();
485 wq = rcu_dereference(sk->sk_wq);
486 set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags);
487 rcu_read_unlock();
488
489 sk->sk_write_space(sk);
490 }
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400491 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400492}
493
Chuck Lever61677ee2011-05-09 15:22:34 -0400494/*
Trond Myklebust4cd34e72018-08-31 10:00:02 -0400495 * Determine if the previous message in the stream was aborted before it
496 * could complete transmission.
497 */
498static bool
499xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
500{
501 return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
502}
503
504/*
Chuck Lever61677ee2011-05-09 15:22:34 -0400505 * Construct a stream transport record marker in @buf.
506 */
507static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
508{
509 u32 reclen = buf->len - sizeof(rpc_fraghdr);
510 rpc_fraghdr *base = buf->head[0].iov_base;
511 *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
512}
513
Chuck Lever262965f2005-08-11 16:25:56 -0400514/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400515 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
Trond Myklebust50f484e2018-08-30 13:27:29 -0400516 * @req: pointer to RPC request
Chuck Lever176e21e2011-05-09 15:22:44 -0400517 * @task: RPC task that manages the state of an RPC request
518 *
519 * Return values:
520 * 0: The request has been sent
521 * EAGAIN: The socket was blocked, please call again later to
522 * complete the request
523 * ENOTCONN: Caller needs to invoke connect logic then call again
524 * other: Some other error occured, the request was not sent
525 */
Trond Myklebust50f484e2018-08-30 13:27:29 -0400526static int xs_local_send_request(struct rpc_rqst *req, struct rpc_task *task)
Chuck Lever176e21e2011-05-09 15:22:44 -0400527{
Chuck Lever176e21e2011-05-09 15:22:44 -0400528 struct rpc_xprt *xprt = req->rq_xprt;
529 struct sock_xprt *transport =
530 container_of(xprt, struct sock_xprt, xprt);
531 struct xdr_buf *xdr = &req->rq_snd_buf;
532 int status;
Jason Baronf279cd02014-09-24 18:08:00 +0000533 int sent = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -0400534
Trond Myklebust4cd34e72018-08-31 10:00:02 -0400535 /* Close the stream if the previous transmission was incomplete */
536 if (xs_send_request_was_aborted(transport, req)) {
537 xs_close(xprt);
538 return -ENOTCONN;
539 }
540
Chuck Lever176e21e2011-05-09 15:22:44 -0400541 xs_encode_stream_record_marker(&req->rq_snd_buf);
542
543 xs_pktdump("packet data:",
544 req->rq_svec->iov_base, req->rq_svec->iov_len);
545
Chuck Lever78215752018-03-05 15:13:07 -0500546 req->rq_xtime = ktime_get();
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400547 status = xs_sendpages(transport->sock, NULL, 0, xdr,
548 transport->xmit.offset,
Jason Baronf279cd02014-09-24 18:08:00 +0000549 true, &sent);
Chuck Lever176e21e2011-05-09 15:22:44 -0400550 dprintk("RPC: %s(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400551 __func__, xdr->len - transport->xmit.offset, status);
NeilBrown743c69e2015-07-27 10:55:35 +1000552
553 if (status == -EAGAIN && sock_writeable(transport->inet))
554 status = -ENOBUFS;
555
Jason Baronf279cd02014-09-24 18:08:00 +0000556 if (likely(sent > 0) || status == 0) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400557 transport->xmit.offset += sent;
558 req->rq_bytes_sent = transport->xmit.offset;
Chuck Lever176e21e2011-05-09 15:22:44 -0400559 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400560 req->rq_xmit_bytes_sent += transport->xmit.offset;
Chuck Lever176e21e2011-05-09 15:22:44 -0400561 req->rq_bytes_sent = 0;
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400562 transport->xmit.offset = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -0400563 return 0;
564 }
565 status = -EAGAIN;
566 }
567
568 switch (status) {
Trond Myklebust3601c4a2014-06-30 13:42:19 -0400569 case -ENOBUFS:
Trond Myklebustb5872f02015-07-03 09:32:23 -0400570 break;
Chuck Lever176e21e2011-05-09 15:22:44 -0400571 case -EAGAIN:
Trond Myklebust50f484e2018-08-30 13:27:29 -0400572 status = xs_nospace(req, task);
Chuck Lever176e21e2011-05-09 15:22:44 -0400573 break;
574 default:
575 dprintk("RPC: sendmsg returned unrecognized error %d\n",
576 -status);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -0500577 /* fall through */
Chuck Lever176e21e2011-05-09 15:22:44 -0400578 case -EPIPE:
579 xs_close(xprt);
580 status = -ENOTCONN;
581 }
582
583 return status;
584}
585
586/**
Chuck Lever262965f2005-08-11 16:25:56 -0400587 * xs_udp_send_request - write an RPC request to a UDP socket
Trond Myklebust50f484e2018-08-30 13:27:29 -0400588 * @req: pointer to RPC request
Chuck Lever262965f2005-08-11 16:25:56 -0400589 * @task: address of RPC task that manages the state of an RPC request
590 *
591 * Return values:
592 * 0: The request has been sent
593 * EAGAIN: The socket was blocked, please call again later to
594 * complete the request
595 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300596 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -0400597 */
Trond Myklebust50f484e2018-08-30 13:27:29 -0400598static int xs_udp_send_request(struct rpc_rqst *req, struct rpc_task *task)
Chuck Lever262965f2005-08-11 16:25:56 -0400599{
Chuck Lever262965f2005-08-11 16:25:56 -0400600 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500601 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400602 struct xdr_buf *xdr = &req->rq_snd_buf;
Jason Baronf279cd02014-09-24 18:08:00 +0000603 int sent = 0;
Chuck Lever262965f2005-08-11 16:25:56 -0400604 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400605
Chuck Lever9903cd12005-08-11 16:25:26 -0400606 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400607 req->rq_svec->iov_base,
608 req->rq_svec->iov_len);
609
Trond Myklebust01d37c42009-03-11 14:09:39 -0400610 if (!xprt_bound(xprt))
611 return -ENOTCONN;
Trond Myklebust75891f52018-09-03 17:37:36 -0400612
613 if (!xprt_request_get_cong(xprt, req))
614 return -EBADSLT;
615
Chuck Lever78215752018-03-05 15:13:07 -0500616 req->rq_xtime = ktime_get();
Jason Baronf279cd02014-09-24 18:08:00 +0000617 status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen,
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400618 xdr, 0, true, &sent);
Chuck Levera246b012005-08-11 16:25:23 -0400619
Chuck Lever46121cf2007-01-31 12:14:08 -0500620 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400621 xdr->len, status);
Chuck Levera246b012005-08-11 16:25:23 -0400622
Jason Baron3dedbb52014-09-24 18:08:04 +0000623 /* firewall is blocking us, don't return -EAGAIN or we end up looping */
624 if (status == -EPERM)
625 goto process_status;
626
NeilBrown743c69e2015-07-27 10:55:35 +1000627 if (status == -EAGAIN && sock_writeable(transport->inet))
628 status = -ENOBUFS;
629
Jason Baronf279cd02014-09-24 18:08:00 +0000630 if (sent > 0 || status == 0) {
631 req->rq_xmit_bytes_sent += sent;
632 if (sent >= req->rq_slen)
Trond Myklebust21997002007-10-01 11:43:37 -0400633 return 0;
634 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400635 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400636 }
Chuck Levera246b012005-08-11 16:25:23 -0400637
Jason Baron3dedbb52014-09-24 18:08:04 +0000638process_status:
Chuck Lever262965f2005-08-11 16:25:56 -0400639 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400640 case -ENOTSOCK:
641 status = -ENOTCONN;
642 /* Should we call xs_close() here? */
643 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400644 case -EAGAIN:
Trond Myklebust50f484e2018-08-30 13:27:29 -0400645 status = xs_nospace(req, task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400646 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400647 case -ENETUNREACH:
Trond Myklebust3601c4a2014-06-30 13:42:19 -0400648 case -ENOBUFS:
Chuck Lever262965f2005-08-11 16:25:56 -0400649 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400650 case -ECONNREFUSED:
Jason Baron3dedbb52014-09-24 18:08:04 +0000651 case -EPERM:
Chuck Levera246b012005-08-11 16:25:23 -0400652 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400653 * prompts ECONNREFUSED. */
Trond Myklebust13331a52016-01-06 08:57:06 -0500654 break;
655 default:
656 dprintk("RPC: sendmsg returned unrecognized error %d\n",
657 -status);
Chuck Levera246b012005-08-11 16:25:23 -0400658 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800659
Chuck Lever262965f2005-08-11 16:25:56 -0400660 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400661}
662
Trond Myklebuste06799f2007-11-05 15:44:12 -0500663/**
Chuck Lever262965f2005-08-11 16:25:56 -0400664 * xs_tcp_send_request - write an RPC request to a TCP socket
Trond Myklebust50f484e2018-08-30 13:27:29 -0400665 * @req: pointer to RPC request
Chuck Lever9903cd12005-08-11 16:25:26 -0400666 * @task: address of RPC task that manages the state of an RPC request
667 *
668 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400669 * 0: The request has been sent
670 * EAGAIN: The socket was blocked, please call again later to
671 * complete the request
672 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300673 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400674 *
675 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400676 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400677 */
Trond Myklebust50f484e2018-08-30 13:27:29 -0400678static int xs_tcp_send_request(struct rpc_rqst *req, struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400679{
Chuck Levera246b012005-08-11 16:25:23 -0400680 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500681 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400682 struct xdr_buf *xdr = &req->rq_snd_buf;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500683 bool zerocopy = true;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -0400684 bool vm_wait = false;
Chuck Leverb595bb12007-08-06 11:56:42 -0400685 int status;
Jason Baronf279cd02014-09-24 18:08:00 +0000686 int sent;
Chuck Levera246b012005-08-11 16:25:23 -0400687
Trond Myklebust4cd34e72018-08-31 10:00:02 -0400688 /* Close the stream if the previous transmission was incomplete */
689 if (xs_send_request_was_aborted(transport, req)) {
690 if (transport->sock != NULL)
691 kernel_sock_shutdown(transport->sock, SHUT_RDWR);
692 return -ENOTCONN;
693 }
694
Chuck Lever61677ee2011-05-09 15:22:34 -0400695 xs_encode_stream_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400696
Chuck Lever262965f2005-08-11 16:25:56 -0400697 xs_pktdump("packet data:",
698 req->rq_svec->iov_base,
699 req->rq_svec->iov_len);
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500700 /* Don't use zero copy if this is a resend. If the RPC call
701 * completes while the socket holds a reference to the pages,
702 * then we may end up resending corrupted data.
703 */
Trond Myklebust50f484e2018-08-30 13:27:29 -0400704 if (req->rq_task->tk_flags & RPC_TASK_SENT)
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500705 zerocopy = false;
Chuck Levera246b012005-08-11 16:25:23 -0400706
Trond Myklebust7196dbb2017-02-08 11:17:54 -0500707 if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
708 xs_tcp_set_socket_timeouts(xprt, transport->sock);
709
Chuck Levera246b012005-08-11 16:25:23 -0400710 /* Continue transmitting the packet/record. We must be careful
711 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400712 * called sendmsg(). */
Chuck Lever78215752018-03-05 15:13:07 -0500713 req->rq_xtime = ktime_get();
Chuck Levera246b012005-08-11 16:25:23 -0400714 while (1) {
Jason Baronf279cd02014-09-24 18:08:00 +0000715 sent = 0;
716 status = xs_sendpages(transport->sock, NULL, 0, xdr,
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400717 transport->xmit.offset,
718 zerocopy, &sent);
Chuck Levera246b012005-08-11 16:25:23 -0400719
Chuck Lever46121cf2007-01-31 12:14:08 -0500720 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400721 xdr->len - transport->xmit.offset, status);
Chuck Lever262965f2005-08-11 16:25:56 -0400722
Chuck Lever262965f2005-08-11 16:25:56 -0400723 /* If we've sent the entire packet, immediately
724 * reset the count of bytes sent. */
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400725 transport->xmit.offset += sent;
726 req->rq_bytes_sent = transport->xmit.offset;
Chuck Lever262965f2005-08-11 16:25:56 -0400727 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400728 req->rq_xmit_bytes_sent += transport->xmit.offset;
Chuck Lever262965f2005-08-11 16:25:56 -0400729 req->rq_bytes_sent = 0;
Trond Myklebust6c7a64e2018-08-13 16:54:57 -0400730 transport->xmit.offset = 0;
Chuck Lever262965f2005-08-11 16:25:56 -0400731 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400732 }
733
Trond Myklebust9ffadfb2016-05-29 00:42:03 -0400734 WARN_ON_ONCE(sent == 0 && status == 0);
735
736 if (status == -EAGAIN ) {
737 /*
738 * Return EAGAIN if we're sure we're hitting the
739 * socket send buffer limits.
740 */
741 if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
742 break;
743 /*
744 * Did we hit a memory allocation failure?
745 */
746 if (sent == 0) {
747 status = -ENOBUFS;
748 if (vm_wait)
749 break;
750 /* Retry, knowing now that we're below the
751 * socket send buffer limit
752 */
753 vm_wait = true;
754 }
755 continue;
756 }
Trond Myklebustf580dd02015-07-11 17:48:52 +0200757 if (status < 0)
758 break;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -0400759 vm_wait = false;
Chuck Levera246b012005-08-11 16:25:23 -0400760 }
761
Chuck Lever262965f2005-08-11 16:25:56 -0400762 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400763 case -ENOTSOCK:
764 status = -ENOTCONN;
765 /* Should we call xs_close() here? */
766 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400767 case -EAGAIN:
Trond Myklebust50f484e2018-08-30 13:27:29 -0400768 status = xs_nospace(req, task);
Chuck Lever262965f2005-08-11 16:25:56 -0400769 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400770 case -ECONNRESET:
Chuck Lever262965f2005-08-11 16:25:56 -0400771 case -ECONNREFUSED:
772 case -ENOTCONN:
Trond Myklebust3913c782015-02-08 21:44:04 -0500773 case -EADDRINUSE:
Trond Myklebustb5872f02015-07-03 09:32:23 -0400774 case -ENOBUFS:
Trond Myklebustb9d2bb22012-10-23 11:40:02 -0400775 case -EPIPE:
Trond Myklebust13331a52016-01-06 08:57:06 -0500776 break;
777 default:
778 dprintk("RPC: sendmsg returned unrecognized error %d\n",
779 -status);
Chuck Levera246b012005-08-11 16:25:23 -0400780 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800781
Chuck Levera246b012005-08-11 16:25:23 -0400782 return status;
783}
784
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400785static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
786{
787 transport->old_data_ready = sk->sk_data_ready;
788 transport->old_state_change = sk->sk_state_change;
789 transport->old_write_space = sk->sk_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -0500790 transport->old_error_report = sk->sk_error_report;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400791}
792
793static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
794{
795 sk->sk_data_ready = transport->old_data_ready;
796 sk->sk_state_change = transport->old_state_change;
797 sk->sk_write_space = transport->old_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -0500798 sk->sk_error_report = transport->old_error_report;
799}
800
Trond Myklebust42d42a52016-05-23 09:24:55 -0400801static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
802{
803 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
804
805 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
806}
807
Trond Myklebustb70ae912015-02-09 09:41:32 -0500808static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
809{
810 smp_mb__before_atomic();
811 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
812 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust42d42a52016-05-23 09:24:55 -0400813 xs_sock_reset_state_flags(xprt);
Trond Myklebustb70ae912015-02-09 09:41:32 -0500814 smp_mb__after_atomic();
815}
816
Trond Myklebust21180712013-12-31 13:22:59 -0500817/**
818 * xs_error_report - callback to handle TCP socket state errors
819 * @sk: socket
820 *
821 * Note: we don't call sock_error() since there may be a rpc_task
822 * using the socket, and so we don't want to clear sk->sk_err.
823 */
824static void xs_error_report(struct sock *sk)
825{
826 struct rpc_xprt *xprt;
827 int err;
828
829 read_lock_bh(&sk->sk_callback_lock);
830 if (!(xprt = xprt_from_sock(sk)))
831 goto out;
832
833 err = -sk->sk_err;
834 if (err == 0)
835 goto out;
836 dprintk("RPC: xs_error_report client %p, error=%d...\n",
837 xprt, -err);
Trond Myklebuste8353c762013-12-31 13:39:22 -0500838 trace_rpc_socket_error(xprt, sk->sk_socket, err);
Trond Myklebust21180712013-12-31 13:22:59 -0500839 xprt_wake_pending_tasks(xprt, err);
840 out:
841 read_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400842}
843
Chuck Leverfe315e72009-03-11 14:10:21 -0400844static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400845{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500846 struct socket *sock = transport->sock;
847 struct sock *sk = transport->inet;
Trond Myklebust6cc7e902015-02-08 18:35:25 -0500848 struct rpc_xprt *xprt = &transport->xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400849
Chuck Leverfe315e72009-03-11 14:10:21 -0400850 if (sk == NULL)
851 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400852
Jeff Layton264d1df2015-06-03 16:14:27 -0400853 if (atomic_read(&transport->xprt.swapper))
854 sk_clear_memalloc(sk);
855
Trond Myklebust09939202015-08-29 19:11:21 -0700856 kernel_sock_shutdown(sock, SHUT_RDWR);
857
Trond Myklebustedc1b012015-10-05 10:53:49 -0400858 mutex_lock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -0400859 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500860 transport->inet = NULL;
861 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400862
Chuck Lever9903cd12005-08-11 16:25:26 -0400863 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400864
865 xs_restore_old_callbacks(transport, sk);
Trond Myklebust0c787892015-08-29 13:36:30 -0700866 xprt_clear_connected(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400867 write_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust6cc7e902015-02-08 18:35:25 -0500868 xs_sock_reset_connection_flags(xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -0400869 mutex_unlock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -0400870
Trond Myklebust6cc7e902015-02-08 18:35:25 -0500871 trace_rpc_socket_close(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -0400872 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -0400873}
874
875/**
876 * xs_close - close a socket
877 * @xprt: transport
878 *
879 * This is used when all requests are complete; ie, no DRC state remains
880 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400881 *
882 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
883 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e72009-03-11 14:10:21 -0400884 */
885static void xs_close(struct rpc_xprt *xprt)
886{
887 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
888
889 dprintk("RPC: xs_close xprt %p\n", xprt);
890
891 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -0400892 xprt->reestablish_timeout = 0;
Chuck Leverfe315e72009-03-11 14:10:21 -0400893
Trond Myklebust62da3b22007-11-06 18:44:20 -0500894 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400895}
896
Chuck Lever4a068252015-05-11 14:02:25 -0400897static void xs_inject_disconnect(struct rpc_xprt *xprt)
898{
899 dprintk("RPC: injecting transport disconnect on xprt=%p\n",
900 xprt);
901 xprt_disconnect_done(xprt);
902}
903
Kinglong Mee315f3812014-03-24 11:07:22 +0800904static void xs_xprt_free(struct rpc_xprt *xprt)
905{
906 xs_free_peer_addresses(xprt);
907 xprt_free(xprt);
908}
909
Chuck Lever9903cd12005-08-11 16:25:26 -0400910/**
911 * xs_destroy - prepare to shutdown a transport
912 * @xprt: doomed transport
913 *
914 */
915static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400916{
Trond Myklebust03c788272015-09-17 10:42:27 -0400917 struct sock_xprt *transport = container_of(xprt,
918 struct sock_xprt, xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -0500919 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400920
Trond Myklebust03c788272015-09-17 10:42:27 -0400921 cancel_delayed_work_sync(&transport->connect_worker);
Trond Myklebusta1311d82013-10-31 09:18:49 -0400922 xs_close(xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -0400923 cancel_work_sync(&transport->recv_worker);
Kinglong Mee315f3812014-03-24 11:07:22 +0800924 xs_xprt_free(xprt);
Trond Myklebusta1311d82013-10-31 09:18:49 -0400925 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400926}
927
Chuck Lever176e21e2011-05-09 15:22:44 -0400928static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
929{
930 struct xdr_skb_reader desc = {
931 .skb = skb,
932 .offset = sizeof(rpc_fraghdr),
933 .count = skb->len - sizeof(rpc_fraghdr),
934 };
935
936 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
937 return -1;
938 if (desc.count)
939 return -1;
940 return 0;
941}
942
943/**
Trond Myklebusta2648092015-10-06 17:03:00 -0400944 * xs_local_data_read_skb
945 * @xprt: transport
946 * @sk: socket
947 * @skb: skbuff
Chuck Lever176e21e2011-05-09 15:22:44 -0400948 *
949 * Currently this assumes we can read the whole reply in a single gulp.
950 */
Trond Myklebusta2648092015-10-06 17:03:00 -0400951static void xs_local_data_read_skb(struct rpc_xprt *xprt,
952 struct sock *sk,
953 struct sk_buff *skb)
Chuck Lever176e21e2011-05-09 15:22:44 -0400954{
955 struct rpc_task *task;
Chuck Lever176e21e2011-05-09 15:22:44 -0400956 struct rpc_rqst *rovr;
Trond Myklebusta2648092015-10-06 17:03:00 -0400957 int repsize, copied;
Chuck Lever176e21e2011-05-09 15:22:44 -0400958 u32 _xid;
959 __be32 *xp;
960
Chuck Lever176e21e2011-05-09 15:22:44 -0400961 repsize = skb->len - sizeof(rpc_fraghdr);
962 if (repsize < 4) {
963 dprintk("RPC: impossible RPC reply size %d\n", repsize);
Trond Myklebusta2648092015-10-06 17:03:00 -0400964 return;
Chuck Lever176e21e2011-05-09 15:22:44 -0400965 }
966
967 /* Copy the XID from the skb... */
968 xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
969 if (xp == NULL)
Trond Myklebusta2648092015-10-06 17:03:00 -0400970 return;
Chuck Lever176e21e2011-05-09 15:22:44 -0400971
972 /* Look up and lock the request corresponding to the given XID */
Trond Myklebust75c84152018-08-31 10:21:00 -0400973 spin_lock(&xprt->queue_lock);
Chuck Lever176e21e2011-05-09 15:22:44 -0400974 rovr = xprt_lookup_rqst(xprt, *xp);
975 if (!rovr)
976 goto out_unlock;
Trond Myklebust729749b2017-08-13 10:03:59 -0400977 xprt_pin_rqst(rovr);
Trond Myklebust75c84152018-08-31 10:21:00 -0400978 spin_unlock(&xprt->queue_lock);
Chuck Lever176e21e2011-05-09 15:22:44 -0400979 task = rovr->rq_task;
980
981 copied = rovr->rq_private_buf.buflen;
982 if (copied > repsize)
983 copied = repsize;
984
985 if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
986 dprintk("RPC: sk_buff copy failed\n");
Trond Myklebust75c84152018-08-31 10:21:00 -0400987 spin_lock(&xprt->queue_lock);
Trond Myklebust729749b2017-08-13 10:03:59 -0400988 goto out_unpin;
Chuck Lever176e21e2011-05-09 15:22:44 -0400989 }
990
Trond Myklebust75c84152018-08-31 10:21:00 -0400991 spin_lock(&xprt->queue_lock);
Chuck Lever176e21e2011-05-09 15:22:44 -0400992 xprt_complete_rqst(task, copied);
Trond Myklebust729749b2017-08-13 10:03:59 -0400993out_unpin:
994 xprt_unpin_rqst(rovr);
Chuck Lever176e21e2011-05-09 15:22:44 -0400995 out_unlock:
Trond Myklebust75c84152018-08-31 10:21:00 -0400996 spin_unlock(&xprt->queue_lock);
Trond Myklebusta2648092015-10-06 17:03:00 -0400997}
998
999static void xs_local_data_receive(struct sock_xprt *transport)
1000{
1001 struct sk_buff *skb;
1002 struct sock *sk;
1003 int err;
1004
Trond Myklebust0af34422018-01-14 15:28:29 -05001005restart:
Trond Myklebusta2648092015-10-06 17:03:00 -04001006 mutex_lock(&transport->recv_mutex);
1007 sk = transport->inet;
1008 if (sk == NULL)
1009 goto out;
1010 for (;;) {
1011 skb = skb_recv_datagram(sk, 0, 1, &err);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001012 if (skb != NULL) {
1013 xs_local_data_read_skb(&transport->xprt, sk, skb);
1014 skb_free_datagram(sk, skb);
1015 continue;
1016 }
1017 if (!test_and_clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebusta2648092015-10-06 17:03:00 -04001018 break;
Trond Myklebust0af34422018-01-14 15:28:29 -05001019 if (need_resched()) {
1020 mutex_unlock(&transport->recv_mutex);
1021 cond_resched();
1022 goto restart;
1023 }
Trond Myklebusta2648092015-10-06 17:03:00 -04001024 }
1025out:
1026 mutex_unlock(&transport->recv_mutex);
1027}
1028
1029static void xs_local_data_receive_workfn(struct work_struct *work)
1030{
1031 struct sock_xprt *transport =
1032 container_of(work, struct sock_xprt, recv_worker);
1033 xs_local_data_receive(transport);
Chuck Lever176e21e2011-05-09 15:22:44 -04001034}
1035
Chuck Lever9903cd12005-08-11 16:25:26 -04001036/**
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001037 * xs_udp_data_read_skb - receive callback for UDP sockets
1038 * @xprt: transport
1039 * @sk: socket
1040 * @skb: skbuff
Chuck Lever9903cd12005-08-11 16:25:26 -04001041 *
1042 */
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001043static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1044 struct sock *sk,
1045 struct sk_buff *skb)
Chuck Lever9903cd12005-08-11 16:25:26 -04001046{
1047 struct rpc_task *task;
Chuck Levera246b012005-08-11 16:25:23 -04001048 struct rpc_rqst *rovr;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001049 int repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001050 u32 _xid;
1051 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -04001052
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001053 repsize = skb->len;
Chuck Levera246b012005-08-11 16:25:23 -04001054 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001055 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001056 return;
Chuck Levera246b012005-08-11 16:25:23 -04001057 }
1058
1059 /* Copy the XID from the skb... */
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001060 xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
Chuck Levera246b012005-08-11 16:25:23 -04001061 if (xp == NULL)
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001062 return;
Chuck Levera246b012005-08-11 16:25:23 -04001063
1064 /* Look up and lock the request corresponding to the given XID */
Trond Myklebust75c84152018-08-31 10:21:00 -04001065 spin_lock(&xprt->queue_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001066 rovr = xprt_lookup_rqst(xprt, *xp);
1067 if (!rovr)
1068 goto out_unlock;
Trond Myklebust729749b2017-08-13 10:03:59 -04001069 xprt_pin_rqst(rovr);
Chuck Leverecd465e2018-03-05 15:12:57 -05001070 xprt_update_rtt(rovr->rq_task);
Trond Myklebust75c84152018-08-31 10:21:00 -04001071 spin_unlock(&xprt->queue_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001072 task = rovr->rq_task;
1073
Chuck Levera246b012005-08-11 16:25:23 -04001074 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1075 copied = repsize;
1076
1077 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001078 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
Trond Myklebust75c84152018-08-31 10:21:00 -04001079 spin_lock(&xprt->queue_lock);
Trond Myklebust0afa6b42018-02-09 09:39:42 -05001080 __UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
Trond Myklebust729749b2017-08-13 10:03:59 -04001081 goto out_unpin;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001082 }
1083
Chuck Levera246b012005-08-11 16:25:23 -04001084
Trond Myklebust729749b2017-08-13 10:03:59 -04001085 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001086 xprt_adjust_cwnd(xprt, task, copied);
Trond Myklebustce7c2522017-08-16 15:30:35 -04001087 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust75c84152018-08-31 10:21:00 -04001088 spin_lock(&xprt->queue_lock);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001089 xprt_complete_rqst(task, copied);
Trond Myklebust0afa6b42018-02-09 09:39:42 -05001090 __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
Trond Myklebust729749b2017-08-13 10:03:59 -04001091out_unpin:
1092 xprt_unpin_rqst(rovr);
Chuck Levera246b012005-08-11 16:25:23 -04001093 out_unlock:
Trond Myklebust75c84152018-08-31 10:21:00 -04001094 spin_unlock(&xprt->queue_lock);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001095}
1096
1097static void xs_udp_data_receive(struct sock_xprt *transport)
1098{
1099 struct sk_buff *skb;
1100 struct sock *sk;
1101 int err;
1102
Trond Myklebust0af34422018-01-14 15:28:29 -05001103restart:
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001104 mutex_lock(&transport->recv_mutex);
1105 sk = transport->inet;
1106 if (sk == NULL)
1107 goto out;
1108 for (;;) {
Paolo Abeni7c13f972016-11-04 11:28:59 +01001109 skb = skb_recv_udp(sk, 0, 1, &err);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001110 if (skb != NULL) {
1111 xs_udp_data_read_skb(&transport->xprt, sk, skb);
Paolo Abeni850cbad2016-10-21 13:55:47 +02001112 consume_skb(skb);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001113 continue;
1114 }
1115 if (!test_and_clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001116 break;
Trond Myklebust0af34422018-01-14 15:28:29 -05001117 if (need_resched()) {
1118 mutex_unlock(&transport->recv_mutex);
1119 cond_resched();
1120 goto restart;
1121 }
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001122 }
1123out:
1124 mutex_unlock(&transport->recv_mutex);
1125}
1126
1127static void xs_udp_data_receive_workfn(struct work_struct *work)
1128{
1129 struct sock_xprt *transport =
1130 container_of(work, struct sock_xprt, recv_worker);
1131 xs_udp_data_receive(transport);
1132}
1133
1134/**
1135 * xs_data_ready - "data ready" callback for UDP sockets
1136 * @sk: socket with data to read
1137 *
1138 */
1139static void xs_data_ready(struct sock *sk)
1140{
1141 struct rpc_xprt *xprt;
1142
1143 read_lock_bh(&sk->sk_callback_lock);
1144 dprintk("RPC: xs_data_ready...\n");
1145 xprt = xprt_from_sock(sk);
1146 if (xprt != NULL) {
1147 struct sock_xprt *transport = container_of(xprt,
1148 struct sock_xprt, xprt);
Trond Myklebust5157b952016-05-29 10:13:24 -04001149 transport->old_data_ready(sk);
1150 /* Any data means we had a useful conversation, so
1151 * then we don't need to delay the next reconnect
1152 */
1153 if (xprt->reestablish_timeout)
1154 xprt->reestablish_timeout = 0;
Trond Myklebust42d42a52016-05-23 09:24:55 -04001155 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebust40a5f1b2016-05-27 10:39:50 -04001156 queue_work(xprtiod_workqueue, &transport->recv_worker);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001157 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001158 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001159}
1160
Trond Myklebusta519fc72012-09-12 16:49:15 -04001161/*
1162 * Helper function to force a TCP close if the server is sending
1163 * junk and/or it has put us in CLOSE_WAIT
1164 */
1165static void xs_tcp_force_close(struct rpc_xprt *xprt)
1166{
Trond Myklebusta519fc72012-09-12 16:49:15 -04001167 xprt_force_disconnect(xprt);
1168}
1169
Chuck Leverdd456472006-12-05 16:35:44 -05001170static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001171{
Chuck Lever51971132006-12-05 16:35:19 -05001172 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001173 size_t len, used;
1174 char *p;
1175
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001176 p = ((char *) &transport->recv.fraghdr) + transport->recv.offset;
1177 len = sizeof(transport->recv.fraghdr) - transport->recv.offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001178 used = xdr_skb_read_bits(desc, p, len);
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001179 transport->recv.offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001180 if (used != len)
1181 return;
Chuck Lever808012f2005-08-25 16:25:49 -07001182
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001183 transport->recv.len = ntohl(transport->recv.fraghdr);
1184 if (transport->recv.len & RPC_LAST_STREAM_FRAGMENT)
1185 transport->recv.flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -04001186 else
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001187 transport->recv.flags &= ~TCP_RCV_LAST_FRAG;
1188 transport->recv.len &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -07001189
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001190 transport->recv.flags &= ~TCP_RCV_COPY_FRAGHDR;
1191 transport->recv.offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -07001192
Chuck Levera246b012005-08-11 16:25:23 -04001193 /* Sanity check of the record length */
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001194 if (unlikely(transport->recv.len < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001195 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebusta519fc72012-09-12 16:49:15 -04001196 xs_tcp_force_close(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001197 return;
Chuck Levera246b012005-08-11 16:25:23 -04001198 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001199 dprintk("RPC: reading TCP record fragment of length %d\n",
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001200 transport->recv.len);
Chuck Levera246b012005-08-11 16:25:23 -04001201}
1202
Chuck Lever51971132006-12-05 16:35:19 -05001203static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001204{
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001205 if (transport->recv.offset == transport->recv.len) {
1206 transport->recv.flags |= TCP_RCV_COPY_FRAGHDR;
1207 transport->recv.offset = 0;
1208 if (transport->recv.flags & TCP_RCV_LAST_FRAG) {
1209 transport->recv.flags &= ~TCP_RCV_COPY_DATA;
1210 transport->recv.flags |= TCP_RCV_COPY_XID;
1211 transport->recv.copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001212 }
1213 }
1214}
1215
Chuck Leverdd456472006-12-05 16:35:44 -05001216static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001217{
1218 size_t len, used;
1219 char *p;
1220
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001221 len = sizeof(transport->recv.xid) - transport->recv.offset;
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001222 dprintk("RPC: reading XID (%zu bytes)\n", len);
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001223 p = ((char *) &transport->recv.xid) + transport->recv.offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001224 used = xdr_skb_read_bits(desc, p, len);
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001225 transport->recv.offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001226 if (used != len)
1227 return;
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001228 transport->recv.flags &= ~TCP_RCV_COPY_XID;
1229 transport->recv.flags |= TCP_RCV_READ_CALLDIR;
1230 transport->recv.copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001231 dprintk("RPC: reading %s XID %08x\n",
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001232 (transport->recv.flags & TCP_RPC_REPLY) ? "reply for"
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001233 : "request with",
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001234 ntohl(transport->recv.xid));
Chuck Lever51971132006-12-05 16:35:19 -05001235 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001236}
1237
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001238static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1239 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001240{
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001241 size_t len, used;
1242 u32 offset;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001243 char *p;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001244
1245 /*
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001246 * We want transport->recv.offset to be 8 at the end of this routine
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001247 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1248 * When this function is called for the first time,
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001249 * transport->recv.offset is 4 (after having already read the xid).
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001250 */
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001251 offset = transport->recv.offset - sizeof(transport->recv.xid);
1252 len = sizeof(transport->recv.calldir) - offset;
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001253 dprintk("RPC: reading CALL/REPLY flag (%zu bytes)\n", len);
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001254 p = ((char *) &transport->recv.calldir) + offset;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001255 used = xdr_skb_read_bits(desc, p, len);
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001256 transport->recv.offset += used;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001257 if (used != len)
1258 return;
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001259 transport->recv.flags &= ~TCP_RCV_READ_CALLDIR;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001260 /*
1261 * We don't yet have the XDR buffer, so we will write the calldir
1262 * out after we get the buffer from the 'struct rpc_rqst'
1263 */
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001264 switch (ntohl(transport->recv.calldir)) {
Trond Myklebustb76ce562010-06-16 13:57:32 -04001265 case RPC_REPLY:
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001266 transport->recv.flags |= TCP_RCV_COPY_CALLDIR;
1267 transport->recv.flags |= TCP_RCV_COPY_DATA;
1268 transport->recv.flags |= TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001269 break;
1270 case RPC_CALL:
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001271 transport->recv.flags |= TCP_RCV_COPY_CALLDIR;
1272 transport->recv.flags |= TCP_RCV_COPY_DATA;
1273 transport->recv.flags &= ~TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001274 break;
1275 default:
1276 dprintk("RPC: invalid request message type\n");
Trond Myklebusta519fc72012-09-12 16:49:15 -04001277 xs_tcp_force_close(&transport->xprt);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001278 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001279 xs_tcp_check_fraghdr(transport);
1280}
1281
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001282static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
1283 struct xdr_skb_reader *desc,
1284 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001285{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001286 struct sock_xprt *transport =
1287 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001288 struct xdr_buf *rcvbuf;
1289 size_t len;
1290 ssize_t r;
1291
Chuck Levera246b012005-08-11 16:25:23 -04001292 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001293
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001294 if (transport->recv.flags & TCP_RCV_COPY_CALLDIR) {
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001295 /*
1296 * Save the RPC direction in the XDR buffer
1297 */
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001298 memcpy(rcvbuf->head[0].iov_base + transport->recv.copied,
1299 &transport->recv.calldir,
1300 sizeof(transport->recv.calldir));
1301 transport->recv.copied += sizeof(transport->recv.calldir);
1302 transport->recv.flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -04001303 }
1304
Chuck Levera246b012005-08-11 16:25:23 -04001305 len = desc->count;
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001306 if (len > transport->recv.len - transport->recv.offset)
1307 desc->count = transport->recv.len - transport->recv.offset;
1308 r = xdr_partial_copy_from_skb(rcvbuf, transport->recv.copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001309 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001310
Trond Myklebust040249d2017-08-13 02:13:45 -04001311 if (desc->count) {
Chuck Levera246b012005-08-11 16:25:23 -04001312 /* Error when copying to the receive buffer,
1313 * usually because we weren't able to allocate
1314 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001315 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001316 * will not receive any additional updates,
1317 * and time out.
1318 * Any remaining data from this record will
1319 * be discarded.
1320 */
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001321 transport->recv.flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001322 dprintk("RPC: XID %08x truncated request\n",
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001323 ntohl(transport->recv.xid));
1324 dprintk("RPC: xprt = %p, recv.copied = %lu, "
1325 "recv.offset = %u, recv.len = %u\n",
1326 xprt, transport->recv.copied,
1327 transport->recv.offset, transport->recv.len);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001328 return;
Chuck Levera246b012005-08-11 16:25:23 -04001329 }
1330
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001331 transport->recv.copied += r;
1332 transport->recv.offset += r;
Trond Myklebust040249d2017-08-13 02:13:45 -04001333 desc->count = len - r;
1334
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001335 dprintk("RPC: XID %08x read %zd bytes\n",
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001336 ntohl(transport->recv.xid), r);
1337 dprintk("RPC: xprt = %p, recv.copied = %lu, recv.offset = %u, "
1338 "recv.len = %u\n", xprt, transport->recv.copied,
1339 transport->recv.offset, transport->recv.len);
Chuck Levera246b012005-08-11 16:25:23 -04001340
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001341 if (transport->recv.copied == req->rq_private_buf.buflen)
1342 transport->recv.flags &= ~TCP_RCV_COPY_DATA;
1343 else if (transport->recv.offset == transport->recv.len) {
1344 if (transport->recv.flags & TCP_RCV_LAST_FRAG)
1345 transport->recv.flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001346 }
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001347}
1348
1349/*
1350 * Finds the request corresponding to the RPC xid and invokes the common
1351 * tcp read code to read the data.
1352 */
1353static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1354 struct xdr_skb_reader *desc)
1355{
1356 struct sock_xprt *transport =
1357 container_of(xprt, struct sock_xprt, xprt);
1358 struct rpc_rqst *req;
1359
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001360 dprintk("RPC: read reply XID %08x\n", ntohl(transport->recv.xid));
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001361
1362 /* Find and lock the request corresponding to this xid */
Trond Myklebust75c84152018-08-31 10:21:00 -04001363 spin_lock(&xprt->queue_lock);
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001364 req = xprt_lookup_rqst(xprt, transport->recv.xid);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001365 if (!req) {
1366 dprintk("RPC: XID %08x request not found!\n",
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001367 ntohl(transport->recv.xid));
Trond Myklebust75c84152018-08-31 10:21:00 -04001368 spin_unlock(&xprt->queue_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001369 return -1;
1370 }
Trond Myklebust729749b2017-08-13 10:03:59 -04001371 xprt_pin_rqst(req);
Trond Myklebust75c84152018-08-31 10:21:00 -04001372 spin_unlock(&xprt->queue_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001373
1374 xs_tcp_read_common(xprt, desc, req);
1375
Trond Myklebust75c84152018-08-31 10:21:00 -04001376 spin_lock(&xprt->queue_lock);
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001377 if (!(transport->recv.flags & TCP_RCV_COPY_DATA))
1378 xprt_complete_rqst(req->rq_task, transport->recv.copied);
Trond Myklebust729749b2017-08-13 10:03:59 -04001379 xprt_unpin_rqst(req);
Trond Myklebust75c84152018-08-31 10:21:00 -04001380 spin_unlock(&xprt->queue_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001381 return 0;
1382}
1383
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001384#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001385/*
1386 * Obtains an rpc_rqst previously allocated and invokes the common
1387 * tcp read code to read the data. The result is placed in the callback
1388 * queue.
1389 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1390 * connection and return -1.
1391 */
Trond Myklebust2ea24492014-02-10 11:18:39 -05001392static int xs_tcp_read_callback(struct rpc_xprt *xprt,
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001393 struct xdr_skb_reader *desc)
1394{
1395 struct sock_xprt *transport =
1396 container_of(xprt, struct sock_xprt, xprt);
1397 struct rpc_rqst *req;
1398
Trond Myklebustc89091c2017-08-16 12:09:44 -04001399 /* Look up the request corresponding to the given XID */
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001400 req = xprt_lookup_bc_request(xprt, transport->recv.xid);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001401 if (req == NULL) {
1402 printk(KERN_WARNING "Callback slot table overflowed\n");
1403 xprt_force_disconnect(xprt);
1404 return -1;
1405 }
1406
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001407 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1408 xs_tcp_read_common(xprt, desc, req);
1409
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001410 if (!(transport->recv.flags & TCP_RCV_COPY_DATA))
1411 xprt_complete_bc_request(req, transport->recv.copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001412
1413 return 0;
1414}
1415
1416static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1417 struct xdr_skb_reader *desc)
1418{
1419 struct sock_xprt *transport =
1420 container_of(xprt, struct sock_xprt, xprt);
1421
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001422 return (transport->recv.flags & TCP_RPC_REPLY) ?
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001423 xs_tcp_read_reply(xprt, desc) :
1424 xs_tcp_read_callback(xprt, desc);
1425}
Chuck Lever76566772015-10-24 17:28:32 -04001426
1427static int xs_tcp_bc_up(struct svc_serv *serv, struct net *net)
1428{
1429 int ret;
1430
1431 ret = svc_create_xprt(serv, "tcp-bc", net, PF_INET, 0,
1432 SVC_SOCK_ANONYMOUS);
1433 if (ret < 0)
1434 return ret;
1435 return 0;
1436}
Chuck Lever6b26cc82016-05-02 14:40:40 -04001437
1438static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
1439{
1440 return PAGE_SIZE;
1441}
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001442#else
1443static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1444 struct xdr_skb_reader *desc)
1445{
1446 return xs_tcp_read_reply(xprt, desc);
1447}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001448#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001449
1450/*
1451 * Read data off the transport. This can be either an RPC_CALL or an
1452 * RPC_REPLY. Relay the processing to helper functions.
1453 */
1454static void xs_tcp_read_data(struct rpc_xprt *xprt,
1455 struct xdr_skb_reader *desc)
1456{
1457 struct sock_xprt *transport =
1458 container_of(xprt, struct sock_xprt, xprt);
1459
1460 if (_xs_tcp_read_data(xprt, desc) == 0)
1461 xs_tcp_check_fraghdr(transport);
1462 else {
1463 /*
1464 * The transport_lock protects the request handling.
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001465 * There's no need to hold it to update the recv.flags.
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001466 */
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001467 transport->recv.flags &= ~TCP_RCV_COPY_DATA;
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001468 }
Chuck Levera246b012005-08-11 16:25:23 -04001469}
1470
Chuck Leverdd456472006-12-05 16:35:44 -05001471static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001472{
1473 size_t len;
1474
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001475 len = transport->recv.len - transport->recv.offset;
Chuck Levera246b012005-08-11 16:25:23 -04001476 if (len > desc->count)
1477 len = desc->count;
1478 desc->count -= len;
1479 desc->offset += len;
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001480 transport->recv.offset += len;
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001481 dprintk("RPC: discarded %zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001482 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001483}
1484
Chuck Lever9903cd12005-08-11 16:25:26 -04001485static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
Chuck Levera246b012005-08-11 16:25:23 -04001486{
1487 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001488 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001489 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001490 .skb = skb,
1491 .offset = offset,
1492 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001493 };
Trond Myklebust3d188802018-01-14 15:47:06 -05001494 size_t ret;
Chuck Levera246b012005-08-11 16:25:23 -04001495
Chuck Lever46121cf2007-01-31 12:14:08 -05001496 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001497 do {
Jeff Layton1a867a02014-10-28 14:24:14 -04001498 trace_xs_tcp_data_recv(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001499 /* Read in a new fragment marker if necessary */
1500 /* Can we ever really expect to get completely empty fragments? */
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001501 if (transport->recv.flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001502 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001503 continue;
1504 }
1505 /* Read in the xid if necessary */
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001506 if (transport->recv.flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001507 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001508 continue;
1509 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001510 /* Read in the call/reply flag */
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001511 if (transport->recv.flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001512 xs_tcp_read_calldir(transport, &desc);
1513 continue;
1514 }
Chuck Levera246b012005-08-11 16:25:23 -04001515 /* Read in the request data */
Trond Myklebustd1109aa2018-08-13 15:48:42 -04001516 if (transport->recv.flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001517 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001518 continue;
1519 }
1520 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001521 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001522 } while (desc.count);
Trond Myklebust3d188802018-01-14 15:47:06 -05001523 ret = len - desc.count;
1524 if (ret < rd_desc->count)
1525 rd_desc->count -= ret;
1526 else
1527 rd_desc->count = 0;
Jeff Layton1a867a02014-10-28 14:24:14 -04001528 trace_xs_tcp_data_recv(transport);
Chuck Lever46121cf2007-01-31 12:14:08 -05001529 dprintk("RPC: xs_tcp_data_recv done\n");
Trond Myklebust3d188802018-01-14 15:47:06 -05001530 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04001531}
1532
Trond Myklebust66d7a562015-10-05 11:19:20 -04001533static void xs_tcp_data_receive(struct sock_xprt *transport)
1534{
1535 struct rpc_xprt *xprt = &transport->xprt;
1536 struct sock *sk;
1537 read_descriptor_t rd_desc = {
Trond Myklebust66d7a562015-10-05 11:19:20 -04001538 .arg.data = xprt,
1539 };
1540 unsigned long total = 0;
1541 int read = 0;
1542
Trond Myklebust0af34422018-01-14 15:28:29 -05001543restart:
Trond Myklebustedc1b012015-10-05 10:53:49 -04001544 mutex_lock(&transport->recv_mutex);
Trond Myklebust66d7a562015-10-05 11:19:20 -04001545 sk = transport->inet;
Trond Myklebustedc1b012015-10-05 10:53:49 -04001546 if (sk == NULL)
1547 goto out;
Trond Myklebust66d7a562015-10-05 11:19:20 -04001548
1549 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Trond Myklebust0af34422018-01-14 15:28:29 -05001550 for (;;) {
Trond Myklebust3d188802018-01-14 15:47:06 -05001551 rd_desc.count = RPC_TCP_READ_CHUNK_SZ;
Trond Myklebustedc1b012015-10-05 10:53:49 -04001552 lock_sock(sk);
Trond Myklebust66d7a562015-10-05 11:19:20 -04001553 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
Trond Myklebust3d188802018-01-14 15:47:06 -05001554 if (rd_desc.count != 0 || read < 0) {
Trond Myklebust42d42a52016-05-23 09:24:55 -04001555 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
1556 release_sock(sk);
Trond Myklebust8d6f97d2017-08-12 15:11:49 -04001557 break;
Trond Myklebust42d42a52016-05-23 09:24:55 -04001558 }
Trond Myklebust8d6f97d2017-08-12 15:11:49 -04001559 release_sock(sk);
1560 total += read;
Trond Myklebust0af34422018-01-14 15:28:29 -05001561 if (need_resched()) {
1562 mutex_unlock(&transport->recv_mutex);
1563 cond_resched();
1564 goto restart;
1565 }
Trond Myklebust66d7a562015-10-05 11:19:20 -04001566 }
Trond Myklebust8d6f97d2017-08-12 15:11:49 -04001567 if (test_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
1568 queue_work(xprtiod_workqueue, &transport->recv_worker);
Trond Myklebustedc1b012015-10-05 10:53:49 -04001569out:
1570 mutex_unlock(&transport->recv_mutex);
Trond Myklebust66d7a562015-10-05 11:19:20 -04001571 trace_xs_tcp_data_ready(xprt, read, total);
1572}
1573
Trond Myklebustedc1b012015-10-05 10:53:49 -04001574static void xs_tcp_data_receive_workfn(struct work_struct *work)
1575{
1576 struct sock_xprt *transport =
1577 container_of(work, struct sock_xprt, recv_worker);
1578 xs_tcp_data_receive(transport);
1579}
1580
Chuck Lever9903cd12005-08-11 16:25:26 -04001581/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001582 * xs_tcp_state_change - callback to handle TCP socket state changes
1583 * @sk: socket whose state has changed
1584 *
1585 */
1586static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001587{
Chuck Lever9903cd12005-08-11 16:25:26 -04001588 struct rpc_xprt *xprt;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001589 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001590
Eric Dumazetf064af12010-09-22 12:43:39 +00001591 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001592 if (!(xprt = xprt_from_sock(sk)))
1593 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001594 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001595 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001596 sk->sk_state, xprt_connected(xprt),
1597 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001598 sock_flag(sk, SOCK_ZAPPED),
1599 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001600
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001601 transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001602 trace_rpc_socket_state_change(xprt, sk->sk_socket);
Chuck Levera246b012005-08-11 16:25:23 -04001603 switch (sk->sk_state) {
1604 case TCP_ESTABLISHED:
Eric Dumazetf064af12010-09-22 12:43:39 +00001605 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001606 if (!xprt_test_and_set_connected(xprt)) {
Trond Myklebust8b717982013-09-26 10:18:04 -04001607 xprt->connect_cookie++;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001608 clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
1609 xprt_clear_connecting(xprt);
Chuck Lever51971132006-12-05 16:35:19 -05001610
Trond Myklebust2a491992009-03-11 14:38:00 -04001611 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001612 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001613 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001614 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001615 case TCP_FIN_WAIT1:
1616 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001617 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001618 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001619 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001620 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001621 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001622 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001623 smp_mb__after_atomic();
Chuck Levera246b012005-08-11 16:25:23 -04001624 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001625 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001626 /* The server initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001627 xprt->connect_cookie++;
Trond Myklebustd0bea452012-10-23 11:35:47 -04001628 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebusta519fc72012-09-12 16:49:15 -04001629 xs_tcp_force_close(xprt);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001630 /* fall through */
Trond Myklebust663b8852008-01-01 18:42:12 -05001631 case TCP_CLOSING:
1632 /*
1633 * If the server closed down the connection, make sure that
1634 * we back off before reconnecting
1635 */
1636 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1637 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001638 break;
1639 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001640 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001641 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001642 clear_bit(XPRT_CONNECTED, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001643 smp_mb__after_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001644 break;
1645 case TCP_CLOSE:
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001646 if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
1647 &transport->sock_state))
1648 xprt_clear_connecting(xprt);
Trond Myklebust9b308892018-02-05 10:20:06 -05001649 clear_bit(XPRT_CLOSING, &xprt->state);
NeilBrown3ffbc1d2017-07-19 14:05:01 +10001650 if (sk->sk_err)
1651 xprt_wake_pending_tasks(xprt, -sk->sk_err);
Trond Myklebust9b308892018-02-05 10:20:06 -05001652 /* Trigger the socket release */
1653 xs_tcp_force_close(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001654 }
1655 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001656 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001657}
1658
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001659static void xs_write_space(struct sock *sk)
1660{
Trond Myklebust13331a52016-01-06 08:57:06 -05001661 struct socket_wq *wq;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001662 struct rpc_xprt *xprt;
1663
Trond Myklebust13331a52016-01-06 08:57:06 -05001664 if (!sk->sk_socket)
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001665 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001666 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001667
1668 if (unlikely(!(xprt = xprt_from_sock(sk))))
1669 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001670 rcu_read_lock();
1671 wq = rcu_dereference(sk->sk_wq);
1672 if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0)
1673 goto out;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001674
1675 xprt_write_space(xprt);
Trond Myklebust13331a52016-01-06 08:57:06 -05001676out:
1677 rcu_read_unlock();
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001678}
1679
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001680/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001681 * xs_udp_write_space - callback invoked when socket buffer space
1682 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001683 * @sk: socket whose state has changed
1684 *
Chuck Levera246b012005-08-11 16:25:23 -04001685 * Called when more output buffer space is available for this socket.
1686 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001687 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001688 * with a bunch of small requests.
1689 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001690static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001691{
Eric Dumazetf064af12010-09-22 12:43:39 +00001692 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001693
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001694 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001695 if (sock_writeable(sk))
1696 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001697
Eric Dumazetf064af12010-09-22 12:43:39 +00001698 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001699}
Chuck Levera246b012005-08-11 16:25:23 -04001700
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001701/**
1702 * xs_tcp_write_space - callback invoked when socket buffer space
1703 * becomes available
1704 * @sk: socket whose state has changed
1705 *
1706 * Called when more output buffer space is available for this socket.
1707 * We try not to wake our writers until they can make "significant"
1708 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1709 * with a bunch of small requests.
1710 */
1711static void xs_tcp_write_space(struct sock *sk)
1712{
Eric Dumazetf064af12010-09-22 12:43:39 +00001713 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001714
1715 /* from net/core/stream.c:sk_stream_write_space */
Eric Dumazet64dc6132013-07-22 20:26:31 -07001716 if (sk_stream_is_writeable(sk))
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001717 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001718
Eric Dumazetf064af12010-09-22 12:43:39 +00001719 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001720}
1721
Chuck Lever470056c2005-08-25 16:25:56 -07001722static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001723{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001724 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1725 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001726
Chuck Lever7c6e0662006-12-05 16:35:30 -05001727 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001728 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001729 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001730 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001731 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001732 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001733 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001734 sk->sk_write_space(sk);
1735 }
1736}
1737
Chuck Lever43118c22005-08-25 16:25:49 -07001738/**
Chuck Lever470056c2005-08-25 16:25:56 -07001739 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001740 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001741 * @sndsize: requested size of send buffer, in bytes
1742 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001743 *
Chuck Lever470056c2005-08-25 16:25:56 -07001744 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001745 */
Chuck Lever470056c2005-08-25 16:25:56 -07001746static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001747{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001748 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1749
1750 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001751 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001752 transport->sndsize = sndsize + 1024;
1753 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001754 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001755 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001756
1757 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001758}
1759
Chuck Lever46c0ee82005-08-25 16:25:52 -07001760/**
1761 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1762 * @task: task that timed out
1763 *
1764 * Adjust the congestion window after a retransmit timeout has occurred.
1765 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001766static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever46c0ee82005-08-25 16:25:52 -07001767{
Chuck Leverb977b642017-02-08 17:00:51 -05001768 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001769 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
Chuck Leverb977b642017-02-08 17:00:51 -05001770 spin_unlock_bh(&xprt->transport_lock);
Chuck Lever46c0ee82005-08-25 16:25:52 -07001771}
1772
Chuck Leverb85d8802006-05-25 01:40:49 -04001773static unsigned short xs_get_random_port(void)
1774{
Frank Sorenson5d718992016-07-08 16:35:23 -05001775 unsigned short range = xprt_max_resvport - xprt_min_resvport + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001776 unsigned short rand = (unsigned short) prandom_u32() % range;
Chuck Leverb85d8802006-05-25 01:40:49 -04001777 return rand + xprt_min_resvport;
1778}
1779
Chuck Lever92200412006-01-03 09:55:51 +01001780/**
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001781 * xs_set_reuseaddr_port - set the socket's port and address reuse options
1782 * @sock: socket
1783 *
1784 * Note that this function has to be called on all sockets that share the
1785 * same port, and it must be called before binding.
1786 */
1787static void xs_sock_set_reuseport(struct socket *sock)
1788{
Trond Myklebust402e23b2015-02-09 17:20:14 -05001789 int opt = 1;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001790
Trond Myklebust402e23b2015-02-09 17:20:14 -05001791 kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
1792 (char *)&opt, sizeof(opt));
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001793}
1794
1795static unsigned short xs_sock_getport(struct socket *sock)
1796{
1797 struct sockaddr_storage buf;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001798 unsigned short port = 0;
1799
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001800 if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001801 goto out;
1802 switch (buf.ss_family) {
1803 case AF_INET6:
1804 port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
1805 break;
1806 case AF_INET:
1807 port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
1808 }
1809out:
1810 return port;
1811}
1812
1813/**
Chuck Lever92200412006-01-03 09:55:51 +01001814 * xs_set_port - reset the port number in the remote endpoint address
1815 * @xprt: generic transport
1816 * @port: new port number
1817 *
1818 */
1819static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1820{
Chuck Lever46121cf2007-01-31 12:14:08 -05001821 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001822
Chuck Lever9dc3b092009-08-09 15:09:46 -04001823 rpc_set_port(xs_addr(xprt), port);
1824 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001825}
1826
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001827static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
1828{
1829 if (transport->srcport == 0)
1830 transport->srcport = xs_sock_getport(sock);
1831}
1832
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001833static unsigned short xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001834{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001835 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001836
1837 if (port == 0 && transport->xprt.resvport)
1838 port = xs_get_random_port();
1839 return port;
1840}
1841
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001842static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001843{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001844 if (transport->srcport != 0)
1845 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001846 if (!transport->xprt.resvport)
1847 return 0;
1848 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1849 return xprt_max_resvport;
1850 return --port;
1851}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001852static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001853{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001854 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001855 int err, nloop = 0;
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001856 unsigned short port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001857 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001858
Chris Perl0f7a6222014-09-05 15:40:21 -04001859 /*
1860 * If we are asking for any ephemeral port (i.e. port == 0 &&
1861 * transport->xprt.resvport == 0), don't bind. Let the local
1862 * port selection happen implicitly when the socket is used
1863 * (for example at connect time).
1864 *
1865 * This ensures that we can continue to establish TCP
1866 * connections even when all local ephemeral ports are already
1867 * a part of some TCP connection. This makes no difference
1868 * for UDP sockets, but also doens't harm them.
1869 *
1870 * If we're asking for any reserved port (i.e. port == 0 &&
1871 * transport->xprt.resvport == 1) xs_get_srcport above will
1872 * ensure that port is non-zero and we will bind as needed.
1873 */
1874 if (port == 0)
1875 return 0;
1876
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001877 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001878 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001879 rpc_set_port((struct sockaddr *)&myaddr, port);
1880 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1881 transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001882 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001883 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001884 break;
Chuck Levera246b012005-08-11 16:25:23 -04001885 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001886 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001887 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001888 if (port > last)
1889 nloop++;
1890 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001891
Chuck Lever4232e862010-10-20 11:52:51 -04001892 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001893 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1894 &((struct sockaddr_in *)&myaddr)->sin_addr,
1895 port, err ? "failed" : "ok", err);
1896 else
1897 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1898 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1899 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001900 return err;
1901}
1902
Chuck Lever176e21e2011-05-09 15:22:44 -04001903/*
1904 * We don't support autobind on AF_LOCAL sockets
1905 */
1906static void xs_local_rpcbind(struct rpc_task *task)
1907{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001908 xprt_set_bound(task->tk_xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04001909}
1910
1911static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1912{
1913}
Chuck Levera246b012005-08-11 16:25:23 -04001914
Peter Zijlstraed075362006-12-06 20:35:24 -08001915#ifdef CONFIG_DEBUG_LOCK_ALLOC
1916static struct lock_class_key xs_key[2];
1917static struct lock_class_key xs_slock_key[2];
1918
Chuck Lever176e21e2011-05-09 15:22:44 -04001919static inline void xs_reclassify_socketu(struct socket *sock)
1920{
1921 struct sock *sk = sock->sk;
1922
Chuck Lever176e21e2011-05-09 15:22:44 -04001923 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1924 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1925}
1926
Chuck Lever8945ee52007-08-06 11:58:04 -04001927static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001928{
1929 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001930
Chuck Lever8945ee52007-08-06 11:58:04 -04001931 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1932 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1933}
Peter Zijlstraed075362006-12-06 20:35:24 -08001934
Chuck Lever8945ee52007-08-06 11:58:04 -04001935static inline void xs_reclassify_socket6(struct socket *sock)
1936{
1937 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001938
Chuck Lever8945ee52007-08-06 11:58:04 -04001939 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1940 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001941}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001942
1943static inline void xs_reclassify_socket(int family, struct socket *sock)
1944{
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02001945 if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -04001946 return;
1947
Chuck Lever4232e862010-10-20 11:52:51 -04001948 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001949 case AF_LOCAL:
1950 xs_reclassify_socketu(sock);
1951 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001952 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001953 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001954 break;
1955 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001956 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001957 break;
1958 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001959}
Peter Zijlstraed075362006-12-06 20:35:24 -08001960#else
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001961static inline void xs_reclassify_socket(int family, struct socket *sock)
1962{
1963}
Peter Zijlstraed075362006-12-06 20:35:24 -08001964#endif
1965
NeilBrown93dc41b2013-10-31 16:14:36 +11001966static void xs_dummy_setup_socket(struct work_struct *work)
1967{
1968}
1969
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001970static struct socket *xs_create_sock(struct rpc_xprt *xprt,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001971 struct sock_xprt *transport, int family, int type,
1972 int protocol, bool reuseport)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001973{
1974 struct socket *sock;
1975 int err;
1976
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001977 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001978 if (err < 0) {
1979 dprintk("RPC: can't create %d transport socket (%d).\n",
1980 protocol, -err);
1981 goto out;
1982 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001983 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001984
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001985 if (reuseport)
1986 xs_sock_set_reuseport(sock);
1987
Ben Hutchings4cea2882011-02-22 21:54:34 +00001988 err = xs_bind(transport, sock);
1989 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001990 sock_release(sock);
1991 goto out;
1992 }
1993
1994 return sock;
1995out:
1996 return ERR_PTR(err);
1997}
1998
Chuck Lever176e21e2011-05-09 15:22:44 -04001999static int xs_local_finish_connecting(struct rpc_xprt *xprt,
2000 struct socket *sock)
2001{
2002 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2003 xprt);
2004
2005 if (!transport->inet) {
2006 struct sock *sk = sock->sk;
2007
2008 write_lock_bh(&sk->sk_callback_lock);
2009
2010 xs_save_old_callbacks(transport, sk);
2011
2012 sk->sk_user_data = xprt;
Trond Myklebusta2648092015-10-06 17:03:00 -04002013 sk->sk_data_ready = xs_data_ready;
Chuck Lever176e21e2011-05-09 15:22:44 -04002014 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002015 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05002016 sk->sk_error_report = xs_error_report;
Trond Myklebustc2126152015-08-19 21:46:15 -05002017 sk->sk_allocation = GFP_NOIO;
Chuck Lever176e21e2011-05-09 15:22:44 -04002018
2019 xprt_clear_connected(xprt);
2020
2021 /* Reset to new socket */
2022 transport->sock = sock;
2023 transport->inet = sk;
2024
2025 write_unlock_bh(&sk->sk_callback_lock);
2026 }
2027
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04002028 transport->xmit.offset = 0;
2029
Chuck Lever176e21e2011-05-09 15:22:44 -04002030 /* Tell the socket layer to start connecting... */
2031 xprt->stat.connect_count++;
2032 xprt->stat.connect_start = jiffies;
2033 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
2034}
2035
2036/**
2037 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
Chuck Lever176e21e2011-05-09 15:22:44 -04002038 * @transport: socket transport to connect
Chuck Lever176e21e2011-05-09 15:22:44 -04002039 */
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002040static int xs_local_setup_socket(struct sock_xprt *transport)
Chuck Lever176e21e2011-05-09 15:22:44 -04002041{
Chuck Lever176e21e2011-05-09 15:22:44 -04002042 struct rpc_xprt *xprt = &transport->xprt;
2043 struct socket *sock;
2044 int status = -EIO;
2045
Chuck Lever176e21e2011-05-09 15:22:44 -04002046 status = __sock_create(xprt->xprt_net, AF_LOCAL,
2047 SOCK_STREAM, 0, &sock, 1);
2048 if (status < 0) {
2049 dprintk("RPC: can't create AF_LOCAL "
2050 "transport socket (%d).\n", -status);
2051 goto out;
2052 }
Stefan Hajnoczid1358912015-12-02 14:17:52 +08002053 xs_reclassify_socket(AF_LOCAL, sock);
Chuck Lever176e21e2011-05-09 15:22:44 -04002054
2055 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
2056 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2057
2058 status = xs_local_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002059 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever176e21e2011-05-09 15:22:44 -04002060 switch (status) {
2061 case 0:
2062 dprintk("RPC: xprt %p connected to %s\n",
2063 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2064 xprt_set_connected(xprt);
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002065 case -ENOBUFS:
Chuck Lever176e21e2011-05-09 15:22:44 -04002066 break;
2067 case -ENOENT:
2068 dprintk("RPC: xprt %p: socket %s does not exist\n",
2069 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2070 break;
Trond Myklebust4a20a982012-12-15 17:02:29 -05002071 case -ECONNREFUSED:
2072 dprintk("RPC: xprt %p: connection refused for %s\n",
2073 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2074 break;
Chuck Lever176e21e2011-05-09 15:22:44 -04002075 default:
2076 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
2077 __func__, -status,
2078 xprt->address_strings[RPC_DISPLAY_ADDR]);
2079 }
2080
2081out:
2082 xprt_clear_connecting(xprt);
2083 xprt_wake_pending_tasks(xprt, status);
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002084 return status;
2085}
2086
Linus Torvaldsb6669732013-02-28 18:02:55 -08002087static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002088{
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002089 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2090 int ret;
2091
2092 if (RPC_IS_ASYNC(task)) {
2093 /*
2094 * We want the AF_LOCAL connect to be resolved in the
2095 * filesystem namespace of the process making the rpc
2096 * call. Thus we connect synchronously.
2097 *
2098 * If we want to support asynchronous AF_LOCAL calls,
2099 * we'll need to figure out how to pass a namespace to
2100 * connect.
2101 */
2102 rpc_exit(task, -ENOTCONN);
2103 return;
2104 }
2105 ret = xs_local_setup_socket(transport);
2106 if (ret && !RPC_IS_SOFTCONN(task))
2107 msleep_interruptible(15000);
Chuck Lever176e21e2011-05-09 15:22:44 -04002108}
2109
Jeff Layton3c87ef62015-06-03 16:14:25 -04002110#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Jeff Laytond6e971d2015-06-03 16:14:28 -04002111/*
2112 * Note that this should be called with XPRT_LOCKED held (or when we otherwise
2113 * know that we have exclusive access to the socket), to guard against
2114 * races with xs_reset_transport.
2115 */
Mel Gormana564b8f2012-07-31 16:45:12 -07002116static void xs_set_memalloc(struct rpc_xprt *xprt)
2117{
2118 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2119 xprt);
2120
Jeff Laytond6e971d2015-06-03 16:14:28 -04002121 /*
2122 * If there's no sock, then we have nothing to set. The
2123 * reconnecting process will get it for us.
2124 */
2125 if (!transport->inet)
2126 return;
Jeff Layton8e228132015-06-03 16:14:26 -04002127 if (atomic_read(&xprt->swapper))
Mel Gormana564b8f2012-07-31 16:45:12 -07002128 sk_set_memalloc(transport->inet);
2129}
2130
2131/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002132 * xs_enable_swap - Tag this transport as being used for swap.
Mel Gormana564b8f2012-07-31 16:45:12 -07002133 * @xprt: transport to tag
Mel Gormana564b8f2012-07-31 16:45:12 -07002134 *
Jeff Layton8e228132015-06-03 16:14:26 -04002135 * Take a reference to this transport on behalf of the rpc_clnt, and
2136 * optionally mark it for swapping if it wasn't already.
Mel Gormana564b8f2012-07-31 16:45:12 -07002137 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002138static int
2139xs_enable_swap(struct rpc_xprt *xprt)
Mel Gormana564b8f2012-07-31 16:45:12 -07002140{
Jeff Laytond6e971d2015-06-03 16:14:28 -04002141 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Mel Gormana564b8f2012-07-31 16:45:12 -07002142
Jeff Laytond6e971d2015-06-03 16:14:28 -04002143 if (atomic_inc_return(&xprt->swapper) != 1)
2144 return 0;
2145 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2146 return -ERESTARTSYS;
2147 if (xs->inet)
2148 sk_set_memalloc(xs->inet);
2149 xprt_release_xprt(xprt, NULL);
Jeff Layton8e228132015-06-03 16:14:26 -04002150 return 0;
Mel Gormana564b8f2012-07-31 16:45:12 -07002151}
Jeff Layton8e228132015-06-03 16:14:26 -04002152
2153/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002154 * xs_disable_swap - Untag this transport as being used for swap.
Jeff Layton8e228132015-06-03 16:14:26 -04002155 * @xprt: transport to tag
2156 *
2157 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
2158 * swapper refcount goes to 0, untag the socket as a memalloc socket.
2159 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002160static void
2161xs_disable_swap(struct rpc_xprt *xprt)
Jeff Layton8e228132015-06-03 16:14:26 -04002162{
Jeff Laytond6e971d2015-06-03 16:14:28 -04002163 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Jeff Layton8e228132015-06-03 16:14:26 -04002164
Jeff Laytond6e971d2015-06-03 16:14:28 -04002165 if (!atomic_dec_and_test(&xprt->swapper))
2166 return;
2167 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2168 return;
2169 if (xs->inet)
2170 sk_clear_memalloc(xs->inet);
2171 xprt_release_xprt(xprt, NULL);
Mel Gormana564b8f2012-07-31 16:45:12 -07002172}
Mel Gormana564b8f2012-07-31 16:45:12 -07002173#else
2174static void xs_set_memalloc(struct rpc_xprt *xprt)
2175{
2176}
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002177
2178static int
2179xs_enable_swap(struct rpc_xprt *xprt)
2180{
2181 return -EINVAL;
2182}
2183
2184static void
2185xs_disable_swap(struct rpc_xprt *xprt)
2186{
2187}
Mel Gormana564b8f2012-07-31 16:45:12 -07002188#endif
2189
Chuck Lever16be2d22007-08-06 11:57:38 -04002190static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04002191{
Chuck Lever16be2d22007-08-06 11:57:38 -04002192 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04002193
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002194 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002195 struct sock *sk = sock->sk;
2196
2197 write_lock_bh(&sk->sk_callback_lock);
2198
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002199 xs_save_old_callbacks(transport, sk);
2200
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002201 sk->sk_user_data = xprt;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002202 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002203 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002204 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebustc2126152015-08-19 21:46:15 -05002205 sk->sk_allocation = GFP_NOIO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002206
2207 xprt_set_connected(xprt);
2208
2209 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002210 transport->sock = sock;
2211 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002212
Mel Gormana564b8f2012-07-31 16:45:12 -07002213 xs_set_memalloc(xprt);
2214
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002215 write_unlock_bh(&sk->sk_callback_lock);
2216 }
Chuck Lever470056c2005-08-25 16:25:56 -07002217 xs_udp_do_set_buffer_size(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002218
2219 xprt->stat.connect_start = jiffies;
Chuck Lever16be2d22007-08-06 11:57:38 -04002220}
2221
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002222static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04002223{
2224 struct sock_xprt *transport =
2225 container_of(work, struct sock_xprt, connect_worker.work);
2226 struct rpc_xprt *xprt = &transport->xprt;
Colin Ian Kingd099b8a2017-09-18 12:21:14 +01002227 struct socket *sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002228 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04002229
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002230 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002231 xs_addr(xprt)->sa_family, SOCK_DGRAM,
2232 IPPROTO_UDP, false);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002233 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04002234 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04002235
Chuck Leverc740eff2009-08-09 15:09:46 -04002236 dprintk("RPC: worker connecting xprt %p via %s to "
2237 "%s (port %s)\n", xprt,
2238 xprt->address_strings[RPC_DISPLAY_PROTO],
2239 xprt->address_strings[RPC_DISPLAY_ADDR],
2240 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04002241
2242 xs_udp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002243 trace_rpc_socket_connect(xprt, sock, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002244 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002245out:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002246 xprt_unlock_connect(xprt, transport);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002247 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002248 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002249}
2250
Trond Myklebust4876cc72015-06-19 16:17:57 -04002251/**
2252 * xs_tcp_shutdown - gracefully shut down a TCP socket
2253 * @xprt: transport
2254 *
2255 * Initiates a graceful shutdown of the TCP socket by calling the
2256 * equivalent of shutdown(SHUT_RDWR);
2257 */
2258static void xs_tcp_shutdown(struct rpc_xprt *xprt)
2259{
2260 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2261 struct socket *sock = transport->sock;
Trond Myklebust9b308892018-02-05 10:20:06 -05002262 int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
Trond Myklebust4876cc72015-06-19 16:17:57 -04002263
2264 if (sock == NULL)
2265 return;
Trond Myklebust9b308892018-02-05 10:20:06 -05002266 switch (skst) {
2267 default:
Trond Myklebust4876cc72015-06-19 16:17:57 -04002268 kernel_sock_shutdown(sock, SHUT_RDWR);
2269 trace_rpc_socket_shutdown(xprt, sock);
Trond Myklebust9b308892018-02-05 10:20:06 -05002270 break;
2271 case TCP_CLOSE:
2272 case TCP_TIME_WAIT:
Trond Myklebust4876cc72015-06-19 16:17:57 -04002273 xs_reset_transport(transport);
Trond Myklebust9b308892018-02-05 10:20:06 -05002274 }
Trond Myklebust4876cc72015-06-19 16:17:57 -04002275}
2276
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002277static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
2278 struct socket *sock)
2279{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002280 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2281 unsigned int keepidle;
2282 unsigned int keepcnt;
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002283 unsigned int opt_on = 1;
2284 unsigned int timeo;
2285
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002286 spin_lock_bh(&xprt->transport_lock);
2287 keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
2288 keepcnt = xprt->timeout->to_retries + 1;
2289 timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
2290 (xprt->timeout->to_retries + 1);
2291 clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2292 spin_unlock_bh(&xprt->transport_lock);
2293
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002294 /* TCP Keepalive options */
2295 kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
2296 (char *)&opt_on, sizeof(opt_on));
2297 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
2298 (char *)&keepidle, sizeof(keepidle));
2299 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
2300 (char *)&keepidle, sizeof(keepidle));
2301 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
2302 (char *)&keepcnt, sizeof(keepcnt));
2303
2304 /* TCP user timeout (see RFC5482) */
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002305 kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT,
2306 (char *)&timeo, sizeof(timeo));
2307}
2308
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002309static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
2310 unsigned long connect_timeout,
2311 unsigned long reconnect_timeout)
2312{
2313 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2314 struct rpc_timeout to;
2315 unsigned long initval;
2316
2317 spin_lock_bh(&xprt->transport_lock);
2318 if (reconnect_timeout < xprt->max_reconnect_timeout)
2319 xprt->max_reconnect_timeout = reconnect_timeout;
2320 if (connect_timeout < xprt->connect_timeout) {
2321 memcpy(&to, xprt->timeout, sizeof(to));
2322 initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
2323 /* Arbitrary lower limit */
2324 if (initval < XS_TCP_INIT_REEST_TO << 1)
2325 initval = XS_TCP_INIT_REEST_TO << 1;
2326 to.to_initval = initval;
2327 to.to_maxval = initval;
2328 memcpy(&transport->tcp_timeout, &to,
2329 sizeof(transport->tcp_timeout));
2330 xprt->timeout = &transport->tcp_timeout;
2331 xprt->connect_timeout = connect_timeout;
2332 }
2333 set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2334 spin_unlock_bh(&xprt->transport_lock);
2335}
2336
Chuck Lever16be2d22007-08-06 11:57:38 -04002337static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002338{
Chuck Lever16be2d22007-08-06 11:57:38 -04002339 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002340 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002341
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002342 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002343 struct sock *sk = sock->sk;
NeilBrownd88e4d82016-08-04 16:24:28 +10002344 unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC;
Trond Myklebust7f260e82013-09-24 11:25:22 -04002345
NeilBrownd88e4d82016-08-04 16:24:28 +10002346 /* Avoid temporary address, they are bad for long-lived
2347 * connections such as NFS mounts.
2348 * RFC4941, section 3.6 suggests that:
2349 * Individual applications, which have specific
2350 * knowledge about the normal duration of connections,
2351 * MAY override this as appropriate.
2352 */
2353 kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES,
2354 (char *)&addr_pref, sizeof(addr_pref));
2355
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002356 xs_tcp_set_socket_timeouts(xprt, sock);
Trond Myklebust775f06a2015-06-20 15:31:54 -04002357
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002358 write_lock_bh(&sk->sk_callback_lock);
2359
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002360 xs_save_old_callbacks(transport, sk);
2361
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002362 sk->sk_user_data = xprt;
Trond Myklebust5157b952016-05-29 10:13:24 -04002363 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002364 sk->sk_state_change = xs_tcp_state_change;
2365 sk->sk_write_space = xs_tcp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002366 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05002367 sk->sk_error_report = xs_error_report;
Trond Myklebustc2126152015-08-19 21:46:15 -05002368 sk->sk_allocation = GFP_NOIO;
Chuck Lever3167e122005-08-25 16:25:55 -07002369
2370 /* socket options */
Chuck Lever3167e122005-08-25 16:25:55 -07002371 sock_reset_flag(sk, SOCK_LINGER);
Chuck Lever3167e122005-08-25 16:25:55 -07002372 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002373
2374 xprt_clear_connected(xprt);
2375
2376 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002377 transport->sock = sock;
2378 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002379
2380 write_unlock_bh(&sk->sk_callback_lock);
2381 }
2382
Trond Myklebust01d37c42009-03-11 14:09:39 -04002383 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002384 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002385
Mel Gormana564b8f2012-07-31 16:45:12 -07002386 xs_set_memalloc(xprt);
2387
Trond Myklebuste1806c72018-08-13 16:50:49 -04002388 /* Reset TCP record info */
2389 transport->recv.offset = 0;
2390 transport->recv.len = 0;
2391 transport->recv.copied = 0;
2392 transport->recv.flags = TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Trond Myklebust6c7a64e2018-08-13 16:54:57 -04002393 transport->xmit.offset = 0;
Trond Myklebuste1806c72018-08-13 16:50:49 -04002394
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002395 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05002396 xprt->stat.connect_count++;
2397 xprt->stat.connect_start = jiffies;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04002398 set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002399 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2400 switch (ret) {
2401 case 0:
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002402 xs_set_srcport(transport, sock);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002403 /* fall through */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002404 case -EINPROGRESS:
2405 /* SYN_SENT! */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002406 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2407 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust1f4c17a2016-08-01 13:36:08 -04002408 break;
2409 case -EADDRNOTAVAIL:
2410 /* Source port number is unavailable. Try a new one! */
2411 transport->srcport = 0;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002412 }
2413out:
2414 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002415}
2416
2417/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002418 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
Chuck Lever16be2d22007-08-06 11:57:38 -04002419 *
2420 * Invoked by a work queue tasklet.
2421 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002422static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002423{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002424 struct sock_xprt *transport =
2425 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002426 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002427 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002428 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002429
Chuck Lever16be2d22007-08-06 11:57:38 -04002430 if (!sock) {
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002431 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002432 xs_addr(xprt)->sa_family, SOCK_STREAM,
2433 IPPROTO_TCP, true);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002434 if (IS_ERR(sock)) {
2435 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002436 goto out;
2437 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002438 }
2439
Chuck Leverc740eff2009-08-09 15:09:46 -04002440 dprintk("RPC: worker connecting xprt %p via %s to "
2441 "%s (port %s)\n", xprt,
2442 xprt->address_strings[RPC_DISPLAY_PROTO],
2443 xprt->address_strings[RPC_DISPLAY_ADDR],
2444 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002445
2446 status = xs_tcp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002447 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever46121cf2007-01-31 12:14:08 -05002448 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2449 xprt, -status, xprt_connected(xprt),
2450 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002451 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002452 default:
2453 printk("%s: connect returned unhandled error %d\n",
2454 __func__, status);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002455 /* fall through */
Trond Myklebustf75e6742009-04-21 17:18:20 -04002456 case -EADDRNOTAVAIL:
2457 /* We're probably in TIME_WAIT. Get rid of existing socket,
2458 * and retry
2459 */
Trond Myklebusta519fc72012-09-12 16:49:15 -04002460 xs_tcp_force_close(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002461 break;
Trond Myklebust2a491992009-03-11 14:38:00 -04002462 case 0:
2463 case -EINPROGRESS:
2464 case -EALREADY:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002465 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002466 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002467 case -EINVAL:
2468 /* Happens, for instance, if the user specified a link
2469 * local IPv6 address without a scope-id.
2470 */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002471 case -ECONNREFUSED:
2472 case -ECONNRESET:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002473 case -ENETDOWN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002474 case -ENETUNREACH:
Trond Myklebust4ba161a2017-11-24 12:00:24 -05002475 case -EHOSTUNREACH:
Trond Myklebust3913c782015-02-08 21:44:04 -05002476 case -EADDRINUSE:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002477 case -ENOBUFS:
NeilBrown6ea44ad2017-05-25 17:00:32 +10002478 /*
2479 * xs_tcp_force_close() wakes tasks with -EIO.
2480 * We need to wake them first to ensure the
2481 * correct error code.
2482 */
2483 xprt_wake_pending_tasks(xprt, status);
Trond Myklebust4efdd922015-02-08 15:34:28 -05002484 xs_tcp_force_close(xprt);
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002485 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002486 }
Trond Myklebust2a491992009-03-11 14:38:00 -04002487 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002488out:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002489 xprt_unlock_connect(xprt, transport);
Chuck Lever2226feb2005-08-11 16:25:38 -04002490 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002491 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04002492}
2493
Trond Myklebust02910172016-08-04 00:00:33 -04002494static unsigned long xs_reconnect_delay(const struct rpc_xprt *xprt)
2495{
2496 unsigned long start, now = jiffies;
2497
2498 start = xprt->stat.connect_start + xprt->reestablish_timeout;
2499 if (time_after(start, now))
2500 return start - now;
2501 return 0;
2502}
2503
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002504static void xs_reconnect_backoff(struct rpc_xprt *xprt)
2505{
2506 xprt->reestablish_timeout <<= 1;
2507 if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
2508 xprt->reestablish_timeout = xprt->max_reconnect_timeout;
2509 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2510 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2511}
2512
Chuck Lever68e220b2007-08-06 11:57:48 -04002513/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002514 * xs_connect - connect a socket to a remote endpoint
Trond Myklebust1b092092013-01-08 09:26:49 -05002515 * @xprt: pointer to transport structure
Chuck Lever9903cd12005-08-11 16:25:26 -04002516 * @task: address of RPC task that manages state of connect request
2517 *
2518 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002519 *
2520 * UDP socket connects are synchronous, but we use a work queue anyway
2521 * to guarantee that even unprivileged user processes can set up a
2522 * socket on a privileged port.
2523 *
2524 * If a UDP socket connect fails, the delay behavior here prevents
2525 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002526 */
Trond Myklebust1b092092013-01-08 09:26:49 -05002527static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002528{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002529 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002530 unsigned long delay = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002531
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002532 WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2533
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002534 if (transport->sock != NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002535 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2536 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002537 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002538
2539 /* Start by resetting any existing state */
2540 xs_reset_transport(transport);
2541
Trond Myklebust02910172016-08-04 00:00:33 -04002542 delay = xs_reconnect_delay(xprt);
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002543 xs_reconnect_backoff(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002544
Trond Myklebust02910172016-08-04 00:00:33 -04002545 } else
Chuck Lever46121cf2007-01-31 12:14:08 -05002546 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002547
2548 queue_delayed_work(xprtiod_workqueue,
2549 &transport->connect_worker,
2550 delay);
Chuck Levera246b012005-08-11 16:25:23 -04002551}
2552
Chuck Lever262ca072006-03-20 13:44:16 -05002553/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002554 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2555 * @xprt: rpc_xprt struct containing statistics
2556 * @seq: output file
2557 *
2558 */
2559static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2560{
2561 long idle_time = 0;
2562
2563 if (xprt_connected(xprt))
2564 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2565
2566 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
Andy Adamson15a45202012-02-14 16:19:18 -05002567 "%llu %llu %lu %llu %llu\n",
Chuck Lever176e21e2011-05-09 15:22:44 -04002568 xprt->stat.bind_count,
2569 xprt->stat.connect_count,
2570 xprt->stat.connect_time,
2571 idle_time,
2572 xprt->stat.sends,
2573 xprt->stat.recvs,
2574 xprt->stat.bad_xids,
2575 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002576 xprt->stat.bklog_u,
2577 xprt->stat.max_slots,
2578 xprt->stat.sending_u,
2579 xprt->stat.pending_u);
Chuck Lever176e21e2011-05-09 15:22:44 -04002580}
2581
2582/**
Chuck Lever262ca072006-03-20 13:44:16 -05002583 * xs_udp_print_stats - display UDP socket-specifc stats
2584 * @xprt: rpc_xprt struct containing statistics
2585 * @seq: output file
2586 *
2587 */
2588static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2589{
Chuck Leverc8475462006-12-05 16:35:26 -05002590 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2591
Andy Adamson15a45202012-02-14 16:19:18 -05002592 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2593 "%lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002594 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002595 xprt->stat.bind_count,
2596 xprt->stat.sends,
2597 xprt->stat.recvs,
2598 xprt->stat.bad_xids,
2599 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002600 xprt->stat.bklog_u,
2601 xprt->stat.max_slots,
2602 xprt->stat.sending_u,
2603 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002604}
2605
2606/**
2607 * xs_tcp_print_stats - display TCP socket-specifc stats
2608 * @xprt: rpc_xprt struct containing statistics
2609 * @seq: output file
2610 *
2611 */
2612static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2613{
Chuck Leverc8475462006-12-05 16:35:26 -05002614 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002615 long idle_time = 0;
2616
2617 if (xprt_connected(xprt))
2618 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2619
Andy Adamson15a45202012-02-14 16:19:18 -05002620 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2621 "%llu %llu %lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002622 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002623 xprt->stat.bind_count,
2624 xprt->stat.connect_count,
2625 xprt->stat.connect_time,
2626 idle_time,
2627 xprt->stat.sends,
2628 xprt->stat.recvs,
2629 xprt->stat.bad_xids,
2630 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002631 xprt->stat.bklog_u,
2632 xprt->stat.max_slots,
2633 xprt->stat.sending_u,
2634 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002635}
2636
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002637/*
2638 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2639 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2640 * to use the server side send routines.
2641 */
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002642static int bc_malloc(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002643{
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002644 struct rpc_rqst *rqst = task->tk_rqstp;
2645 size_t size = rqst->rq_callsize;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002646 struct page *page;
2647 struct rpc_buffer *buf;
2648
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002649 if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
2650 WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
2651 size);
2652 return -EINVAL;
2653 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002654
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002655 page = alloc_page(GFP_KERNEL);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002656 if (!page)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002657 return -ENOMEM;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002658
2659 buf = page_address(page);
2660 buf->len = PAGE_SIZE;
2661
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002662 rqst->rq_buffer = buf->data;
Jeff Layton18e601d2016-10-24 20:33:23 -04002663 rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002664 return 0;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002665}
2666
2667/*
2668 * Free the space allocated in the bc_alloc routine
2669 */
Chuck Lever3435c742016-09-15 10:55:29 -04002670static void bc_free(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002671{
Chuck Lever3435c742016-09-15 10:55:29 -04002672 void *buffer = task->tk_rqstp->rq_buffer;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002673 struct rpc_buffer *buf;
2674
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002675 buf = container_of(buffer, struct rpc_buffer, data);
2676 free_page((unsigned long)buf);
2677}
2678
2679/*
2680 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2681 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2682 */
2683static int bc_sendto(struct rpc_rqst *req)
2684{
2685 int len;
2686 struct xdr_buf *xbufp = &req->rq_snd_buf;
2687 struct rpc_xprt *xprt = req->rq_xprt;
2688 struct sock_xprt *transport =
2689 container_of(xprt, struct sock_xprt, xprt);
2690 struct socket *sock = transport->sock;
2691 unsigned long headoff;
2692 unsigned long tailoff;
2693
Chuck Lever61677ee2011-05-09 15:22:34 -04002694 xs_encode_stream_record_marker(xbufp);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002695
2696 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2697 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2698 len = svc_send_common(sock, xbufp,
2699 virt_to_page(xbufp->head[0].iov_base), headoff,
2700 xbufp->tail[0].iov_base, tailoff);
2701
2702 if (len != xbufp->len) {
2703 printk(KERN_NOTICE "Error sending entire callback!\n");
2704 len = -EAGAIN;
2705 }
2706
2707 return len;
2708}
2709
2710/*
2711 * The send routine. Borrows from svc_send
2712 */
Trond Myklebust50f484e2018-08-30 13:27:29 -04002713static int bc_send_request(struct rpc_rqst *req, struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002714{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002715 struct svc_xprt *xprt;
Andrzej Hajda7fc56132015-09-24 16:00:09 +02002716 int len;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002717
2718 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2719 /*
2720 * Get the server socket associated with this callback xprt
2721 */
2722 xprt = req->rq_xprt->bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002723
2724 /*
2725 * Grab the mutex to serialize data as the connection is shared
2726 * with the fore channel
2727 */
2728 if (!mutex_trylock(&xprt->xpt_mutex)) {
2729 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2730 if (!mutex_trylock(&xprt->xpt_mutex))
2731 return -EAGAIN;
2732 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2733 }
2734 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2735 len = -ENOTCONN;
2736 else
2737 len = bc_sendto(req);
2738 mutex_unlock(&xprt->xpt_mutex);
2739
2740 if (len > 0)
2741 len = 0;
2742
2743 return len;
2744}
2745
2746/*
2747 * The close routine. Since this is client initiated, we do nothing
2748 */
2749
2750static void bc_close(struct rpc_xprt *xprt)
2751{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002752}
2753
2754/*
2755 * The xprt destroy routine. Again, because this connection is client
2756 * initiated, we do nothing
2757 */
2758
2759static void bc_destroy(struct rpc_xprt *xprt)
2760{
Kinglong Mee47f72ef2014-03-24 11:58:16 +08002761 dprintk("RPC: bc_destroy xprt %p\n", xprt);
2762
2763 xs_xprt_free(xprt);
2764 module_put(THIS_MODULE);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002765}
2766
Chuck Leverd31ae252017-08-01 12:00:39 -04002767static const struct rpc_xprt_ops xs_local_ops = {
Chuck Lever176e21e2011-05-09 15:22:44 -04002768 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebust4cd34e72018-08-31 10:00:02 -04002769 .release_xprt = xprt_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002770 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002771 .free_slot = xprt_free_slot,
Chuck Lever176e21e2011-05-09 15:22:44 -04002772 .rpcbind = xs_local_rpcbind,
2773 .set_port = xs_local_set_port,
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002774 .connect = xs_local_connect,
Chuck Lever176e21e2011-05-09 15:22:44 -04002775 .buf_alloc = rpc_malloc,
2776 .buf_free = rpc_free,
2777 .send_request = xs_local_send_request,
2778 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2779 .close = xs_close,
Trond Myklebusta1311d82013-10-31 09:18:49 -04002780 .destroy = xs_destroy,
Chuck Lever176e21e2011-05-09 15:22:44 -04002781 .print_stats = xs_local_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002782 .enable_swap = xs_enable_swap,
2783 .disable_swap = xs_disable_swap,
Chuck Lever176e21e2011-05-09 15:22:44 -04002784};
2785
Chuck Leverd31ae252017-08-01 12:00:39 -04002786static const struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002787 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002788 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002789 .release_xprt = xprt_release_xprt_cong,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002790 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002791 .free_slot = xprt_free_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002792 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002793 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002794 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002795 .buf_alloc = rpc_malloc,
2796 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002797 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002798 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002799 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002800 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002801 .close = xs_close,
2802 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002803 .print_stats = xs_udp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002804 .enable_swap = xs_enable_swap,
2805 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002806 .inject_disconnect = xs_inject_disconnect,
Chuck Lever262965f2005-08-11 16:25:56 -04002807};
2808
Chuck Leverd31ae252017-08-01 12:00:39 -04002809static const struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002810 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebust4cd34e72018-08-31 10:00:02 -04002811 .release_xprt = xprt_release_xprt,
Trond Myklebust36bd7de2018-09-03 18:41:32 -04002812 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002813 .free_slot = xprt_free_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002814 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002815 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002816 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002817 .buf_alloc = rpc_malloc,
2818 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002819 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002820 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustc627d312015-02-10 11:06:04 -05002821 .close = xs_tcp_shutdown,
Chuck Lever9903cd12005-08-11 16:25:26 -04002822 .destroy = xs_destroy,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002823 .set_connect_timeout = xs_tcp_set_connect_timeout,
Chuck Lever262ca072006-03-20 13:44:16 -05002824 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002825 .enable_swap = xs_enable_swap,
2826 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002827 .inject_disconnect = xs_inject_disconnect,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002828#ifdef CONFIG_SUNRPC_BACKCHANNEL
2829 .bc_setup = xprt_setup_bc,
Chuck Lever76566772015-10-24 17:28:32 -04002830 .bc_up = xs_tcp_bc_up,
Chuck Lever6b26cc82016-05-02 14:40:40 -04002831 .bc_maxpayload = xs_tcp_bc_maxpayload,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002832 .bc_free_rqst = xprt_free_bc_rqst,
2833 .bc_destroy = xprt_destroy_bc,
2834#endif
Chuck Levera246b012005-08-11 16:25:23 -04002835};
2836
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002837/*
2838 * The rpc_xprt_ops for the server backchannel
2839 */
2840
Chuck Leverd31ae252017-08-01 12:00:39 -04002841static const struct rpc_xprt_ops bc_tcp_ops = {
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002842 .reserve_xprt = xprt_reserve_xprt,
2843 .release_xprt = xprt_release_xprt,
Bryan Schumaker84e28a32012-09-24 13:39:01 -04002844 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002845 .free_slot = xprt_free_slot,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002846 .buf_alloc = bc_malloc,
2847 .buf_free = bc_free,
2848 .send_request = bc_send_request,
2849 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2850 .close = bc_close,
2851 .destroy = bc_destroy,
2852 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002853 .enable_swap = xs_enable_swap,
2854 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002855 .inject_disconnect = xs_inject_disconnect,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002856};
2857
Chuck Lever92476852010-10-20 11:53:01 -04002858static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2859{
2860 static const struct sockaddr_in sin = {
2861 .sin_family = AF_INET,
2862 .sin_addr.s_addr = htonl(INADDR_ANY),
2863 };
2864 static const struct sockaddr_in6 sin6 = {
2865 .sin6_family = AF_INET6,
2866 .sin6_addr = IN6ADDR_ANY_INIT,
2867 };
2868
2869 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002870 case AF_LOCAL:
2871 break;
Chuck Lever92476852010-10-20 11:53:01 -04002872 case AF_INET:
2873 memcpy(sap, &sin, sizeof(sin));
2874 break;
2875 case AF_INET6:
2876 memcpy(sap, &sin6, sizeof(sin6));
2877 break;
2878 default:
2879 dprintk("RPC: %s: Bad address family\n", __func__);
2880 return -EAFNOSUPPORT;
2881 }
2882 return 0;
2883}
2884
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002885static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002886 unsigned int slot_table_size,
2887 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002888{
2889 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002890 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002891
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002892 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002893 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002894 return ERR_PTR(-EBADF);
2895 }
2896
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002897 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2898 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002899 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002900 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2901 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002902 return ERR_PTR(-ENOMEM);
2903 }
2904
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002905 new = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002906 mutex_init(&new->recv_mutex);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002907 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2908 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002909 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002910 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002911 else {
2912 int err;
2913 err = xs_init_anyaddr(args->dstaddr->sa_family,
2914 (struct sockaddr *)&new->srcaddr);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002915 if (err != 0) {
2916 xprt_free(xprt);
Chuck Lever92476852010-10-20 11:53:01 -04002917 return ERR_PTR(err);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002918 }
Chuck Lever92476852010-10-20 11:53:01 -04002919 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002920
2921 return xprt;
2922}
2923
Chuck Lever176e21e2011-05-09 15:22:44 -04002924static const struct rpc_timeout xs_local_default_timeout = {
2925 .to_initval = 10 * HZ,
2926 .to_maxval = 10 * HZ,
2927 .to_retries = 2,
2928};
2929
2930/**
2931 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2932 * @args: rpc transport creation arguments
2933 *
2934 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2935 */
2936static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2937{
2938 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2939 struct sock_xprt *transport;
2940 struct rpc_xprt *xprt;
2941 struct rpc_xprt *ret;
2942
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002943 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2944 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002945 if (IS_ERR(xprt))
2946 return xprt;
2947 transport = container_of(xprt, struct sock_xprt, xprt);
2948
2949 xprt->prot = 0;
2950 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2951 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2952
2953 xprt->bind_timeout = XS_BIND_TO;
2954 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2955 xprt->idle_timeout = XS_IDLE_DISC_TO;
2956
2957 xprt->ops = &xs_local_ops;
2958 xprt->timeout = &xs_local_default_timeout;
2959
Trond Myklebusta2648092015-10-06 17:03:00 -04002960 INIT_WORK(&transport->recv_worker, xs_local_data_receive_workfn);
NeilBrown93dc41b2013-10-31 16:14:36 +11002961 INIT_DELAYED_WORK(&transport->connect_worker,
2962 xs_dummy_setup_socket);
2963
Chuck Lever176e21e2011-05-09 15:22:44 -04002964 switch (sun->sun_family) {
2965 case AF_LOCAL:
2966 if (sun->sun_path[0] != '/') {
2967 dprintk("RPC: bad AF_LOCAL address: %s\n",
2968 sun->sun_path);
2969 ret = ERR_PTR(-EINVAL);
2970 goto out_err;
2971 }
2972 xprt_set_bound(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002973 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
J. Bruce Fields7073ea82013-02-21 10:14:22 -05002974 ret = ERR_PTR(xs_local_setup_socket(transport));
2975 if (ret)
2976 goto out_err;
Chuck Lever176e21e2011-05-09 15:22:44 -04002977 break;
2978 default:
2979 ret = ERR_PTR(-EAFNOSUPPORT);
2980 goto out_err;
2981 }
2982
2983 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2984 xprt->address_strings[RPC_DISPLAY_ADDR]);
2985
2986 if (try_module_get(THIS_MODULE))
2987 return xprt;
2988 ret = ERR_PTR(-EINVAL);
2989out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002990 xs_xprt_free(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002991 return ret;
2992}
2993
Trond Myklebust2881ae72007-12-20 16:03:54 -05002994static const struct rpc_timeout xs_udp_default_timeout = {
2995 .to_initval = 5 * HZ,
2996 .to_maxval = 30 * HZ,
2997 .to_increment = 5 * HZ,
2998 .to_retries = 5,
2999};
3000
Chuck Lever9903cd12005-08-11 16:25:26 -04003001/**
3002 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02003003 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04003004 *
3005 */
Adrian Bunk483066d2007-10-24 18:24:02 +02003006static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04003007{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003008 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04003009 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05003010 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003011 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04003012
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003013 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
3014 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04003015 if (IS_ERR(xprt))
3016 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05003017 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04003018
Chuck Leverec739ef2006-08-22 20:06:15 -04003019 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07003020 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04003021 /* XXX: header size can vary due to auth type, IPv6, etc. */
3022 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
3023
Chuck Lever03bf4b72005-08-25 16:25:55 -07003024 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07003025 xprt->reestablish_timeout = XS_UDP_REEST_TO;
3026 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04003027
Chuck Lever262965f2005-08-11 16:25:56 -04003028 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04003029
Trond Myklebustba7392b2007-12-20 16:03:55 -05003030 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04003031
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04003032 INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
Trond Myklebustedc1b012015-10-05 10:53:49 -04003033 INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
3034
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003035 switch (addr->sa_family) {
3036 case AF_INET:
3037 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
3038 xprt_set_bound(xprt);
3039
Chuck Lever9dc3b092009-08-09 15:09:46 -04003040 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003041 break;
3042 case AF_INET6:
3043 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
3044 xprt_set_bound(xprt);
3045
Chuck Lever9dc3b092009-08-09 15:09:46 -04003046 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003047 break;
3048 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003049 ret = ERR_PTR(-EAFNOSUPPORT);
3050 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003051 }
3052
Chuck Leverc740eff2009-08-09 15:09:46 -04003053 if (xprt_bound(xprt))
3054 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3055 xprt->address_strings[RPC_DISPLAY_ADDR],
3056 xprt->address_strings[RPC_DISPLAY_PORT],
3057 xprt->address_strings[RPC_DISPLAY_PROTO]);
3058 else
3059 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
3060 xprt->address_strings[RPC_DISPLAY_ADDR],
3061 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04003062
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003063 if (try_module_get(THIS_MODULE))
3064 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003065 ret = ERR_PTR(-EINVAL);
3066out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003067 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003068 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04003069}
3070
Trond Myklebust2881ae72007-12-20 16:03:54 -05003071static const struct rpc_timeout xs_tcp_default_timeout = {
3072 .to_initval = 60 * HZ,
3073 .to_maxval = 60 * HZ,
3074 .to_retries = 2,
3075};
3076
Chuck Lever9903cd12005-08-11 16:25:26 -04003077/**
3078 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02003079 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04003080 *
3081 */
Adrian Bunk483066d2007-10-24 18:24:02 +02003082static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04003083{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003084 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04003085 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05003086 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003087 struct rpc_xprt *ret;
Trond Myklebustb7993ce2013-04-14 11:42:00 -04003088 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
3089
3090 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
3091 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Levera246b012005-08-11 16:25:23 -04003092
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003093 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
Trond Myklebustb7993ce2013-04-14 11:42:00 -04003094 max_slot_table_size);
Chuck Leverc8541ec2006-10-17 14:44:27 -04003095 if (IS_ERR(xprt))
3096 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05003097 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04003098
Chuck Leverec739ef2006-08-22 20:06:15 -04003099 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07003100 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07003101 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04003102
Chuck Lever03bf4b72005-08-25 16:25:55 -07003103 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07003104 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
3105 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04003106
Chuck Lever262965f2005-08-11 16:25:56 -04003107 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05003108 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04003109
Trond Myklebust3851f1c2016-08-04 00:08:45 -04003110 xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05003111 xprt->connect_timeout = xprt->timeout->to_initval *
3112 (xprt->timeout->to_retries + 1);
Trond Myklebust3851f1c2016-08-04 00:08:45 -04003113
Trond Myklebustedc1b012015-10-05 10:53:49 -04003114 INIT_WORK(&transport->recv_worker, xs_tcp_data_receive_workfn);
3115 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
3116
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003117 switch (addr->sa_family) {
3118 case AF_INET:
3119 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
3120 xprt_set_bound(xprt);
3121
Chuck Lever9dc3b092009-08-09 15:09:46 -04003122 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003123 break;
3124 case AF_INET6:
3125 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
3126 xprt_set_bound(xprt);
3127
Chuck Lever9dc3b092009-08-09 15:09:46 -04003128 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003129 break;
3130 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003131 ret = ERR_PTR(-EAFNOSUPPORT);
3132 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003133 }
3134
Chuck Leverc740eff2009-08-09 15:09:46 -04003135 if (xprt_bound(xprt))
3136 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3137 xprt->address_strings[RPC_DISPLAY_ADDR],
3138 xprt->address_strings[RPC_DISPLAY_PORT],
3139 xprt->address_strings[RPC_DISPLAY_PROTO]);
3140 else
3141 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
3142 xprt->address_strings[RPC_DISPLAY_ADDR],
3143 xprt->address_strings[RPC_DISPLAY_PROTO]);
3144
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003145 if (try_module_get(THIS_MODULE))
3146 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003147 ret = ERR_PTR(-EINVAL);
3148out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003149 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003150 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04003151}
Chuck Lever282b32e2006-12-05 16:35:51 -05003152
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003153/**
3154 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3155 * @args: rpc transport creation arguments
3156 *
3157 */
3158static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
3159{
3160 struct sockaddr *addr = args->dstaddr;
3161 struct rpc_xprt *xprt;
3162 struct sock_xprt *transport;
3163 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003164 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003165
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003166 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3167 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003168 if (IS_ERR(xprt))
3169 return xprt;
3170 transport = container_of(xprt, struct sock_xprt, xprt);
3171
3172 xprt->prot = IPPROTO_TCP;
3173 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
3174 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3175 xprt->timeout = &xs_tcp_default_timeout;
3176
3177 /* backchannel */
3178 xprt_set_bound(xprt);
3179 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003180 xprt->reestablish_timeout = 0;
3181 xprt->idle_timeout = 0;
3182
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003183 xprt->ops = &bc_tcp_ops;
3184
3185 switch (addr->sa_family) {
3186 case AF_INET:
3187 xs_format_peer_addresses(xprt, "tcp",
3188 RPCBIND_NETID_TCP);
3189 break;
3190 case AF_INET6:
3191 xs_format_peer_addresses(xprt, "tcp",
3192 RPCBIND_NETID_TCP6);
3193 break;
3194 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003195 ret = ERR_PTR(-EAFNOSUPPORT);
3196 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003197 }
3198
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04003199 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3200 xprt->address_strings[RPC_DISPLAY_ADDR],
3201 xprt->address_strings[RPC_DISPLAY_PORT],
3202 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003203
3204 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003205 * Once we've associated a backchannel xprt with a connection,
Weng Meiling28303ca2013-11-30 17:56:44 +08003206 * we want to keep it around as long as the connection lasts,
3207 * in case we need to start using it for a backchannel again;
3208 * this reference won't be dropped until bc_xprt is destroyed.
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003209 */
3210 xprt_get(xprt);
3211 args->bc_xprt->xpt_bc_xprt = xprt;
3212 xprt->bc_xprt = args->bc_xprt;
3213 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
3214 transport->sock = bc_sock->sk_sock;
3215 transport->inet = bc_sock->sk_sk;
3216
3217 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003218 * Since we don't want connections for the backchannel, we set
3219 * the xprt status to connected
3220 */
3221 xprt_set_connected(xprt);
3222
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003223 if (try_module_get(THIS_MODULE))
3224 return xprt;
Kinglong Mee642aab52014-03-24 12:00:28 +08003225
3226 args->bc_xprt->xpt_bc_xprt = NULL;
J. Bruce Fields39a9bea2016-05-17 12:38:21 -04003227 args->bc_xprt->xpt_bc_xps = NULL;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003228 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003229 ret = ERR_PTR(-EINVAL);
3230out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003231 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003232 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003233}
3234
Chuck Lever176e21e2011-05-09 15:22:44 -04003235static struct xprt_class xs_local_transport = {
3236 .list = LIST_HEAD_INIT(xs_local_transport.list),
3237 .name = "named UNIX socket",
3238 .owner = THIS_MODULE,
3239 .ident = XPRT_TRANSPORT_LOCAL,
3240 .setup = xs_setup_local,
3241};
3242
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003243static struct xprt_class xs_udp_transport = {
3244 .list = LIST_HEAD_INIT(xs_udp_transport.list),
3245 .name = "udp",
3246 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003247 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003248 .setup = xs_setup_udp,
3249};
3250
3251static struct xprt_class xs_tcp_transport = {
3252 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
3253 .name = "tcp",
3254 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003255 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003256 .setup = xs_setup_tcp,
3257};
3258
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003259static struct xprt_class xs_bc_tcp_transport = {
3260 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3261 .name = "tcp NFSv4.1 backchannel",
3262 .owner = THIS_MODULE,
3263 .ident = XPRT_TRANSPORT_BC_TCP,
3264 .setup = xs_setup_bc_tcp,
3265};
3266
Chuck Lever282b32e2006-12-05 16:35:51 -05003267/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003268 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05003269 *
3270 */
3271int init_socket_xprt(void)
3272{
Jeff Laytonf895b252014-11-17 16:58:04 -05003273#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08003274 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08003275 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05003276#endif
3277
Chuck Lever176e21e2011-05-09 15:22:44 -04003278 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003279 xprt_register_transport(&xs_udp_transport);
3280 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003281 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003282
Chuck Lever282b32e2006-12-05 16:35:51 -05003283 return 0;
3284}
3285
3286/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003287 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05003288 *
3289 */
3290void cleanup_socket_xprt(void)
3291{
Jeff Laytonf895b252014-11-17 16:58:04 -05003292#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Leverfbf76682006-12-05 16:35:54 -05003293 if (sunrpc_table_header) {
3294 unregister_sysctl_table(sunrpc_table_header);
3295 sunrpc_table_header = NULL;
3296 }
3297#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003298
Chuck Lever176e21e2011-05-09 15:22:44 -04003299 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003300 xprt_unregister_transport(&xs_udp_transport);
3301 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003302 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05003303}
Trond Myklebustcbf11072009-08-09 15:06:19 -04003304
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003305static int param_set_uint_minmax(const char *val,
3306 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003307 unsigned int min, unsigned int max)
3308{
Daniel Walter00cfaa92014-06-21 13:06:38 +01003309 unsigned int num;
Trond Myklebustcbf11072009-08-09 15:06:19 -04003310 int ret;
3311
3312 if (!val)
3313 return -EINVAL;
Daniel Walter00cfaa92014-06-21 13:06:38 +01003314 ret = kstrtouint(val, 0, &num);
Dan Carpenter9761a242017-02-19 00:34:59 +03003315 if (ret)
3316 return ret;
3317 if (num < min || num > max)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003318 return -EINVAL;
3319 *((unsigned int *)kp->arg) = num;
3320 return 0;
3321}
3322
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003323static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003324{
Frank Sorensonffb6ca32016-07-08 16:35:25 -05003325 if (kp->arg == &xprt_min_resvport)
3326 return param_set_uint_minmax(val, kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003327 RPC_MIN_RESVPORT,
Frank Sorensonffb6ca32016-07-08 16:35:25 -05003328 xprt_max_resvport);
3329 return param_set_uint_minmax(val, kp,
3330 xprt_min_resvport,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003331 RPC_MAX_RESVPORT);
3332}
3333
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303334static const struct kernel_param_ops param_ops_portnr = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003335 .set = param_set_portnr,
3336 .get = param_get_uint,
3337};
3338
Trond Myklebustcbf11072009-08-09 15:06:19 -04003339#define param_check_portnr(name, p) \
3340 __param_check(name, p, unsigned int);
3341
3342module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3343module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3344
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003345static int param_set_slot_table_size(const char *val,
3346 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003347{
3348 return param_set_uint_minmax(val, kp,
3349 RPC_MIN_SLOT_TABLE,
3350 RPC_MAX_SLOT_TABLE);
3351}
3352
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303353static const struct kernel_param_ops param_ops_slot_table_size = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003354 .set = param_set_slot_table_size,
3355 .get = param_get_uint,
3356};
3357
Trond Myklebustcbf11072009-08-09 15:06:19 -04003358#define param_check_slot_table_size(name, p) \
3359 __param_check(name, p, unsigned int);
3360
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003361static int param_set_max_slot_table_size(const char *val,
3362 const struct kernel_param *kp)
3363{
3364 return param_set_uint_minmax(val, kp,
3365 RPC_MIN_SLOT_TABLE,
3366 RPC_MAX_SLOT_TABLE_LIMIT);
3367}
3368
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303369static const struct kernel_param_ops param_ops_max_slot_table_size = {
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003370 .set = param_set_max_slot_table_size,
3371 .get = param_get_uint,
3372};
3373
3374#define param_check_max_slot_table_size(name, p) \
3375 __param_check(name, p, unsigned int);
3376
Trond Myklebustcbf11072009-08-09 15:06:19 -04003377module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3378 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003379module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3380 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003381module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3382 slot_table_size, 0644);