blob: 9e1c5024aba97750f05946a98e38238b9aaa20e6 [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
452 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400453 *
Chuck Levera246b012005-08-11 16:25:23 -0400454 */
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400455static int xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400456{
Chuck Lever262965f2005-08-11 16:25:56 -0400457 struct rpc_rqst *req = task->tk_rqstp;
458 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",
Chuck Lever262965f2005-08-11 16:25:56 -0400464 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
465 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/*
495 * Construct a stream transport record marker in @buf.
496 */
497static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
498{
499 u32 reclen = buf->len - sizeof(rpc_fraghdr);
500 rpc_fraghdr *base = buf->head[0].iov_base;
501 *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
502}
503
Chuck Lever262965f2005-08-11 16:25:56 -0400504/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400505 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
506 * @task: RPC task that manages the state of an RPC request
507 *
508 * Return values:
509 * 0: The request has been sent
510 * EAGAIN: The socket was blocked, please call again later to
511 * complete the request
512 * ENOTCONN: Caller needs to invoke connect logic then call again
513 * other: Some other error occured, the request was not sent
514 */
515static int xs_local_send_request(struct rpc_task *task)
516{
517 struct rpc_rqst *req = task->tk_rqstp;
518 struct rpc_xprt *xprt = req->rq_xprt;
519 struct sock_xprt *transport =
520 container_of(xprt, struct sock_xprt, xprt);
521 struct xdr_buf *xdr = &req->rq_snd_buf;
522 int status;
Jason Baronf279cd02014-09-24 18:08:00 +0000523 int sent = 0;
Chuck Lever176e21e2011-05-09 15:22:44 -0400524
525 xs_encode_stream_record_marker(&req->rq_snd_buf);
526
527 xs_pktdump("packet data:",
528 req->rq_svec->iov_base, req->rq_svec->iov_len);
529
Chuck Lever78215752018-03-05 15:13:07 -0500530 req->rq_xtime = ktime_get();
Jason Baronf279cd02014-09-24 18:08:00 +0000531 status = xs_sendpages(transport->sock, NULL, 0, xdr, req->rq_bytes_sent,
532 true, &sent);
Chuck Lever176e21e2011-05-09 15:22:44 -0400533 dprintk("RPC: %s(%u) = %d\n",
534 __func__, xdr->len - req->rq_bytes_sent, status);
NeilBrown743c69e2015-07-27 10:55:35 +1000535
536 if (status == -EAGAIN && sock_writeable(transport->inet))
537 status = -ENOBUFS;
538
Jason Baronf279cd02014-09-24 18:08:00 +0000539 if (likely(sent > 0) || status == 0) {
540 req->rq_bytes_sent += sent;
541 req->rq_xmit_bytes_sent += sent;
Chuck Lever176e21e2011-05-09 15:22:44 -0400542 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
543 req->rq_bytes_sent = 0;
544 return 0;
545 }
546 status = -EAGAIN;
547 }
548
549 switch (status) {
Trond Myklebust3601c4a2014-06-30 13:42:19 -0400550 case -ENOBUFS:
Trond Myklebustb5872f02015-07-03 09:32:23 -0400551 break;
Chuck Lever176e21e2011-05-09 15:22:44 -0400552 case -EAGAIN:
553 status = xs_nospace(task);
554 break;
555 default:
556 dprintk("RPC: sendmsg returned unrecognized error %d\n",
557 -status);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -0500558 /* fall through */
Chuck Lever176e21e2011-05-09 15:22:44 -0400559 case -EPIPE:
560 xs_close(xprt);
561 status = -ENOTCONN;
562 }
563
564 return status;
565}
566
567/**
Chuck Lever262965f2005-08-11 16:25:56 -0400568 * xs_udp_send_request - write an RPC request to a UDP socket
569 * @task: address of RPC task that manages the state of an RPC request
570 *
571 * Return values:
572 * 0: The request has been sent
573 * EAGAIN: The socket was blocked, please call again later to
574 * complete the request
575 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300576 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -0400577 */
578static int xs_udp_send_request(struct rpc_task *task)
579{
580 struct rpc_rqst *req = task->tk_rqstp;
581 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500582 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400583 struct xdr_buf *xdr = &req->rq_snd_buf;
Jason Baronf279cd02014-09-24 18:08:00 +0000584 int sent = 0;
Chuck Lever262965f2005-08-11 16:25:56 -0400585 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400586
Chuck Lever9903cd12005-08-11 16:25:26 -0400587 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400588 req->rq_svec->iov_base,
589 req->rq_svec->iov_len);
590
Trond Myklebust01d37c42009-03-11 14:09:39 -0400591 if (!xprt_bound(xprt))
592 return -ENOTCONN;
Chuck Lever78215752018-03-05 15:13:07 -0500593 req->rq_xtime = ktime_get();
Jason Baronf279cd02014-09-24 18:08:00 +0000594 status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen,
595 xdr, req->rq_bytes_sent, true, &sent);
Chuck Levera246b012005-08-11 16:25:23 -0400596
Chuck Lever46121cf2007-01-31 12:14:08 -0500597 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400598 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400599
Jason Baron3dedbb52014-09-24 18:08:04 +0000600 /* firewall is blocking us, don't return -EAGAIN or we end up looping */
601 if (status == -EPERM)
602 goto process_status;
603
NeilBrown743c69e2015-07-27 10:55:35 +1000604 if (status == -EAGAIN && sock_writeable(transport->inet))
605 status = -ENOBUFS;
606
Jason Baronf279cd02014-09-24 18:08:00 +0000607 if (sent > 0 || status == 0) {
608 req->rq_xmit_bytes_sent += sent;
609 if (sent >= req->rq_slen)
Trond Myklebust21997002007-10-01 11:43:37 -0400610 return 0;
611 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400612 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400613 }
Chuck Levera246b012005-08-11 16:25:23 -0400614
Jason Baron3dedbb52014-09-24 18:08:04 +0000615process_status:
Chuck Lever262965f2005-08-11 16:25:56 -0400616 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400617 case -ENOTSOCK:
618 status = -ENOTCONN;
619 /* Should we call xs_close() here? */
620 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400621 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400622 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400623 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400624 case -ENETUNREACH:
Trond Myklebust3601c4a2014-06-30 13:42:19 -0400625 case -ENOBUFS:
Chuck Lever262965f2005-08-11 16:25:56 -0400626 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400627 case -ECONNREFUSED:
Jason Baron3dedbb52014-09-24 18:08:04 +0000628 case -EPERM:
Chuck Levera246b012005-08-11 16:25:23 -0400629 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400630 * prompts ECONNREFUSED. */
Trond Myklebust13331a52016-01-06 08:57:06 -0500631 break;
632 default:
633 dprintk("RPC: sendmsg returned unrecognized error %d\n",
634 -status);
Chuck Levera246b012005-08-11 16:25:23 -0400635 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800636
Chuck Lever262965f2005-08-11 16:25:56 -0400637 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400638}
639
Trond Myklebuste06799f2007-11-05 15:44:12 -0500640/**
Chuck Lever262965f2005-08-11 16:25:56 -0400641 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400642 * @task: address of RPC task that manages the state of an RPC request
643 *
644 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400645 * 0: The request has been sent
646 * EAGAIN: The socket was blocked, please call again later to
647 * complete the request
648 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300649 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400650 *
651 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400652 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400653 */
Chuck Lever262965f2005-08-11 16:25:56 -0400654static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400655{
656 struct rpc_rqst *req = task->tk_rqstp;
657 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500658 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400659 struct xdr_buf *xdr = &req->rq_snd_buf;
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500660 bool zerocopy = true;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -0400661 bool vm_wait = false;
Chuck Leverb595bb12007-08-06 11:56:42 -0400662 int status;
Jason Baronf279cd02014-09-24 18:08:00 +0000663 int sent;
Chuck Levera246b012005-08-11 16:25:23 -0400664
Chuck Lever61677ee2011-05-09 15:22:34 -0400665 xs_encode_stream_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400666
Chuck Lever262965f2005-08-11 16:25:56 -0400667 xs_pktdump("packet data:",
668 req->rq_svec->iov_base,
669 req->rq_svec->iov_len);
Trond Myklebusta6b31d12013-11-08 16:03:50 -0500670 /* Don't use zero copy if this is a resend. If the RPC call
671 * completes while the socket holds a reference to the pages,
672 * then we may end up resending corrupted data.
673 */
674 if (task->tk_flags & RPC_TASK_SENT)
675 zerocopy = false;
Chuck Levera246b012005-08-11 16:25:23 -0400676
Trond Myklebust7196dbb2017-02-08 11:17:54 -0500677 if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
678 xs_tcp_set_socket_timeouts(xprt, transport->sock);
679
Chuck Levera246b012005-08-11 16:25:23 -0400680 /* Continue transmitting the packet/record. We must be careful
681 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400682 * called sendmsg(). */
Chuck Lever78215752018-03-05 15:13:07 -0500683 req->rq_xtime = ktime_get();
Chuck Levera246b012005-08-11 16:25:23 -0400684 while (1) {
Jason Baronf279cd02014-09-24 18:08:00 +0000685 sent = 0;
686 status = xs_sendpages(transport->sock, NULL, 0, xdr,
687 req->rq_bytes_sent, zerocopy, &sent);
Chuck Levera246b012005-08-11 16:25:23 -0400688
Chuck Lever46121cf2007-01-31 12:14:08 -0500689 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400690 xdr->len - req->rq_bytes_sent, status);
691
Chuck Lever262965f2005-08-11 16:25:56 -0400692 /* If we've sent the entire packet, immediately
693 * reset the count of bytes sent. */
Jason Baronf279cd02014-09-24 18:08:00 +0000694 req->rq_bytes_sent += sent;
695 req->rq_xmit_bytes_sent += sent;
Chuck Lever262965f2005-08-11 16:25:56 -0400696 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
697 req->rq_bytes_sent = 0;
698 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400699 }
700
Trond Myklebust9ffadfb2016-05-29 00:42:03 -0400701 WARN_ON_ONCE(sent == 0 && status == 0);
702
703 if (status == -EAGAIN ) {
704 /*
705 * Return EAGAIN if we're sure we're hitting the
706 * socket send buffer limits.
707 */
708 if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
709 break;
710 /*
711 * Did we hit a memory allocation failure?
712 */
713 if (sent == 0) {
714 status = -ENOBUFS;
715 if (vm_wait)
716 break;
717 /* Retry, knowing now that we're below the
718 * socket send buffer limit
719 */
720 vm_wait = true;
721 }
722 continue;
723 }
Trond Myklebustf580dd02015-07-11 17:48:52 +0200724 if (status < 0)
725 break;
Trond Myklebust9ffadfb2016-05-29 00:42:03 -0400726 vm_wait = false;
Chuck Levera246b012005-08-11 16:25:23 -0400727 }
728
Chuck Lever262965f2005-08-11 16:25:56 -0400729 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400730 case -ENOTSOCK:
731 status = -ENOTCONN;
732 /* Should we call xs_close() here? */
733 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400734 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400735 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400736 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400737 case -ECONNRESET:
Chuck Lever262965f2005-08-11 16:25:56 -0400738 case -ECONNREFUSED:
739 case -ENOTCONN:
Trond Myklebust3913c782015-02-08 21:44:04 -0500740 case -EADDRINUSE:
Trond Myklebustb5872f02015-07-03 09:32:23 -0400741 case -ENOBUFS:
Trond Myklebustb9d2bb22012-10-23 11:40:02 -0400742 case -EPIPE:
Trond Myklebust13331a52016-01-06 08:57:06 -0500743 break;
744 default:
745 dprintk("RPC: sendmsg returned unrecognized error %d\n",
746 -status);
Chuck Levera246b012005-08-11 16:25:23 -0400747 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800748
Chuck Levera246b012005-08-11 16:25:23 -0400749 return status;
750}
751
Chuck Lever9903cd12005-08-11 16:25:26 -0400752/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400753 * xs_tcp_release_xprt - clean up after a tcp transmission
754 * @xprt: transport
755 * @task: rpc task
756 *
757 * This cleans up if an error causes us to abort the transmission of a request.
758 * In this case, the socket may need to be reset in order to avoid confusing
759 * the server.
760 */
761static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
762{
763 struct rpc_rqst *req;
764
765 if (task != xprt->snd_task)
766 return;
767 if (task == NULL)
768 goto out_release;
769 req = task->tk_rqstp;
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400770 if (req == NULL)
771 goto out_release;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400772 if (req->rq_bytes_sent == 0)
773 goto out_release;
774 if (req->rq_bytes_sent == req->rq_snd_buf.len)
775 goto out_release;
Trond Myklebusta4f08352013-01-08 09:10:21 -0500776 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400777out_release:
778 xprt_release_xprt(xprt, task);
779}
780
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400781static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
782{
783 transport->old_data_ready = sk->sk_data_ready;
784 transport->old_state_change = sk->sk_state_change;
785 transport->old_write_space = sk->sk_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -0500786 transport->old_error_report = sk->sk_error_report;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400787}
788
789static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
790{
791 sk->sk_data_ready = transport->old_data_ready;
792 sk->sk_state_change = transport->old_state_change;
793 sk->sk_write_space = transport->old_write_space;
Trond Myklebust21180712013-12-31 13:22:59 -0500794 sk->sk_error_report = transport->old_error_report;
795}
796
Trond Myklebust42d42a52016-05-23 09:24:55 -0400797static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
798{
799 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
800
801 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
802}
803
Trond Myklebustb70ae912015-02-09 09:41:32 -0500804static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
805{
806 smp_mb__before_atomic();
807 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
808 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust42d42a52016-05-23 09:24:55 -0400809 xs_sock_reset_state_flags(xprt);
Trond Myklebustb70ae912015-02-09 09:41:32 -0500810 smp_mb__after_atomic();
811}
812
Trond Myklebust21180712013-12-31 13:22:59 -0500813/**
814 * xs_error_report - callback to handle TCP socket state errors
815 * @sk: socket
816 *
817 * Note: we don't call sock_error() since there may be a rpc_task
818 * using the socket, and so we don't want to clear sk->sk_err.
819 */
820static void xs_error_report(struct sock *sk)
821{
822 struct rpc_xprt *xprt;
823 int err;
824
825 read_lock_bh(&sk->sk_callback_lock);
826 if (!(xprt = xprt_from_sock(sk)))
827 goto out;
828
829 err = -sk->sk_err;
830 if (err == 0)
831 goto out;
832 dprintk("RPC: xs_error_report client %p, error=%d...\n",
833 xprt, -err);
Trond Myklebuste8353c762013-12-31 13:39:22 -0500834 trace_rpc_socket_error(xprt, sk->sk_socket, err);
Trond Myklebust21180712013-12-31 13:22:59 -0500835 xprt_wake_pending_tasks(xprt, err);
836 out:
837 read_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400838}
839
Chuck Leverfe315e72009-03-11 14:10:21 -0400840static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400841{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500842 struct socket *sock = transport->sock;
843 struct sock *sk = transport->inet;
Trond Myklebust6cc7e902015-02-08 18:35:25 -0500844 struct rpc_xprt *xprt = &transport->xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400845
Chuck Leverfe315e72009-03-11 14:10:21 -0400846 if (sk == NULL)
847 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400848
Jeff Layton264d1df2015-06-03 16:14:27 -0400849 if (atomic_read(&transport->xprt.swapper))
850 sk_clear_memalloc(sk);
851
Trond Myklebust09939202015-08-29 19:11:21 -0700852 kernel_sock_shutdown(sock, SHUT_RDWR);
853
Trond Myklebustedc1b012015-10-05 10:53:49 -0400854 mutex_lock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -0400855 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500856 transport->inet = NULL;
857 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400858
Chuck Lever9903cd12005-08-11 16:25:26 -0400859 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400860
861 xs_restore_old_callbacks(transport, sk);
Trond Myklebust0c787892015-08-29 13:36:30 -0700862 xprt_clear_connected(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400863 write_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust6cc7e902015-02-08 18:35:25 -0500864 xs_sock_reset_connection_flags(xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -0400865 mutex_unlock(&transport->recv_mutex);
Chuck Levera246b012005-08-11 16:25:23 -0400866
Trond Myklebust6cc7e902015-02-08 18:35:25 -0500867 trace_rpc_socket_close(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -0400868 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -0400869}
870
871/**
872 * xs_close - close a socket
873 * @xprt: transport
874 *
875 * This is used when all requests are complete; ie, no DRC state remains
876 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400877 *
878 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
879 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e72009-03-11 14:10:21 -0400880 */
881static void xs_close(struct rpc_xprt *xprt)
882{
883 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
884
885 dprintk("RPC: xs_close xprt %p\n", xprt);
886
887 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -0400888 xprt->reestablish_timeout = 0;
Chuck Leverfe315e72009-03-11 14:10:21 -0400889
Trond Myklebust62da3b22007-11-06 18:44:20 -0500890 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400891}
892
Chuck Lever4a068252015-05-11 14:02:25 -0400893static void xs_inject_disconnect(struct rpc_xprt *xprt)
894{
895 dprintk("RPC: injecting transport disconnect on xprt=%p\n",
896 xprt);
897 xprt_disconnect_done(xprt);
898}
899
Kinglong Mee315f3812014-03-24 11:07:22 +0800900static void xs_xprt_free(struct rpc_xprt *xprt)
901{
902 xs_free_peer_addresses(xprt);
903 xprt_free(xprt);
904}
905
Chuck Lever9903cd12005-08-11 16:25:26 -0400906/**
907 * xs_destroy - prepare to shutdown a transport
908 * @xprt: doomed transport
909 *
910 */
911static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400912{
Trond Myklebust03c788272015-09-17 10:42:27 -0400913 struct sock_xprt *transport = container_of(xprt,
914 struct sock_xprt, xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -0500915 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400916
Trond Myklebust03c788272015-09-17 10:42:27 -0400917 cancel_delayed_work_sync(&transport->connect_worker);
Trond Myklebusta1311d82013-10-31 09:18:49 -0400918 xs_close(xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -0400919 cancel_work_sync(&transport->recv_worker);
Kinglong Mee315f3812014-03-24 11:07:22 +0800920 xs_xprt_free(xprt);
Trond Myklebusta1311d82013-10-31 09:18:49 -0400921 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400922}
923
Chuck Lever176e21e2011-05-09 15:22:44 -0400924static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
925{
926 struct xdr_skb_reader desc = {
927 .skb = skb,
928 .offset = sizeof(rpc_fraghdr),
929 .count = skb->len - sizeof(rpc_fraghdr),
930 };
931
932 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
933 return -1;
934 if (desc.count)
935 return -1;
936 return 0;
937}
938
939/**
Trond Myklebusta2648092015-10-06 17:03:00 -0400940 * xs_local_data_read_skb
941 * @xprt: transport
942 * @sk: socket
943 * @skb: skbuff
Chuck Lever176e21e2011-05-09 15:22:44 -0400944 *
945 * Currently this assumes we can read the whole reply in a single gulp.
946 */
Trond Myklebusta2648092015-10-06 17:03:00 -0400947static void xs_local_data_read_skb(struct rpc_xprt *xprt,
948 struct sock *sk,
949 struct sk_buff *skb)
Chuck Lever176e21e2011-05-09 15:22:44 -0400950{
951 struct rpc_task *task;
Chuck Lever176e21e2011-05-09 15:22:44 -0400952 struct rpc_rqst *rovr;
Trond Myklebusta2648092015-10-06 17:03:00 -0400953 int repsize, copied;
Chuck Lever176e21e2011-05-09 15:22:44 -0400954 u32 _xid;
955 __be32 *xp;
956
Chuck Lever176e21e2011-05-09 15:22:44 -0400957 repsize = skb->len - sizeof(rpc_fraghdr);
958 if (repsize < 4) {
959 dprintk("RPC: impossible RPC reply size %d\n", repsize);
Trond Myklebusta2648092015-10-06 17:03:00 -0400960 return;
Chuck Lever176e21e2011-05-09 15:22:44 -0400961 }
962
963 /* Copy the XID from the skb... */
964 xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
965 if (xp == NULL)
Trond Myklebusta2648092015-10-06 17:03:00 -0400966 return;
Chuck Lever176e21e2011-05-09 15:22:44 -0400967
968 /* Look up and lock the request corresponding to the given XID */
Trond Myklebustce7c2522017-08-16 15:30:35 -0400969 spin_lock(&xprt->recv_lock);
Chuck Lever176e21e2011-05-09 15:22:44 -0400970 rovr = xprt_lookup_rqst(xprt, *xp);
971 if (!rovr)
972 goto out_unlock;
Trond Myklebust729749b2017-08-13 10:03:59 -0400973 xprt_pin_rqst(rovr);
Trond Myklebustce7c2522017-08-16 15:30:35 -0400974 spin_unlock(&xprt->recv_lock);
Chuck Lever176e21e2011-05-09 15:22:44 -0400975 task = rovr->rq_task;
976
977 copied = rovr->rq_private_buf.buflen;
978 if (copied > repsize)
979 copied = repsize;
980
981 if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
982 dprintk("RPC: sk_buff copy failed\n");
Trond Myklebustce7c2522017-08-16 15:30:35 -0400983 spin_lock(&xprt->recv_lock);
Trond Myklebust729749b2017-08-13 10:03:59 -0400984 goto out_unpin;
Chuck Lever176e21e2011-05-09 15:22:44 -0400985 }
986
Trond Myklebustce7c2522017-08-16 15:30:35 -0400987 spin_lock(&xprt->recv_lock);
Chuck Lever176e21e2011-05-09 15:22:44 -0400988 xprt_complete_rqst(task, copied);
Trond Myklebust729749b2017-08-13 10:03:59 -0400989out_unpin:
990 xprt_unpin_rqst(rovr);
Chuck Lever176e21e2011-05-09 15:22:44 -0400991 out_unlock:
Trond Myklebustce7c2522017-08-16 15:30:35 -0400992 spin_unlock(&xprt->recv_lock);
Trond Myklebusta2648092015-10-06 17:03:00 -0400993}
994
995static void xs_local_data_receive(struct sock_xprt *transport)
996{
997 struct sk_buff *skb;
998 struct sock *sk;
999 int err;
1000
Trond Myklebust0af34422018-01-14 15:28:29 -05001001restart:
Trond Myklebusta2648092015-10-06 17:03:00 -04001002 mutex_lock(&transport->recv_mutex);
1003 sk = transport->inet;
1004 if (sk == NULL)
1005 goto out;
1006 for (;;) {
1007 skb = skb_recv_datagram(sk, 0, 1, &err);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001008 if (skb != NULL) {
1009 xs_local_data_read_skb(&transport->xprt, sk, skb);
1010 skb_free_datagram(sk, skb);
1011 continue;
1012 }
1013 if (!test_and_clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebusta2648092015-10-06 17:03:00 -04001014 break;
Trond Myklebust0af34422018-01-14 15:28:29 -05001015 if (need_resched()) {
1016 mutex_unlock(&transport->recv_mutex);
1017 cond_resched();
1018 goto restart;
1019 }
Trond Myklebusta2648092015-10-06 17:03:00 -04001020 }
1021out:
1022 mutex_unlock(&transport->recv_mutex);
1023}
1024
1025static void xs_local_data_receive_workfn(struct work_struct *work)
1026{
1027 struct sock_xprt *transport =
1028 container_of(work, struct sock_xprt, recv_worker);
1029 xs_local_data_receive(transport);
Chuck Lever176e21e2011-05-09 15:22:44 -04001030}
1031
Chuck Lever9903cd12005-08-11 16:25:26 -04001032/**
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001033 * xs_udp_data_read_skb - receive callback for UDP sockets
1034 * @xprt: transport
1035 * @sk: socket
1036 * @skb: skbuff
Chuck Lever9903cd12005-08-11 16:25:26 -04001037 *
1038 */
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001039static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1040 struct sock *sk,
1041 struct sk_buff *skb)
Chuck Lever9903cd12005-08-11 16:25:26 -04001042{
1043 struct rpc_task *task;
Chuck Levera246b012005-08-11 16:25:23 -04001044 struct rpc_rqst *rovr;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001045 int repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001046 u32 _xid;
1047 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -04001048
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001049 repsize = skb->len;
Chuck Levera246b012005-08-11 16:25:23 -04001050 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001051 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001052 return;
Chuck Levera246b012005-08-11 16:25:23 -04001053 }
1054
1055 /* Copy the XID from the skb... */
Willem de Bruijn1da8c6812016-04-07 11:44:58 -04001056 xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
Chuck Levera246b012005-08-11 16:25:23 -04001057 if (xp == NULL)
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001058 return;
Chuck Levera246b012005-08-11 16:25:23 -04001059
1060 /* Look up and lock the request corresponding to the given XID */
Trond Myklebustce7c2522017-08-16 15:30:35 -04001061 spin_lock(&xprt->recv_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001062 rovr = xprt_lookup_rqst(xprt, *xp);
1063 if (!rovr)
1064 goto out_unlock;
Trond Myklebust729749b2017-08-13 10:03:59 -04001065 xprt_pin_rqst(rovr);
Chuck Leverecd465e2018-03-05 15:12:57 -05001066 xprt_update_rtt(rovr->rq_task);
Trond Myklebustce7c2522017-08-16 15:30:35 -04001067 spin_unlock(&xprt->recv_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001068 task = rovr->rq_task;
1069
Chuck Levera246b012005-08-11 16:25:23 -04001070 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1071 copied = repsize;
1072
1073 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001074 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
Trond Myklebustce7c2522017-08-16 15:30:35 -04001075 spin_lock(&xprt->recv_lock);
Trond Myklebust0afa6b42018-02-09 09:39:42 -05001076 __UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
Trond Myklebust729749b2017-08-13 10:03:59 -04001077 goto out_unpin;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001078 }
1079
Chuck Levera246b012005-08-11 16:25:23 -04001080
Trond Myklebust729749b2017-08-13 10:03:59 -04001081 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001082 xprt_adjust_cwnd(xprt, task, copied);
Trond Myklebustce7c2522017-08-16 15:30:35 -04001083 spin_unlock_bh(&xprt->transport_lock);
1084 spin_lock(&xprt->recv_lock);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001085 xprt_complete_rqst(task, copied);
Trond Myklebust0afa6b42018-02-09 09:39:42 -05001086 __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
Trond Myklebust729749b2017-08-13 10:03:59 -04001087out_unpin:
1088 xprt_unpin_rqst(rovr);
Chuck Levera246b012005-08-11 16:25:23 -04001089 out_unlock:
Trond Myklebustce7c2522017-08-16 15:30:35 -04001090 spin_unlock(&xprt->recv_lock);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001091}
1092
1093static void xs_udp_data_receive(struct sock_xprt *transport)
1094{
1095 struct sk_buff *skb;
1096 struct sock *sk;
1097 int err;
1098
Trond Myklebust0af34422018-01-14 15:28:29 -05001099restart:
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001100 mutex_lock(&transport->recv_mutex);
1101 sk = transport->inet;
1102 if (sk == NULL)
1103 goto out;
1104 for (;;) {
Paolo Abeni7c13f972016-11-04 11:28:59 +01001105 skb = skb_recv_udp(sk, 0, 1, &err);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001106 if (skb != NULL) {
1107 xs_udp_data_read_skb(&transport->xprt, sk, skb);
Paolo Abeni850cbad2016-10-21 13:55:47 +02001108 consume_skb(skb);
Trond Myklebust42d42a52016-05-23 09:24:55 -04001109 continue;
1110 }
1111 if (!test_and_clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001112 break;
Trond Myklebust0af34422018-01-14 15:28:29 -05001113 if (need_resched()) {
1114 mutex_unlock(&transport->recv_mutex);
1115 cond_resched();
1116 goto restart;
1117 }
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001118 }
1119out:
1120 mutex_unlock(&transport->recv_mutex);
1121}
1122
1123static void xs_udp_data_receive_workfn(struct work_struct *work)
1124{
1125 struct sock_xprt *transport =
1126 container_of(work, struct sock_xprt, recv_worker);
1127 xs_udp_data_receive(transport);
1128}
1129
1130/**
1131 * xs_data_ready - "data ready" callback for UDP sockets
1132 * @sk: socket with data to read
1133 *
1134 */
1135static void xs_data_ready(struct sock *sk)
1136{
1137 struct rpc_xprt *xprt;
1138
1139 read_lock_bh(&sk->sk_callback_lock);
1140 dprintk("RPC: xs_data_ready...\n");
1141 xprt = xprt_from_sock(sk);
1142 if (xprt != NULL) {
1143 struct sock_xprt *transport = container_of(xprt,
1144 struct sock_xprt, xprt);
Trond Myklebust5157b952016-05-29 10:13:24 -04001145 transport->old_data_ready(sk);
1146 /* Any data means we had a useful conversation, so
1147 * then we don't need to delay the next reconnect
1148 */
1149 if (xprt->reestablish_timeout)
1150 xprt->reestablish_timeout = 0;
Trond Myklebust42d42a52016-05-23 09:24:55 -04001151 if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
Trond Myklebust40a5f1b2016-05-27 10:39:50 -04001152 queue_work(xprtiod_workqueue, &transport->recv_worker);
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04001153 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001154 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001155}
1156
Trond Myklebusta519fc72012-09-12 16:49:15 -04001157/*
1158 * Helper function to force a TCP close if the server is sending
1159 * junk and/or it has put us in CLOSE_WAIT
1160 */
1161static void xs_tcp_force_close(struct rpc_xprt *xprt)
1162{
Trond Myklebusta519fc72012-09-12 16:49:15 -04001163 xprt_force_disconnect(xprt);
1164}
1165
Chuck Leverdd456472006-12-05 16:35:44 -05001166static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001167{
Chuck Lever51971132006-12-05 16:35:19 -05001168 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001169 size_t len, used;
1170 char *p;
1171
Chuck Lever51971132006-12-05 16:35:19 -05001172 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
1173 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001174 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001175 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001176 if (used != len)
1177 return;
Chuck Lever808012f2005-08-25 16:25:49 -07001178
Chuck Lever51971132006-12-05 16:35:19 -05001179 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
1180 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -05001181 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -04001182 else
Chuck Levere136d092006-12-05 16:35:23 -05001183 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -05001184 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -07001185
Chuck Levere136d092006-12-05 16:35:23 -05001186 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001187 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -07001188
Chuck Levera246b012005-08-11 16:25:23 -04001189 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001190 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001191 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebusta519fc72012-09-12 16:49:15 -04001192 xs_tcp_force_close(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001193 return;
Chuck Levera246b012005-08-11 16:25:23 -04001194 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001195 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -05001196 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001197}
1198
Chuck Lever51971132006-12-05 16:35:19 -05001199static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001200{
Chuck Lever51971132006-12-05 16:35:19 -05001201 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001202 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001203 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001204 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
1205 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1206 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001207 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001208 }
1209 }
1210}
1211
Chuck Leverdd456472006-12-05 16:35:44 -05001212static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001213{
1214 size_t len, used;
1215 char *p;
1216
Chuck Lever51971132006-12-05 16:35:19 -05001217 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001218 dprintk("RPC: reading XID (%zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001219 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001220 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001221 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001222 if (used != len)
1223 return;
Chuck Levere136d092006-12-05 16:35:23 -05001224 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001225 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -05001226 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001227 dprintk("RPC: reading %s XID %08x\n",
1228 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1229 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -05001230 ntohl(transport->tcp_xid));
1231 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001232}
1233
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001234static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1235 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001236{
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001237 size_t len, used;
1238 u32 offset;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001239 char *p;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001240
1241 /*
1242 * We want transport->tcp_offset to be 8 at the end of this routine
1243 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1244 * When this function is called for the first time,
1245 * transport->tcp_offset is 4 (after having already read the xid).
1246 */
1247 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001248 len = sizeof(transport->tcp_calldir) - offset;
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001249 dprintk("RPC: reading CALL/REPLY flag (%zu bytes)\n", len);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001250 p = ((char *) &transport->tcp_calldir) + offset;
1251 used = xdr_skb_read_bits(desc, p, len);
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001252 transport->tcp_offset += used;
1253 if (used != len)
1254 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001255 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001256 /*
1257 * We don't yet have the XDR buffer, so we will write the calldir
1258 * out after we get the buffer from the 'struct rpc_rqst'
1259 */
Trond Myklebustb76ce562010-06-16 13:57:32 -04001260 switch (ntohl(transport->tcp_calldir)) {
1261 case RPC_REPLY:
1262 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1263 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001264 transport->tcp_flags |= TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001265 break;
1266 case RPC_CALL:
1267 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1268 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001269 transport->tcp_flags &= ~TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001270 break;
1271 default:
1272 dprintk("RPC: invalid request message type\n");
Trond Myklebusta519fc72012-09-12 16:49:15 -04001273 xs_tcp_force_close(&transport->xprt);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001274 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001275 xs_tcp_check_fraghdr(transport);
1276}
1277
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001278static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
1279 struct xdr_skb_reader *desc,
1280 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001281{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001282 struct sock_xprt *transport =
1283 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001284 struct xdr_buf *rcvbuf;
1285 size_t len;
1286 ssize_t r;
1287
Chuck Levera246b012005-08-11 16:25:23 -04001288 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001289
1290 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1291 /*
1292 * Save the RPC direction in the XDR buffer
1293 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001294 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
Trond Myklebustb76ce562010-06-16 13:57:32 -04001295 &transport->tcp_calldir,
1296 sizeof(transport->tcp_calldir));
1297 transport->tcp_copied += sizeof(transport->tcp_calldir);
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001298 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -04001299 }
1300
Chuck Levera246b012005-08-11 16:25:23 -04001301 len = desc->count;
Trond Myklebust040249d2017-08-13 02:13:45 -04001302 if (len > transport->tcp_reclen - transport->tcp_offset)
1303 desc->count = transport->tcp_reclen - transport->tcp_offset;
1304 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001305 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001306
Trond Myklebust040249d2017-08-13 02:13:45 -04001307 if (desc->count) {
Chuck Levera246b012005-08-11 16:25:23 -04001308 /* Error when copying to the receive buffer,
1309 * usually because we weren't able to allocate
1310 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001311 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001312 * will not receive any additional updates,
1313 * and time out.
1314 * Any remaining data from this record will
1315 * be discarded.
1316 */
Chuck Levere136d092006-12-05 16:35:23 -05001317 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001318 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001319 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001320 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1321 "tcp_offset = %u, tcp_reclen = %u\n",
1322 xprt, transport->tcp_copied,
1323 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001324 return;
Chuck Levera246b012005-08-11 16:25:23 -04001325 }
1326
Trond Myklebust040249d2017-08-13 02:13:45 -04001327 transport->tcp_copied += r;
1328 transport->tcp_offset += r;
1329 desc->count = len - r;
1330
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001331 dprintk("RPC: XID %08x read %zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001332 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001333 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1334 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1335 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001336
Chuck Lever51971132006-12-05 16:35:19 -05001337 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001338 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001339 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001340 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1341 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001342 }
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001343}
1344
1345/*
1346 * Finds the request corresponding to the RPC xid and invokes the common
1347 * tcp read code to read the data.
1348 */
1349static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1350 struct xdr_skb_reader *desc)
1351{
1352 struct sock_xprt *transport =
1353 container_of(xprt, struct sock_xprt, xprt);
1354 struct rpc_rqst *req;
1355
1356 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1357
1358 /* Find and lock the request corresponding to this xid */
Trond Myklebustce7c2522017-08-16 15:30:35 -04001359 spin_lock(&xprt->recv_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001360 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1361 if (!req) {
1362 dprintk("RPC: XID %08x request not found!\n",
1363 ntohl(transport->tcp_xid));
Trond Myklebustce7c2522017-08-16 15:30:35 -04001364 spin_unlock(&xprt->recv_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001365 return -1;
1366 }
Trond Myklebust729749b2017-08-13 10:03:59 -04001367 xprt_pin_rqst(req);
Trond Myklebustce7c2522017-08-16 15:30:35 -04001368 spin_unlock(&xprt->recv_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001369
1370 xs_tcp_read_common(xprt, desc, req);
1371
Trond Myklebustce7c2522017-08-16 15:30:35 -04001372 spin_lock(&xprt->recv_lock);
Chuck Levere136d092006-12-05 16:35:23 -05001373 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001374 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Trond Myklebust729749b2017-08-13 10:03:59 -04001375 xprt_unpin_rqst(req);
Trond Myklebustce7c2522017-08-16 15:30:35 -04001376 spin_unlock(&xprt->recv_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001377 return 0;
1378}
1379
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001380#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001381/*
1382 * Obtains an rpc_rqst previously allocated and invokes the common
1383 * tcp read code to read the data. The result is placed in the callback
1384 * queue.
1385 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1386 * connection and return -1.
1387 */
Trond Myklebust2ea24492014-02-10 11:18:39 -05001388static int xs_tcp_read_callback(struct rpc_xprt *xprt,
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001389 struct xdr_skb_reader *desc)
1390{
1391 struct sock_xprt *transport =
1392 container_of(xprt, struct sock_xprt, xprt);
1393 struct rpc_rqst *req;
1394
Trond Myklebustc89091c2017-08-16 12:09:44 -04001395 /* Look up the request corresponding to the given XID */
Trond Myklebust2ea24492014-02-10 11:18:39 -05001396 req = xprt_lookup_bc_request(xprt, transport->tcp_xid);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001397 if (req == NULL) {
1398 printk(KERN_WARNING "Callback slot table overflowed\n");
1399 xprt_force_disconnect(xprt);
1400 return -1;
1401 }
1402
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001403 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1404 xs_tcp_read_common(xprt, desc, req);
1405
Trond Myklebust2ea24492014-02-10 11:18:39 -05001406 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
1407 xprt_complete_bc_request(req, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001408
1409 return 0;
1410}
1411
1412static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1413 struct xdr_skb_reader *desc)
1414{
1415 struct sock_xprt *transport =
1416 container_of(xprt, struct sock_xprt, xprt);
1417
1418 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1419 xs_tcp_read_reply(xprt, desc) :
1420 xs_tcp_read_callback(xprt, desc);
1421}
Chuck Lever76566772015-10-24 17:28:32 -04001422
1423static int xs_tcp_bc_up(struct svc_serv *serv, struct net *net)
1424{
1425 int ret;
1426
1427 ret = svc_create_xprt(serv, "tcp-bc", net, PF_INET, 0,
1428 SVC_SOCK_ANONYMOUS);
1429 if (ret < 0)
1430 return ret;
1431 return 0;
1432}
Chuck Lever6b26cc82016-05-02 14:40:40 -04001433
1434static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
1435{
1436 return PAGE_SIZE;
1437}
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001438#else
1439static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1440 struct xdr_skb_reader *desc)
1441{
1442 return xs_tcp_read_reply(xprt, desc);
1443}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001444#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001445
1446/*
1447 * Read data off the transport. This can be either an RPC_CALL or an
1448 * RPC_REPLY. Relay the processing to helper functions.
1449 */
1450static void xs_tcp_read_data(struct rpc_xprt *xprt,
1451 struct xdr_skb_reader *desc)
1452{
1453 struct sock_xprt *transport =
1454 container_of(xprt, struct sock_xprt, xprt);
1455
1456 if (_xs_tcp_read_data(xprt, desc) == 0)
1457 xs_tcp_check_fraghdr(transport);
1458 else {
1459 /*
1460 * The transport_lock protects the request handling.
1461 * There's no need to hold it to update the tcp_flags.
1462 */
1463 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1464 }
Chuck Levera246b012005-08-11 16:25:23 -04001465}
1466
Chuck Leverdd456472006-12-05 16:35:44 -05001467static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001468{
1469 size_t len;
1470
Chuck Lever51971132006-12-05 16:35:19 -05001471 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001472 if (len > desc->count)
1473 len = desc->count;
1474 desc->count -= len;
1475 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001476 transport->tcp_offset += len;
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001477 dprintk("RPC: discarded %zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001478 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001479}
1480
Chuck Lever9903cd12005-08-11 16:25:26 -04001481static 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 -04001482{
1483 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001484 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001485 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001486 .skb = skb,
1487 .offset = offset,
1488 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001489 };
Trond Myklebust3d188802018-01-14 15:47:06 -05001490 size_t ret;
Chuck Levera246b012005-08-11 16:25:23 -04001491
Chuck Lever46121cf2007-01-31 12:14:08 -05001492 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001493 do {
Jeff Layton1a867a02014-10-28 14:24:14 -04001494 trace_xs_tcp_data_recv(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001495 /* Read in a new fragment marker if necessary */
1496 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001497 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001498 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001499 continue;
1500 }
1501 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001502 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001503 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001504 continue;
1505 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001506 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001507 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001508 xs_tcp_read_calldir(transport, &desc);
1509 continue;
1510 }
Chuck Levera246b012005-08-11 16:25:23 -04001511 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001512 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001513 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001514 continue;
1515 }
1516 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001517 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001518 } while (desc.count);
Trond Myklebust3d188802018-01-14 15:47:06 -05001519 ret = len - desc.count;
1520 if (ret < rd_desc->count)
1521 rd_desc->count -= ret;
1522 else
1523 rd_desc->count = 0;
Jeff Layton1a867a02014-10-28 14:24:14 -04001524 trace_xs_tcp_data_recv(transport);
Chuck Lever46121cf2007-01-31 12:14:08 -05001525 dprintk("RPC: xs_tcp_data_recv done\n");
Trond Myklebust3d188802018-01-14 15:47:06 -05001526 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04001527}
1528
Trond Myklebust66d7a562015-10-05 11:19:20 -04001529static void xs_tcp_data_receive(struct sock_xprt *transport)
1530{
1531 struct rpc_xprt *xprt = &transport->xprt;
1532 struct sock *sk;
1533 read_descriptor_t rd_desc = {
Trond Myklebust66d7a562015-10-05 11:19:20 -04001534 .arg.data = xprt,
1535 };
1536 unsigned long total = 0;
1537 int read = 0;
1538
Trond Myklebust0af34422018-01-14 15:28:29 -05001539restart:
Trond Myklebustedc1b012015-10-05 10:53:49 -04001540 mutex_lock(&transport->recv_mutex);
Trond Myklebust66d7a562015-10-05 11:19:20 -04001541 sk = transport->inet;
Trond Myklebustedc1b012015-10-05 10:53:49 -04001542 if (sk == NULL)
1543 goto out;
Trond Myklebust66d7a562015-10-05 11:19:20 -04001544
1545 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Trond Myklebust0af34422018-01-14 15:28:29 -05001546 for (;;) {
Trond Myklebust3d188802018-01-14 15:47:06 -05001547 rd_desc.count = RPC_TCP_READ_CHUNK_SZ;
Trond Myklebustedc1b012015-10-05 10:53:49 -04001548 lock_sock(sk);
Trond Myklebust66d7a562015-10-05 11:19:20 -04001549 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
Trond Myklebust3d188802018-01-14 15:47:06 -05001550 if (rd_desc.count != 0 || read < 0) {
Trond Myklebust42d42a52016-05-23 09:24:55 -04001551 clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
1552 release_sock(sk);
Trond Myklebust8d6f97d2017-08-12 15:11:49 -04001553 break;
Trond Myklebust42d42a52016-05-23 09:24:55 -04001554 }
Trond Myklebust8d6f97d2017-08-12 15:11:49 -04001555 release_sock(sk);
1556 total += read;
Trond Myklebust0af34422018-01-14 15:28:29 -05001557 if (need_resched()) {
1558 mutex_unlock(&transport->recv_mutex);
1559 cond_resched();
1560 goto restart;
1561 }
Trond Myklebust66d7a562015-10-05 11:19:20 -04001562 }
Trond Myklebust8d6f97d2017-08-12 15:11:49 -04001563 if (test_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
1564 queue_work(xprtiod_workqueue, &transport->recv_worker);
Trond Myklebustedc1b012015-10-05 10:53:49 -04001565out:
1566 mutex_unlock(&transport->recv_mutex);
Trond Myklebust66d7a562015-10-05 11:19:20 -04001567 trace_xs_tcp_data_ready(xprt, read, total);
1568}
1569
Trond Myklebustedc1b012015-10-05 10:53:49 -04001570static void xs_tcp_data_receive_workfn(struct work_struct *work)
1571{
1572 struct sock_xprt *transport =
1573 container_of(work, struct sock_xprt, recv_worker);
1574 xs_tcp_data_receive(transport);
1575}
1576
Chuck Lever9903cd12005-08-11 16:25:26 -04001577/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001578 * xs_tcp_state_change - callback to handle TCP socket state changes
1579 * @sk: socket whose state has changed
1580 *
1581 */
1582static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001583{
Chuck Lever9903cd12005-08-11 16:25:26 -04001584 struct rpc_xprt *xprt;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001585 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001586
Eric Dumazetf064af12010-09-22 12:43:39 +00001587 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001588 if (!(xprt = xprt_from_sock(sk)))
1589 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001590 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001591 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001592 sk->sk_state, xprt_connected(xprt),
1593 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001594 sock_flag(sk, SOCK_ZAPPED),
1595 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001596
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001597 transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04001598 trace_rpc_socket_state_change(xprt, sk->sk_socket);
Chuck Levera246b012005-08-11 16:25:23 -04001599 switch (sk->sk_state) {
1600 case TCP_ESTABLISHED:
Eric Dumazetf064af12010-09-22 12:43:39 +00001601 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001602 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001603
Chuck Levera246b012005-08-11 16:25:23 -04001604 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001605 transport->tcp_offset = 0;
1606 transport->tcp_reclen = 0;
1607 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001608 transport->tcp_flags =
1609 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Trond Myklebust8b717982013-09-26 10:18:04 -04001610 xprt->connect_cookie++;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001611 clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
1612 xprt_clear_connecting(xprt);
Chuck Lever51971132006-12-05 16:35:19 -05001613
Trond Myklebust2a491992009-03-11 14:38:00 -04001614 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001615 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001616 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001617 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001618 case TCP_FIN_WAIT1:
1619 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001620 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001621 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001622 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001623 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001624 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001625 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001626 smp_mb__after_atomic();
Chuck Levera246b012005-08-11 16:25:23 -04001627 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001628 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001629 /* The server initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001630 xprt->connect_cookie++;
Trond Myklebustd0bea452012-10-23 11:35:47 -04001631 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebusta519fc72012-09-12 16:49:15 -04001632 xs_tcp_force_close(xprt);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001633 /* fall through */
Trond Myklebust663b8852008-01-01 18:42:12 -05001634 case TCP_CLOSING:
1635 /*
1636 * If the server closed down the connection, make sure that
1637 * we back off before reconnecting
1638 */
1639 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1640 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001641 break;
1642 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001643 set_bit(XPRT_CLOSING, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001644 smp_mb__before_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001645 clear_bit(XPRT_CONNECTED, &xprt->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001646 smp_mb__after_atomic();
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001647 break;
1648 case TCP_CLOSE:
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04001649 if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
1650 &transport->sock_state))
1651 xprt_clear_connecting(xprt);
Trond Myklebust9b308892018-02-05 10:20:06 -05001652 clear_bit(XPRT_CLOSING, &xprt->state);
NeilBrown3ffbc1d2017-07-19 14:05:01 +10001653 if (sk->sk_err)
1654 xprt_wake_pending_tasks(xprt, -sk->sk_err);
Trond Myklebust9b308892018-02-05 10:20:06 -05001655 /* Trigger the socket release */
1656 xs_tcp_force_close(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001657 }
1658 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001659 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001660}
1661
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001662static void xs_write_space(struct sock *sk)
1663{
Trond Myklebust13331a52016-01-06 08:57:06 -05001664 struct socket_wq *wq;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001665 struct rpc_xprt *xprt;
1666
Trond Myklebust13331a52016-01-06 08:57:06 -05001667 if (!sk->sk_socket)
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001668 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001669 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001670
1671 if (unlikely(!(xprt = xprt_from_sock(sk))))
1672 return;
Trond Myklebust13331a52016-01-06 08:57:06 -05001673 rcu_read_lock();
1674 wq = rcu_dereference(sk->sk_wq);
1675 if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0)
1676 goto out;
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001677
1678 xprt_write_space(xprt);
Trond Myklebust13331a52016-01-06 08:57:06 -05001679out:
1680 rcu_read_unlock();
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001681}
1682
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001683/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001684 * xs_udp_write_space - callback invoked when socket buffer space
1685 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001686 * @sk: socket whose state has changed
1687 *
Chuck Levera246b012005-08-11 16:25:23 -04001688 * Called when more output buffer space is available for this socket.
1689 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001690 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001691 * with a bunch of small requests.
1692 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001693static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001694{
Eric Dumazetf064af12010-09-22 12:43:39 +00001695 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001696
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001697 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001698 if (sock_writeable(sk))
1699 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001700
Eric Dumazetf064af12010-09-22 12:43:39 +00001701 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001702}
Chuck Levera246b012005-08-11 16:25:23 -04001703
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001704/**
1705 * xs_tcp_write_space - callback invoked when socket buffer space
1706 * becomes available
1707 * @sk: socket whose state has changed
1708 *
1709 * Called when more output buffer space is available for this socket.
1710 * We try not to wake our writers until they can make "significant"
1711 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1712 * with a bunch of small requests.
1713 */
1714static void xs_tcp_write_space(struct sock *sk)
1715{
Eric Dumazetf064af12010-09-22 12:43:39 +00001716 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001717
1718 /* from net/core/stream.c:sk_stream_write_space */
Eric Dumazet64dc6132013-07-22 20:26:31 -07001719 if (sk_stream_is_writeable(sk))
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001720 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001721
Eric Dumazetf064af12010-09-22 12:43:39 +00001722 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001723}
1724
Chuck Lever470056c2005-08-25 16:25:56 -07001725static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001726{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001727 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1728 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001729
Chuck Lever7c6e0662006-12-05 16:35:30 -05001730 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001731 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001732 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001733 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001734 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001735 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001736 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001737 sk->sk_write_space(sk);
1738 }
1739}
1740
Chuck Lever43118c22005-08-25 16:25:49 -07001741/**
Chuck Lever470056c2005-08-25 16:25:56 -07001742 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001743 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001744 * @sndsize: requested size of send buffer, in bytes
1745 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001746 *
Chuck Lever470056c2005-08-25 16:25:56 -07001747 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001748 */
Chuck Lever470056c2005-08-25 16:25:56 -07001749static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001750{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001751 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1752
1753 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001754 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001755 transport->sndsize = sndsize + 1024;
1756 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001757 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001758 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001759
1760 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001761}
1762
Chuck Lever46c0ee82005-08-25 16:25:52 -07001763/**
1764 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1765 * @task: task that timed out
1766 *
1767 * Adjust the congestion window after a retransmit timeout has occurred.
1768 */
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001769static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Lever46c0ee82005-08-25 16:25:52 -07001770{
Chuck Leverb977b642017-02-08 17:00:51 -05001771 spin_lock_bh(&xprt->transport_lock);
Trond Myklebust6a24dfb2013-01-08 09:48:15 -05001772 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
Chuck Leverb977b642017-02-08 17:00:51 -05001773 spin_unlock_bh(&xprt->transport_lock);
Chuck Lever46c0ee82005-08-25 16:25:52 -07001774}
1775
Chuck Leverb85d8802006-05-25 01:40:49 -04001776static unsigned short xs_get_random_port(void)
1777{
Frank Sorenson5d718992016-07-08 16:35:23 -05001778 unsigned short range = xprt_max_resvport - xprt_min_resvport + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001779 unsigned short rand = (unsigned short) prandom_u32() % range;
Chuck Leverb85d8802006-05-25 01:40:49 -04001780 return rand + xprt_min_resvport;
1781}
1782
Chuck Lever92200412006-01-03 09:55:51 +01001783/**
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001784 * xs_set_reuseaddr_port - set the socket's port and address reuse options
1785 * @sock: socket
1786 *
1787 * Note that this function has to be called on all sockets that share the
1788 * same port, and it must be called before binding.
1789 */
1790static void xs_sock_set_reuseport(struct socket *sock)
1791{
Trond Myklebust402e23b2015-02-09 17:20:14 -05001792 int opt = 1;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001793
Trond Myklebust402e23b2015-02-09 17:20:14 -05001794 kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
1795 (char *)&opt, sizeof(opt));
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001796}
1797
1798static unsigned short xs_sock_getport(struct socket *sock)
1799{
1800 struct sockaddr_storage buf;
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001801 unsigned short port = 0;
1802
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001803 if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001804 goto out;
1805 switch (buf.ss_family) {
1806 case AF_INET6:
1807 port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
1808 break;
1809 case AF_INET:
1810 port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
1811 }
1812out:
1813 return port;
1814}
1815
1816/**
Chuck Lever92200412006-01-03 09:55:51 +01001817 * xs_set_port - reset the port number in the remote endpoint address
1818 * @xprt: generic transport
1819 * @port: new port number
1820 *
1821 */
1822static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1823{
Chuck Lever46121cf2007-01-31 12:14:08 -05001824 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001825
Chuck Lever9dc3b092009-08-09 15:09:46 -04001826 rpc_set_port(xs_addr(xprt), port);
1827 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001828}
1829
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001830static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
1831{
1832 if (transport->srcport == 0)
1833 transport->srcport = xs_sock_getport(sock);
1834}
1835
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001836static unsigned short xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001837{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001838 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001839
1840 if (port == 0 && transport->xprt.resvport)
1841 port = xs_get_random_port();
1842 return port;
1843}
1844
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001845static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001846{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001847 if (transport->srcport != 0)
1848 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001849 if (!transport->xprt.resvport)
1850 return 0;
1851 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1852 return xprt_max_resvport;
1853 return --port;
1854}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001855static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001856{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001857 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001858 int err, nloop = 0;
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001859 unsigned short port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001860 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001861
Chris Perl0f7a6222014-09-05 15:40:21 -04001862 /*
1863 * If we are asking for any ephemeral port (i.e. port == 0 &&
1864 * transport->xprt.resvport == 0), don't bind. Let the local
1865 * port selection happen implicitly when the socket is used
1866 * (for example at connect time).
1867 *
1868 * This ensures that we can continue to establish TCP
1869 * connections even when all local ephemeral ports are already
1870 * a part of some TCP connection. This makes no difference
1871 * for UDP sockets, but also doens't harm them.
1872 *
1873 * If we're asking for any reserved port (i.e. port == 0 &&
1874 * transport->xprt.resvport == 1) xs_get_srcport above will
1875 * ensure that port is non-zero and we will bind as needed.
1876 */
1877 if (port == 0)
1878 return 0;
1879
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001880 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001881 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001882 rpc_set_port((struct sockaddr *)&myaddr, port);
1883 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1884 transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001885 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001886 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001887 break;
Chuck Levera246b012005-08-11 16:25:23 -04001888 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001889 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001890 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001891 if (port > last)
1892 nloop++;
1893 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001894
Chuck Lever4232e862010-10-20 11:52:51 -04001895 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001896 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1897 &((struct sockaddr_in *)&myaddr)->sin_addr,
1898 port, err ? "failed" : "ok", err);
1899 else
1900 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1901 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1902 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001903 return err;
1904}
1905
Chuck Lever176e21e2011-05-09 15:22:44 -04001906/*
1907 * We don't support autobind on AF_LOCAL sockets
1908 */
1909static void xs_local_rpcbind(struct rpc_task *task)
1910{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001911 xprt_set_bound(task->tk_xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04001912}
1913
1914static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1915{
1916}
Chuck Levera246b012005-08-11 16:25:23 -04001917
Peter Zijlstraed075362006-12-06 20:35:24 -08001918#ifdef CONFIG_DEBUG_LOCK_ALLOC
1919static struct lock_class_key xs_key[2];
1920static struct lock_class_key xs_slock_key[2];
1921
Chuck Lever176e21e2011-05-09 15:22:44 -04001922static inline void xs_reclassify_socketu(struct socket *sock)
1923{
1924 struct sock *sk = sock->sk;
1925
Chuck Lever176e21e2011-05-09 15:22:44 -04001926 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1927 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1928}
1929
Chuck Lever8945ee52007-08-06 11:58:04 -04001930static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001931{
1932 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001933
Chuck Lever8945ee52007-08-06 11:58:04 -04001934 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1935 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1936}
Peter Zijlstraed075362006-12-06 20:35:24 -08001937
Chuck Lever8945ee52007-08-06 11:58:04 -04001938static inline void xs_reclassify_socket6(struct socket *sock)
1939{
1940 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001941
Chuck Lever8945ee52007-08-06 11:58:04 -04001942 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1943 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001944}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001945
1946static inline void xs_reclassify_socket(int family, struct socket *sock)
1947{
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02001948 if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
Weston Andros Adamson1b7a1812012-10-23 10:43:39 -04001949 return;
1950
Chuck Lever4232e862010-10-20 11:52:51 -04001951 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001952 case AF_LOCAL:
1953 xs_reclassify_socketu(sock);
1954 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001955 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001956 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001957 break;
1958 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001959 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001960 break;
1961 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001962}
Peter Zijlstraed075362006-12-06 20:35:24 -08001963#else
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001964static inline void xs_reclassify_socket(int family, struct socket *sock)
1965{
1966}
Peter Zijlstraed075362006-12-06 20:35:24 -08001967#endif
1968
NeilBrown93dc41b2013-10-31 16:14:36 +11001969static void xs_dummy_setup_socket(struct work_struct *work)
1970{
1971}
1972
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001973static struct socket *xs_create_sock(struct rpc_xprt *xprt,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001974 struct sock_xprt *transport, int family, int type,
1975 int protocol, bool reuseport)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001976{
1977 struct socket *sock;
1978 int err;
1979
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001980 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001981 if (err < 0) {
1982 dprintk("RPC: can't create %d transport socket (%d).\n",
1983 protocol, -err);
1984 goto out;
1985 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001986 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001987
Trond Myklebust4dda9c82015-02-08 15:00:06 -05001988 if (reuseport)
1989 xs_sock_set_reuseport(sock);
1990
Ben Hutchings4cea2882011-02-22 21:54:34 +00001991 err = xs_bind(transport, sock);
1992 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001993 sock_release(sock);
1994 goto out;
1995 }
1996
1997 return sock;
1998out:
1999 return ERR_PTR(err);
2000}
2001
Chuck Lever176e21e2011-05-09 15:22:44 -04002002static int xs_local_finish_connecting(struct rpc_xprt *xprt,
2003 struct socket *sock)
2004{
2005 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2006 xprt);
2007
2008 if (!transport->inet) {
2009 struct sock *sk = sock->sk;
2010
2011 write_lock_bh(&sk->sk_callback_lock);
2012
2013 xs_save_old_callbacks(transport, sk);
2014
2015 sk->sk_user_data = xprt;
Trond Myklebusta2648092015-10-06 17:03:00 -04002016 sk->sk_data_ready = xs_data_ready;
Chuck Lever176e21e2011-05-09 15:22:44 -04002017 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002018 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05002019 sk->sk_error_report = xs_error_report;
Trond Myklebustc2126152015-08-19 21:46:15 -05002020 sk->sk_allocation = GFP_NOIO;
Chuck Lever176e21e2011-05-09 15:22:44 -04002021
2022 xprt_clear_connected(xprt);
2023
2024 /* Reset to new socket */
2025 transport->sock = sock;
2026 transport->inet = sk;
2027
2028 write_unlock_bh(&sk->sk_callback_lock);
2029 }
2030
2031 /* Tell the socket layer to start connecting... */
2032 xprt->stat.connect_count++;
2033 xprt->stat.connect_start = jiffies;
2034 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
2035}
2036
2037/**
2038 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
Chuck Lever176e21e2011-05-09 15:22:44 -04002039 * @transport: socket transport to connect
Chuck Lever176e21e2011-05-09 15:22:44 -04002040 */
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002041static int xs_local_setup_socket(struct sock_xprt *transport)
Chuck Lever176e21e2011-05-09 15:22:44 -04002042{
Chuck Lever176e21e2011-05-09 15:22:44 -04002043 struct rpc_xprt *xprt = &transport->xprt;
2044 struct socket *sock;
2045 int status = -EIO;
2046
Chuck Lever176e21e2011-05-09 15:22:44 -04002047 status = __sock_create(xprt->xprt_net, AF_LOCAL,
2048 SOCK_STREAM, 0, &sock, 1);
2049 if (status < 0) {
2050 dprintk("RPC: can't create AF_LOCAL "
2051 "transport socket (%d).\n", -status);
2052 goto out;
2053 }
Stefan Hajnoczid1358912015-12-02 14:17:52 +08002054 xs_reclassify_socket(AF_LOCAL, sock);
Chuck Lever176e21e2011-05-09 15:22:44 -04002055
2056 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
2057 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2058
2059 status = xs_local_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002060 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever176e21e2011-05-09 15:22:44 -04002061 switch (status) {
2062 case 0:
2063 dprintk("RPC: xprt %p connected to %s\n",
2064 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2065 xprt_set_connected(xprt);
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002066 case -ENOBUFS:
Chuck Lever176e21e2011-05-09 15:22:44 -04002067 break;
2068 case -ENOENT:
2069 dprintk("RPC: xprt %p: socket %s does not exist\n",
2070 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2071 break;
Trond Myklebust4a20a982012-12-15 17:02:29 -05002072 case -ECONNREFUSED:
2073 dprintk("RPC: xprt %p: connection refused for %s\n",
2074 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2075 break;
Chuck Lever176e21e2011-05-09 15:22:44 -04002076 default:
2077 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
2078 __func__, -status,
2079 xprt->address_strings[RPC_DISPLAY_ADDR]);
2080 }
2081
2082out:
2083 xprt_clear_connecting(xprt);
2084 xprt_wake_pending_tasks(xprt, status);
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002085 return status;
2086}
2087
Linus Torvaldsb6669732013-02-28 18:02:55 -08002088static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002089{
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002090 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2091 int ret;
2092
2093 if (RPC_IS_ASYNC(task)) {
2094 /*
2095 * We want the AF_LOCAL connect to be resolved in the
2096 * filesystem namespace of the process making the rpc
2097 * call. Thus we connect synchronously.
2098 *
2099 * If we want to support asynchronous AF_LOCAL calls,
2100 * we'll need to figure out how to pass a namespace to
2101 * connect.
2102 */
2103 rpc_exit(task, -ENOTCONN);
2104 return;
2105 }
2106 ret = xs_local_setup_socket(transport);
2107 if (ret && !RPC_IS_SOFTCONN(task))
2108 msleep_interruptible(15000);
Chuck Lever176e21e2011-05-09 15:22:44 -04002109}
2110
Jeff Layton3c87ef62015-06-03 16:14:25 -04002111#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Jeff Laytond6e971d2015-06-03 16:14:28 -04002112/*
2113 * Note that this should be called with XPRT_LOCKED held (or when we otherwise
2114 * know that we have exclusive access to the socket), to guard against
2115 * races with xs_reset_transport.
2116 */
Mel Gormana564b8f2012-07-31 16:45:12 -07002117static void xs_set_memalloc(struct rpc_xprt *xprt)
2118{
2119 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2120 xprt);
2121
Jeff Laytond6e971d2015-06-03 16:14:28 -04002122 /*
2123 * If there's no sock, then we have nothing to set. The
2124 * reconnecting process will get it for us.
2125 */
2126 if (!transport->inet)
2127 return;
Jeff Layton8e228132015-06-03 16:14:26 -04002128 if (atomic_read(&xprt->swapper))
Mel Gormana564b8f2012-07-31 16:45:12 -07002129 sk_set_memalloc(transport->inet);
2130}
2131
2132/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002133 * xs_enable_swap - Tag this transport as being used for swap.
Mel Gormana564b8f2012-07-31 16:45:12 -07002134 * @xprt: transport to tag
Mel Gormana564b8f2012-07-31 16:45:12 -07002135 *
Jeff Layton8e228132015-06-03 16:14:26 -04002136 * Take a reference to this transport on behalf of the rpc_clnt, and
2137 * optionally mark it for swapping if it wasn't already.
Mel Gormana564b8f2012-07-31 16:45:12 -07002138 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002139static int
2140xs_enable_swap(struct rpc_xprt *xprt)
Mel Gormana564b8f2012-07-31 16:45:12 -07002141{
Jeff Laytond6e971d2015-06-03 16:14:28 -04002142 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Mel Gormana564b8f2012-07-31 16:45:12 -07002143
Jeff Laytond6e971d2015-06-03 16:14:28 -04002144 if (atomic_inc_return(&xprt->swapper) != 1)
2145 return 0;
2146 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2147 return -ERESTARTSYS;
2148 if (xs->inet)
2149 sk_set_memalloc(xs->inet);
2150 xprt_release_xprt(xprt, NULL);
Jeff Layton8e228132015-06-03 16:14:26 -04002151 return 0;
Mel Gormana564b8f2012-07-31 16:45:12 -07002152}
Jeff Layton8e228132015-06-03 16:14:26 -04002153
2154/**
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002155 * xs_disable_swap - Untag this transport as being used for swap.
Jeff Layton8e228132015-06-03 16:14:26 -04002156 * @xprt: transport to tag
2157 *
2158 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
2159 * swapper refcount goes to 0, untag the socket as a memalloc socket.
2160 */
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002161static void
2162xs_disable_swap(struct rpc_xprt *xprt)
Jeff Layton8e228132015-06-03 16:14:26 -04002163{
Jeff Laytond6e971d2015-06-03 16:14:28 -04002164 struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
Jeff Layton8e228132015-06-03 16:14:26 -04002165
Jeff Laytond6e971d2015-06-03 16:14:28 -04002166 if (!atomic_dec_and_test(&xprt->swapper))
2167 return;
2168 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2169 return;
2170 if (xs->inet)
2171 sk_clear_memalloc(xs->inet);
2172 xprt_release_xprt(xprt, NULL);
Mel Gormana564b8f2012-07-31 16:45:12 -07002173}
Mel Gormana564b8f2012-07-31 16:45:12 -07002174#else
2175static void xs_set_memalloc(struct rpc_xprt *xprt)
2176{
2177}
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002178
2179static int
2180xs_enable_swap(struct rpc_xprt *xprt)
2181{
2182 return -EINVAL;
2183}
2184
2185static void
2186xs_disable_swap(struct rpc_xprt *xprt)
2187{
2188}
Mel Gormana564b8f2012-07-31 16:45:12 -07002189#endif
2190
Chuck Lever16be2d22007-08-06 11:57:38 -04002191static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04002192{
Chuck Lever16be2d22007-08-06 11:57:38 -04002193 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04002194
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002195 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002196 struct sock *sk = sock->sk;
2197
2198 write_lock_bh(&sk->sk_callback_lock);
2199
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002200 xs_save_old_callbacks(transport, sk);
2201
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002202 sk->sk_user_data = xprt;
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04002203 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002204 sk->sk_write_space = xs_udp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002205 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebustc2126152015-08-19 21:46:15 -05002206 sk->sk_allocation = GFP_NOIO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002207
2208 xprt_set_connected(xprt);
2209
2210 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002211 transport->sock = sock;
2212 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002213
Mel Gormana564b8f2012-07-31 16:45:12 -07002214 xs_set_memalloc(xprt);
2215
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002216 write_unlock_bh(&sk->sk_callback_lock);
2217 }
Chuck Lever470056c2005-08-25 16:25:56 -07002218 xs_udp_do_set_buffer_size(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002219
2220 xprt->stat.connect_start = jiffies;
Chuck Lever16be2d22007-08-06 11:57:38 -04002221}
2222
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002223static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04002224{
2225 struct sock_xprt *transport =
2226 container_of(work, struct sock_xprt, connect_worker.work);
2227 struct rpc_xprt *xprt = &transport->xprt;
Colin Ian Kingd099b8a2017-09-18 12:21:14 +01002228 struct socket *sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002229 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04002230
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002231 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002232 xs_addr(xprt)->sa_family, SOCK_DGRAM,
2233 IPPROTO_UDP, false);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002234 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04002235 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04002236
Chuck Leverc740eff2009-08-09 15:09:46 -04002237 dprintk("RPC: worker connecting xprt %p via %s to "
2238 "%s (port %s)\n", xprt,
2239 xprt->address_strings[RPC_DISPLAY_PROTO],
2240 xprt->address_strings[RPC_DISPLAY_ADDR],
2241 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04002242
2243 xs_udp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002244 trace_rpc_socket_connect(xprt, sock, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002245 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002246out:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002247 xprt_unlock_connect(xprt, transport);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002248 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002249 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002250}
2251
Trond Myklebust4876cc72015-06-19 16:17:57 -04002252/**
2253 * xs_tcp_shutdown - gracefully shut down a TCP socket
2254 * @xprt: transport
2255 *
2256 * Initiates a graceful shutdown of the TCP socket by calling the
2257 * equivalent of shutdown(SHUT_RDWR);
2258 */
2259static void xs_tcp_shutdown(struct rpc_xprt *xprt)
2260{
2261 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2262 struct socket *sock = transport->sock;
Trond Myklebust9b308892018-02-05 10:20:06 -05002263 int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
Trond Myklebust4876cc72015-06-19 16:17:57 -04002264
2265 if (sock == NULL)
2266 return;
Trond Myklebust9b308892018-02-05 10:20:06 -05002267 switch (skst) {
2268 default:
Trond Myklebust4876cc72015-06-19 16:17:57 -04002269 kernel_sock_shutdown(sock, SHUT_RDWR);
2270 trace_rpc_socket_shutdown(xprt, sock);
Trond Myklebust9b308892018-02-05 10:20:06 -05002271 break;
2272 case TCP_CLOSE:
2273 case TCP_TIME_WAIT:
Trond Myklebust4876cc72015-06-19 16:17:57 -04002274 xs_reset_transport(transport);
Trond Myklebust9b308892018-02-05 10:20:06 -05002275 }
Trond Myklebust4876cc72015-06-19 16:17:57 -04002276}
2277
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002278static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
2279 struct socket *sock)
2280{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002281 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2282 unsigned int keepidle;
2283 unsigned int keepcnt;
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002284 unsigned int opt_on = 1;
2285 unsigned int timeo;
2286
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002287 spin_lock_bh(&xprt->transport_lock);
2288 keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
2289 keepcnt = xprt->timeout->to_retries + 1;
2290 timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
2291 (xprt->timeout->to_retries + 1);
2292 clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2293 spin_unlock_bh(&xprt->transport_lock);
2294
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002295 /* TCP Keepalive options */
2296 kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
2297 (char *)&opt_on, sizeof(opt_on));
2298 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
2299 (char *)&keepidle, sizeof(keepidle));
2300 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
2301 (char *)&keepidle, sizeof(keepidle));
2302 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
2303 (char *)&keepcnt, sizeof(keepcnt));
2304
2305 /* TCP user timeout (see RFC5482) */
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002306 kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT,
2307 (char *)&timeo, sizeof(timeo));
2308}
2309
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002310static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
2311 unsigned long connect_timeout,
2312 unsigned long reconnect_timeout)
2313{
2314 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2315 struct rpc_timeout to;
2316 unsigned long initval;
2317
2318 spin_lock_bh(&xprt->transport_lock);
2319 if (reconnect_timeout < xprt->max_reconnect_timeout)
2320 xprt->max_reconnect_timeout = reconnect_timeout;
2321 if (connect_timeout < xprt->connect_timeout) {
2322 memcpy(&to, xprt->timeout, sizeof(to));
2323 initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
2324 /* Arbitrary lower limit */
2325 if (initval < XS_TCP_INIT_REEST_TO << 1)
2326 initval = XS_TCP_INIT_REEST_TO << 1;
2327 to.to_initval = initval;
2328 to.to_maxval = initval;
2329 memcpy(&transport->tcp_timeout, &to,
2330 sizeof(transport->tcp_timeout));
2331 xprt->timeout = &transport->tcp_timeout;
2332 xprt->connect_timeout = connect_timeout;
2333 }
2334 set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2335 spin_unlock_bh(&xprt->transport_lock);
2336}
2337
Chuck Lever16be2d22007-08-06 11:57:38 -04002338static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002339{
Chuck Lever16be2d22007-08-06 11:57:38 -04002340 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002341 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002342
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002343 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002344 struct sock *sk = sock->sk;
NeilBrownd88e4d82016-08-04 16:24:28 +10002345 unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC;
Trond Myklebust7f260e82013-09-24 11:25:22 -04002346
NeilBrownd88e4d82016-08-04 16:24:28 +10002347 /* Avoid temporary address, they are bad for long-lived
2348 * connections such as NFS mounts.
2349 * RFC4941, section 3.6 suggests that:
2350 * Individual applications, which have specific
2351 * knowledge about the normal duration of connections,
2352 * MAY override this as appropriate.
2353 */
2354 kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES,
2355 (char *)&addr_pref, sizeof(addr_pref));
2356
Trond Myklebust8d1b8c62017-02-08 11:17:53 -05002357 xs_tcp_set_socket_timeouts(xprt, sock);
Trond Myklebust775f06a2015-06-20 15:31:54 -04002358
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002359 write_lock_bh(&sk->sk_callback_lock);
2360
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002361 xs_save_old_callbacks(transport, sk);
2362
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002363 sk->sk_user_data = xprt;
Trond Myklebust5157b952016-05-29 10:13:24 -04002364 sk->sk_data_ready = xs_data_ready;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002365 sk->sk_state_change = xs_tcp_state_change;
2366 sk->sk_write_space = xs_tcp_write_space;
Eric Dumazetb44114572016-05-12 21:41:39 -07002367 sock_set_flag(sk, SOCK_FASYNC);
Trond Myklebust21180712013-12-31 13:22:59 -05002368 sk->sk_error_report = xs_error_report;
Trond Myklebustc2126152015-08-19 21:46:15 -05002369 sk->sk_allocation = GFP_NOIO;
Chuck Lever3167e122005-08-25 16:25:55 -07002370
2371 /* socket options */
Chuck Lever3167e122005-08-25 16:25:55 -07002372 sock_reset_flag(sk, SOCK_LINGER);
Chuck Lever3167e122005-08-25 16:25:55 -07002373 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002374
2375 xprt_clear_connected(xprt);
2376
2377 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002378 transport->sock = sock;
2379 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002380
2381 write_unlock_bh(&sk->sk_callback_lock);
2382 }
2383
Trond Myklebust01d37c42009-03-11 14:09:39 -04002384 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002385 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002386
Mel Gormana564b8f2012-07-31 16:45:12 -07002387 xs_set_memalloc(xprt);
2388
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002389 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05002390 xprt->stat.connect_count++;
2391 xprt->stat.connect_start = jiffies;
Trond Myklebust0fdea1e2015-09-16 23:43:17 -04002392 set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002393 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2394 switch (ret) {
2395 case 0:
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002396 xs_set_srcport(transport, sock);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002397 /* fall through */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002398 case -EINPROGRESS:
2399 /* SYN_SENT! */
Trond Myklebustfe19a962011-03-18 20:21:23 -04002400 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2401 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust1f4c17a2016-08-01 13:36:08 -04002402 break;
2403 case -EADDRNOTAVAIL:
2404 /* Source port number is unavailable. Try a new one! */
2405 transport->srcport = 0;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002406 }
2407out:
2408 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002409}
2410
2411/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002412 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
Chuck Lever16be2d22007-08-06 11:57:38 -04002413 *
2414 * Invoked by a work queue tasklet.
2415 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002416static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002417{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002418 struct sock_xprt *transport =
2419 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002420 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002421 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002422 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002423
Chuck Lever16be2d22007-08-06 11:57:38 -04002424 if (!sock) {
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002425 sock = xs_create_sock(xprt, transport,
Trond Myklebust4dda9c82015-02-08 15:00:06 -05002426 xs_addr(xprt)->sa_family, SOCK_STREAM,
2427 IPPROTO_TCP, true);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002428 if (IS_ERR(sock)) {
2429 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002430 goto out;
2431 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002432 }
2433
Chuck Leverc740eff2009-08-09 15:09:46 -04002434 dprintk("RPC: worker connecting xprt %p via %s to "
2435 "%s (port %s)\n", xprt,
2436 xprt->address_strings[RPC_DISPLAY_PROTO],
2437 xprt->address_strings[RPC_DISPLAY_ADDR],
2438 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002439
2440 status = xs_tcp_finish_connecting(xprt, sock);
Trond Myklebust40b5ea02013-09-04 12:16:23 -04002441 trace_rpc_socket_connect(xprt, sock, status);
Chuck Lever46121cf2007-01-31 12:14:08 -05002442 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2443 xprt, -status, xprt_connected(xprt),
2444 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002445 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002446 default:
2447 printk("%s: connect returned unhandled error %d\n",
2448 __func__, status);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002449 /* fall through */
Trond Myklebustf75e6742009-04-21 17:18:20 -04002450 case -EADDRNOTAVAIL:
2451 /* We're probably in TIME_WAIT. Get rid of existing socket,
2452 * and retry
2453 */
Trond Myklebusta519fc72012-09-12 16:49:15 -04002454 xs_tcp_force_close(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002455 break;
Trond Myklebust2a491992009-03-11 14:38:00 -04002456 case 0:
2457 case -EINPROGRESS:
2458 case -EALREADY:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002459 xprt_unlock_connect(xprt, transport);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002460 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002461 case -EINVAL:
2462 /* Happens, for instance, if the user specified a link
2463 * local IPv6 address without a scope-id.
2464 */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002465 case -ECONNREFUSED:
2466 case -ECONNRESET:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002467 case -ENETDOWN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002468 case -ENETUNREACH:
Trond Myklebust4ba161a2017-11-24 12:00:24 -05002469 case -EHOSTUNREACH:
Trond Myklebust3913c782015-02-08 21:44:04 -05002470 case -EADDRINUSE:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002471 case -ENOBUFS:
NeilBrown6ea44ad2017-05-25 17:00:32 +10002472 /*
2473 * xs_tcp_force_close() wakes tasks with -EIO.
2474 * We need to wake them first to ensure the
2475 * correct error code.
2476 */
2477 xprt_wake_pending_tasks(xprt, status);
Trond Myklebust4efdd922015-02-08 15:34:28 -05002478 xs_tcp_force_close(xprt);
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002479 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002480 }
Trond Myklebust2a491992009-03-11 14:38:00 -04002481 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002482out:
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002483 xprt_unlock_connect(xprt, transport);
Chuck Lever2226feb2005-08-11 16:25:38 -04002484 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002485 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04002486}
2487
Trond Myklebust02910172016-08-04 00:00:33 -04002488static unsigned long xs_reconnect_delay(const struct rpc_xprt *xprt)
2489{
2490 unsigned long start, now = jiffies;
2491
2492 start = xprt->stat.connect_start + xprt->reestablish_timeout;
2493 if (time_after(start, now))
2494 return start - now;
2495 return 0;
2496}
2497
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002498static void xs_reconnect_backoff(struct rpc_xprt *xprt)
2499{
2500 xprt->reestablish_timeout <<= 1;
2501 if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
2502 xprt->reestablish_timeout = xprt->max_reconnect_timeout;
2503 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2504 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2505}
2506
Chuck Lever68e220b2007-08-06 11:57:48 -04002507/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002508 * xs_connect - connect a socket to a remote endpoint
Trond Myklebust1b092092013-01-08 09:26:49 -05002509 * @xprt: pointer to transport structure
Chuck Lever9903cd12005-08-11 16:25:26 -04002510 * @task: address of RPC task that manages state of connect request
2511 *
2512 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002513 *
2514 * UDP socket connects are synchronous, but we use a work queue anyway
2515 * to guarantee that even unprivileged user processes can set up a
2516 * socket on a privileged port.
2517 *
2518 * If a UDP socket connect fails, the delay behavior here prevents
2519 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002520 */
Trond Myklebust1b092092013-01-08 09:26:49 -05002521static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002522{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002523 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002524 unsigned long delay = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002525
Trond Myklebust718ba5b2015-02-08 18:19:25 -05002526 WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2527
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002528 if (transport->sock != NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002529 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2530 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002531 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebust99b1a4c2015-08-13 15:33:51 -04002532
2533 /* Start by resetting any existing state */
2534 xs_reset_transport(transport);
2535
Trond Myklebust02910172016-08-04 00:00:33 -04002536 delay = xs_reconnect_delay(xprt);
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002537 xs_reconnect_backoff(xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002538
Trond Myklebust02910172016-08-04 00:00:33 -04002539 } else
Chuck Lever46121cf2007-01-31 12:14:08 -05002540 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebust02910172016-08-04 00:00:33 -04002541
2542 queue_delayed_work(xprtiod_workqueue,
2543 &transport->connect_worker,
2544 delay);
Chuck Levera246b012005-08-11 16:25:23 -04002545}
2546
Chuck Lever262ca072006-03-20 13:44:16 -05002547/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002548 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2549 * @xprt: rpc_xprt struct containing statistics
2550 * @seq: output file
2551 *
2552 */
2553static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2554{
2555 long idle_time = 0;
2556
2557 if (xprt_connected(xprt))
2558 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2559
2560 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
Andy Adamson15a45202012-02-14 16:19:18 -05002561 "%llu %llu %lu %llu %llu\n",
Chuck Lever176e21e2011-05-09 15:22:44 -04002562 xprt->stat.bind_count,
2563 xprt->stat.connect_count,
2564 xprt->stat.connect_time,
2565 idle_time,
2566 xprt->stat.sends,
2567 xprt->stat.recvs,
2568 xprt->stat.bad_xids,
2569 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002570 xprt->stat.bklog_u,
2571 xprt->stat.max_slots,
2572 xprt->stat.sending_u,
2573 xprt->stat.pending_u);
Chuck Lever176e21e2011-05-09 15:22:44 -04002574}
2575
2576/**
Chuck Lever262ca072006-03-20 13:44:16 -05002577 * xs_udp_print_stats - display UDP socket-specifc stats
2578 * @xprt: rpc_xprt struct containing statistics
2579 * @seq: output file
2580 *
2581 */
2582static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2583{
Chuck Leverc8475462006-12-05 16:35:26 -05002584 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2585
Andy Adamson15a45202012-02-14 16:19:18 -05002586 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2587 "%lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002588 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002589 xprt->stat.bind_count,
2590 xprt->stat.sends,
2591 xprt->stat.recvs,
2592 xprt->stat.bad_xids,
2593 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002594 xprt->stat.bklog_u,
2595 xprt->stat.max_slots,
2596 xprt->stat.sending_u,
2597 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002598}
2599
2600/**
2601 * xs_tcp_print_stats - display TCP socket-specifc stats
2602 * @xprt: rpc_xprt struct containing statistics
2603 * @seq: output file
2604 *
2605 */
2606static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2607{
Chuck Leverc8475462006-12-05 16:35:26 -05002608 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002609 long idle_time = 0;
2610
2611 if (xprt_connected(xprt))
2612 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2613
Andy Adamson15a45202012-02-14 16:19:18 -05002614 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2615 "%llu %llu %lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002616 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002617 xprt->stat.bind_count,
2618 xprt->stat.connect_count,
2619 xprt->stat.connect_time,
2620 idle_time,
2621 xprt->stat.sends,
2622 xprt->stat.recvs,
2623 xprt->stat.bad_xids,
2624 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002625 xprt->stat.bklog_u,
2626 xprt->stat.max_slots,
2627 xprt->stat.sending_u,
2628 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002629}
2630
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002631/*
2632 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2633 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2634 * to use the server side send routines.
2635 */
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002636static int bc_malloc(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002637{
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002638 struct rpc_rqst *rqst = task->tk_rqstp;
2639 size_t size = rqst->rq_callsize;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002640 struct page *page;
2641 struct rpc_buffer *buf;
2642
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002643 if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
2644 WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
2645 size);
2646 return -EINVAL;
2647 }
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002648
Weston Andros Adamsonb8a13d02012-10-23 10:43:43 -04002649 page = alloc_page(GFP_KERNEL);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002650 if (!page)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002651 return -ENOMEM;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002652
2653 buf = page_address(page);
2654 buf->len = PAGE_SIZE;
2655
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002656 rqst->rq_buffer = buf->data;
Jeff Layton18e601d2016-10-24 20:33:23 -04002657 rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04002658 return 0;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002659}
2660
2661/*
2662 * Free the space allocated in the bc_alloc routine
2663 */
Chuck Lever3435c742016-09-15 10:55:29 -04002664static void bc_free(struct rpc_task *task)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002665{
Chuck Lever3435c742016-09-15 10:55:29 -04002666 void *buffer = task->tk_rqstp->rq_buffer;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002667 struct rpc_buffer *buf;
2668
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002669 buf = container_of(buffer, struct rpc_buffer, data);
2670 free_page((unsigned long)buf);
2671}
2672
2673/*
2674 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2675 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2676 */
2677static int bc_sendto(struct rpc_rqst *req)
2678{
2679 int len;
2680 struct xdr_buf *xbufp = &req->rq_snd_buf;
2681 struct rpc_xprt *xprt = req->rq_xprt;
2682 struct sock_xprt *transport =
2683 container_of(xprt, struct sock_xprt, xprt);
2684 struct socket *sock = transport->sock;
2685 unsigned long headoff;
2686 unsigned long tailoff;
2687
Chuck Lever61677ee2011-05-09 15:22:34 -04002688 xs_encode_stream_record_marker(xbufp);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002689
2690 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2691 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2692 len = svc_send_common(sock, xbufp,
2693 virt_to_page(xbufp->head[0].iov_base), headoff,
2694 xbufp->tail[0].iov_base, tailoff);
2695
2696 if (len != xbufp->len) {
2697 printk(KERN_NOTICE "Error sending entire callback!\n");
2698 len = -EAGAIN;
2699 }
2700
2701 return len;
2702}
2703
2704/*
2705 * The send routine. Borrows from svc_send
2706 */
2707static int bc_send_request(struct rpc_task *task)
2708{
2709 struct rpc_rqst *req = task->tk_rqstp;
2710 struct svc_xprt *xprt;
Andrzej Hajda7fc56132015-09-24 16:00:09 +02002711 int len;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002712
2713 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2714 /*
2715 * Get the server socket associated with this callback xprt
2716 */
2717 xprt = req->rq_xprt->bc_xprt;
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002718
2719 /*
2720 * Grab the mutex to serialize data as the connection is shared
2721 * with the fore channel
2722 */
2723 if (!mutex_trylock(&xprt->xpt_mutex)) {
2724 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2725 if (!mutex_trylock(&xprt->xpt_mutex))
2726 return -EAGAIN;
2727 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2728 }
2729 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2730 len = -ENOTCONN;
2731 else
2732 len = bc_sendto(req);
2733 mutex_unlock(&xprt->xpt_mutex);
2734
2735 if (len > 0)
2736 len = 0;
2737
2738 return len;
2739}
2740
2741/*
2742 * The close routine. Since this is client initiated, we do nothing
2743 */
2744
2745static void bc_close(struct rpc_xprt *xprt)
2746{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002747}
2748
2749/*
2750 * The xprt destroy routine. Again, because this connection is client
2751 * initiated, we do nothing
2752 */
2753
2754static void bc_destroy(struct rpc_xprt *xprt)
2755{
Kinglong Mee47f72ef2014-03-24 11:58:16 +08002756 dprintk("RPC: bc_destroy xprt %p\n", xprt);
2757
2758 xs_xprt_free(xprt);
2759 module_put(THIS_MODULE);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002760}
2761
Chuck Leverd31ae252017-08-01 12:00:39 -04002762static const struct rpc_xprt_ops xs_local_ops = {
Chuck Lever176e21e2011-05-09 15:22:44 -04002763 .reserve_xprt = xprt_reserve_xprt,
2764 .release_xprt = xs_tcp_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002765 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002766 .free_slot = xprt_free_slot,
Chuck Lever176e21e2011-05-09 15:22:44 -04002767 .rpcbind = xs_local_rpcbind,
2768 .set_port = xs_local_set_port,
J. Bruce Fieldsdc107402013-02-20 17:52:19 -05002769 .connect = xs_local_connect,
Chuck Lever176e21e2011-05-09 15:22:44 -04002770 .buf_alloc = rpc_malloc,
2771 .buf_free = rpc_free,
2772 .send_request = xs_local_send_request,
2773 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2774 .close = xs_close,
Trond Myklebusta1311d82013-10-31 09:18:49 -04002775 .destroy = xs_destroy,
Chuck Lever176e21e2011-05-09 15:22:44 -04002776 .print_stats = xs_local_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002777 .enable_swap = xs_enable_swap,
2778 .disable_swap = xs_disable_swap,
Chuck Lever176e21e2011-05-09 15:22:44 -04002779};
2780
Chuck Leverd31ae252017-08-01 12:00:39 -04002781static const struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002782 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002783 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002784 .release_xprt = xprt_release_xprt_cong,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002785 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002786 .free_slot = xprt_free_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002787 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002788 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002789 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002790 .buf_alloc = rpc_malloc,
2791 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002792 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002793 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002794 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002795 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002796 .close = xs_close,
2797 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002798 .print_stats = xs_udp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002799 .enable_swap = xs_enable_swap,
2800 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002801 .inject_disconnect = xs_inject_disconnect,
Chuck Lever262965f2005-08-11 16:25:56 -04002802};
2803
Chuck Leverd31ae252017-08-01 12:00:39 -04002804static const struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002805 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002806 .release_xprt = xs_tcp_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002807 .alloc_slot = xprt_lock_and_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002808 .free_slot = xprt_free_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002809 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002810 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002811 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002812 .buf_alloc = rpc_malloc,
2813 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002814 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002815 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustc627d312015-02-10 11:06:04 -05002816 .close = xs_tcp_shutdown,
Chuck Lever9903cd12005-08-11 16:25:26 -04002817 .destroy = xs_destroy,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002818 .set_connect_timeout = xs_tcp_set_connect_timeout,
Chuck Lever262ca072006-03-20 13:44:16 -05002819 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002820 .enable_swap = xs_enable_swap,
2821 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002822 .inject_disconnect = xs_inject_disconnect,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002823#ifdef CONFIG_SUNRPC_BACKCHANNEL
2824 .bc_setup = xprt_setup_bc,
Chuck Lever76566772015-10-24 17:28:32 -04002825 .bc_up = xs_tcp_bc_up,
Chuck Lever6b26cc82016-05-02 14:40:40 -04002826 .bc_maxpayload = xs_tcp_bc_maxpayload,
Chuck Lever42e5c3e2015-10-24 17:27:35 -04002827 .bc_free_rqst = xprt_free_bc_rqst,
2828 .bc_destroy = xprt_destroy_bc,
2829#endif
Chuck Levera246b012005-08-11 16:25:23 -04002830};
2831
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002832/*
2833 * The rpc_xprt_ops for the server backchannel
2834 */
2835
Chuck Leverd31ae252017-08-01 12:00:39 -04002836static const struct rpc_xprt_ops bc_tcp_ops = {
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002837 .reserve_xprt = xprt_reserve_xprt,
2838 .release_xprt = xprt_release_xprt,
Bryan Schumaker84e28a32012-09-24 13:39:01 -04002839 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -04002840 .free_slot = xprt_free_slot,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002841 .buf_alloc = bc_malloc,
2842 .buf_free = bc_free,
2843 .send_request = bc_send_request,
2844 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2845 .close = bc_close,
2846 .destroy = bc_destroy,
2847 .print_stats = xs_tcp_print_stats,
Jeff Laytond67fa4d2015-06-03 16:14:29 -04002848 .enable_swap = xs_enable_swap,
2849 .disable_swap = xs_disable_swap,
Chuck Lever4a068252015-05-11 14:02:25 -04002850 .inject_disconnect = xs_inject_disconnect,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002851};
2852
Chuck Lever92476852010-10-20 11:53:01 -04002853static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2854{
2855 static const struct sockaddr_in sin = {
2856 .sin_family = AF_INET,
2857 .sin_addr.s_addr = htonl(INADDR_ANY),
2858 };
2859 static const struct sockaddr_in6 sin6 = {
2860 .sin6_family = AF_INET6,
2861 .sin6_addr = IN6ADDR_ANY_INIT,
2862 };
2863
2864 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002865 case AF_LOCAL:
2866 break;
Chuck Lever92476852010-10-20 11:53:01 -04002867 case AF_INET:
2868 memcpy(sap, &sin, sizeof(sin));
2869 break;
2870 case AF_INET6:
2871 memcpy(sap, &sin6, sizeof(sin6));
2872 break;
2873 default:
2874 dprintk("RPC: %s: Bad address family\n", __func__);
2875 return -EAFNOSUPPORT;
2876 }
2877 return 0;
2878}
2879
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002880static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002881 unsigned int slot_table_size,
2882 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002883{
2884 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002885 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002886
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002887 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002888 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002889 return ERR_PTR(-EBADF);
2890 }
2891
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002892 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2893 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002894 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002895 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2896 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002897 return ERR_PTR(-ENOMEM);
2898 }
2899
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002900 new = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustedc1b012015-10-05 10:53:49 -04002901 mutex_init(&new->recv_mutex);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002902 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2903 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002904 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002905 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002906 else {
2907 int err;
2908 err = xs_init_anyaddr(args->dstaddr->sa_family,
2909 (struct sockaddr *)&new->srcaddr);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002910 if (err != 0) {
2911 xprt_free(xprt);
Chuck Lever92476852010-10-20 11:53:01 -04002912 return ERR_PTR(err);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002913 }
Chuck Lever92476852010-10-20 11:53:01 -04002914 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002915
2916 return xprt;
2917}
2918
Chuck Lever176e21e2011-05-09 15:22:44 -04002919static const struct rpc_timeout xs_local_default_timeout = {
2920 .to_initval = 10 * HZ,
2921 .to_maxval = 10 * HZ,
2922 .to_retries = 2,
2923};
2924
2925/**
2926 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2927 * @args: rpc transport creation arguments
2928 *
2929 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2930 */
2931static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2932{
2933 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2934 struct sock_xprt *transport;
2935 struct rpc_xprt *xprt;
2936 struct rpc_xprt *ret;
2937
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002938 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2939 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002940 if (IS_ERR(xprt))
2941 return xprt;
2942 transport = container_of(xprt, struct sock_xprt, xprt);
2943
2944 xprt->prot = 0;
2945 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2946 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2947
2948 xprt->bind_timeout = XS_BIND_TO;
2949 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2950 xprt->idle_timeout = XS_IDLE_DISC_TO;
2951
2952 xprt->ops = &xs_local_ops;
2953 xprt->timeout = &xs_local_default_timeout;
2954
Trond Myklebusta2648092015-10-06 17:03:00 -04002955 INIT_WORK(&transport->recv_worker, xs_local_data_receive_workfn);
NeilBrown93dc41b2013-10-31 16:14:36 +11002956 INIT_DELAYED_WORK(&transport->connect_worker,
2957 xs_dummy_setup_socket);
2958
Chuck Lever176e21e2011-05-09 15:22:44 -04002959 switch (sun->sun_family) {
2960 case AF_LOCAL:
2961 if (sun->sun_path[0] != '/') {
2962 dprintk("RPC: bad AF_LOCAL address: %s\n",
2963 sun->sun_path);
2964 ret = ERR_PTR(-EINVAL);
2965 goto out_err;
2966 }
2967 xprt_set_bound(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002968 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
J. Bruce Fields7073ea82013-02-21 10:14:22 -05002969 ret = ERR_PTR(xs_local_setup_socket(transport));
2970 if (ret)
2971 goto out_err;
Chuck Lever176e21e2011-05-09 15:22:44 -04002972 break;
2973 default:
2974 ret = ERR_PTR(-EAFNOSUPPORT);
2975 goto out_err;
2976 }
2977
2978 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2979 xprt->address_strings[RPC_DISPLAY_ADDR]);
2980
2981 if (try_module_get(THIS_MODULE))
2982 return xprt;
2983 ret = ERR_PTR(-EINVAL);
2984out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08002985 xs_xprt_free(xprt);
Chuck Lever176e21e2011-05-09 15:22:44 -04002986 return ret;
2987}
2988
Trond Myklebust2881ae72007-12-20 16:03:54 -05002989static const struct rpc_timeout xs_udp_default_timeout = {
2990 .to_initval = 5 * HZ,
2991 .to_maxval = 30 * HZ,
2992 .to_increment = 5 * HZ,
2993 .to_retries = 5,
2994};
2995
Chuck Lever9903cd12005-08-11 16:25:26 -04002996/**
2997 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002998 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002999 *
3000 */
Adrian Bunk483066d2007-10-24 18:24:02 +02003001static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04003002{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003003 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04003004 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05003005 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003006 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04003007
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003008 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
3009 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04003010 if (IS_ERR(xprt))
3011 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05003012 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04003013
Chuck Leverec739ef2006-08-22 20:06:15 -04003014 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07003015 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04003016 /* XXX: header size can vary due to auth type, IPv6, etc. */
3017 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
3018
Chuck Lever03bf4b72005-08-25 16:25:55 -07003019 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07003020 xprt->reestablish_timeout = XS_UDP_REEST_TO;
3021 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04003022
Chuck Lever262965f2005-08-11 16:25:56 -04003023 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04003024
Trond Myklebustba7392b2007-12-20 16:03:55 -05003025 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04003026
Trond Myklebustf9b2ee72015-10-06 16:26:05 -04003027 INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
Trond Myklebustedc1b012015-10-05 10:53:49 -04003028 INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
3029
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003030 switch (addr->sa_family) {
3031 case AF_INET:
3032 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
3033 xprt_set_bound(xprt);
3034
Chuck Lever9dc3b092009-08-09 15:09:46 -04003035 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003036 break;
3037 case AF_INET6:
3038 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
3039 xprt_set_bound(xprt);
3040
Chuck Lever9dc3b092009-08-09 15:09:46 -04003041 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003042 break;
3043 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003044 ret = ERR_PTR(-EAFNOSUPPORT);
3045 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003046 }
3047
Chuck Leverc740eff2009-08-09 15:09:46 -04003048 if (xprt_bound(xprt))
3049 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3050 xprt->address_strings[RPC_DISPLAY_ADDR],
3051 xprt->address_strings[RPC_DISPLAY_PORT],
3052 xprt->address_strings[RPC_DISPLAY_PROTO]);
3053 else
3054 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
3055 xprt->address_strings[RPC_DISPLAY_ADDR],
3056 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04003057
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003058 if (try_module_get(THIS_MODULE))
3059 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003060 ret = ERR_PTR(-EINVAL);
3061out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003062 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003063 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04003064}
3065
Trond Myklebust2881ae72007-12-20 16:03:54 -05003066static const struct rpc_timeout xs_tcp_default_timeout = {
3067 .to_initval = 60 * HZ,
3068 .to_maxval = 60 * HZ,
3069 .to_retries = 2,
3070};
3071
Chuck Lever9903cd12005-08-11 16:25:26 -04003072/**
3073 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02003074 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04003075 *
3076 */
Adrian Bunk483066d2007-10-24 18:24:02 +02003077static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04003078{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003079 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04003080 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05003081 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003082 struct rpc_xprt *ret;
Trond Myklebustb7993ce2013-04-14 11:42:00 -04003083 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
3084
3085 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
3086 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Levera246b012005-08-11 16:25:23 -04003087
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003088 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
Trond Myklebustb7993ce2013-04-14 11:42:00 -04003089 max_slot_table_size);
Chuck Leverc8541ec2006-10-17 14:44:27 -04003090 if (IS_ERR(xprt))
3091 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05003092 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04003093
Chuck Leverec739ef2006-08-22 20:06:15 -04003094 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07003095 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07003096 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04003097
Chuck Lever03bf4b72005-08-25 16:25:55 -07003098 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07003099 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
3100 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04003101
Chuck Lever262965f2005-08-11 16:25:56 -04003102 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05003103 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04003104
Trond Myklebust3851f1c2016-08-04 00:08:45 -04003105 xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05003106 xprt->connect_timeout = xprt->timeout->to_initval *
3107 (xprt->timeout->to_retries + 1);
Trond Myklebust3851f1c2016-08-04 00:08:45 -04003108
Trond Myklebustedc1b012015-10-05 10:53:49 -04003109 INIT_WORK(&transport->recv_worker, xs_tcp_data_receive_workfn);
3110 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
3111
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003112 switch (addr->sa_family) {
3113 case AF_INET:
3114 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
3115 xprt_set_bound(xprt);
3116
Chuck Lever9dc3b092009-08-09 15:09:46 -04003117 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003118 break;
3119 case AF_INET6:
3120 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
3121 xprt_set_bound(xprt);
3122
Chuck Lever9dc3b092009-08-09 15:09:46 -04003123 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003124 break;
3125 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003126 ret = ERR_PTR(-EAFNOSUPPORT);
3127 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04003128 }
3129
Chuck Leverc740eff2009-08-09 15:09:46 -04003130 if (xprt_bound(xprt))
3131 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3132 xprt->address_strings[RPC_DISPLAY_ADDR],
3133 xprt->address_strings[RPC_DISPLAY_PORT],
3134 xprt->address_strings[RPC_DISPLAY_PROTO]);
3135 else
3136 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
3137 xprt->address_strings[RPC_DISPLAY_ADDR],
3138 xprt->address_strings[RPC_DISPLAY_PROTO]);
3139
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003140 if (try_module_get(THIS_MODULE))
3141 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003142 ret = ERR_PTR(-EINVAL);
3143out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003144 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003145 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04003146}
Chuck Lever282b32e2006-12-05 16:35:51 -05003147
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003148/**
3149 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3150 * @args: rpc transport creation arguments
3151 *
3152 */
3153static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
3154{
3155 struct sockaddr *addr = args->dstaddr;
3156 struct rpc_xprt *xprt;
3157 struct sock_xprt *transport;
3158 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003159 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003160
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003161 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3162 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003163 if (IS_ERR(xprt))
3164 return xprt;
3165 transport = container_of(xprt, struct sock_xprt, xprt);
3166
3167 xprt->prot = IPPROTO_TCP;
3168 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
3169 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3170 xprt->timeout = &xs_tcp_default_timeout;
3171
3172 /* backchannel */
3173 xprt_set_bound(xprt);
3174 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003175 xprt->reestablish_timeout = 0;
3176 xprt->idle_timeout = 0;
3177
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003178 xprt->ops = &bc_tcp_ops;
3179
3180 switch (addr->sa_family) {
3181 case AF_INET:
3182 xs_format_peer_addresses(xprt, "tcp",
3183 RPCBIND_NETID_TCP);
3184 break;
3185 case AF_INET6:
3186 xs_format_peer_addresses(xprt, "tcp",
3187 RPCBIND_NETID_TCP6);
3188 break;
3189 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003190 ret = ERR_PTR(-EAFNOSUPPORT);
3191 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003192 }
3193
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04003194 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3195 xprt->address_strings[RPC_DISPLAY_ADDR],
3196 xprt->address_strings[RPC_DISPLAY_PORT],
3197 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003198
3199 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003200 * Once we've associated a backchannel xprt with a connection,
Weng Meiling28303ca2013-11-30 17:56:44 +08003201 * we want to keep it around as long as the connection lasts,
3202 * in case we need to start using it for a backchannel again;
3203 * this reference won't be dropped until bc_xprt is destroyed.
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003204 */
3205 xprt_get(xprt);
3206 args->bc_xprt->xpt_bc_xprt = xprt;
3207 xprt->bc_xprt = args->bc_xprt;
3208 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
3209 transport->sock = bc_sock->sk_sock;
3210 transport->inet = bc_sock->sk_sk;
3211
3212 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003213 * Since we don't want connections for the backchannel, we set
3214 * the xprt status to connected
3215 */
3216 xprt_set_connected(xprt);
3217
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003218 if (try_module_get(THIS_MODULE))
3219 return xprt;
Kinglong Mee642aab52014-03-24 12:00:28 +08003220
3221 args->bc_xprt->xpt_bc_xprt = NULL;
J. Bruce Fields39a9bea2016-05-17 12:38:21 -04003222 args->bc_xprt->xpt_bc_xps = NULL;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05003223 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003224 ret = ERR_PTR(-EINVAL);
3225out_err:
Kinglong Mee315f3812014-03-24 11:07:22 +08003226 xs_xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04003227 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003228}
3229
Chuck Lever176e21e2011-05-09 15:22:44 -04003230static struct xprt_class xs_local_transport = {
3231 .list = LIST_HEAD_INIT(xs_local_transport.list),
3232 .name = "named UNIX socket",
3233 .owner = THIS_MODULE,
3234 .ident = XPRT_TRANSPORT_LOCAL,
3235 .setup = xs_setup_local,
3236};
3237
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003238static struct xprt_class xs_udp_transport = {
3239 .list = LIST_HEAD_INIT(xs_udp_transport.list),
3240 .name = "udp",
3241 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003242 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003243 .setup = xs_setup_udp,
3244};
3245
3246static struct xprt_class xs_tcp_transport = {
3247 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
3248 .name = "tcp",
3249 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003250 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003251 .setup = xs_setup_tcp,
3252};
3253
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003254static struct xprt_class xs_bc_tcp_transport = {
3255 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3256 .name = "tcp NFSv4.1 backchannel",
3257 .owner = THIS_MODULE,
3258 .ident = XPRT_TRANSPORT_BC_TCP,
3259 .setup = xs_setup_bc_tcp,
3260};
3261
Chuck Lever282b32e2006-12-05 16:35:51 -05003262/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003263 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05003264 *
3265 */
3266int init_socket_xprt(void)
3267{
Jeff Laytonf895b252014-11-17 16:58:04 -05003268#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08003269 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08003270 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05003271#endif
3272
Chuck Lever176e21e2011-05-09 15:22:44 -04003273 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003274 xprt_register_transport(&xs_udp_transport);
3275 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003276 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003277
Chuck Lever282b32e2006-12-05 16:35:51 -05003278 return 0;
3279}
3280
3281/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003282 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05003283 *
3284 */
3285void cleanup_socket_xprt(void)
3286{
Jeff Laytonf895b252014-11-17 16:58:04 -05003287#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Leverfbf76682006-12-05 16:35:54 -05003288 if (sunrpc_table_header) {
3289 unregister_sysctl_table(sunrpc_table_header);
3290 sunrpc_table_header = NULL;
3291 }
3292#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003293
Chuck Lever176e21e2011-05-09 15:22:44 -04003294 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04003295 xprt_unregister_transport(&xs_udp_transport);
3296 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03003297 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05003298}
Trond Myklebustcbf11072009-08-09 15:06:19 -04003299
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003300static int param_set_uint_minmax(const char *val,
3301 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003302 unsigned int min, unsigned int max)
3303{
Daniel Walter00cfaa92014-06-21 13:06:38 +01003304 unsigned int num;
Trond Myklebustcbf11072009-08-09 15:06:19 -04003305 int ret;
3306
3307 if (!val)
3308 return -EINVAL;
Daniel Walter00cfaa92014-06-21 13:06:38 +01003309 ret = kstrtouint(val, 0, &num);
Dan Carpenter9761a242017-02-19 00:34:59 +03003310 if (ret)
3311 return ret;
3312 if (num < min || num > max)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003313 return -EINVAL;
3314 *((unsigned int *)kp->arg) = num;
3315 return 0;
3316}
3317
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003318static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003319{
Frank Sorensonffb6ca32016-07-08 16:35:25 -05003320 if (kp->arg == &xprt_min_resvport)
3321 return param_set_uint_minmax(val, kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003322 RPC_MIN_RESVPORT,
Frank Sorensonffb6ca32016-07-08 16:35:25 -05003323 xprt_max_resvport);
3324 return param_set_uint_minmax(val, kp,
3325 xprt_min_resvport,
Trond Myklebustcbf11072009-08-09 15:06:19 -04003326 RPC_MAX_RESVPORT);
3327}
3328
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303329static const struct kernel_param_ops param_ops_portnr = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003330 .set = param_set_portnr,
3331 .get = param_get_uint,
3332};
3333
Trond Myklebustcbf11072009-08-09 15:06:19 -04003334#define param_check_portnr(name, p) \
3335 __param_check(name, p, unsigned int);
3336
3337module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3338module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3339
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003340static int param_set_slot_table_size(const char *val,
3341 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003342{
3343 return param_set_uint_minmax(val, kp,
3344 RPC_MIN_SLOT_TABLE,
3345 RPC_MAX_SLOT_TABLE);
3346}
3347
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303348static const struct kernel_param_ops param_ops_slot_table_size = {
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003349 .set = param_set_slot_table_size,
3350 .get = param_get_uint,
3351};
3352
Trond Myklebustcbf11072009-08-09 15:06:19 -04003353#define param_check_slot_table_size(name, p) \
3354 __param_check(name, p, unsigned int);
3355
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003356static int param_set_max_slot_table_size(const char *val,
3357 const struct kernel_param *kp)
3358{
3359 return param_set_uint_minmax(val, kp,
3360 RPC_MIN_SLOT_TABLE,
3361 RPC_MAX_SLOT_TABLE_LIMIT);
3362}
3363
Luis R. Rodriguez9c278472015-05-27 11:09:38 +09303364static const struct kernel_param_ops param_ops_max_slot_table_size = {
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003365 .set = param_set_max_slot_table_size,
3366 .get = param_get_uint,
3367};
3368
3369#define param_check_max_slot_table_size(name, p) \
3370 __param_check(name, p, unsigned int);
3371
Trond Myklebustcbf11072009-08-09 15:06:19 -04003372module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3373 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003374module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3375 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003376module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3377 slot_table_size, 0644);
3378