Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2 | /* |
| 3 | * linux/net/sunrpc/xprtsock.c |
| 4 | * |
| 5 | * Client-side transport implementation for sockets. |
| 6 | * |
Alan Cox | 113aa83 | 2008-10-13 19:01:08 -0700 | [diff] [blame] | 7 | * TCP callback races fixes (C) 1998 Red Hat |
| 8 | * TCP send fixes (C) 1998 Red Hat |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 9 | * 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 Lever | 55aa4f5 | 2005-08-11 16:25:47 -0400 | [diff] [blame] | 15 | * |
| 16 | * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com> |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 17 | * |
| 18 | * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005. |
| 19 | * <gilles.quillard@bull.net> |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <linux/types.h> |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 23 | #include <linux/string.h> |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 24 | #include <linux/slab.h> |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 25 | #include <linux/module.h> |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 26 | #include <linux/capability.h> |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 27 | #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 Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 33 | #include <linux/un.h> |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 34 | #include <linux/udp.h> |
| 35 | #include <linux/tcp.h> |
| 36 | #include <linux/sunrpc/clnt.h> |
Jeff Layton | 5976687 | 2013-02-04 12:50:00 -0500 | [diff] [blame] | 37 | #include <linux/sunrpc/addr.h> |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 38 | #include <linux/sunrpc/sched.h> |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 39 | #include <linux/sunrpc/svcsock.h> |
\"Talpey, Thomas\ | 49c36fc | 2007-09-10 13:47:31 -0400 | [diff] [blame] | 40 | #include <linux/sunrpc/xprtsock.h> |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 41 | #include <linux/file.h> |
Trond Myklebust | 9e00abc | 2011-07-13 19:20:49 -0400 | [diff] [blame] | 42 | #ifdef CONFIG_SUNRPC_BACKCHANNEL |
Ricardo Labiaga | 44b98ef | 2009-04-01 09:23:02 -0400 | [diff] [blame] | 43 | #include <linux/sunrpc/bc_xprt.h> |
| 44 | #endif |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 45 | |
| 46 | #include <net/sock.h> |
| 47 | #include <net/checksum.h> |
| 48 | #include <net/udp.h> |
| 49 | #include <net/tcp.h> |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 50 | #include <linux/bvec.h> |
Trond Myklebust | 6a829eb | 2019-01-03 09:04:45 -0500 | [diff] [blame] | 51 | #include <linux/highmem.h> |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 52 | #include <linux/uio.h> |
Trond Myklebust | a1231fd | 2019-02-18 10:02:29 -0500 | [diff] [blame] | 53 | #include <linux/sched/mm.h> |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 54 | |
Trond Myklebust | 40b5ea0 | 2013-09-04 12:16:23 -0400 | [diff] [blame] | 55 | #include <trace/events/sunrpc.h> |
| 56 | |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 57 | #include "sunrpc.h" |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 58 | |
| 59 | static void xs_close(struct rpc_xprt *xprt); |
Trond Myklebust | 7196dbb | 2017-02-08 11:17:54 -0500 | [diff] [blame] | 60 | static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, |
| 61 | struct socket *sock); |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 62 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 63 | /* |
Chuck Lever | c556b75 | 2005-11-01 12:24:48 -0500 | [diff] [blame] | 64 | * xprtsock tunables |
| 65 | */ |
Trond Myklebust | 09acfea | 2012-03-11 15:22:54 -0400 | [diff] [blame] | 66 | static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE; |
| 67 | static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE; |
| 68 | static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE; |
Chuck Lever | c556b75 | 2005-11-01 12:24:48 -0500 | [diff] [blame] | 69 | |
Trond Myklebust | 09acfea | 2012-03-11 15:22:54 -0400 | [diff] [blame] | 70 | static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT; |
| 71 | static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT; |
Chuck Lever | c556b75 | 2005-11-01 12:24:48 -0500 | [diff] [blame] | 72 | |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 73 | #define XS_TCP_LINGER_TO (15U * HZ) |
Trond Myklebust | 25fe614 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 74 | static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO; |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 75 | |
Chuck Lever | c556b75 | 2005-11-01 12:24:48 -0500 | [diff] [blame] | 76 | /* |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 77 | * 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 Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 85 | static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE; |
| 86 | static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE; |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 87 | static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT; |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 88 | static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT; |
| 89 | static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT; |
| 90 | |
| 91 | static 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 Perches | fe2c633 | 2013-06-11 23:04:25 -0700 | [diff] [blame] | 97 | static struct ctl_table xs_tunables_table[] = { |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 98 | { |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 99 | .procname = "udp_slot_table_entries", |
| 100 | .data = &xprt_udp_slot_table_entries, |
| 101 | .maxlen = sizeof(unsigned int), |
| 102 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 103 | .proc_handler = proc_dointvec_minmax, |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 104 | .extra1 = &min_slot_table_size, |
| 105 | .extra2 = &max_slot_table_size |
| 106 | }, |
| 107 | { |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 108 | .procname = "tcp_slot_table_entries", |
| 109 | .data = &xprt_tcp_slot_table_entries, |
| 110 | .maxlen = sizeof(unsigned int), |
| 111 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 112 | .proc_handler = proc_dointvec_minmax, |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 113 | .extra1 = &min_slot_table_size, |
| 114 | .extra2 = &max_slot_table_size |
| 115 | }, |
| 116 | { |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 117 | .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 Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 126 | .procname = "min_resvport", |
| 127 | .data = &xprt_min_resvport, |
| 128 | .maxlen = sizeof(unsigned int), |
| 129 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 130 | .proc_handler = proc_dointvec_minmax, |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 131 | .extra1 = &xprt_min_resvport_limit, |
J. Bruce Fields | 826799e | 2018-10-18 15:27:02 -0400 | [diff] [blame] | 132 | .extra2 = &xprt_max_resvport_limit |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 133 | }, |
| 134 | { |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 135 | .procname = "max_resvport", |
| 136 | .data = &xprt_max_resvport, |
| 137 | .maxlen = sizeof(unsigned int), |
| 138 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 139 | .proc_handler = proc_dointvec_minmax, |
J. Bruce Fields | 826799e | 2018-10-18 15:27:02 -0400 | [diff] [blame] | 140 | .extra1 = &xprt_min_resvport_limit, |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 141 | .extra2 = &xprt_max_resvport_limit |
| 142 | }, |
| 143 | { |
Trond Myklebust | 25fe614 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 144 | .procname = "tcp_fin_timeout", |
| 145 | .data = &xs_tcp_fin_timeout, |
| 146 | .maxlen = sizeof(xs_tcp_fin_timeout), |
| 147 | .mode = 0644, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 148 | .proc_handler = proc_dointvec_jiffies, |
Trond Myklebust | 25fe614 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 149 | }, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 150 | { }, |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 151 | }; |
| 152 | |
Joe Perches | fe2c633 | 2013-06-11 23:04:25 -0700 | [diff] [blame] | 153 | static struct ctl_table sunrpc_table[] = { |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 154 | { |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 155 | .procname = "sunrpc", |
| 156 | .mode = 0555, |
| 157 | .child = xs_tunables_table |
| 158 | }, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 159 | { }, |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 160 | }; |
| 161 | |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 162 | /* |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 163 | * Wait duration for a reply from the RPC portmapper. |
| 164 | */ |
| 165 | #define XS_BIND_TO (60U * HZ) |
| 166 | |
| 167 | /* |
| 168 | * Delay if a UDP socket connect error occurs. This is most likely some |
| 169 | * kind of resource problem on the local host. |
| 170 | */ |
| 171 | #define XS_UDP_REEST_TO (2U * HZ) |
| 172 | |
| 173 | /* |
| 174 | * The reestablish timeout allows clients to delay for a bit before attempting |
| 175 | * to reconnect to a server that just dropped our connection. |
| 176 | * |
| 177 | * We implement an exponential backoff when trying to reestablish a TCP |
| 178 | * transport connection with the server. Some servers like to drop a TCP |
| 179 | * connection when they are overworked, so we start with a short timeout and |
| 180 | * increase over time if the server is down or not responding. |
| 181 | */ |
| 182 | #define XS_TCP_INIT_REEST_TO (3U * HZ) |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 183 | |
| 184 | /* |
| 185 | * TCP idle timeout; client drops the transport socket if it is idle |
| 186 | * for this long. Note that we also timeout UDP sockets to prevent |
| 187 | * holding port numbers when there is no RPC traffic. |
| 188 | */ |
| 189 | #define XS_IDLE_DISC_TO (5U * 60 * HZ) |
| 190 | |
Jeff Layton | f895b25 | 2014-11-17 16:58:04 -0500 | [diff] [blame] | 191 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 192 | # undef RPC_DEBUG_DATA |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 193 | # define RPCDBG_FACILITY RPCDBG_TRANS |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 194 | #endif |
| 195 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 196 | #ifdef RPC_DEBUG_DATA |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 197 | static void xs_pktdump(char *msg, u32 *packet, unsigned int count) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 198 | { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 199 | u8 *buf = (u8 *) packet; |
| 200 | int j; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 201 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 202 | dprintk("RPC: %s\n", msg); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 203 | for (j = 0; j < count && j < 128; j += 4) { |
| 204 | if (!(j & 31)) { |
| 205 | if (j) |
| 206 | dprintk("\n"); |
| 207 | dprintk("0x%04x ", j); |
| 208 | } |
| 209 | dprintk("%02x%02x%02x%02x ", |
| 210 | buf[j], buf[j+1], buf[j+2], buf[j+3]); |
| 211 | } |
| 212 | dprintk("\n"); |
| 213 | } |
| 214 | #else |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 215 | static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 216 | { |
| 217 | /* NOP */ |
| 218 | } |
| 219 | #endif |
| 220 | |
Trond Myklebust | 2118071 | 2013-12-31 13:22:59 -0500 | [diff] [blame] | 221 | static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) |
| 222 | { |
| 223 | return (struct rpc_xprt *) sk->sk_user_data; |
| 224 | } |
| 225 | |
Chuck Lever | 95392c5 | 2007-08-06 11:57:58 -0400 | [diff] [blame] | 226 | static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt) |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 227 | { |
Chuck Lever | 95392c5 | 2007-08-06 11:57:58 -0400 | [diff] [blame] | 228 | return (struct sockaddr *) &xprt->addr; |
| 229 | } |
| 230 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 231 | static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt) |
| 232 | { |
| 233 | return (struct sockaddr_un *) &xprt->addr; |
| 234 | } |
| 235 | |
Chuck Lever | 95392c5 | 2007-08-06 11:57:58 -0400 | [diff] [blame] | 236 | static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt) |
| 237 | { |
| 238 | return (struct sockaddr_in *) &xprt->addr; |
| 239 | } |
| 240 | |
| 241 | static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt) |
| 242 | { |
| 243 | return (struct sockaddr_in6 *) &xprt->addr; |
| 244 | } |
| 245 | |
Chuck Lever | c877b84 | 2009-08-09 15:09:36 -0400 | [diff] [blame] | 246 | static void xs_format_common_peer_addresses(struct rpc_xprt *xprt) |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 247 | { |
Chuck Lever | c877b84 | 2009-08-09 15:09:36 -0400 | [diff] [blame] | 248 | struct sockaddr *sap = xs_addr(xprt); |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 249 | struct sockaddr_in6 *sin6; |
| 250 | struct sockaddr_in *sin; |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 251 | struct sockaddr_un *sun; |
Chuck Lever | c877b84 | 2009-08-09 15:09:36 -0400 | [diff] [blame] | 252 | char buf[128]; |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 253 | |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 254 | switch (sap->sa_family) { |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 255 | case AF_LOCAL: |
| 256 | sun = xs_addr_un(xprt); |
| 257 | strlcpy(buf, sun->sun_path, sizeof(buf)); |
| 258 | xprt->address_strings[RPC_DISPLAY_ADDR] = |
| 259 | kstrdup(buf, GFP_KERNEL); |
| 260 | break; |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 261 | case AF_INET: |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 262 | (void)rpc_ntop(sap, buf, sizeof(buf)); |
| 263 | xprt->address_strings[RPC_DISPLAY_ADDR] = |
| 264 | kstrdup(buf, GFP_KERNEL); |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 265 | sin = xs_addr_in(xprt); |
Joe Perches | fc0b5791 | 2010-03-08 12:15:28 -0800 | [diff] [blame] | 266 | snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr)); |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 267 | break; |
| 268 | case AF_INET6: |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 269 | (void)rpc_ntop(sap, buf, sizeof(buf)); |
| 270 | xprt->address_strings[RPC_DISPLAY_ADDR] = |
| 271 | kstrdup(buf, GFP_KERNEL); |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 272 | sin6 = xs_addr_in6(xprt); |
Joe Perches | fc0b5791 | 2010-03-08 12:15:28 -0800 | [diff] [blame] | 273 | snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr); |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 274 | break; |
| 275 | default: |
| 276 | BUG(); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 277 | } |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 278 | |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 279 | xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 280 | } |
| 281 | |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 282 | static void xs_format_common_peer_ports(struct rpc_xprt *xprt) |
Chuck Lever | 4b6473f | 2007-08-06 11:57:12 -0400 | [diff] [blame] | 283 | { |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 284 | struct sockaddr *sap = xs_addr(xprt); |
| 285 | char buf[128]; |
Chuck Lever | 4b6473f | 2007-08-06 11:57:12 -0400 | [diff] [blame] | 286 | |
Joe Perches | 81160e66 | 2010-03-08 12:15:59 -0800 | [diff] [blame] | 287 | snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap)); |
Chuck Lever | c877b84 | 2009-08-09 15:09:36 -0400 | [diff] [blame] | 288 | xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL); |
Chuck Lever | 4b6473f | 2007-08-06 11:57:12 -0400 | [diff] [blame] | 289 | |
Joe Perches | 81160e66 | 2010-03-08 12:15:59 -0800 | [diff] [blame] | 290 | snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap)); |
Chuck Lever | c877b84 | 2009-08-09 15:09:36 -0400 | [diff] [blame] | 291 | xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL); |
| 292 | } |
Chuck Lever | 4b6473f | 2007-08-06 11:57:12 -0400 | [diff] [blame] | 293 | |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 294 | static void xs_format_peer_addresses(struct rpc_xprt *xprt, |
| 295 | const char *protocol, |
| 296 | const char *netid) |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 297 | { |
Chuck Lever | b454ae9 | 2008-01-07 18:34:48 -0500 | [diff] [blame] | 298 | xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; |
Chuck Lever | b454ae9 | 2008-01-07 18:34:48 -0500 | [diff] [blame] | 299 | xprt->address_strings[RPC_DISPLAY_NETID] = netid; |
Chuck Lever | c877b84 | 2009-08-09 15:09:36 -0400 | [diff] [blame] | 300 | xs_format_common_peer_addresses(xprt); |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 301 | xs_format_common_peer_ports(xprt); |
Chuck Lever | 4b6473f | 2007-08-06 11:57:12 -0400 | [diff] [blame] | 302 | } |
| 303 | |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 304 | static void xs_update_peer_port(struct rpc_xprt *xprt) |
Chuck Lever | 4b6473f | 2007-08-06 11:57:12 -0400 | [diff] [blame] | 305 | { |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 306 | kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]); |
| 307 | kfree(xprt->address_strings[RPC_DISPLAY_PORT]); |
Chuck Lever | 4b6473f | 2007-08-06 11:57:12 -0400 | [diff] [blame] | 308 | |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 309 | xs_format_common_peer_ports(xprt); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static void xs_free_peer_addresses(struct rpc_xprt *xprt) |
| 313 | { |
Chuck Lever | 33e01dc | 2008-01-14 12:32:20 -0500 | [diff] [blame] | 314 | unsigned int i; |
| 315 | |
| 316 | for (i = 0; i < RPC_DISPLAY_MAX; i++) |
| 317 | switch (i) { |
| 318 | case RPC_DISPLAY_PROTO: |
| 319 | case RPC_DISPLAY_NETID: |
| 320 | continue; |
| 321 | default: |
| 322 | kfree(xprt->address_strings[i]); |
| 323 | } |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 324 | } |
| 325 | |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 326 | static size_t |
| 327 | xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp) |
| 328 | { |
| 329 | size_t i,n; |
| 330 | |
Trond Myklebust | 16e5e90 | 2018-12-02 15:22:48 -0500 | [diff] [blame] | 331 | if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES)) |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 332 | return want; |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 333 | n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 334 | for (i = 0; i < n; i++) { |
| 335 | if (buf->pages[i]) |
| 336 | continue; |
| 337 | buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp); |
| 338 | if (!buf->pages[i]) { |
Trond Myklebust | 16e5e90 | 2018-12-02 15:22:48 -0500 | [diff] [blame] | 339 | i *= PAGE_SIZE; |
| 340 | return i > buf->page_base ? i - buf->page_base : 0; |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | return want; |
| 344 | } |
| 345 | |
| 346 | static ssize_t |
| 347 | xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek) |
| 348 | { |
| 349 | ssize_t ret; |
| 350 | if (seek != 0) |
| 351 | iov_iter_advance(&msg->msg_iter, seek); |
| 352 | ret = sock_recvmsg(sock, msg, flags); |
| 353 | return ret > 0 ? ret + seek : ret; |
| 354 | } |
| 355 | |
| 356 | static ssize_t |
| 357 | xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags, |
| 358 | struct kvec *kvec, size_t count, size_t seek) |
| 359 | { |
Al Viro | 0e9b4a8 | 2018-11-01 18:19:03 -0400 | [diff] [blame] | 360 | iov_iter_kvec(&msg->msg_iter, READ, kvec, 1, count); |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 361 | return xs_sock_recvmsg(sock, msg, flags, seek); |
| 362 | } |
| 363 | |
| 364 | static ssize_t |
| 365 | xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags, |
| 366 | struct bio_vec *bvec, unsigned long nr, size_t count, |
| 367 | size_t seek) |
| 368 | { |
Al Viro | 0e9b4a8 | 2018-11-01 18:19:03 -0400 | [diff] [blame] | 369 | iov_iter_bvec(&msg->msg_iter, READ, bvec, nr, count); |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 370 | return xs_sock_recvmsg(sock, msg, flags, seek); |
| 371 | } |
| 372 | |
| 373 | static ssize_t |
| 374 | xs_read_discard(struct socket *sock, struct msghdr *msg, int flags, |
| 375 | size_t count) |
| 376 | { |
Trond Myklebust | b76a5af | 2018-12-03 15:58:58 -0500 | [diff] [blame] | 377 | iov_iter_discard(&msg->msg_iter, READ, count); |
| 378 | return sock_recvmsg(sock, msg, flags); |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 379 | } |
| 380 | |
Trond Myklebust | 6a829eb | 2019-01-03 09:04:45 -0500 | [diff] [blame] | 381 | #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE |
| 382 | static void |
| 383 | xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek) |
| 384 | { |
| 385 | struct bvec_iter bi = { |
| 386 | .bi_size = count, |
| 387 | }; |
| 388 | struct bio_vec bv; |
| 389 | |
| 390 | bvec_iter_advance(bvec, &bi, seek & PAGE_MASK); |
| 391 | for_each_bvec(bv, bvec, bi, bi) |
| 392 | flush_dcache_page(bv.bv_page); |
| 393 | } |
| 394 | #else |
| 395 | static inline void |
| 396 | xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek) |
| 397 | { |
| 398 | } |
| 399 | #endif |
| 400 | |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 401 | static ssize_t |
| 402 | xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags, |
| 403 | struct xdr_buf *buf, size_t count, size_t seek, size_t *read) |
| 404 | { |
| 405 | size_t want, seek_init = seek, offset = 0; |
| 406 | ssize_t ret; |
| 407 | |
Trond Myklebust | e92053a | 2019-02-15 18:14:27 -0500 | [diff] [blame] | 408 | want = min_t(size_t, count, buf->head[0].iov_len); |
| 409 | if (seek < want) { |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 410 | ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek); |
| 411 | if (ret <= 0) |
| 412 | goto sock_err; |
| 413 | offset += ret; |
| 414 | if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) |
| 415 | goto out; |
| 416 | if (ret != want) |
Trond Myklebust | c443305 | 2018-12-04 07:52:11 -0500 | [diff] [blame] | 417 | goto out; |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 418 | seek = 0; |
| 419 | } else { |
Trond Myklebust | e92053a | 2019-02-15 18:14:27 -0500 | [diff] [blame] | 420 | seek -= want; |
| 421 | offset += want; |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 422 | } |
Trond Myklebust | 16e5e90 | 2018-12-02 15:22:48 -0500 | [diff] [blame] | 423 | |
| 424 | want = xs_alloc_sparse_pages(buf, |
| 425 | min_t(size_t, count - offset, buf->page_len), |
| 426 | GFP_NOWAIT); |
| 427 | if (seek < want) { |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 428 | ret = xs_read_bvec(sock, msg, flags, buf->bvec, |
| 429 | xdr_buf_pagecount(buf), |
| 430 | want + buf->page_base, |
| 431 | seek + buf->page_base); |
| 432 | if (ret <= 0) |
| 433 | goto sock_err; |
Trond Myklebust | 6a829eb | 2019-01-03 09:04:45 -0500 | [diff] [blame] | 434 | xs_flush_bvec(buf->bvec, ret, seek + buf->page_base); |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 435 | offset += ret - buf->page_base; |
| 436 | if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) |
| 437 | goto out; |
| 438 | if (ret != want) |
Trond Myklebust | c443305 | 2018-12-04 07:52:11 -0500 | [diff] [blame] | 439 | goto out; |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 440 | seek = 0; |
| 441 | } else { |
Trond Myklebust | 16e5e90 | 2018-12-02 15:22:48 -0500 | [diff] [blame] | 442 | seek -= want; |
| 443 | offset += want; |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 444 | } |
Trond Myklebust | 16e5e90 | 2018-12-02 15:22:48 -0500 | [diff] [blame] | 445 | |
Trond Myklebust | e92053a | 2019-02-15 18:14:27 -0500 | [diff] [blame] | 446 | want = min_t(size_t, count - offset, buf->tail[0].iov_len); |
| 447 | if (seek < want) { |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 448 | ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek); |
| 449 | if (ret <= 0) |
| 450 | goto sock_err; |
| 451 | offset += ret; |
| 452 | if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) |
| 453 | goto out; |
| 454 | if (ret != want) |
Trond Myklebust | c443305 | 2018-12-04 07:52:11 -0500 | [diff] [blame] | 455 | goto out; |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 456 | } else |
Trond Myklebust | e92053a | 2019-02-15 18:14:27 -0500 | [diff] [blame] | 457 | offset = seek_init; |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 458 | ret = -EMSGSIZE; |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 459 | out: |
| 460 | *read = offset - seek_init; |
| 461 | return ret; |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 462 | sock_err: |
| 463 | offset += seek; |
| 464 | goto out; |
| 465 | } |
| 466 | |
| 467 | static void |
| 468 | xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf) |
| 469 | { |
| 470 | if (!transport->recv.copied) { |
| 471 | if (buf->head[0].iov_len >= transport->recv.offset) |
| 472 | memcpy(buf->head[0].iov_base, |
| 473 | &transport->recv.xid, |
| 474 | transport->recv.offset); |
| 475 | transport->recv.copied = transport->recv.offset; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | static bool |
| 480 | xs_read_stream_request_done(struct sock_xprt *transport) |
| 481 | { |
| 482 | return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT); |
| 483 | } |
| 484 | |
Trond Myklebust | e92053a | 2019-02-15 18:14:27 -0500 | [diff] [blame] | 485 | static void |
| 486 | xs_read_stream_check_eor(struct sock_xprt *transport, |
| 487 | struct msghdr *msg) |
| 488 | { |
| 489 | if (xs_read_stream_request_done(transport)) |
| 490 | msg->msg_flags |= MSG_EOR; |
| 491 | } |
| 492 | |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 493 | static ssize_t |
| 494 | xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg, |
| 495 | int flags, struct rpc_rqst *req) |
| 496 | { |
| 497 | struct xdr_buf *buf = &req->rq_private_buf; |
| 498 | size_t want, read; |
| 499 | ssize_t ret; |
| 500 | |
| 501 | xs_read_header(transport, buf); |
| 502 | |
| 503 | want = transport->recv.len - transport->recv.offset; |
Trond Myklebust | e92053a | 2019-02-15 18:14:27 -0500 | [diff] [blame] | 504 | if (want != 0) { |
| 505 | ret = xs_read_xdr_buf(transport->sock, msg, flags, buf, |
| 506 | transport->recv.copied + want, |
| 507 | transport->recv.copied, |
| 508 | &read); |
| 509 | transport->recv.offset += read; |
| 510 | transport->recv.copied += read; |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 511 | } |
| 512 | |
Trond Myklebust | 727fcc6 | 2019-02-15 16:53:04 -0500 | [diff] [blame] | 513 | if (transport->recv.offset == transport->recv.len) |
| 514 | xs_read_stream_check_eor(transport, msg); |
| 515 | |
Trond Myklebust | e92053a | 2019-02-15 18:14:27 -0500 | [diff] [blame] | 516 | if (want == 0) |
| 517 | return 0; |
| 518 | |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 519 | switch (ret) { |
Trond Myklebust | c443305 | 2018-12-04 07:52:11 -0500 | [diff] [blame] | 520 | default: |
| 521 | break; |
Trond Myklebust | 26781ea | 2018-12-03 15:41:38 -0500 | [diff] [blame] | 522 | case -EFAULT: |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 523 | case -EMSGSIZE: |
Trond Myklebust | 26781ea | 2018-12-03 15:41:38 -0500 | [diff] [blame] | 524 | msg->msg_flags |= MSG_TRUNC; |
Trond Myklebust | c443305 | 2018-12-04 07:52:11 -0500 | [diff] [blame] | 525 | return read; |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 526 | case 0: |
| 527 | return -ESHUTDOWN; |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 528 | } |
Trond Myklebust | c443305 | 2018-12-04 07:52:11 -0500 | [diff] [blame] | 529 | return ret < 0 ? ret : read; |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | static size_t |
| 533 | xs_read_stream_headersize(bool isfrag) |
| 534 | { |
| 535 | if (isfrag) |
| 536 | return sizeof(__be32); |
| 537 | return 3 * sizeof(__be32); |
| 538 | } |
| 539 | |
| 540 | static ssize_t |
| 541 | xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg, |
| 542 | int flags, size_t want, size_t seek) |
| 543 | { |
| 544 | struct kvec kvec = { |
| 545 | .iov_base = &transport->recv.fraghdr, |
| 546 | .iov_len = want, |
| 547 | }; |
| 548 | return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek); |
| 549 | } |
| 550 | |
| 551 | #if defined(CONFIG_SUNRPC_BACKCHANNEL) |
| 552 | static ssize_t |
| 553 | xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags) |
| 554 | { |
| 555 | struct rpc_xprt *xprt = &transport->xprt; |
| 556 | struct rpc_rqst *req; |
| 557 | ssize_t ret; |
| 558 | |
| 559 | /* Look up and lock the request corresponding to the given XID */ |
| 560 | req = xprt_lookup_bc_request(xprt, transport->recv.xid); |
| 561 | if (!req) { |
| 562 | printk(KERN_WARNING "Callback slot table overflowed\n"); |
| 563 | return -ESHUTDOWN; |
| 564 | } |
| 565 | |
| 566 | ret = xs_read_stream_request(transport, msg, flags, req); |
| 567 | if (msg->msg_flags & (MSG_EOR|MSG_TRUNC)) |
Trond Myklebust | c443305 | 2018-12-04 07:52:11 -0500 | [diff] [blame] | 568 | xprt_complete_bc_request(req, transport->recv.copied); |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 569 | |
| 570 | return ret; |
| 571 | } |
| 572 | #else /* CONFIG_SUNRPC_BACKCHANNEL */ |
| 573 | static ssize_t |
| 574 | xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags) |
| 575 | { |
| 576 | return -ESHUTDOWN; |
| 577 | } |
| 578 | #endif /* CONFIG_SUNRPC_BACKCHANNEL */ |
| 579 | |
| 580 | static ssize_t |
| 581 | xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags) |
| 582 | { |
| 583 | struct rpc_xprt *xprt = &transport->xprt; |
| 584 | struct rpc_rqst *req; |
| 585 | ssize_t ret = 0; |
| 586 | |
| 587 | /* Look up and lock the request corresponding to the given XID */ |
| 588 | spin_lock(&xprt->queue_lock); |
| 589 | req = xprt_lookup_rqst(xprt, transport->recv.xid); |
| 590 | if (!req) { |
| 591 | msg->msg_flags |= MSG_TRUNC; |
| 592 | goto out; |
| 593 | } |
| 594 | xprt_pin_rqst(req); |
| 595 | spin_unlock(&xprt->queue_lock); |
| 596 | |
| 597 | ret = xs_read_stream_request(transport, msg, flags, req); |
| 598 | |
| 599 | spin_lock(&xprt->queue_lock); |
| 600 | if (msg->msg_flags & (MSG_EOR|MSG_TRUNC)) |
Trond Myklebust | c443305 | 2018-12-04 07:52:11 -0500 | [diff] [blame] | 601 | xprt_complete_rqst(req->rq_task, transport->recv.copied); |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 602 | xprt_unpin_rqst(req); |
| 603 | out: |
| 604 | spin_unlock(&xprt->queue_lock); |
| 605 | return ret; |
| 606 | } |
| 607 | |
| 608 | static ssize_t |
| 609 | xs_read_stream(struct sock_xprt *transport, int flags) |
| 610 | { |
| 611 | struct msghdr msg = { 0 }; |
| 612 | size_t want, read = 0; |
| 613 | ssize_t ret = 0; |
| 614 | |
| 615 | if (transport->recv.len == 0) { |
| 616 | want = xs_read_stream_headersize(transport->recv.copied != 0); |
| 617 | ret = xs_read_stream_header(transport, &msg, flags, want, |
| 618 | transport->recv.offset); |
| 619 | if (ret <= 0) |
| 620 | goto out_err; |
| 621 | transport->recv.offset = ret; |
Trond Myklebust | c443305 | 2018-12-04 07:52:11 -0500 | [diff] [blame] | 622 | if (transport->recv.offset != want) |
| 623 | return transport->recv.offset; |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 624 | transport->recv.len = be32_to_cpu(transport->recv.fraghdr) & |
| 625 | RPC_FRAGMENT_SIZE_MASK; |
| 626 | transport->recv.offset -= sizeof(transport->recv.fraghdr); |
| 627 | read = ret; |
| 628 | } |
| 629 | |
| 630 | switch (be32_to_cpu(transport->recv.calldir)) { |
Trond Myklebust | c443305 | 2018-12-04 07:52:11 -0500 | [diff] [blame] | 631 | default: |
| 632 | msg.msg_flags |= MSG_TRUNC; |
| 633 | break; |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 634 | case RPC_CALL: |
| 635 | ret = xs_read_stream_call(transport, &msg, flags); |
| 636 | break; |
| 637 | case RPC_REPLY: |
| 638 | ret = xs_read_stream_reply(transport, &msg, flags); |
| 639 | } |
| 640 | if (msg.msg_flags & MSG_TRUNC) { |
| 641 | transport->recv.calldir = cpu_to_be32(-1); |
| 642 | transport->recv.copied = -1; |
| 643 | } |
| 644 | if (ret < 0) |
| 645 | goto out_err; |
| 646 | read += ret; |
| 647 | if (transport->recv.offset < transport->recv.len) { |
Trond Myklebust | c443305 | 2018-12-04 07:52:11 -0500 | [diff] [blame] | 648 | if (!(msg.msg_flags & MSG_TRUNC)) |
| 649 | return read; |
Trond Myklebust | b76a5af | 2018-12-03 15:58:58 -0500 | [diff] [blame] | 650 | msg.msg_flags = 0; |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 651 | ret = xs_read_discard(transport->sock, &msg, flags, |
| 652 | transport->recv.len - transport->recv.offset); |
| 653 | if (ret <= 0) |
| 654 | goto out_err; |
| 655 | transport->recv.offset += ret; |
| 656 | read += ret; |
| 657 | if (transport->recv.offset != transport->recv.len) |
Trond Myklebust | c443305 | 2018-12-04 07:52:11 -0500 | [diff] [blame] | 658 | return read; |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 659 | } |
| 660 | if (xs_read_stream_request_done(transport)) { |
Trond Myklebust | c50b8ee | 2018-09-14 14:26:28 -0400 | [diff] [blame] | 661 | trace_xs_stream_read_request(transport); |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 662 | transport->recv.copied = 0; |
| 663 | } |
| 664 | transport->recv.offset = 0; |
| 665 | transport->recv.len = 0; |
| 666 | return read; |
| 667 | out_err: |
Trond Myklebust | 79462857 | 2018-12-03 18:49:00 -0500 | [diff] [blame] | 668 | return ret != 0 ? ret : -ESHUTDOWN; |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 669 | } |
| 670 | |
Trond Myklebust | 0ffe86f | 2019-01-30 14:51:26 -0500 | [diff] [blame] | 671 | static __poll_t xs_poll_socket(struct sock_xprt *transport) |
| 672 | { |
| 673 | return transport->sock->ops->poll(NULL, transport->sock, NULL); |
| 674 | } |
| 675 | |
| 676 | static bool xs_poll_socket_readable(struct sock_xprt *transport) |
| 677 | { |
| 678 | __poll_t events = xs_poll_socket(transport); |
| 679 | |
| 680 | return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP); |
| 681 | } |
| 682 | |
| 683 | static void xs_poll_check_readable(struct sock_xprt *transport) |
| 684 | { |
| 685 | |
| 686 | clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); |
| 687 | if (!xs_poll_socket_readable(transport)) |
| 688 | return; |
| 689 | if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) |
| 690 | queue_work(xprtiod_workqueue, &transport->recv_worker); |
| 691 | } |
| 692 | |
Trond Myklebust | c50b8ee | 2018-09-14 14:26:28 -0400 | [diff] [blame] | 693 | static void xs_stream_data_receive(struct sock_xprt *transport) |
| 694 | { |
| 695 | size_t read = 0; |
| 696 | ssize_t ret = 0; |
| 697 | |
| 698 | mutex_lock(&transport->recv_mutex); |
| 699 | if (transport->sock == NULL) |
| 700 | goto out; |
Trond Myklebust | c50b8ee | 2018-09-14 14:26:28 -0400 | [diff] [blame] | 701 | for (;;) { |
| 702 | ret = xs_read_stream(transport, MSG_DONTWAIT); |
Trond Myklebust | c443305 | 2018-12-04 07:52:11 -0500 | [diff] [blame] | 703 | if (ret < 0) |
Trond Myklebust | c50b8ee | 2018-09-14 14:26:28 -0400 | [diff] [blame] | 704 | break; |
| 705 | read += ret; |
| 706 | cond_resched(); |
| 707 | } |
Trond Myklebust | 5f52a9d | 2019-02-16 09:31:47 -0500 | [diff] [blame] | 708 | if (ret == -ESHUTDOWN) |
| 709 | kernel_sock_shutdown(transport->sock, SHUT_RDWR); |
| 710 | else |
| 711 | xs_poll_check_readable(transport); |
Trond Myklebust | c50b8ee | 2018-09-14 14:26:28 -0400 | [diff] [blame] | 712 | out: |
| 713 | mutex_unlock(&transport->recv_mutex); |
| 714 | trace_xs_stream_read_data(&transport->xprt, ret, read); |
| 715 | } |
| 716 | |
| 717 | static void xs_stream_data_receive_workfn(struct work_struct *work) |
| 718 | { |
| 719 | struct sock_xprt *transport = |
| 720 | container_of(work, struct sock_xprt, recv_worker); |
Trond Myklebust | a1231fd | 2019-02-18 10:02:29 -0500 | [diff] [blame] | 721 | unsigned int pflags = memalloc_nofs_save(); |
| 722 | |
Trond Myklebust | c50b8ee | 2018-09-14 14:26:28 -0400 | [diff] [blame] | 723 | xs_stream_data_receive(transport); |
Trond Myklebust | a1231fd | 2019-02-18 10:02:29 -0500 | [diff] [blame] | 724 | memalloc_nofs_restore(pflags); |
Trond Myklebust | c50b8ee | 2018-09-14 14:26:28 -0400 | [diff] [blame] | 725 | } |
| 726 | |
Trond Myklebust | 550aebf | 2018-09-14 14:32:45 -0400 | [diff] [blame] | 727 | static void |
| 728 | xs_stream_reset_connect(struct sock_xprt *transport) |
| 729 | { |
| 730 | transport->recv.offset = 0; |
| 731 | transport->recv.len = 0; |
| 732 | transport->recv.copied = 0; |
| 733 | transport->xmit.offset = 0; |
Trond Myklebust | ae05355 | 2019-02-20 14:56:20 -0500 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | static void |
| 737 | xs_stream_start_connect(struct sock_xprt *transport) |
| 738 | { |
Trond Myklebust | 550aebf | 2018-09-14 14:32:45 -0400 | [diff] [blame] | 739 | transport->xprt.stat.connect_count++; |
| 740 | transport->xprt.stat.connect_start = jiffies; |
| 741 | } |
| 742 | |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 743 | #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) |
| 744 | |
Trond Myklebust | e791f8e | 2019-02-19 12:39:40 -0500 | [diff] [blame^] | 745 | static int xs_sendmsg(struct socket *sock, struct msghdr *msg, size_t seek) |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 746 | { |
Trond Myklebust | e791f8e | 2019-02-19 12:39:40 -0500 | [diff] [blame^] | 747 | if (seek) |
| 748 | iov_iter_advance(&msg->msg_iter, seek); |
| 749 | return sock_sendmsg(sock, msg); |
| 750 | } |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 751 | |
Trond Myklebust | e791f8e | 2019-02-19 12:39:40 -0500 | [diff] [blame^] | 752 | static int xs_send_kvec(struct socket *sock, struct msghdr *msg, struct kvec *vec, size_t seek) |
| 753 | { |
| 754 | if (!vec) { |
| 755 | iov_iter_kvec(&msg->msg_iter, WRITE, NULL, 0, 0); |
| 756 | return sock_sendmsg(sock, msg); |
| 757 | } |
| 758 | iov_iter_kvec(&msg->msg_iter, WRITE, vec, 1, vec->iov_len); |
| 759 | return xs_sendmsg(sock, msg, seek); |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 760 | } |
| 761 | |
Jason Baron | f279cd0 | 2014-09-24 18:08:00 +0000 | [diff] [blame] | 762 | static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more, bool zerocopy, int *sent_p) |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 763 | { |
Trond Myklebust | a6b31d1 | 2013-11-08 16:03:50 -0500 | [diff] [blame] | 764 | ssize_t (*do_sendpage)(struct socket *sock, struct page *page, |
| 765 | int offset, size_t size, int flags); |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 766 | struct page **ppage; |
| 767 | unsigned int remainder; |
Jason Baron | f279cd0 | 2014-09-24 18:08:00 +0000 | [diff] [blame] | 768 | int err; |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 769 | |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 770 | remainder = xdr->page_len - base; |
| 771 | base += xdr->page_base; |
| 772 | ppage = xdr->pages + (base >> PAGE_SHIFT); |
| 773 | base &= ~PAGE_MASK; |
Trond Myklebust | a6b31d1 | 2013-11-08 16:03:50 -0500 | [diff] [blame] | 774 | do_sendpage = sock->ops->sendpage; |
| 775 | if (!zerocopy) |
| 776 | do_sendpage = sock_no_sendpage; |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 777 | for(;;) { |
| 778 | unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder); |
| 779 | int flags = XS_SENDMSG_FLAGS; |
| 780 | |
| 781 | remainder -= len; |
Trond Myklebust | 31303d6 | 2015-10-06 15:59:20 -0400 | [diff] [blame] | 782 | if (more) |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 783 | flags |= MSG_MORE; |
Trond Myklebust | 31303d6 | 2015-10-06 15:59:20 -0400 | [diff] [blame] | 784 | if (remainder != 0) |
| 785 | flags |= MSG_SENDPAGE_NOTLAST | MSG_MORE; |
Trond Myklebust | a6b31d1 | 2013-11-08 16:03:50 -0500 | [diff] [blame] | 786 | err = do_sendpage(sock, *ppage, base, len, flags); |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 787 | if (remainder == 0 || err != len) |
| 788 | break; |
Jason Baron | f279cd0 | 2014-09-24 18:08:00 +0000 | [diff] [blame] | 789 | *sent_p += err; |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 790 | ppage++; |
| 791 | base = 0; |
| 792 | } |
Jason Baron | f279cd0 | 2014-09-24 18:08:00 +0000 | [diff] [blame] | 793 | if (err > 0) { |
| 794 | *sent_p += err; |
| 795 | err = 0; |
| 796 | } |
| 797 | return err; |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 798 | } |
| 799 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 800 | /** |
| 801 | * xs_sendpages - write pages directly to a socket |
| 802 | * @sock: socket to send on |
| 803 | * @addr: UDP only -- address of destination |
| 804 | * @addrlen: UDP only -- length of destination address |
| 805 | * @xdr: buffer containing this request |
| 806 | * @base: starting position in the buffer |
Trond Myklebust | a6b31d1 | 2013-11-08 16:03:50 -0500 | [diff] [blame] | 807 | * @zerocopy: true if it is safe to use sendpage() |
Jason Baron | f279cd0 | 2014-09-24 18:08:00 +0000 | [diff] [blame] | 808 | * @sent_p: return the total number of bytes successfully queued for sending |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 809 | * |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 810 | */ |
Jason Baron | f279cd0 | 2014-09-24 18:08:00 +0000 | [diff] [blame] | 811 | static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, bool zerocopy, int *sent_p) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 812 | { |
Trond Myklebust | e791f8e | 2019-02-19 12:39:40 -0500 | [diff] [blame^] | 813 | struct msghdr msg = { |
| 814 | .msg_name = addr, |
| 815 | .msg_namelen = addrlen, |
| 816 | .msg_flags = XS_SENDMSG_FLAGS | MSG_MORE, |
| 817 | }; |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 818 | unsigned int remainder = xdr->len - base; |
Jason Baron | f279cd0 | 2014-09-24 18:08:00 +0000 | [diff] [blame] | 819 | int err = 0; |
| 820 | int sent = 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 821 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 822 | if (unlikely(!sock)) |
Trond Myklebust | fba91af | 2009-03-11 14:06:41 -0400 | [diff] [blame] | 823 | return -ENOTSOCK; |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 824 | |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 825 | if (base != 0) { |
| 826 | addr = NULL; |
| 827 | addrlen = 0; |
| 828 | } |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 829 | |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 830 | if (base < xdr->head[0].iov_len || addr != NULL) { |
| 831 | unsigned int len = xdr->head[0].iov_len - base; |
| 832 | remainder -= len; |
Trond Myklebust | e791f8e | 2019-02-19 12:39:40 -0500 | [diff] [blame^] | 833 | if (remainder == 0) |
| 834 | msg.msg_flags &= ~MSG_MORE; |
| 835 | err = xs_send_kvec(sock, &msg, &xdr->head[0], base); |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 836 | if (remainder == 0 || err != len) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 837 | goto out; |
Jason Baron | f279cd0 | 2014-09-24 18:08:00 +0000 | [diff] [blame] | 838 | *sent_p += err; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 839 | base = 0; |
| 840 | } else |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 841 | base -= xdr->head[0].iov_len; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 842 | |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 843 | if (base < xdr->page_len) { |
| 844 | unsigned int len = xdr->page_len - base; |
| 845 | remainder -= len; |
Jason Baron | f279cd0 | 2014-09-24 18:08:00 +0000 | [diff] [blame] | 846 | err = xs_send_pagedata(sock, xdr, base, remainder != 0, zerocopy, &sent); |
| 847 | *sent_p += sent; |
| 848 | if (remainder == 0 || sent != len) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 849 | goto out; |
| 850 | base = 0; |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 851 | } else |
| 852 | base -= xdr->page_len; |
| 853 | |
| 854 | if (base >= xdr->tail[0].iov_len) |
Jason Baron | f279cd0 | 2014-09-24 18:08:00 +0000 | [diff] [blame] | 855 | return 0; |
Trond Myklebust | e791f8e | 2019-02-19 12:39:40 -0500 | [diff] [blame^] | 856 | msg.msg_flags &= ~MSG_MORE; |
| 857 | err = xs_send_kvec(sock, &msg, &xdr->tail[0], base); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 858 | out: |
Jason Baron | f279cd0 | 2014-09-24 18:08:00 +0000 | [diff] [blame] | 859 | if (err > 0) { |
| 860 | *sent_p += err; |
| 861 | err = 0; |
| 862 | } |
| 863 | return err; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 864 | } |
| 865 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 866 | /** |
Trond Myklebust | c544577 | 2018-09-03 23:39:27 -0400 | [diff] [blame] | 867 | * xs_nospace - handle transmit was incomplete |
Trond Myklebust | 50f484e | 2018-08-30 13:27:29 -0400 | [diff] [blame] | 868 | * @req: pointer to RPC request |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 869 | * |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 870 | */ |
Trond Myklebust | c544577 | 2018-09-03 23:39:27 -0400 | [diff] [blame] | 871 | static int xs_nospace(struct rpc_rqst *req) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 872 | { |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 873 | struct rpc_xprt *xprt = req->rq_xprt; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 874 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Trond Myklebust | 06ea0bf | 2014-02-11 09:15:54 -0500 | [diff] [blame] | 875 | struct sock *sk = transport->inet; |
Trond Myklebust | 24ca9a8 | 2011-11-22 14:44:28 +0200 | [diff] [blame] | 876 | int ret = -EAGAIN; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 877 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 878 | dprintk("RPC: %5u xmit incomplete (%u left of %u)\n", |
Trond Myklebust | c544577 | 2018-09-03 23:39:27 -0400 | [diff] [blame] | 879 | req->rq_task->tk_pid, |
| 880 | req->rq_slen - transport->xmit.offset, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 881 | req->rq_slen); |
| 882 | |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 883 | /* Protect against races with write_space */ |
| 884 | spin_lock_bh(&xprt->transport_lock); |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 885 | |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 886 | /* Don't race with disconnect */ |
| 887 | if (xprt_connected(xprt)) { |
Trond Myklebust | 13331a5 | 2016-01-06 08:57:06 -0500 | [diff] [blame] | 888 | /* wait for more buffer space */ |
| 889 | sk->sk_write_pending++; |
Trond Myklebust | c544577 | 2018-09-03 23:39:27 -0400 | [diff] [blame] | 890 | xprt_wait_for_buffer_space(xprt); |
Trond Myklebust | 13331a5 | 2016-01-06 08:57:06 -0500 | [diff] [blame] | 891 | } else |
Trond Myklebust | 5e3771c | 2009-03-11 14:38:01 -0400 | [diff] [blame] | 892 | ret = -ENOTCONN; |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 893 | |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 894 | spin_unlock_bh(&xprt->transport_lock); |
Trond Myklebust | 06ea0bf | 2014-02-11 09:15:54 -0500 | [diff] [blame] | 895 | |
| 896 | /* Race breaker in case memory is freed before above code is called */ |
David Vrabel | d48f9ce | 2016-09-19 13:58:30 +0100 | [diff] [blame] | 897 | if (ret == -EAGAIN) { |
| 898 | struct socket_wq *wq; |
| 899 | |
| 900 | rcu_read_lock(); |
| 901 | wq = rcu_dereference(sk->sk_wq); |
| 902 | set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags); |
| 903 | rcu_read_unlock(); |
| 904 | |
| 905 | sk->sk_write_space(sk); |
| 906 | } |
Trond Myklebust | 5e3771c | 2009-03-11 14:38:01 -0400 | [diff] [blame] | 907 | return ret; |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 908 | } |
| 909 | |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 910 | static void |
| 911 | xs_stream_prepare_request(struct rpc_rqst *req) |
| 912 | { |
| 913 | req->rq_task->tk_status = xdr_alloc_bvec(&req->rq_rcv_buf, GFP_NOIO); |
| 914 | } |
| 915 | |
Chuck Lever | 61677ee | 2011-05-09 15:22:34 -0400 | [diff] [blame] | 916 | /* |
Trond Myklebust | 4cd34e7 | 2018-08-31 10:00:02 -0400 | [diff] [blame] | 917 | * Determine if the previous message in the stream was aborted before it |
| 918 | * could complete transmission. |
| 919 | */ |
| 920 | static bool |
| 921 | xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req) |
| 922 | { |
| 923 | return transport->xmit.offset != 0 && req->rq_bytes_sent == 0; |
| 924 | } |
| 925 | |
| 926 | /* |
Chuck Lever | 61677ee | 2011-05-09 15:22:34 -0400 | [diff] [blame] | 927 | * Construct a stream transport record marker in @buf. |
| 928 | */ |
| 929 | static inline void xs_encode_stream_record_marker(struct xdr_buf *buf) |
| 930 | { |
| 931 | u32 reclen = buf->len - sizeof(rpc_fraghdr); |
| 932 | rpc_fraghdr *base = buf->head[0].iov_base; |
| 933 | *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen); |
| 934 | } |
| 935 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 936 | /** |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 937 | * xs_local_send_request - write an RPC request to an AF_LOCAL socket |
Trond Myklebust | 50f484e | 2018-08-30 13:27:29 -0400 | [diff] [blame] | 938 | * @req: pointer to RPC request |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 939 | * |
| 940 | * Return values: |
| 941 | * 0: The request has been sent |
| 942 | * EAGAIN: The socket was blocked, please call again later to |
| 943 | * complete the request |
| 944 | * ENOTCONN: Caller needs to invoke connect logic then call again |
| 945 | * other: Some other error occured, the request was not sent |
| 946 | */ |
Trond Myklebust | adfa714 | 2018-09-03 23:58:59 -0400 | [diff] [blame] | 947 | static int xs_local_send_request(struct rpc_rqst *req) |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 948 | { |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 949 | struct rpc_xprt *xprt = req->rq_xprt; |
| 950 | struct sock_xprt *transport = |
| 951 | container_of(xprt, struct sock_xprt, xprt); |
| 952 | struct xdr_buf *xdr = &req->rq_snd_buf; |
| 953 | int status; |
Jason Baron | f279cd0 | 2014-09-24 18:08:00 +0000 | [diff] [blame] | 954 | int sent = 0; |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 955 | |
Trond Myklebust | 4cd34e7 | 2018-08-31 10:00:02 -0400 | [diff] [blame] | 956 | /* Close the stream if the previous transmission was incomplete */ |
| 957 | if (xs_send_request_was_aborted(transport, req)) { |
| 958 | xs_close(xprt); |
| 959 | return -ENOTCONN; |
| 960 | } |
| 961 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 962 | xs_encode_stream_record_marker(&req->rq_snd_buf); |
| 963 | |
| 964 | xs_pktdump("packet data:", |
| 965 | req->rq_svec->iov_base, req->rq_svec->iov_len); |
| 966 | |
Chuck Lever | 7821575 | 2018-03-05 15:13:07 -0500 | [diff] [blame] | 967 | req->rq_xtime = ktime_get(); |
Trond Myklebust | 6c7a64e | 2018-08-13 16:54:57 -0400 | [diff] [blame] | 968 | status = xs_sendpages(transport->sock, NULL, 0, xdr, |
| 969 | transport->xmit.offset, |
Jason Baron | f279cd0 | 2014-09-24 18:08:00 +0000 | [diff] [blame] | 970 | true, &sent); |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 971 | dprintk("RPC: %s(%u) = %d\n", |
Trond Myklebust | 6c7a64e | 2018-08-13 16:54:57 -0400 | [diff] [blame] | 972 | __func__, xdr->len - transport->xmit.offset, status); |
NeilBrown | 743c69e | 2015-07-27 10:55:35 +1000 | [diff] [blame] | 973 | |
| 974 | if (status == -EAGAIN && sock_writeable(transport->inet)) |
| 975 | status = -ENOBUFS; |
| 976 | |
Jason Baron | f279cd0 | 2014-09-24 18:08:00 +0000 | [diff] [blame] | 977 | if (likely(sent > 0) || status == 0) { |
Trond Myklebust | 6c7a64e | 2018-08-13 16:54:57 -0400 | [diff] [blame] | 978 | transport->xmit.offset += sent; |
| 979 | req->rq_bytes_sent = transport->xmit.offset; |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 980 | if (likely(req->rq_bytes_sent >= req->rq_slen)) { |
Trond Myklebust | 6c7a64e | 2018-08-13 16:54:57 -0400 | [diff] [blame] | 981 | req->rq_xmit_bytes_sent += transport->xmit.offset; |
Trond Myklebust | 6c7a64e | 2018-08-13 16:54:57 -0400 | [diff] [blame] | 982 | transport->xmit.offset = 0; |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 983 | return 0; |
| 984 | } |
| 985 | status = -EAGAIN; |
| 986 | } |
| 987 | |
| 988 | switch (status) { |
Trond Myklebust | 3601c4a | 2014-06-30 13:42:19 -0400 | [diff] [blame] | 989 | case -ENOBUFS: |
Trond Myklebust | b5872f0 | 2015-07-03 09:32:23 -0400 | [diff] [blame] | 990 | break; |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 991 | case -EAGAIN: |
Trond Myklebust | c544577 | 2018-09-03 23:39:27 -0400 | [diff] [blame] | 992 | status = xs_nospace(req); |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 993 | break; |
| 994 | default: |
| 995 | dprintk("RPC: sendmsg returned unrecognized error %d\n", |
| 996 | -status); |
Gustavo A. R. Silva | e9d4763 | 2017-10-20 11:48:30 -0500 | [diff] [blame] | 997 | /* fall through */ |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 998 | case -EPIPE: |
| 999 | xs_close(xprt); |
| 1000 | status = -ENOTCONN; |
| 1001 | } |
| 1002 | |
| 1003 | return status; |
| 1004 | } |
| 1005 | |
| 1006 | /** |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1007 | * xs_udp_send_request - write an RPC request to a UDP socket |
Trond Myklebust | 50f484e | 2018-08-30 13:27:29 -0400 | [diff] [blame] | 1008 | * @req: pointer to RPC request |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1009 | * |
| 1010 | * Return values: |
| 1011 | * 0: The request has been sent |
| 1012 | * EAGAIN: The socket was blocked, please call again later to |
| 1013 | * complete the request |
| 1014 | * ENOTCONN: Caller needs to invoke connect logic then call again |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1015 | * other: Some other error occurred, the request was not sent |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1016 | */ |
Trond Myklebust | adfa714 | 2018-09-03 23:58:59 -0400 | [diff] [blame] | 1017 | static int xs_udp_send_request(struct rpc_rqst *req) |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1018 | { |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1019 | struct rpc_xprt *xprt = req->rq_xprt; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 1020 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1021 | struct xdr_buf *xdr = &req->rq_snd_buf; |
Jason Baron | f279cd0 | 2014-09-24 18:08:00 +0000 | [diff] [blame] | 1022 | int sent = 0; |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1023 | int status; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1024 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1025 | xs_pktdump("packet data:", |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1026 | req->rq_svec->iov_base, |
| 1027 | req->rq_svec->iov_len); |
| 1028 | |
Trond Myklebust | 01d37c4 | 2009-03-11 14:09:39 -0400 | [diff] [blame] | 1029 | if (!xprt_bound(xprt)) |
| 1030 | return -ENOTCONN; |
Trond Myklebust | 75891f5 | 2018-09-03 17:37:36 -0400 | [diff] [blame] | 1031 | |
| 1032 | if (!xprt_request_get_cong(xprt, req)) |
| 1033 | return -EBADSLT; |
| 1034 | |
Chuck Lever | 7821575 | 2018-03-05 15:13:07 -0500 | [diff] [blame] | 1035 | req->rq_xtime = ktime_get(); |
Jason Baron | f279cd0 | 2014-09-24 18:08:00 +0000 | [diff] [blame] | 1036 | status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen, |
Trond Myklebust | 6c7a64e | 2018-08-13 16:54:57 -0400 | [diff] [blame] | 1037 | xdr, 0, true, &sent); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1038 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1039 | dprintk("RPC: xs_udp_send_request(%u) = %d\n", |
Trond Myklebust | 6c7a64e | 2018-08-13 16:54:57 -0400 | [diff] [blame] | 1040 | xdr->len, status); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1041 | |
Jason Baron | 3dedbb5 | 2014-09-24 18:08:04 +0000 | [diff] [blame] | 1042 | /* firewall is blocking us, don't return -EAGAIN or we end up looping */ |
| 1043 | if (status == -EPERM) |
| 1044 | goto process_status; |
| 1045 | |
NeilBrown | 743c69e | 2015-07-27 10:55:35 +1000 | [diff] [blame] | 1046 | if (status == -EAGAIN && sock_writeable(transport->inet)) |
| 1047 | status = -ENOBUFS; |
| 1048 | |
Jason Baron | f279cd0 | 2014-09-24 18:08:00 +0000 | [diff] [blame] | 1049 | if (sent > 0 || status == 0) { |
| 1050 | req->rq_xmit_bytes_sent += sent; |
| 1051 | if (sent >= req->rq_slen) |
Trond Myklebust | 2199700 | 2007-10-01 11:43:37 -0400 | [diff] [blame] | 1052 | return 0; |
| 1053 | /* Still some bytes left; set up for a retry later. */ |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1054 | status = -EAGAIN; |
Trond Myklebust | 2199700 | 2007-10-01 11:43:37 -0400 | [diff] [blame] | 1055 | } |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1056 | |
Jason Baron | 3dedbb5 | 2014-09-24 18:08:04 +0000 | [diff] [blame] | 1057 | process_status: |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1058 | switch (status) { |
Trond Myklebust | fba91af | 2009-03-11 14:06:41 -0400 | [diff] [blame] | 1059 | case -ENOTSOCK: |
| 1060 | status = -ENOTCONN; |
| 1061 | /* Should we call xs_close() here? */ |
| 1062 | break; |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 1063 | case -EAGAIN: |
Trond Myklebust | c544577 | 2018-09-03 23:39:27 -0400 | [diff] [blame] | 1064 | status = xs_nospace(req); |
Trond Myklebust | b6ddf64 | 2008-04-17 18:52:19 -0400 | [diff] [blame] | 1065 | break; |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1066 | case -ENETUNREACH: |
Trond Myklebust | 3601c4a | 2014-06-30 13:42:19 -0400 | [diff] [blame] | 1067 | case -ENOBUFS: |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1068 | case -EPIPE: |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1069 | case -ECONNREFUSED: |
Jason Baron | 3dedbb5 | 2014-09-24 18:08:04 +0000 | [diff] [blame] | 1070 | case -EPERM: |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1071 | /* When the server has died, an ICMP port unreachable message |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1072 | * prompts ECONNREFUSED. */ |
Trond Myklebust | 13331a5 | 2016-01-06 08:57:06 -0500 | [diff] [blame] | 1073 | break; |
| 1074 | default: |
| 1075 | dprintk("RPC: sendmsg returned unrecognized error %d\n", |
| 1076 | -status); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1077 | } |
Bian Naimeng | 5fe46e9 | 2010-03-08 14:49:01 +0800 | [diff] [blame] | 1078 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1079 | return status; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1080 | } |
| 1081 | |
Trond Myklebust | e06799f | 2007-11-05 15:44:12 -0500 | [diff] [blame] | 1082 | /** |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1083 | * xs_tcp_send_request - write an RPC request to a TCP socket |
Trond Myklebust | 50f484e | 2018-08-30 13:27:29 -0400 | [diff] [blame] | 1084 | * @req: pointer to RPC request |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1085 | * |
| 1086 | * Return values: |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1087 | * 0: The request has been sent |
| 1088 | * EAGAIN: The socket was blocked, please call again later to |
| 1089 | * complete the request |
| 1090 | * ENOTCONN: Caller needs to invoke connect logic then call again |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1091 | * other: Some other error occurred, the request was not sent |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1092 | * |
| 1093 | * XXX: In the case of soft timeouts, should we eventually give up |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1094 | * if sendmsg is not able to make progress? |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1095 | */ |
Trond Myklebust | adfa714 | 2018-09-03 23:58:59 -0400 | [diff] [blame] | 1096 | static int xs_tcp_send_request(struct rpc_rqst *req) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1097 | { |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1098 | struct rpc_xprt *xprt = req->rq_xprt; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 1099 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1100 | struct xdr_buf *xdr = &req->rq_snd_buf; |
Trond Myklebust | a6b31d1 | 2013-11-08 16:03:50 -0500 | [diff] [blame] | 1101 | bool zerocopy = true; |
Trond Myklebust | 9ffadfb | 2016-05-29 00:42:03 -0400 | [diff] [blame] | 1102 | bool vm_wait = false; |
Chuck Lever | b595bb1 | 2007-08-06 11:56:42 -0400 | [diff] [blame] | 1103 | int status; |
Jason Baron | f279cd0 | 2014-09-24 18:08:00 +0000 | [diff] [blame] | 1104 | int sent; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1105 | |
Trond Myklebust | 4cd34e7 | 2018-08-31 10:00:02 -0400 | [diff] [blame] | 1106 | /* Close the stream if the previous transmission was incomplete */ |
| 1107 | if (xs_send_request_was_aborted(transport, req)) { |
| 1108 | if (transport->sock != NULL) |
| 1109 | kernel_sock_shutdown(transport->sock, SHUT_RDWR); |
| 1110 | return -ENOTCONN; |
| 1111 | } |
| 1112 | |
Chuck Lever | 61677ee | 2011-05-09 15:22:34 -0400 | [diff] [blame] | 1113 | xs_encode_stream_record_marker(&req->rq_snd_buf); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1114 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1115 | xs_pktdump("packet data:", |
| 1116 | req->rq_svec->iov_base, |
| 1117 | req->rq_svec->iov_len); |
Trond Myklebust | a6b31d1 | 2013-11-08 16:03:50 -0500 | [diff] [blame] | 1118 | /* Don't use zero copy if this is a resend. If the RPC call |
| 1119 | * completes while the socket holds a reference to the pages, |
| 1120 | * then we may end up resending corrupted data. |
| 1121 | */ |
Trond Myklebust | 50f484e | 2018-08-30 13:27:29 -0400 | [diff] [blame] | 1122 | if (req->rq_task->tk_flags & RPC_TASK_SENT) |
Trond Myklebust | a6b31d1 | 2013-11-08 16:03:50 -0500 | [diff] [blame] | 1123 | zerocopy = false; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1124 | |
Trond Myklebust | 7196dbb | 2017-02-08 11:17:54 -0500 | [diff] [blame] | 1125 | if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state)) |
| 1126 | xs_tcp_set_socket_timeouts(xprt, transport->sock); |
| 1127 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1128 | /* Continue transmitting the packet/record. We must be careful |
| 1129 | * to cope with writespace callbacks arriving _after_ we have |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1130 | * called sendmsg(). */ |
Chuck Lever | 7821575 | 2018-03-05 15:13:07 -0500 | [diff] [blame] | 1131 | req->rq_xtime = ktime_get(); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1132 | while (1) { |
Jason Baron | f279cd0 | 2014-09-24 18:08:00 +0000 | [diff] [blame] | 1133 | sent = 0; |
| 1134 | status = xs_sendpages(transport->sock, NULL, 0, xdr, |
Trond Myklebust | 6c7a64e | 2018-08-13 16:54:57 -0400 | [diff] [blame] | 1135 | transport->xmit.offset, |
| 1136 | zerocopy, &sent); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1137 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1138 | dprintk("RPC: xs_tcp_send_request(%u) = %d\n", |
Trond Myklebust | 6c7a64e | 2018-08-13 16:54:57 -0400 | [diff] [blame] | 1139 | xdr->len - transport->xmit.offset, status); |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1140 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1141 | /* If we've sent the entire packet, immediately |
| 1142 | * reset the count of bytes sent. */ |
Trond Myklebust | 6c7a64e | 2018-08-13 16:54:57 -0400 | [diff] [blame] | 1143 | transport->xmit.offset += sent; |
| 1144 | req->rq_bytes_sent = transport->xmit.offset; |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1145 | if (likely(req->rq_bytes_sent >= req->rq_slen)) { |
Trond Myklebust | 6c7a64e | 2018-08-13 16:54:57 -0400 | [diff] [blame] | 1146 | req->rq_xmit_bytes_sent += transport->xmit.offset; |
Trond Myklebust | 6c7a64e | 2018-08-13 16:54:57 -0400 | [diff] [blame] | 1147 | transport->xmit.offset = 0; |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1148 | return 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1149 | } |
| 1150 | |
Trond Myklebust | 9ffadfb | 2016-05-29 00:42:03 -0400 | [diff] [blame] | 1151 | WARN_ON_ONCE(sent == 0 && status == 0); |
| 1152 | |
| 1153 | if (status == -EAGAIN ) { |
| 1154 | /* |
| 1155 | * Return EAGAIN if we're sure we're hitting the |
| 1156 | * socket send buffer limits. |
| 1157 | */ |
| 1158 | if (test_bit(SOCK_NOSPACE, &transport->sock->flags)) |
| 1159 | break; |
| 1160 | /* |
| 1161 | * Did we hit a memory allocation failure? |
| 1162 | */ |
| 1163 | if (sent == 0) { |
| 1164 | status = -ENOBUFS; |
| 1165 | if (vm_wait) |
| 1166 | break; |
| 1167 | /* Retry, knowing now that we're below the |
| 1168 | * socket send buffer limit |
| 1169 | */ |
| 1170 | vm_wait = true; |
| 1171 | } |
| 1172 | continue; |
| 1173 | } |
Trond Myklebust | f580dd0 | 2015-07-11 17:48:52 +0200 | [diff] [blame] | 1174 | if (status < 0) |
| 1175 | break; |
Trond Myklebust | 9ffadfb | 2016-05-29 00:42:03 -0400 | [diff] [blame] | 1176 | vm_wait = false; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1177 | } |
| 1178 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1179 | switch (status) { |
Trond Myklebust | fba91af | 2009-03-11 14:06:41 -0400 | [diff] [blame] | 1180 | case -ENOTSOCK: |
| 1181 | status = -ENOTCONN; |
| 1182 | /* Should we call xs_close() here? */ |
| 1183 | break; |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1184 | case -EAGAIN: |
Trond Myklebust | c544577 | 2018-09-03 23:39:27 -0400 | [diff] [blame] | 1185 | status = xs_nospace(req); |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1186 | break; |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1187 | case -ECONNRESET: |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1188 | case -ECONNREFUSED: |
| 1189 | case -ENOTCONN: |
Trond Myklebust | 3913c78 | 2015-02-08 21:44:04 -0500 | [diff] [blame] | 1190 | case -EADDRINUSE: |
Trond Myklebust | b5872f0 | 2015-07-03 09:32:23 -0400 | [diff] [blame] | 1191 | case -ENOBUFS: |
Trond Myklebust | b9d2bb2 | 2012-10-23 11:40:02 -0400 | [diff] [blame] | 1192 | case -EPIPE: |
Trond Myklebust | 13331a5 | 2016-01-06 08:57:06 -0500 | [diff] [blame] | 1193 | break; |
| 1194 | default: |
| 1195 | dprintk("RPC: sendmsg returned unrecognized error %d\n", |
| 1196 | -status); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1197 | } |
Bian Naimeng | 5fe46e9 | 2010-03-08 14:49:01 +0800 | [diff] [blame] | 1198 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1199 | return status; |
| 1200 | } |
| 1201 | |
Trond Myklebust | 2a9e1cf | 2008-10-28 15:21:39 -0400 | [diff] [blame] | 1202 | static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) |
| 1203 | { |
| 1204 | transport->old_data_ready = sk->sk_data_ready; |
| 1205 | transport->old_state_change = sk->sk_state_change; |
| 1206 | transport->old_write_space = sk->sk_write_space; |
Trond Myklebust | 2118071 | 2013-12-31 13:22:59 -0500 | [diff] [blame] | 1207 | transport->old_error_report = sk->sk_error_report; |
Trond Myklebust | 2a9e1cf | 2008-10-28 15:21:39 -0400 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) |
| 1211 | { |
| 1212 | sk->sk_data_ready = transport->old_data_ready; |
| 1213 | sk->sk_state_change = transport->old_state_change; |
| 1214 | sk->sk_write_space = transport->old_write_space; |
Trond Myklebust | 2118071 | 2013-12-31 13:22:59 -0500 | [diff] [blame] | 1215 | sk->sk_error_report = transport->old_error_report; |
| 1216 | } |
| 1217 | |
Trond Myklebust | 42d42a5 | 2016-05-23 09:24:55 -0400 | [diff] [blame] | 1218 | static void xs_sock_reset_state_flags(struct rpc_xprt *xprt) |
| 1219 | { |
| 1220 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 1221 | |
| 1222 | clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); |
| 1223 | } |
| 1224 | |
Trond Myklebust | b70ae91 | 2015-02-09 09:41:32 -0500 | [diff] [blame] | 1225 | static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt) |
| 1226 | { |
| 1227 | smp_mb__before_atomic(); |
| 1228 | clear_bit(XPRT_CLOSE_WAIT, &xprt->state); |
| 1229 | clear_bit(XPRT_CLOSING, &xprt->state); |
Trond Myklebust | 42d42a5 | 2016-05-23 09:24:55 -0400 | [diff] [blame] | 1230 | xs_sock_reset_state_flags(xprt); |
Trond Myklebust | b70ae91 | 2015-02-09 09:41:32 -0500 | [diff] [blame] | 1231 | smp_mb__after_atomic(); |
| 1232 | } |
| 1233 | |
Trond Myklebust | 2118071 | 2013-12-31 13:22:59 -0500 | [diff] [blame] | 1234 | /** |
| 1235 | * xs_error_report - callback to handle TCP socket state errors |
| 1236 | * @sk: socket |
| 1237 | * |
| 1238 | * Note: we don't call sock_error() since there may be a rpc_task |
| 1239 | * using the socket, and so we don't want to clear sk->sk_err. |
| 1240 | */ |
| 1241 | static void xs_error_report(struct sock *sk) |
| 1242 | { |
| 1243 | struct rpc_xprt *xprt; |
| 1244 | int err; |
| 1245 | |
| 1246 | read_lock_bh(&sk->sk_callback_lock); |
| 1247 | if (!(xprt = xprt_from_sock(sk))) |
| 1248 | goto out; |
| 1249 | |
| 1250 | err = -sk->sk_err; |
| 1251 | if (err == 0) |
| 1252 | goto out; |
| 1253 | dprintk("RPC: xs_error_report client %p, error=%d...\n", |
| 1254 | xprt, -err); |
Trond Myklebust | e8353c76 | 2013-12-31 13:39:22 -0500 | [diff] [blame] | 1255 | trace_rpc_socket_error(xprt, sk->sk_socket, err); |
Trond Myklebust | 2118071 | 2013-12-31 13:22:59 -0500 | [diff] [blame] | 1256 | xprt_wake_pending_tasks(xprt, err); |
| 1257 | out: |
| 1258 | read_unlock_bh(&sk->sk_callback_lock); |
Trond Myklebust | 2a9e1cf | 2008-10-28 15:21:39 -0400 | [diff] [blame] | 1259 | } |
| 1260 | |
Chuck Lever | fe315e76 | 2009-03-11 14:10:21 -0400 | [diff] [blame] | 1261 | static void xs_reset_transport(struct sock_xprt *transport) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1262 | { |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 1263 | struct socket *sock = transport->sock; |
| 1264 | struct sock *sk = transport->inet; |
Trond Myklebust | 6cc7e90 | 2015-02-08 18:35:25 -0500 | [diff] [blame] | 1265 | struct rpc_xprt *xprt = &transport->xprt; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1266 | |
Chuck Lever | fe315e76 | 2009-03-11 14:10:21 -0400 | [diff] [blame] | 1267 | if (sk == NULL) |
| 1268 | return; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1269 | |
Jeff Layton | 264d1df | 2015-06-03 16:14:27 -0400 | [diff] [blame] | 1270 | if (atomic_read(&transport->xprt.swapper)) |
| 1271 | sk_clear_memalloc(sk); |
| 1272 | |
Trond Myklebust | 0993920 | 2015-08-29 19:11:21 -0700 | [diff] [blame] | 1273 | kernel_sock_shutdown(sock, SHUT_RDWR); |
| 1274 | |
Trond Myklebust | edc1b01 | 2015-10-05 10:53:49 -0400 | [diff] [blame] | 1275 | mutex_lock(&transport->recv_mutex); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1276 | write_lock_bh(&sk->sk_callback_lock); |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 1277 | transport->inet = NULL; |
| 1278 | transport->sock = NULL; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1279 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1280 | sk->sk_user_data = NULL; |
Trond Myklebust | 2a9e1cf | 2008-10-28 15:21:39 -0400 | [diff] [blame] | 1281 | |
| 1282 | xs_restore_old_callbacks(transport, sk); |
Trond Myklebust | 0c78789 | 2015-08-29 13:36:30 -0700 | [diff] [blame] | 1283 | xprt_clear_connected(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1284 | write_unlock_bh(&sk->sk_callback_lock); |
Trond Myklebust | 6cc7e90 | 2015-02-08 18:35:25 -0500 | [diff] [blame] | 1285 | xs_sock_reset_connection_flags(xprt); |
Trond Myklebust | ae05355 | 2019-02-20 14:56:20 -0500 | [diff] [blame] | 1286 | /* Reset stream record info */ |
| 1287 | xs_stream_reset_connect(transport); |
Trond Myklebust | edc1b01 | 2015-10-05 10:53:49 -0400 | [diff] [blame] | 1288 | mutex_unlock(&transport->recv_mutex); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1289 | |
Trond Myklebust | 6cc7e90 | 2015-02-08 18:35:25 -0500 | [diff] [blame] | 1290 | trace_rpc_socket_close(xprt, sock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1291 | sock_release(sock); |
Trond Myklebust | 0445f92 | 2018-12-17 13:34:59 -0500 | [diff] [blame] | 1292 | |
| 1293 | xprt_disconnect_done(xprt); |
Chuck Lever | fe315e76 | 2009-03-11 14:10:21 -0400 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | /** |
| 1297 | * xs_close - close a socket |
| 1298 | * @xprt: transport |
| 1299 | * |
| 1300 | * This is used when all requests are complete; ie, no DRC state remains |
| 1301 | * on the server we want to save. |
Trond Myklebust | f75e674 | 2009-04-21 17:18:20 -0400 | [diff] [blame] | 1302 | * |
| 1303 | * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with |
| 1304 | * xs_reset_transport() zeroing the socket from underneath a writer. |
Chuck Lever | fe315e76 | 2009-03-11 14:10:21 -0400 | [diff] [blame] | 1305 | */ |
| 1306 | static void xs_close(struct rpc_xprt *xprt) |
| 1307 | { |
| 1308 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 1309 | |
| 1310 | dprintk("RPC: xs_close xprt %p\n", xprt); |
| 1311 | |
| 1312 | xs_reset_transport(transport); |
Neil Brown | 61d0a8e | 2009-09-23 14:36:37 -0400 | [diff] [blame] | 1313 | xprt->reestablish_timeout = 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1314 | } |
| 1315 | |
Chuck Lever | 4a06825 | 2015-05-11 14:02:25 -0400 | [diff] [blame] | 1316 | static void xs_inject_disconnect(struct rpc_xprt *xprt) |
| 1317 | { |
| 1318 | dprintk("RPC: injecting transport disconnect on xprt=%p\n", |
| 1319 | xprt); |
| 1320 | xprt_disconnect_done(xprt); |
| 1321 | } |
| 1322 | |
Kinglong Mee | 315f381 | 2014-03-24 11:07:22 +0800 | [diff] [blame] | 1323 | static void xs_xprt_free(struct rpc_xprt *xprt) |
| 1324 | { |
| 1325 | xs_free_peer_addresses(xprt); |
| 1326 | xprt_free(xprt); |
| 1327 | } |
| 1328 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1329 | /** |
| 1330 | * xs_destroy - prepare to shutdown a transport |
| 1331 | * @xprt: doomed transport |
| 1332 | * |
| 1333 | */ |
| 1334 | static void xs_destroy(struct rpc_xprt *xprt) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1335 | { |
Trond Myklebust | 03c78827 | 2015-09-17 10:42:27 -0400 | [diff] [blame] | 1336 | struct sock_xprt *transport = container_of(xprt, |
| 1337 | struct sock_xprt, xprt); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1338 | dprintk("RPC: xs_destroy xprt %p\n", xprt); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1339 | |
Trond Myklebust | 03c78827 | 2015-09-17 10:42:27 -0400 | [diff] [blame] | 1340 | cancel_delayed_work_sync(&transport->connect_worker); |
Trond Myklebust | a1311d8 | 2013-10-31 09:18:49 -0400 | [diff] [blame] | 1341 | xs_close(xprt); |
Trond Myklebust | edc1b01 | 2015-10-05 10:53:49 -0400 | [diff] [blame] | 1342 | cancel_work_sync(&transport->recv_worker); |
Kinglong Mee | 315f381 | 2014-03-24 11:07:22 +0800 | [diff] [blame] | 1343 | xs_xprt_free(xprt); |
Trond Myklebust | a1311d8 | 2013-10-31 09:18:49 -0400 | [diff] [blame] | 1344 | module_put(THIS_MODULE); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1345 | } |
| 1346 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1347 | /** |
Trond Myklebust | f9b2ee7 | 2015-10-06 16:26:05 -0400 | [diff] [blame] | 1348 | * xs_udp_data_read_skb - receive callback for UDP sockets |
| 1349 | * @xprt: transport |
| 1350 | * @sk: socket |
| 1351 | * @skb: skbuff |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1352 | * |
| 1353 | */ |
Trond Myklebust | f9b2ee7 | 2015-10-06 16:26:05 -0400 | [diff] [blame] | 1354 | static void xs_udp_data_read_skb(struct rpc_xprt *xprt, |
| 1355 | struct sock *sk, |
| 1356 | struct sk_buff *skb) |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1357 | { |
| 1358 | struct rpc_task *task; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1359 | struct rpc_rqst *rovr; |
Trond Myklebust | f9b2ee7 | 2015-10-06 16:26:05 -0400 | [diff] [blame] | 1360 | int repsize, copied; |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1361 | u32 _xid; |
| 1362 | __be32 *xp; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1363 | |
Willem de Bruijn | 1da8c681 | 2016-04-07 11:44:58 -0400 | [diff] [blame] | 1364 | repsize = skb->len; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1365 | if (repsize < 4) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1366 | dprintk("RPC: impossible RPC reply size %d!\n", repsize); |
Trond Myklebust | f9b2ee7 | 2015-10-06 16:26:05 -0400 | [diff] [blame] | 1367 | return; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | /* Copy the XID from the skb... */ |
Willem de Bruijn | 1da8c681 | 2016-04-07 11:44:58 -0400 | [diff] [blame] | 1371 | xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1372 | if (xp == NULL) |
Trond Myklebust | f9b2ee7 | 2015-10-06 16:26:05 -0400 | [diff] [blame] | 1373 | return; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1374 | |
| 1375 | /* Look up and lock the request corresponding to the given XID */ |
Trond Myklebust | 75c8415 | 2018-08-31 10:21:00 -0400 | [diff] [blame] | 1376 | spin_lock(&xprt->queue_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1377 | rovr = xprt_lookup_rqst(xprt, *xp); |
| 1378 | if (!rovr) |
| 1379 | goto out_unlock; |
Trond Myklebust | 729749b | 2017-08-13 10:03:59 -0400 | [diff] [blame] | 1380 | xprt_pin_rqst(rovr); |
Chuck Lever | ecd465e | 2018-03-05 15:12:57 -0500 | [diff] [blame] | 1381 | xprt_update_rtt(rovr->rq_task); |
Trond Myklebust | 75c8415 | 2018-08-31 10:21:00 -0400 | [diff] [blame] | 1382 | spin_unlock(&xprt->queue_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1383 | task = rovr->rq_task; |
| 1384 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1385 | if ((copied = rovr->rq_private_buf.buflen) > repsize) |
| 1386 | copied = repsize; |
| 1387 | |
| 1388 | /* Suck it into the iovec, verify checksum if not done by hw. */ |
Herbert Xu | 1781f7f | 2007-12-11 11:30:32 -0800 | [diff] [blame] | 1389 | if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { |
Trond Myklebust | 75c8415 | 2018-08-31 10:21:00 -0400 | [diff] [blame] | 1390 | spin_lock(&xprt->queue_lock); |
Trond Myklebust | 0afa6b4 | 2018-02-09 09:39:42 -0500 | [diff] [blame] | 1391 | __UDPX_INC_STATS(sk, UDP_MIB_INERRORS); |
Trond Myklebust | 729749b | 2017-08-13 10:03:59 -0400 | [diff] [blame] | 1392 | goto out_unpin; |
Herbert Xu | 1781f7f | 2007-12-11 11:30:32 -0800 | [diff] [blame] | 1393 | } |
| 1394 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1395 | |
Trond Myklebust | 729749b | 2017-08-13 10:03:59 -0400 | [diff] [blame] | 1396 | spin_lock_bh(&xprt->transport_lock); |
Trond Myklebust | 6a24dfb | 2013-01-08 09:48:15 -0500 | [diff] [blame] | 1397 | xprt_adjust_cwnd(xprt, task, copied); |
Trond Myklebust | ce7c252 | 2017-08-16 15:30:35 -0400 | [diff] [blame] | 1398 | spin_unlock_bh(&xprt->transport_lock); |
Trond Myklebust | 75c8415 | 2018-08-31 10:21:00 -0400 | [diff] [blame] | 1399 | spin_lock(&xprt->queue_lock); |
Chuck Lever | 1570c1e | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 1400 | xprt_complete_rqst(task, copied); |
Trond Myklebust | 0afa6b4 | 2018-02-09 09:39:42 -0500 | [diff] [blame] | 1401 | __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS); |
Trond Myklebust | 729749b | 2017-08-13 10:03:59 -0400 | [diff] [blame] | 1402 | out_unpin: |
| 1403 | xprt_unpin_rqst(rovr); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1404 | out_unlock: |
Trond Myklebust | 75c8415 | 2018-08-31 10:21:00 -0400 | [diff] [blame] | 1405 | spin_unlock(&xprt->queue_lock); |
Trond Myklebust | f9b2ee7 | 2015-10-06 16:26:05 -0400 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | static void xs_udp_data_receive(struct sock_xprt *transport) |
| 1409 | { |
| 1410 | struct sk_buff *skb; |
| 1411 | struct sock *sk; |
| 1412 | int err; |
| 1413 | |
| 1414 | mutex_lock(&transport->recv_mutex); |
| 1415 | sk = transport->inet; |
| 1416 | if (sk == NULL) |
| 1417 | goto out; |
| 1418 | for (;;) { |
Paolo Abeni | 7c13f97 | 2016-11-04 11:28:59 +0100 | [diff] [blame] | 1419 | skb = skb_recv_udp(sk, 0, 1, &err); |
Trond Myklebust | 4f54614 | 2018-09-14 17:45:23 -0400 | [diff] [blame] | 1420 | if (skb == NULL) |
Trond Myklebust | f9b2ee7 | 2015-10-06 16:26:05 -0400 | [diff] [blame] | 1421 | break; |
Trond Myklebust | 4f54614 | 2018-09-14 17:45:23 -0400 | [diff] [blame] | 1422 | xs_udp_data_read_skb(&transport->xprt, sk, skb); |
| 1423 | consume_skb(skb); |
| 1424 | cond_resched(); |
Trond Myklebust | f9b2ee7 | 2015-10-06 16:26:05 -0400 | [diff] [blame] | 1425 | } |
Trond Myklebust | 0ffe86f | 2019-01-30 14:51:26 -0500 | [diff] [blame] | 1426 | xs_poll_check_readable(transport); |
Trond Myklebust | f9b2ee7 | 2015-10-06 16:26:05 -0400 | [diff] [blame] | 1427 | out: |
| 1428 | mutex_unlock(&transport->recv_mutex); |
| 1429 | } |
| 1430 | |
| 1431 | static void xs_udp_data_receive_workfn(struct work_struct *work) |
| 1432 | { |
| 1433 | struct sock_xprt *transport = |
| 1434 | container_of(work, struct sock_xprt, recv_worker); |
Trond Myklebust | a1231fd | 2019-02-18 10:02:29 -0500 | [diff] [blame] | 1435 | unsigned int pflags = memalloc_nofs_save(); |
| 1436 | |
Trond Myklebust | f9b2ee7 | 2015-10-06 16:26:05 -0400 | [diff] [blame] | 1437 | xs_udp_data_receive(transport); |
Trond Myklebust | a1231fd | 2019-02-18 10:02:29 -0500 | [diff] [blame] | 1438 | memalloc_nofs_restore(pflags); |
Trond Myklebust | f9b2ee7 | 2015-10-06 16:26:05 -0400 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | /** |
| 1442 | * xs_data_ready - "data ready" callback for UDP sockets |
| 1443 | * @sk: socket with data to read |
| 1444 | * |
| 1445 | */ |
| 1446 | static void xs_data_ready(struct sock *sk) |
| 1447 | { |
| 1448 | struct rpc_xprt *xprt; |
| 1449 | |
| 1450 | read_lock_bh(&sk->sk_callback_lock); |
| 1451 | dprintk("RPC: xs_data_ready...\n"); |
| 1452 | xprt = xprt_from_sock(sk); |
| 1453 | if (xprt != NULL) { |
| 1454 | struct sock_xprt *transport = container_of(xprt, |
| 1455 | struct sock_xprt, xprt); |
Trond Myklebust | 5157b95 | 2016-05-29 10:13:24 -0400 | [diff] [blame] | 1456 | transport->old_data_ready(sk); |
| 1457 | /* Any data means we had a useful conversation, so |
| 1458 | * then we don't need to delay the next reconnect |
| 1459 | */ |
| 1460 | if (xprt->reestablish_timeout) |
| 1461 | xprt->reestablish_timeout = 0; |
Trond Myklebust | 42d42a5 | 2016-05-23 09:24:55 -0400 | [diff] [blame] | 1462 | if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) |
Trond Myklebust | 40a5f1b | 2016-05-27 10:39:50 -0400 | [diff] [blame] | 1463 | queue_work(xprtiod_workqueue, &transport->recv_worker); |
Trond Myklebust | f9b2ee7 | 2015-10-06 16:26:05 -0400 | [diff] [blame] | 1464 | } |
Eric Dumazet | f064af1 | 2010-09-22 12:43:39 +0000 | [diff] [blame] | 1465 | read_unlock_bh(&sk->sk_callback_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1466 | } |
| 1467 | |
Trond Myklebust | a519fc7 | 2012-09-12 16:49:15 -0400 | [diff] [blame] | 1468 | /* |
| 1469 | * Helper function to force a TCP close if the server is sending |
| 1470 | * junk and/or it has put us in CLOSE_WAIT |
| 1471 | */ |
| 1472 | static void xs_tcp_force_close(struct rpc_xprt *xprt) |
| 1473 | { |
Trond Myklebust | a519fc7 | 2012-09-12 16:49:15 -0400 | [diff] [blame] | 1474 | xprt_force_disconnect(xprt); |
| 1475 | } |
| 1476 | |
Trond Myklebust | 9e00abc | 2011-07-13 19:20:49 -0400 | [diff] [blame] | 1477 | #if defined(CONFIG_SUNRPC_BACKCHANNEL) |
Chuck Lever | 6b26cc8 | 2016-05-02 14:40:40 -0400 | [diff] [blame] | 1478 | static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt) |
| 1479 | { |
| 1480 | return PAGE_SIZE; |
| 1481 | } |
Trond Myklebust | 9e00abc | 2011-07-13 19:20:49 -0400 | [diff] [blame] | 1482 | #endif /* CONFIG_SUNRPC_BACKCHANNEL */ |
Ricardo Labiaga | 44b98ef | 2009-04-01 09:23:02 -0400 | [diff] [blame] | 1483 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1484 | /** |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1485 | * xs_tcp_state_change - callback to handle TCP socket state changes |
| 1486 | * @sk: socket whose state has changed |
| 1487 | * |
| 1488 | */ |
| 1489 | static void xs_tcp_state_change(struct sock *sk) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1490 | { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1491 | struct rpc_xprt *xprt; |
Trond Myklebust | 0fdea1e | 2015-09-16 23:43:17 -0400 | [diff] [blame] | 1492 | struct sock_xprt *transport; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1493 | |
Eric Dumazet | f064af1 | 2010-09-22 12:43:39 +0000 | [diff] [blame] | 1494 | read_lock_bh(&sk->sk_callback_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1495 | if (!(xprt = xprt_from_sock(sk))) |
| 1496 | goto out; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1497 | dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); |
Andy Chittenden | 669502f | 2010-08-10 10:19:53 -0400 | [diff] [blame] | 1498 | dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n", |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1499 | sk->sk_state, xprt_connected(xprt), |
| 1500 | sock_flag(sk, SOCK_DEAD), |
Andy Chittenden | 669502f | 2010-08-10 10:19:53 -0400 | [diff] [blame] | 1501 | sock_flag(sk, SOCK_ZAPPED), |
| 1502 | sk->sk_shutdown); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1503 | |
Trond Myklebust | 0fdea1e | 2015-09-16 23:43:17 -0400 | [diff] [blame] | 1504 | transport = container_of(xprt, struct sock_xprt, xprt); |
Trond Myklebust | 40b5ea0 | 2013-09-04 12:16:23 -0400 | [diff] [blame] | 1505 | trace_rpc_socket_state_change(xprt, sk->sk_socket); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1506 | switch (sk->sk_state) { |
| 1507 | case TCP_ESTABLISHED: |
Eric Dumazet | f064af1 | 2010-09-22 12:43:39 +0000 | [diff] [blame] | 1508 | spin_lock(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1509 | if (!xprt_test_and_set_connected(xprt)) { |
Trond Myklebust | 8b71798 | 2013-09-26 10:18:04 -0400 | [diff] [blame] | 1510 | xprt->connect_cookie++; |
Trond Myklebust | 0fdea1e | 2015-09-16 23:43:17 -0400 | [diff] [blame] | 1511 | clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); |
| 1512 | xprt_clear_connecting(xprt); |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1513 | |
Chuck Lever | 3968a8a | 2018-10-01 14:25:36 -0400 | [diff] [blame] | 1514 | xprt->stat.connect_count++; |
| 1515 | xprt->stat.connect_time += (long)jiffies - |
| 1516 | xprt->stat.connect_start; |
Trond Myklebust | 2a49199 | 2009-03-11 14:38:00 -0400 | [diff] [blame] | 1517 | xprt_wake_pending_tasks(xprt, -EAGAIN); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1518 | } |
Eric Dumazet | f064af1 | 2010-09-22 12:43:39 +0000 | [diff] [blame] | 1519 | spin_unlock(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1520 | break; |
Trond Myklebust | 3b948ae | 2007-11-05 17:42:39 -0500 | [diff] [blame] | 1521 | case TCP_FIN_WAIT1: |
| 1522 | /* The client initiated a shutdown of the socket */ |
Trond Myklebust | 7c1d71c | 2008-04-17 16:52:57 -0400 | [diff] [blame] | 1523 | xprt->connect_cookie++; |
Trond Myklebust | 663b885 | 2008-01-01 18:42:12 -0500 | [diff] [blame] | 1524 | xprt->reestablish_timeout = 0; |
Trond Myklebust | 3b948ae | 2007-11-05 17:42:39 -0500 | [diff] [blame] | 1525 | set_bit(XPRT_CLOSING, &xprt->state); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 1526 | smp_mb__before_atomic(); |
Trond Myklebust | 3b948ae | 2007-11-05 17:42:39 -0500 | [diff] [blame] | 1527 | clear_bit(XPRT_CONNECTED, &xprt->state); |
Trond Myklebust | ef80367 | 2007-12-31 16:19:17 -0500 | [diff] [blame] | 1528 | clear_bit(XPRT_CLOSE_WAIT, &xprt->state); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 1529 | smp_mb__after_atomic(); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1530 | break; |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 1531 | case TCP_CLOSE_WAIT: |
Trond Myklebust | 3b948ae | 2007-11-05 17:42:39 -0500 | [diff] [blame] | 1532 | /* The server initiated a shutdown of the socket */ |
Trond Myklebust | 7c1d71c | 2008-04-17 16:52:57 -0400 | [diff] [blame] | 1533 | xprt->connect_cookie++; |
Trond Myklebust | d0bea45 | 2012-10-23 11:35:47 -0400 | [diff] [blame] | 1534 | clear_bit(XPRT_CONNECTED, &xprt->state); |
Trond Myklebust | a519fc7 | 2012-09-12 16:49:15 -0400 | [diff] [blame] | 1535 | xs_tcp_force_close(xprt); |
Gustavo A. R. Silva | e9d4763 | 2017-10-20 11:48:30 -0500 | [diff] [blame] | 1536 | /* fall through */ |
Trond Myklebust | 663b885 | 2008-01-01 18:42:12 -0500 | [diff] [blame] | 1537 | case TCP_CLOSING: |
| 1538 | /* |
| 1539 | * If the server closed down the connection, make sure that |
| 1540 | * we back off before reconnecting |
| 1541 | */ |
| 1542 | if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) |
| 1543 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; |
Trond Myklebust | 3b948ae | 2007-11-05 17:42:39 -0500 | [diff] [blame] | 1544 | break; |
| 1545 | case TCP_LAST_ACK: |
Trond Myklebust | 670f945 | 2009-03-11 14:37:58 -0400 | [diff] [blame] | 1546 | set_bit(XPRT_CLOSING, &xprt->state); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 1547 | smp_mb__before_atomic(); |
Trond Myklebust | 3b948ae | 2007-11-05 17:42:39 -0500 | [diff] [blame] | 1548 | clear_bit(XPRT_CONNECTED, &xprt->state); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 1549 | smp_mb__after_atomic(); |
Trond Myklebust | 3b948ae | 2007-11-05 17:42:39 -0500 | [diff] [blame] | 1550 | break; |
| 1551 | case TCP_CLOSE: |
Trond Myklebust | 0fdea1e | 2015-09-16 23:43:17 -0400 | [diff] [blame] | 1552 | if (test_and_clear_bit(XPRT_SOCK_CONNECTING, |
| 1553 | &transport->sock_state)) |
| 1554 | xprt_clear_connecting(xprt); |
Trond Myklebust | 9b30889 | 2018-02-05 10:20:06 -0500 | [diff] [blame] | 1555 | clear_bit(XPRT_CLOSING, &xprt->state); |
Trond Myklebust | 9b30889 | 2018-02-05 10:20:06 -0500 | [diff] [blame] | 1556 | /* Trigger the socket release */ |
| 1557 | xs_tcp_force_close(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1558 | } |
| 1559 | out: |
Eric Dumazet | f064af1 | 2010-09-22 12:43:39 +0000 | [diff] [blame] | 1560 | read_unlock_bh(&sk->sk_callback_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1561 | } |
| 1562 | |
Ilpo Järvinen | 1f0fa15 | 2009-02-06 23:48:33 -0800 | [diff] [blame] | 1563 | static void xs_write_space(struct sock *sk) |
| 1564 | { |
Trond Myklebust | 13331a5 | 2016-01-06 08:57:06 -0500 | [diff] [blame] | 1565 | struct socket_wq *wq; |
Ilpo Järvinen | 1f0fa15 | 2009-02-06 23:48:33 -0800 | [diff] [blame] | 1566 | struct rpc_xprt *xprt; |
| 1567 | |
Trond Myklebust | 13331a5 | 2016-01-06 08:57:06 -0500 | [diff] [blame] | 1568 | if (!sk->sk_socket) |
Ilpo Järvinen | 1f0fa15 | 2009-02-06 23:48:33 -0800 | [diff] [blame] | 1569 | return; |
Trond Myklebust | 13331a5 | 2016-01-06 08:57:06 -0500 | [diff] [blame] | 1570 | clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
Ilpo Järvinen | 1f0fa15 | 2009-02-06 23:48:33 -0800 | [diff] [blame] | 1571 | |
| 1572 | if (unlikely(!(xprt = xprt_from_sock(sk)))) |
| 1573 | return; |
Trond Myklebust | 13331a5 | 2016-01-06 08:57:06 -0500 | [diff] [blame] | 1574 | rcu_read_lock(); |
| 1575 | wq = rcu_dereference(sk->sk_wq); |
| 1576 | if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0) |
| 1577 | goto out; |
Ilpo Järvinen | 1f0fa15 | 2009-02-06 23:48:33 -0800 | [diff] [blame] | 1578 | |
Trond Myklebust | c544577 | 2018-09-03 23:39:27 -0400 | [diff] [blame] | 1579 | if (xprt_write_space(xprt)) |
| 1580 | sk->sk_write_pending--; |
Trond Myklebust | 13331a5 | 2016-01-06 08:57:06 -0500 | [diff] [blame] | 1581 | out: |
| 1582 | rcu_read_unlock(); |
Ilpo Järvinen | 1f0fa15 | 2009-02-06 23:48:33 -0800 | [diff] [blame] | 1583 | } |
| 1584 | |
Trond Myklebust | 2a9e1cf | 2008-10-28 15:21:39 -0400 | [diff] [blame] | 1585 | /** |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1586 | * xs_udp_write_space - callback invoked when socket buffer space |
| 1587 | * becomes available |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1588 | * @sk: socket whose state has changed |
| 1589 | * |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1590 | * Called when more output buffer space is available for this socket. |
| 1591 | * We try not to wake our writers until they can make "significant" |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1592 | * progress, otherwise we'll waste resources thrashing kernel_sendmsg |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1593 | * with a bunch of small requests. |
| 1594 | */ |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1595 | static void xs_udp_write_space(struct sock *sk) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1596 | { |
Eric Dumazet | f064af1 | 2010-09-22 12:43:39 +0000 | [diff] [blame] | 1597 | read_lock_bh(&sk->sk_callback_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1598 | |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1599 | /* from net/core/sock.c:sock_def_write_space */ |
Ilpo Järvinen | 1f0fa15 | 2009-02-06 23:48:33 -0800 | [diff] [blame] | 1600 | if (sock_writeable(sk)) |
| 1601 | xs_write_space(sk); |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1602 | |
Eric Dumazet | f064af1 | 2010-09-22 12:43:39 +0000 | [diff] [blame] | 1603 | read_unlock_bh(&sk->sk_callback_lock); |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1604 | } |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1605 | |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1606 | /** |
| 1607 | * xs_tcp_write_space - callback invoked when socket buffer space |
| 1608 | * becomes available |
| 1609 | * @sk: socket whose state has changed |
| 1610 | * |
| 1611 | * Called when more output buffer space is available for this socket. |
| 1612 | * We try not to wake our writers until they can make "significant" |
| 1613 | * progress, otherwise we'll waste resources thrashing kernel_sendmsg |
| 1614 | * with a bunch of small requests. |
| 1615 | */ |
| 1616 | static void xs_tcp_write_space(struct sock *sk) |
| 1617 | { |
Eric Dumazet | f064af1 | 2010-09-22 12:43:39 +0000 | [diff] [blame] | 1618 | read_lock_bh(&sk->sk_callback_lock); |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1619 | |
| 1620 | /* from net/core/stream.c:sk_stream_write_space */ |
Eric Dumazet | 64dc613 | 2013-07-22 20:26:31 -0700 | [diff] [blame] | 1621 | if (sk_stream_is_writeable(sk)) |
Ilpo Järvinen | 1f0fa15 | 2009-02-06 23:48:33 -0800 | [diff] [blame] | 1622 | xs_write_space(sk); |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1623 | |
Eric Dumazet | f064af1 | 2010-09-22 12:43:39 +0000 | [diff] [blame] | 1624 | read_unlock_bh(&sk->sk_callback_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1625 | } |
| 1626 | |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1627 | static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1628 | { |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 1629 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 1630 | struct sock *sk = transport->inet; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1631 | |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1632 | if (transport->rcvsize) { |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1633 | sk->sk_userlocks |= SOCK_RCVBUF_LOCK; |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1634 | sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1635 | } |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1636 | if (transport->sndsize) { |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1637 | sk->sk_userlocks |= SOCK_SNDBUF_LOCK; |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1638 | sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1639 | sk->sk_write_space(sk); |
| 1640 | } |
| 1641 | } |
| 1642 | |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1643 | /** |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1644 | * xs_udp_set_buffer_size - set send and receive limits |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1645 | * @xprt: generic transport |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1646 | * @sndsize: requested size of send buffer, in bytes |
| 1647 | * @rcvsize: requested size of receive buffer, in bytes |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1648 | * |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1649 | * Set socket send and receive buffer size limits. |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1650 | */ |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1651 | static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1652 | { |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1653 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 1654 | |
| 1655 | transport->sndsize = 0; |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1656 | if (sndsize) |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1657 | transport->sndsize = sndsize + 1024; |
| 1658 | transport->rcvsize = 0; |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1659 | if (rcvsize) |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1660 | transport->rcvsize = rcvsize + 1024; |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1661 | |
| 1662 | xs_udp_do_set_buffer_size(xprt); |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1663 | } |
| 1664 | |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 1665 | /** |
| 1666 | * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport |
Chuck Lever | acf0a39 | 2018-12-19 11:00:22 -0500 | [diff] [blame] | 1667 | * @xprt: controlling transport |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 1668 | * @task: task that timed out |
| 1669 | * |
| 1670 | * Adjust the congestion window after a retransmit timeout has occurred. |
| 1671 | */ |
Trond Myklebust | 6a24dfb | 2013-01-08 09:48:15 -0500 | [diff] [blame] | 1672 | static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task) |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 1673 | { |
Chuck Lever | b977b64 | 2017-02-08 17:00:51 -0500 | [diff] [blame] | 1674 | spin_lock_bh(&xprt->transport_lock); |
Trond Myklebust | 6a24dfb | 2013-01-08 09:48:15 -0500 | [diff] [blame] | 1675 | xprt_adjust_cwnd(xprt, task, -ETIMEDOUT); |
Chuck Lever | b977b64 | 2017-02-08 17:00:51 -0500 | [diff] [blame] | 1676 | spin_unlock_bh(&xprt->transport_lock); |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 1677 | } |
| 1678 | |
J. Bruce Fields | 826799e | 2018-10-18 15:27:02 -0400 | [diff] [blame] | 1679 | static int xs_get_random_port(void) |
Chuck Lever | b85d880 | 2006-05-25 01:40:49 -0400 | [diff] [blame] | 1680 | { |
J. Bruce Fields | 826799e | 2018-10-18 15:27:02 -0400 | [diff] [blame] | 1681 | unsigned short min = xprt_min_resvport, max = xprt_max_resvport; |
| 1682 | unsigned short range; |
| 1683 | unsigned short rand; |
| 1684 | |
| 1685 | if (max < min) |
| 1686 | return -EADDRINUSE; |
| 1687 | range = max - min + 1; |
| 1688 | rand = (unsigned short) prandom_u32() % range; |
| 1689 | return rand + min; |
Chuck Lever | b85d880 | 2006-05-25 01:40:49 -0400 | [diff] [blame] | 1690 | } |
| 1691 | |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame] | 1692 | /** |
Trond Myklebust | 4dda9c8 | 2015-02-08 15:00:06 -0500 | [diff] [blame] | 1693 | * xs_set_reuseaddr_port - set the socket's port and address reuse options |
| 1694 | * @sock: socket |
| 1695 | * |
| 1696 | * Note that this function has to be called on all sockets that share the |
| 1697 | * same port, and it must be called before binding. |
| 1698 | */ |
| 1699 | static void xs_sock_set_reuseport(struct socket *sock) |
| 1700 | { |
Trond Myklebust | 402e23b | 2015-02-09 17:20:14 -0500 | [diff] [blame] | 1701 | int opt = 1; |
Trond Myklebust | 4dda9c8 | 2015-02-08 15:00:06 -0500 | [diff] [blame] | 1702 | |
Trond Myklebust | 402e23b | 2015-02-09 17:20:14 -0500 | [diff] [blame] | 1703 | kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, |
| 1704 | (char *)&opt, sizeof(opt)); |
Trond Myklebust | 4dda9c8 | 2015-02-08 15:00:06 -0500 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | static unsigned short xs_sock_getport(struct socket *sock) |
| 1708 | { |
| 1709 | struct sockaddr_storage buf; |
Trond Myklebust | 4dda9c8 | 2015-02-08 15:00:06 -0500 | [diff] [blame] | 1710 | unsigned short port = 0; |
| 1711 | |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 1712 | if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0) |
Trond Myklebust | 4dda9c8 | 2015-02-08 15:00:06 -0500 | [diff] [blame] | 1713 | goto out; |
| 1714 | switch (buf.ss_family) { |
| 1715 | case AF_INET6: |
| 1716 | port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port); |
| 1717 | break; |
| 1718 | case AF_INET: |
| 1719 | port = ntohs(((struct sockaddr_in *)&buf)->sin_port); |
| 1720 | } |
| 1721 | out: |
| 1722 | return port; |
| 1723 | } |
| 1724 | |
| 1725 | /** |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame] | 1726 | * xs_set_port - reset the port number in the remote endpoint address |
| 1727 | * @xprt: generic transport |
| 1728 | * @port: new port number |
| 1729 | * |
| 1730 | */ |
| 1731 | static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) |
| 1732 | { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1733 | dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); |
Chuck Lever | c4efcb1 | 2006-08-22 20:06:19 -0400 | [diff] [blame] | 1734 | |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 1735 | rpc_set_port(xs_addr(xprt), port); |
| 1736 | xs_update_peer_port(xprt); |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame] | 1737 | } |
| 1738 | |
Trond Myklebust | 4dda9c8 | 2015-02-08 15:00:06 -0500 | [diff] [blame] | 1739 | static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock) |
| 1740 | { |
| 1741 | if (transport->srcport == 0) |
| 1742 | transport->srcport = xs_sock_getport(sock); |
| 1743 | } |
| 1744 | |
J. Bruce Fields | 826799e | 2018-10-18 15:27:02 -0400 | [diff] [blame] | 1745 | static int xs_get_srcport(struct sock_xprt *transport) |
Trond Myklebust | 67a391d | 2007-11-05 17:40:58 -0500 | [diff] [blame] | 1746 | { |
J. Bruce Fields | 826799e | 2018-10-18 15:27:02 -0400 | [diff] [blame] | 1747 | int port = transport->srcport; |
Trond Myklebust | 67a391d | 2007-11-05 17:40:58 -0500 | [diff] [blame] | 1748 | |
| 1749 | if (port == 0 && transport->xprt.resvport) |
| 1750 | port = xs_get_random_port(); |
| 1751 | return port; |
| 1752 | } |
| 1753 | |
Pavel Emelyanov | baaf4e4 | 2010-10-04 16:51:56 +0400 | [diff] [blame] | 1754 | static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port) |
Trond Myklebust | 67a391d | 2007-11-05 17:40:58 -0500 | [diff] [blame] | 1755 | { |
Chuck Lever | fbfffbd | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 1756 | if (transport->srcport != 0) |
| 1757 | transport->srcport = 0; |
Trond Myklebust | 67a391d | 2007-11-05 17:40:58 -0500 | [diff] [blame] | 1758 | if (!transport->xprt.resvport) |
| 1759 | return 0; |
| 1760 | if (port <= xprt_min_resvport || port > xprt_max_resvport) |
| 1761 | return xprt_max_resvport; |
| 1762 | return --port; |
| 1763 | } |
Pavel Emelyanov | beb59b6 | 2010-10-05 15:53:08 +0400 | [diff] [blame] | 1764 | static int xs_bind(struct sock_xprt *transport, struct socket *sock) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1765 | { |
Pavel Emelyanov | beb59b6 | 2010-10-05 15:53:08 +0400 | [diff] [blame] | 1766 | struct sockaddr_storage myaddr; |
Trond Myklebust | 67a391d | 2007-11-05 17:40:58 -0500 | [diff] [blame] | 1767 | int err, nloop = 0; |
J. Bruce Fields | 826799e | 2018-10-18 15:27:02 -0400 | [diff] [blame] | 1768 | int port = xs_get_srcport(transport); |
Trond Myklebust | 67a391d | 2007-11-05 17:40:58 -0500 | [diff] [blame] | 1769 | unsigned short last; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1770 | |
Chris Perl | 0f7a622 | 2014-09-05 15:40:21 -0400 | [diff] [blame] | 1771 | /* |
| 1772 | * If we are asking for any ephemeral port (i.e. port == 0 && |
| 1773 | * transport->xprt.resvport == 0), don't bind. Let the local |
| 1774 | * port selection happen implicitly when the socket is used |
| 1775 | * (for example at connect time). |
| 1776 | * |
| 1777 | * This ensures that we can continue to establish TCP |
| 1778 | * connections even when all local ephemeral ports are already |
| 1779 | * a part of some TCP connection. This makes no difference |
| 1780 | * for UDP sockets, but also doens't harm them. |
| 1781 | * |
| 1782 | * If we're asking for any reserved port (i.e. port == 0 && |
| 1783 | * transport->xprt.resvport == 1) xs_get_srcport above will |
| 1784 | * ensure that port is non-zero and we will bind as needed. |
| 1785 | */ |
J. Bruce Fields | 826799e | 2018-10-18 15:27:02 -0400 | [diff] [blame] | 1786 | if (port <= 0) |
| 1787 | return port; |
Chris Perl | 0f7a622 | 2014-09-05 15:40:21 -0400 | [diff] [blame] | 1788 | |
Pavel Emelyanov | beb59b6 | 2010-10-05 15:53:08 +0400 | [diff] [blame] | 1789 | memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1790 | do { |
Pavel Emelyanov | beb59b6 | 2010-10-05 15:53:08 +0400 | [diff] [blame] | 1791 | rpc_set_port((struct sockaddr *)&myaddr, port); |
| 1792 | err = kernel_bind(sock, (struct sockaddr *)&myaddr, |
| 1793 | transport->xprt.addrlen); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1794 | if (err == 0) { |
Chuck Lever | fbfffbd | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 1795 | transport->srcport = port; |
Frank van Maarseveen | d3bc9a1 | 2007-07-09 22:23:35 +0200 | [diff] [blame] | 1796 | break; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1797 | } |
Trond Myklebust | 67a391d | 2007-11-05 17:40:58 -0500 | [diff] [blame] | 1798 | last = port; |
Pavel Emelyanov | baaf4e4 | 2010-10-04 16:51:56 +0400 | [diff] [blame] | 1799 | port = xs_next_srcport(transport, port); |
Trond Myklebust | 67a391d | 2007-11-05 17:40:58 -0500 | [diff] [blame] | 1800 | if (port > last) |
| 1801 | nloop++; |
| 1802 | } while (err == -EADDRINUSE && nloop != 2); |
Pavel Emelyanov | beb59b6 | 2010-10-05 15:53:08 +0400 | [diff] [blame] | 1803 | |
Chuck Lever | 4232e86 | 2010-10-20 11:52:51 -0400 | [diff] [blame] | 1804 | if (myaddr.ss_family == AF_INET) |
Pavel Emelyanov | beb59b6 | 2010-10-05 15:53:08 +0400 | [diff] [blame] | 1805 | dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__, |
| 1806 | &((struct sockaddr_in *)&myaddr)->sin_addr, |
| 1807 | port, err ? "failed" : "ok", err); |
| 1808 | else |
| 1809 | dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__, |
| 1810 | &((struct sockaddr_in6 *)&myaddr)->sin6_addr, |
| 1811 | port, err ? "failed" : "ok", err); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1812 | return err; |
| 1813 | } |
| 1814 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1815 | /* |
| 1816 | * We don't support autobind on AF_LOCAL sockets |
| 1817 | */ |
| 1818 | static void xs_local_rpcbind(struct rpc_task *task) |
| 1819 | { |
Trond Myklebust | fb43d17 | 2016-01-30 16:39:26 -0500 | [diff] [blame] | 1820 | xprt_set_bound(task->tk_xprt); |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1821 | } |
| 1822 | |
| 1823 | static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port) |
| 1824 | { |
| 1825 | } |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1826 | |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1827 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 1828 | static struct lock_class_key xs_key[2]; |
| 1829 | static struct lock_class_key xs_slock_key[2]; |
| 1830 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1831 | static inline void xs_reclassify_socketu(struct socket *sock) |
| 1832 | { |
| 1833 | struct sock *sk = sock->sk; |
| 1834 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1835 | sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC", |
| 1836 | &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]); |
| 1837 | } |
| 1838 | |
Chuck Lever | 8945ee5 | 2007-08-06 11:58:04 -0400 | [diff] [blame] | 1839 | static inline void xs_reclassify_socket4(struct socket *sock) |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1840 | { |
| 1841 | struct sock *sk = sock->sk; |
Chuck Lever | 8945ee5 | 2007-08-06 11:58:04 -0400 | [diff] [blame] | 1842 | |
Chuck Lever | 8945ee5 | 2007-08-06 11:58:04 -0400 | [diff] [blame] | 1843 | sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", |
| 1844 | &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]); |
| 1845 | } |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1846 | |
Chuck Lever | 8945ee5 | 2007-08-06 11:58:04 -0400 | [diff] [blame] | 1847 | static inline void xs_reclassify_socket6(struct socket *sock) |
| 1848 | { |
| 1849 | struct sock *sk = sock->sk; |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1850 | |
Chuck Lever | 8945ee5 | 2007-08-06 11:58:04 -0400 | [diff] [blame] | 1851 | sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", |
| 1852 | &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]); |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1853 | } |
Pavel Emelyanov | 6bc9638 | 2010-10-04 16:56:38 +0400 | [diff] [blame] | 1854 | |
| 1855 | static inline void xs_reclassify_socket(int family, struct socket *sock) |
| 1856 | { |
Hannes Frederic Sowa | fafc4e1 | 2016-04-08 15:11:27 +0200 | [diff] [blame] | 1857 | if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk))) |
Weston Andros Adamson | 1b7a181 | 2012-10-23 10:43:39 -0400 | [diff] [blame] | 1858 | return; |
| 1859 | |
Chuck Lever | 4232e86 | 2010-10-20 11:52:51 -0400 | [diff] [blame] | 1860 | switch (family) { |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1861 | case AF_LOCAL: |
| 1862 | xs_reclassify_socketu(sock); |
| 1863 | break; |
Chuck Lever | 4232e86 | 2010-10-20 11:52:51 -0400 | [diff] [blame] | 1864 | case AF_INET: |
Pavel Emelyanov | 6bc9638 | 2010-10-04 16:56:38 +0400 | [diff] [blame] | 1865 | xs_reclassify_socket4(sock); |
Chuck Lever | 4232e86 | 2010-10-20 11:52:51 -0400 | [diff] [blame] | 1866 | break; |
| 1867 | case AF_INET6: |
Pavel Emelyanov | 6bc9638 | 2010-10-04 16:56:38 +0400 | [diff] [blame] | 1868 | xs_reclassify_socket6(sock); |
Chuck Lever | 4232e86 | 2010-10-20 11:52:51 -0400 | [diff] [blame] | 1869 | break; |
| 1870 | } |
Pavel Emelyanov | 6bc9638 | 2010-10-04 16:56:38 +0400 | [diff] [blame] | 1871 | } |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1872 | #else |
Pavel Emelyanov | 6bc9638 | 2010-10-04 16:56:38 +0400 | [diff] [blame] | 1873 | static inline void xs_reclassify_socket(int family, struct socket *sock) |
| 1874 | { |
| 1875 | } |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1876 | #endif |
| 1877 | |
NeilBrown | 93dc41b | 2013-10-31 16:14:36 +1100 | [diff] [blame] | 1878 | static void xs_dummy_setup_socket(struct work_struct *work) |
| 1879 | { |
| 1880 | } |
| 1881 | |
Pavel Emelyanov | 6bc9638 | 2010-10-04 16:56:38 +0400 | [diff] [blame] | 1882 | static struct socket *xs_create_sock(struct rpc_xprt *xprt, |
Trond Myklebust | 4dda9c8 | 2015-02-08 15:00:06 -0500 | [diff] [blame] | 1883 | struct sock_xprt *transport, int family, int type, |
| 1884 | int protocol, bool reuseport) |
Pavel Emelyanov | 22f7932 | 2010-10-04 16:54:26 +0400 | [diff] [blame] | 1885 | { |
| 1886 | struct socket *sock; |
| 1887 | int err; |
| 1888 | |
Pavel Emelyanov | 6bc9638 | 2010-10-04 16:56:38 +0400 | [diff] [blame] | 1889 | err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1); |
Pavel Emelyanov | 22f7932 | 2010-10-04 16:54:26 +0400 | [diff] [blame] | 1890 | if (err < 0) { |
| 1891 | dprintk("RPC: can't create %d transport socket (%d).\n", |
| 1892 | protocol, -err); |
| 1893 | goto out; |
| 1894 | } |
Pavel Emelyanov | 6bc9638 | 2010-10-04 16:56:38 +0400 | [diff] [blame] | 1895 | xs_reclassify_socket(family, sock); |
Pavel Emelyanov | 22f7932 | 2010-10-04 16:54:26 +0400 | [diff] [blame] | 1896 | |
Trond Myklebust | 4dda9c8 | 2015-02-08 15:00:06 -0500 | [diff] [blame] | 1897 | if (reuseport) |
| 1898 | xs_sock_set_reuseport(sock); |
| 1899 | |
Ben Hutchings | 4cea288 | 2011-02-22 21:54:34 +0000 | [diff] [blame] | 1900 | err = xs_bind(transport, sock); |
| 1901 | if (err) { |
Pavel Emelyanov | 22f7932 | 2010-10-04 16:54:26 +0400 | [diff] [blame] | 1902 | sock_release(sock); |
| 1903 | goto out; |
| 1904 | } |
| 1905 | |
| 1906 | return sock; |
| 1907 | out: |
| 1908 | return ERR_PTR(err); |
| 1909 | } |
| 1910 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1911 | static int xs_local_finish_connecting(struct rpc_xprt *xprt, |
| 1912 | struct socket *sock) |
| 1913 | { |
| 1914 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, |
| 1915 | xprt); |
| 1916 | |
| 1917 | if (!transport->inet) { |
| 1918 | struct sock *sk = sock->sk; |
| 1919 | |
| 1920 | write_lock_bh(&sk->sk_callback_lock); |
| 1921 | |
| 1922 | xs_save_old_callbacks(transport, sk); |
| 1923 | |
| 1924 | sk->sk_user_data = xprt; |
Trond Myklebust | a264809 | 2015-10-06 17:03:00 -0400 | [diff] [blame] | 1925 | sk->sk_data_ready = xs_data_ready; |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1926 | sk->sk_write_space = xs_udp_write_space; |
Eric Dumazet | b4411457 | 2016-05-12 21:41:39 -0700 | [diff] [blame] | 1927 | sock_set_flag(sk, SOCK_FASYNC); |
Trond Myklebust | 2118071 | 2013-12-31 13:22:59 -0500 | [diff] [blame] | 1928 | sk->sk_error_report = xs_error_report; |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1929 | |
| 1930 | xprt_clear_connected(xprt); |
| 1931 | |
| 1932 | /* Reset to new socket */ |
| 1933 | transport->sock = sock; |
| 1934 | transport->inet = sk; |
| 1935 | |
| 1936 | write_unlock_bh(&sk->sk_callback_lock); |
| 1937 | } |
| 1938 | |
Trond Myklebust | ae05355 | 2019-02-20 14:56:20 -0500 | [diff] [blame] | 1939 | xs_stream_start_connect(transport); |
Trond Myklebust | 6c7a64e | 2018-08-13 16:54:57 -0400 | [diff] [blame] | 1940 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1941 | return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0); |
| 1942 | } |
| 1943 | |
| 1944 | /** |
| 1945 | * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1946 | * @transport: socket transport to connect |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1947 | */ |
J. Bruce Fields | dc10740 | 2013-02-20 17:52:19 -0500 | [diff] [blame] | 1948 | static int xs_local_setup_socket(struct sock_xprt *transport) |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1949 | { |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1950 | struct rpc_xprt *xprt = &transport->xprt; |
| 1951 | struct socket *sock; |
| 1952 | int status = -EIO; |
| 1953 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1954 | status = __sock_create(xprt->xprt_net, AF_LOCAL, |
| 1955 | SOCK_STREAM, 0, &sock, 1); |
| 1956 | if (status < 0) { |
| 1957 | dprintk("RPC: can't create AF_LOCAL " |
| 1958 | "transport socket (%d).\n", -status); |
| 1959 | goto out; |
| 1960 | } |
Stefan Hajnoczi | d135891 | 2015-12-02 14:17:52 +0800 | [diff] [blame] | 1961 | xs_reclassify_socket(AF_LOCAL, sock); |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1962 | |
| 1963 | dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n", |
| 1964 | xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); |
| 1965 | |
| 1966 | status = xs_local_finish_connecting(xprt, sock); |
Trond Myklebust | 40b5ea0 | 2013-09-04 12:16:23 -0400 | [diff] [blame] | 1967 | trace_rpc_socket_connect(xprt, sock, status); |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1968 | switch (status) { |
| 1969 | case 0: |
| 1970 | dprintk("RPC: xprt %p connected to %s\n", |
| 1971 | xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); |
Chuck Lever | 3968a8a | 2018-10-01 14:25:36 -0400 | [diff] [blame] | 1972 | xprt->stat.connect_count++; |
| 1973 | xprt->stat.connect_time += (long)jiffies - |
| 1974 | xprt->stat.connect_start; |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1975 | xprt_set_connected(xprt); |
Trond Myklebust | 3601c4a | 2014-06-30 13:42:19 -0400 | [diff] [blame] | 1976 | case -ENOBUFS: |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1977 | break; |
| 1978 | case -ENOENT: |
| 1979 | dprintk("RPC: xprt %p: socket %s does not exist\n", |
| 1980 | xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); |
| 1981 | break; |
Trond Myklebust | 4a20a98 | 2012-12-15 17:02:29 -0500 | [diff] [blame] | 1982 | case -ECONNREFUSED: |
| 1983 | dprintk("RPC: xprt %p: connection refused for %s\n", |
| 1984 | xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); |
| 1985 | break; |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 1986 | default: |
| 1987 | printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n", |
| 1988 | __func__, -status, |
| 1989 | xprt->address_strings[RPC_DISPLAY_ADDR]); |
| 1990 | } |
| 1991 | |
| 1992 | out: |
| 1993 | xprt_clear_connecting(xprt); |
| 1994 | xprt_wake_pending_tasks(xprt, status); |
J. Bruce Fields | dc10740 | 2013-02-20 17:52:19 -0500 | [diff] [blame] | 1995 | return status; |
| 1996 | } |
| 1997 | |
Linus Torvalds | b666973 | 2013-02-28 18:02:55 -0800 | [diff] [blame] | 1998 | static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task) |
J. Bruce Fields | dc10740 | 2013-02-20 17:52:19 -0500 | [diff] [blame] | 1999 | { |
J. Bruce Fields | dc10740 | 2013-02-20 17:52:19 -0500 | [diff] [blame] | 2000 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 2001 | int ret; |
| 2002 | |
| 2003 | if (RPC_IS_ASYNC(task)) { |
| 2004 | /* |
| 2005 | * We want the AF_LOCAL connect to be resolved in the |
| 2006 | * filesystem namespace of the process making the rpc |
| 2007 | * call. Thus we connect synchronously. |
| 2008 | * |
| 2009 | * If we want to support asynchronous AF_LOCAL calls, |
| 2010 | * we'll need to figure out how to pass a namespace to |
| 2011 | * connect. |
| 2012 | */ |
| 2013 | rpc_exit(task, -ENOTCONN); |
| 2014 | return; |
| 2015 | } |
| 2016 | ret = xs_local_setup_socket(transport); |
| 2017 | if (ret && !RPC_IS_SOFTCONN(task)) |
| 2018 | msleep_interruptible(15000); |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2019 | } |
| 2020 | |
Jeff Layton | 3c87ef6 | 2015-06-03 16:14:25 -0400 | [diff] [blame] | 2021 | #if IS_ENABLED(CONFIG_SUNRPC_SWAP) |
Jeff Layton | d6e971d | 2015-06-03 16:14:28 -0400 | [diff] [blame] | 2022 | /* |
| 2023 | * Note that this should be called with XPRT_LOCKED held (or when we otherwise |
| 2024 | * know that we have exclusive access to the socket), to guard against |
| 2025 | * races with xs_reset_transport. |
| 2026 | */ |
Mel Gorman | a564b8f | 2012-07-31 16:45:12 -0700 | [diff] [blame] | 2027 | static void xs_set_memalloc(struct rpc_xprt *xprt) |
| 2028 | { |
| 2029 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, |
| 2030 | xprt); |
| 2031 | |
Jeff Layton | d6e971d | 2015-06-03 16:14:28 -0400 | [diff] [blame] | 2032 | /* |
| 2033 | * If there's no sock, then we have nothing to set. The |
| 2034 | * reconnecting process will get it for us. |
| 2035 | */ |
| 2036 | if (!transport->inet) |
| 2037 | return; |
Jeff Layton | 8e22813 | 2015-06-03 16:14:26 -0400 | [diff] [blame] | 2038 | if (atomic_read(&xprt->swapper)) |
Mel Gorman | a564b8f | 2012-07-31 16:45:12 -0700 | [diff] [blame] | 2039 | sk_set_memalloc(transport->inet); |
| 2040 | } |
| 2041 | |
| 2042 | /** |
Jeff Layton | d67fa4d | 2015-06-03 16:14:29 -0400 | [diff] [blame] | 2043 | * xs_enable_swap - Tag this transport as being used for swap. |
Mel Gorman | a564b8f | 2012-07-31 16:45:12 -0700 | [diff] [blame] | 2044 | * @xprt: transport to tag |
Mel Gorman | a564b8f | 2012-07-31 16:45:12 -0700 | [diff] [blame] | 2045 | * |
Jeff Layton | 8e22813 | 2015-06-03 16:14:26 -0400 | [diff] [blame] | 2046 | * Take a reference to this transport on behalf of the rpc_clnt, and |
| 2047 | * optionally mark it for swapping if it wasn't already. |
Mel Gorman | a564b8f | 2012-07-31 16:45:12 -0700 | [diff] [blame] | 2048 | */ |
Jeff Layton | d67fa4d | 2015-06-03 16:14:29 -0400 | [diff] [blame] | 2049 | static int |
| 2050 | xs_enable_swap(struct rpc_xprt *xprt) |
Mel Gorman | a564b8f | 2012-07-31 16:45:12 -0700 | [diff] [blame] | 2051 | { |
Jeff Layton | d6e971d | 2015-06-03 16:14:28 -0400 | [diff] [blame] | 2052 | struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); |
Mel Gorman | a564b8f | 2012-07-31 16:45:12 -0700 | [diff] [blame] | 2053 | |
Jeff Layton | d6e971d | 2015-06-03 16:14:28 -0400 | [diff] [blame] | 2054 | if (atomic_inc_return(&xprt->swapper) != 1) |
| 2055 | return 0; |
| 2056 | if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) |
| 2057 | return -ERESTARTSYS; |
| 2058 | if (xs->inet) |
| 2059 | sk_set_memalloc(xs->inet); |
| 2060 | xprt_release_xprt(xprt, NULL); |
Jeff Layton | 8e22813 | 2015-06-03 16:14:26 -0400 | [diff] [blame] | 2061 | return 0; |
Mel Gorman | a564b8f | 2012-07-31 16:45:12 -0700 | [diff] [blame] | 2062 | } |
Jeff Layton | 8e22813 | 2015-06-03 16:14:26 -0400 | [diff] [blame] | 2063 | |
| 2064 | /** |
Jeff Layton | d67fa4d | 2015-06-03 16:14:29 -0400 | [diff] [blame] | 2065 | * xs_disable_swap - Untag this transport as being used for swap. |
Jeff Layton | 8e22813 | 2015-06-03 16:14:26 -0400 | [diff] [blame] | 2066 | * @xprt: transport to tag |
| 2067 | * |
| 2068 | * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the |
| 2069 | * swapper refcount goes to 0, untag the socket as a memalloc socket. |
| 2070 | */ |
Jeff Layton | d67fa4d | 2015-06-03 16:14:29 -0400 | [diff] [blame] | 2071 | static void |
| 2072 | xs_disable_swap(struct rpc_xprt *xprt) |
Jeff Layton | 8e22813 | 2015-06-03 16:14:26 -0400 | [diff] [blame] | 2073 | { |
Jeff Layton | d6e971d | 2015-06-03 16:14:28 -0400 | [diff] [blame] | 2074 | struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); |
Jeff Layton | 8e22813 | 2015-06-03 16:14:26 -0400 | [diff] [blame] | 2075 | |
Jeff Layton | d6e971d | 2015-06-03 16:14:28 -0400 | [diff] [blame] | 2076 | if (!atomic_dec_and_test(&xprt->swapper)) |
| 2077 | return; |
| 2078 | if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) |
| 2079 | return; |
| 2080 | if (xs->inet) |
| 2081 | sk_clear_memalloc(xs->inet); |
| 2082 | xprt_release_xprt(xprt, NULL); |
Mel Gorman | a564b8f | 2012-07-31 16:45:12 -0700 | [diff] [blame] | 2083 | } |
Mel Gorman | a564b8f | 2012-07-31 16:45:12 -0700 | [diff] [blame] | 2084 | #else |
| 2085 | static void xs_set_memalloc(struct rpc_xprt *xprt) |
| 2086 | { |
| 2087 | } |
Jeff Layton | d67fa4d | 2015-06-03 16:14:29 -0400 | [diff] [blame] | 2088 | |
| 2089 | static int |
| 2090 | xs_enable_swap(struct rpc_xprt *xprt) |
| 2091 | { |
| 2092 | return -EINVAL; |
| 2093 | } |
| 2094 | |
| 2095 | static void |
| 2096 | xs_disable_swap(struct rpc_xprt *xprt) |
| 2097 | { |
| 2098 | } |
Mel Gorman | a564b8f | 2012-07-31 16:45:12 -0700 | [diff] [blame] | 2099 | #endif |
| 2100 | |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2101 | static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2102 | { |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2103 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 2104 | |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 2105 | if (!transport->inet) { |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2106 | struct sock *sk = sock->sk; |
| 2107 | |
| 2108 | write_lock_bh(&sk->sk_callback_lock); |
| 2109 | |
Trond Myklebust | 2a9e1cf | 2008-10-28 15:21:39 -0400 | [diff] [blame] | 2110 | xs_save_old_callbacks(transport, sk); |
| 2111 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2112 | sk->sk_user_data = xprt; |
Trond Myklebust | f9b2ee7 | 2015-10-06 16:26:05 -0400 | [diff] [blame] | 2113 | sk->sk_data_ready = xs_data_ready; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2114 | sk->sk_write_space = xs_udp_write_space; |
Eric Dumazet | b4411457 | 2016-05-12 21:41:39 -0700 | [diff] [blame] | 2115 | sock_set_flag(sk, SOCK_FASYNC); |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2116 | |
| 2117 | xprt_set_connected(xprt); |
| 2118 | |
| 2119 | /* Reset to new socket */ |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 2120 | transport->sock = sock; |
| 2121 | transport->inet = sk; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2122 | |
Mel Gorman | a564b8f | 2012-07-31 16:45:12 -0700 | [diff] [blame] | 2123 | xs_set_memalloc(xprt); |
| 2124 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2125 | write_unlock_bh(&sk->sk_callback_lock); |
| 2126 | } |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 2127 | xs_udp_do_set_buffer_size(xprt); |
Trond Myklebust | 0291017 | 2016-08-04 00:00:33 -0400 | [diff] [blame] | 2128 | |
| 2129 | xprt->stat.connect_start = jiffies; |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2130 | } |
| 2131 | |
Pavel Emelyanov | 8c14ff2 | 2010-10-04 16:58:02 +0400 | [diff] [blame] | 2132 | static void xs_udp_setup_socket(struct work_struct *work) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2133 | { |
| 2134 | struct sock_xprt *transport = |
| 2135 | container_of(work, struct sock_xprt, connect_worker.work); |
| 2136 | struct rpc_xprt *xprt = &transport->xprt; |
Colin Ian King | d099b8a | 2017-09-18 12:21:14 +0100 | [diff] [blame] | 2137 | struct socket *sock; |
Pavel Emelyanov | b65c031 | 2010-10-04 16:53:46 +0400 | [diff] [blame] | 2138 | int status = -EIO; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2139 | |
Pavel Emelyanov | 8c14ff2 | 2010-10-04 16:58:02 +0400 | [diff] [blame] | 2140 | sock = xs_create_sock(xprt, transport, |
Trond Myklebust | 4dda9c8 | 2015-02-08 15:00:06 -0500 | [diff] [blame] | 2141 | xs_addr(xprt)->sa_family, SOCK_DGRAM, |
| 2142 | IPPROTO_UDP, false); |
Pavel Emelyanov | b65c031 | 2010-10-04 16:53:46 +0400 | [diff] [blame] | 2143 | if (IS_ERR(sock)) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2144 | goto out; |
Chuck Lever | 68e220b | 2007-08-06 11:57:48 -0400 | [diff] [blame] | 2145 | |
Chuck Lever | c740eff | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 2146 | dprintk("RPC: worker connecting xprt %p via %s to " |
| 2147 | "%s (port %s)\n", xprt, |
| 2148 | xprt->address_strings[RPC_DISPLAY_PROTO], |
| 2149 | xprt->address_strings[RPC_DISPLAY_ADDR], |
| 2150 | xprt->address_strings[RPC_DISPLAY_PORT]); |
Chuck Lever | 68e220b | 2007-08-06 11:57:48 -0400 | [diff] [blame] | 2151 | |
| 2152 | xs_udp_finish_connecting(xprt, sock); |
Trond Myklebust | 40b5ea0 | 2013-09-04 12:16:23 -0400 | [diff] [blame] | 2153 | trace_rpc_socket_connect(xprt, sock, 0); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2154 | status = 0; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2155 | out: |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2156 | xprt_clear_connecting(xprt); |
Trond Myklebust | cf76785 | 2018-12-17 17:38:51 -0500 | [diff] [blame] | 2157 | xprt_unlock_connect(xprt, transport); |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 2158 | xprt_wake_pending_tasks(xprt, status); |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2159 | } |
| 2160 | |
Trond Myklebust | 4876cc7 | 2015-06-19 16:17:57 -0400 | [diff] [blame] | 2161 | /** |
| 2162 | * xs_tcp_shutdown - gracefully shut down a TCP socket |
| 2163 | * @xprt: transport |
| 2164 | * |
| 2165 | * Initiates a graceful shutdown of the TCP socket by calling the |
| 2166 | * equivalent of shutdown(SHUT_RDWR); |
| 2167 | */ |
| 2168 | static void xs_tcp_shutdown(struct rpc_xprt *xprt) |
| 2169 | { |
| 2170 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 2171 | struct socket *sock = transport->sock; |
Trond Myklebust | 9b30889 | 2018-02-05 10:20:06 -0500 | [diff] [blame] | 2172 | int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE; |
Trond Myklebust | 4876cc7 | 2015-06-19 16:17:57 -0400 | [diff] [blame] | 2173 | |
| 2174 | if (sock == NULL) |
| 2175 | return; |
Trond Myklebust | 9b30889 | 2018-02-05 10:20:06 -0500 | [diff] [blame] | 2176 | switch (skst) { |
| 2177 | default: |
Trond Myklebust | 4876cc7 | 2015-06-19 16:17:57 -0400 | [diff] [blame] | 2178 | kernel_sock_shutdown(sock, SHUT_RDWR); |
| 2179 | trace_rpc_socket_shutdown(xprt, sock); |
Trond Myklebust | 9b30889 | 2018-02-05 10:20:06 -0500 | [diff] [blame] | 2180 | break; |
| 2181 | case TCP_CLOSE: |
| 2182 | case TCP_TIME_WAIT: |
Trond Myklebust | 4876cc7 | 2015-06-19 16:17:57 -0400 | [diff] [blame] | 2183 | xs_reset_transport(transport); |
Trond Myklebust | 9b30889 | 2018-02-05 10:20:06 -0500 | [diff] [blame] | 2184 | } |
Trond Myklebust | 4876cc7 | 2015-06-19 16:17:57 -0400 | [diff] [blame] | 2185 | } |
| 2186 | |
Trond Myklebust | 8d1b8c6 | 2017-02-08 11:17:53 -0500 | [diff] [blame] | 2187 | static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, |
| 2188 | struct socket *sock) |
| 2189 | { |
Trond Myklebust | 7196dbb | 2017-02-08 11:17:54 -0500 | [diff] [blame] | 2190 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 2191 | unsigned int keepidle; |
| 2192 | unsigned int keepcnt; |
Trond Myklebust | 8d1b8c6 | 2017-02-08 11:17:53 -0500 | [diff] [blame] | 2193 | unsigned int opt_on = 1; |
| 2194 | unsigned int timeo; |
| 2195 | |
Trond Myklebust | 7196dbb | 2017-02-08 11:17:54 -0500 | [diff] [blame] | 2196 | spin_lock_bh(&xprt->transport_lock); |
| 2197 | keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ); |
| 2198 | keepcnt = xprt->timeout->to_retries + 1; |
| 2199 | timeo = jiffies_to_msecs(xprt->timeout->to_initval) * |
| 2200 | (xprt->timeout->to_retries + 1); |
| 2201 | clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); |
| 2202 | spin_unlock_bh(&xprt->transport_lock); |
| 2203 | |
Trond Myklebust | 8d1b8c6 | 2017-02-08 11:17:53 -0500 | [diff] [blame] | 2204 | /* TCP Keepalive options */ |
| 2205 | kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, |
| 2206 | (char *)&opt_on, sizeof(opt_on)); |
| 2207 | kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, |
| 2208 | (char *)&keepidle, sizeof(keepidle)); |
| 2209 | kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, |
| 2210 | (char *)&keepidle, sizeof(keepidle)); |
| 2211 | kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT, |
| 2212 | (char *)&keepcnt, sizeof(keepcnt)); |
| 2213 | |
| 2214 | /* TCP user timeout (see RFC5482) */ |
Trond Myklebust | 8d1b8c6 | 2017-02-08 11:17:53 -0500 | [diff] [blame] | 2215 | kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT, |
| 2216 | (char *)&timeo, sizeof(timeo)); |
| 2217 | } |
| 2218 | |
Trond Myklebust | 7196dbb | 2017-02-08 11:17:54 -0500 | [diff] [blame] | 2219 | static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt, |
| 2220 | unsigned long connect_timeout, |
| 2221 | unsigned long reconnect_timeout) |
| 2222 | { |
| 2223 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 2224 | struct rpc_timeout to; |
| 2225 | unsigned long initval; |
| 2226 | |
| 2227 | spin_lock_bh(&xprt->transport_lock); |
| 2228 | if (reconnect_timeout < xprt->max_reconnect_timeout) |
| 2229 | xprt->max_reconnect_timeout = reconnect_timeout; |
| 2230 | if (connect_timeout < xprt->connect_timeout) { |
| 2231 | memcpy(&to, xprt->timeout, sizeof(to)); |
| 2232 | initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1); |
| 2233 | /* Arbitrary lower limit */ |
| 2234 | if (initval < XS_TCP_INIT_REEST_TO << 1) |
| 2235 | initval = XS_TCP_INIT_REEST_TO << 1; |
| 2236 | to.to_initval = initval; |
| 2237 | to.to_maxval = initval; |
| 2238 | memcpy(&transport->tcp_timeout, &to, |
| 2239 | sizeof(transport->tcp_timeout)); |
| 2240 | xprt->timeout = &transport->tcp_timeout; |
| 2241 | xprt->connect_timeout = connect_timeout; |
| 2242 | } |
| 2243 | set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); |
| 2244 | spin_unlock_bh(&xprt->transport_lock); |
| 2245 | } |
| 2246 | |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2247 | static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2248 | { |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2249 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Trond Myklebust | fe19a96 | 2011-03-18 20:21:23 -0400 | [diff] [blame] | 2250 | int ret = -ENOTCONN; |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 2251 | |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 2252 | if (!transport->inet) { |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2253 | struct sock *sk = sock->sk; |
NeilBrown | d88e4d8 | 2016-08-04 16:24:28 +1000 | [diff] [blame] | 2254 | unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC; |
Trond Myklebust | 7f260e8 | 2013-09-24 11:25:22 -0400 | [diff] [blame] | 2255 | |
NeilBrown | d88e4d8 | 2016-08-04 16:24:28 +1000 | [diff] [blame] | 2256 | /* Avoid temporary address, they are bad for long-lived |
| 2257 | * connections such as NFS mounts. |
| 2258 | * RFC4941, section 3.6 suggests that: |
| 2259 | * Individual applications, which have specific |
| 2260 | * knowledge about the normal duration of connections, |
| 2261 | * MAY override this as appropriate. |
| 2262 | */ |
| 2263 | kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES, |
| 2264 | (char *)&addr_pref, sizeof(addr_pref)); |
| 2265 | |
Trond Myklebust | 8d1b8c6 | 2017-02-08 11:17:53 -0500 | [diff] [blame] | 2266 | xs_tcp_set_socket_timeouts(xprt, sock); |
Trond Myklebust | 775f06a | 2015-06-20 15:31:54 -0400 | [diff] [blame] | 2267 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2268 | write_lock_bh(&sk->sk_callback_lock); |
| 2269 | |
Trond Myklebust | 2a9e1cf | 2008-10-28 15:21:39 -0400 | [diff] [blame] | 2270 | xs_save_old_callbacks(transport, sk); |
| 2271 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2272 | sk->sk_user_data = xprt; |
Trond Myklebust | 5157b95 | 2016-05-29 10:13:24 -0400 | [diff] [blame] | 2273 | sk->sk_data_ready = xs_data_ready; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2274 | sk->sk_state_change = xs_tcp_state_change; |
| 2275 | sk->sk_write_space = xs_tcp_write_space; |
Eric Dumazet | b4411457 | 2016-05-12 21:41:39 -0700 | [diff] [blame] | 2276 | sock_set_flag(sk, SOCK_FASYNC); |
Trond Myklebust | 2118071 | 2013-12-31 13:22:59 -0500 | [diff] [blame] | 2277 | sk->sk_error_report = xs_error_report; |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 2278 | |
| 2279 | /* socket options */ |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 2280 | sock_reset_flag(sk, SOCK_LINGER); |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 2281 | tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2282 | |
| 2283 | xprt_clear_connected(xprt); |
| 2284 | |
| 2285 | /* Reset to new socket */ |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 2286 | transport->sock = sock; |
| 2287 | transport->inet = sk; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2288 | |
| 2289 | write_unlock_bh(&sk->sk_callback_lock); |
| 2290 | } |
| 2291 | |
Trond Myklebust | 01d37c4 | 2009-03-11 14:09:39 -0400 | [diff] [blame] | 2292 | if (!xprt_bound(xprt)) |
Trond Myklebust | fe19a96 | 2011-03-18 20:21:23 -0400 | [diff] [blame] | 2293 | goto out; |
Trond Myklebust | 01d37c4 | 2009-03-11 14:09:39 -0400 | [diff] [blame] | 2294 | |
Mel Gorman | a564b8f | 2012-07-31 16:45:12 -0700 | [diff] [blame] | 2295 | xs_set_memalloc(xprt); |
| 2296 | |
Trond Myklebust | ae05355 | 2019-02-20 14:56:20 -0500 | [diff] [blame] | 2297 | xs_stream_start_connect(transport); |
Trond Myklebust | e1806c7 | 2018-08-13 16:50:49 -0400 | [diff] [blame] | 2298 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 2299 | /* Tell the socket layer to start connecting... */ |
Trond Myklebust | 0fdea1e | 2015-09-16 23:43:17 -0400 | [diff] [blame] | 2300 | set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); |
Trond Myklebust | fe19a96 | 2011-03-18 20:21:23 -0400 | [diff] [blame] | 2301 | ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); |
| 2302 | switch (ret) { |
| 2303 | case 0: |
Trond Myklebust | 4dda9c8 | 2015-02-08 15:00:06 -0500 | [diff] [blame] | 2304 | xs_set_srcport(transport, sock); |
Gustavo A. R. Silva | e9d4763 | 2017-10-20 11:48:30 -0500 | [diff] [blame] | 2305 | /* fall through */ |
Trond Myklebust | fe19a96 | 2011-03-18 20:21:23 -0400 | [diff] [blame] | 2306 | case -EINPROGRESS: |
| 2307 | /* SYN_SENT! */ |
Trond Myklebust | fe19a96 | 2011-03-18 20:21:23 -0400 | [diff] [blame] | 2308 | if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) |
| 2309 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; |
Trond Myklebust | 1f4c17a | 2016-08-01 13:36:08 -0400 | [diff] [blame] | 2310 | break; |
| 2311 | case -EADDRNOTAVAIL: |
| 2312 | /* Source port number is unavailable. Try a new one! */ |
| 2313 | transport->srcport = 0; |
Trond Myklebust | fe19a96 | 2011-03-18 20:21:23 -0400 | [diff] [blame] | 2314 | } |
| 2315 | out: |
| 2316 | return ret; |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2317 | } |
| 2318 | |
| 2319 | /** |
Trond Myklebust | b61d59f | 2009-03-11 14:38:04 -0400 | [diff] [blame] | 2320 | * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint |
Chuck Lever | acf0a39 | 2018-12-19 11:00:22 -0500 | [diff] [blame] | 2321 | * @work: queued work item |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2322 | * |
| 2323 | * Invoked by a work queue tasklet. |
| 2324 | */ |
Pavel Emelyanov | cdd518d | 2010-10-04 16:57:40 +0400 | [diff] [blame] | 2325 | static void xs_tcp_setup_socket(struct work_struct *work) |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2326 | { |
Pavel Emelyanov | cdd518d | 2010-10-04 16:57:40 +0400 | [diff] [blame] | 2327 | struct sock_xprt *transport = |
| 2328 | container_of(work, struct sock_xprt, connect_worker.work); |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2329 | struct socket *sock = transport->sock; |
Pavel Emelyanov | a9f5f0f | 2010-10-04 16:52:25 +0400 | [diff] [blame] | 2330 | struct rpc_xprt *xprt = &transport->xprt; |
Trond Myklebust | b61d59f | 2009-03-11 14:38:04 -0400 | [diff] [blame] | 2331 | int status = -EIO; |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2332 | |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2333 | if (!sock) { |
Pavel Emelyanov | cdd518d | 2010-10-04 16:57:40 +0400 | [diff] [blame] | 2334 | sock = xs_create_sock(xprt, transport, |
Trond Myklebust | 4dda9c8 | 2015-02-08 15:00:06 -0500 | [diff] [blame] | 2335 | xs_addr(xprt)->sa_family, SOCK_STREAM, |
| 2336 | IPPROTO_TCP, true); |
Trond Myklebust | b61d59f | 2009-03-11 14:38:04 -0400 | [diff] [blame] | 2337 | if (IS_ERR(sock)) { |
| 2338 | status = PTR_ERR(sock); |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2339 | goto out; |
| 2340 | } |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 2341 | } |
| 2342 | |
Chuck Lever | c740eff | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 2343 | dprintk("RPC: worker connecting xprt %p via %s to " |
| 2344 | "%s (port %s)\n", xprt, |
| 2345 | xprt->address_strings[RPC_DISPLAY_PROTO], |
| 2346 | xprt->address_strings[RPC_DISPLAY_ADDR], |
| 2347 | xprt->address_strings[RPC_DISPLAY_PORT]); |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 2348 | |
| 2349 | status = xs_tcp_finish_connecting(xprt, sock); |
Trond Myklebust | 40b5ea0 | 2013-09-04 12:16:23 -0400 | [diff] [blame] | 2350 | trace_rpc_socket_connect(xprt, sock, status); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 2351 | dprintk("RPC: %p connect status %d connected %d sock state %d\n", |
| 2352 | xprt, -status, xprt_connected(xprt), |
| 2353 | sock->sk->sk_state); |
Trond Myklebust | 2a49199 | 2009-03-11 14:38:00 -0400 | [diff] [blame] | 2354 | switch (status) { |
Trond Myklebust | f75e674 | 2009-04-21 17:18:20 -0400 | [diff] [blame] | 2355 | default: |
| 2356 | printk("%s: connect returned unhandled error %d\n", |
| 2357 | __func__, status); |
Gustavo A. R. Silva | e9d4763 | 2017-10-20 11:48:30 -0500 | [diff] [blame] | 2358 | /* fall through */ |
Trond Myklebust | f75e674 | 2009-04-21 17:18:20 -0400 | [diff] [blame] | 2359 | case -EADDRNOTAVAIL: |
| 2360 | /* We're probably in TIME_WAIT. Get rid of existing socket, |
| 2361 | * and retry |
| 2362 | */ |
Trond Myklebust | a519fc7 | 2012-09-12 16:49:15 -0400 | [diff] [blame] | 2363 | xs_tcp_force_close(xprt); |
Trond Myklebust | 88b5ed7 | 2009-06-17 13:22:57 -0700 | [diff] [blame] | 2364 | break; |
Trond Myklebust | 2a49199 | 2009-03-11 14:38:00 -0400 | [diff] [blame] | 2365 | case 0: |
| 2366 | case -EINPROGRESS: |
| 2367 | case -EALREADY: |
Trond Myklebust | 718ba5b | 2015-02-08 18:19:25 -0500 | [diff] [blame] | 2368 | xprt_unlock_connect(xprt, transport); |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 2369 | return; |
Trond Myklebust | 9fcfe0c | 2010-03-02 13:06:21 -0500 | [diff] [blame] | 2370 | case -EINVAL: |
| 2371 | /* Happens, for instance, if the user specified a link |
| 2372 | * local IPv6 address without a scope-id. |
| 2373 | */ |
Trond Myklebust | 3ed5e2a | 2013-03-04 17:29:33 -0500 | [diff] [blame] | 2374 | case -ECONNREFUSED: |
| 2375 | case -ECONNRESET: |
Trond Myklebust | eb5b46f | 2017-11-30 07:21:33 -0500 | [diff] [blame] | 2376 | case -ENETDOWN: |
Trond Myklebust | 3ed5e2a | 2013-03-04 17:29:33 -0500 | [diff] [blame] | 2377 | case -ENETUNREACH: |
Trond Myklebust | 4ba161a | 2017-11-24 12:00:24 -0500 | [diff] [blame] | 2378 | case -EHOSTUNREACH: |
Trond Myklebust | 3913c78 | 2015-02-08 21:44:04 -0500 | [diff] [blame] | 2379 | case -EADDRINUSE: |
Trond Myklebust | 3601c4a | 2014-06-30 13:42:19 -0400 | [diff] [blame] | 2380 | case -ENOBUFS: |
NeilBrown | 6ea44ad | 2017-05-25 17:00:32 +1000 | [diff] [blame] | 2381 | /* |
| 2382 | * xs_tcp_force_close() wakes tasks with -EIO. |
| 2383 | * We need to wake them first to ensure the |
| 2384 | * correct error code. |
| 2385 | */ |
| 2386 | xprt_wake_pending_tasks(xprt, status); |
Trond Myklebust | 4efdd92 | 2015-02-08 15:34:28 -0500 | [diff] [blame] | 2387 | xs_tcp_force_close(xprt); |
Trond Myklebust | 9fcfe0c | 2010-03-02 13:06:21 -0500 | [diff] [blame] | 2388 | goto out; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2389 | } |
Trond Myklebust | 2a49199 | 2009-03-11 14:38:00 -0400 | [diff] [blame] | 2390 | status = -EAGAIN; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2391 | out: |
Chuck Lever | 2226feb | 2005-08-11 16:25:38 -0400 | [diff] [blame] | 2392 | xprt_clear_connecting(xprt); |
Trond Myklebust | cf76785 | 2018-12-17 17:38:51 -0500 | [diff] [blame] | 2393 | xprt_unlock_connect(xprt, transport); |
Trond Myklebust | 7d1e825 | 2009-03-11 14:38:03 -0400 | [diff] [blame] | 2394 | xprt_wake_pending_tasks(xprt, status); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2395 | } |
| 2396 | |
Trond Myklebust | 0291017 | 2016-08-04 00:00:33 -0400 | [diff] [blame] | 2397 | static unsigned long xs_reconnect_delay(const struct rpc_xprt *xprt) |
| 2398 | { |
| 2399 | unsigned long start, now = jiffies; |
| 2400 | |
| 2401 | start = xprt->stat.connect_start + xprt->reestablish_timeout; |
| 2402 | if (time_after(start, now)) |
| 2403 | return start - now; |
| 2404 | return 0; |
| 2405 | } |
| 2406 | |
Trond Myklebust | 3851f1c | 2016-08-04 00:08:45 -0400 | [diff] [blame] | 2407 | static void xs_reconnect_backoff(struct rpc_xprt *xprt) |
| 2408 | { |
| 2409 | xprt->reestablish_timeout <<= 1; |
| 2410 | if (xprt->reestablish_timeout > xprt->max_reconnect_timeout) |
| 2411 | xprt->reestablish_timeout = xprt->max_reconnect_timeout; |
| 2412 | if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) |
| 2413 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; |
| 2414 | } |
| 2415 | |
Chuck Lever | 68e220b | 2007-08-06 11:57:48 -0400 | [diff] [blame] | 2416 | /** |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 2417 | * xs_connect - connect a socket to a remote endpoint |
Trond Myklebust | 1b09209 | 2013-01-08 09:26:49 -0500 | [diff] [blame] | 2418 | * @xprt: pointer to transport structure |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 2419 | * @task: address of RPC task that manages state of connect request |
| 2420 | * |
| 2421 | * TCP: If the remote end dropped the connection, delay reconnecting. |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 2422 | * |
| 2423 | * UDP socket connects are synchronous, but we use a work queue anyway |
| 2424 | * to guarantee that even unprivileged user processes can set up a |
| 2425 | * socket on a privileged port. |
| 2426 | * |
| 2427 | * If a UDP socket connect fails, the delay behavior here prevents |
| 2428 | * retry floods (hard mounts). |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 2429 | */ |
Trond Myklebust | 1b09209 | 2013-01-08 09:26:49 -0500 | [diff] [blame] | 2430 | static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2431 | { |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 2432 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Trond Myklebust | 0291017 | 2016-08-04 00:00:33 -0400 | [diff] [blame] | 2433 | unsigned long delay = 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2434 | |
Trond Myklebust | 718ba5b | 2015-02-08 18:19:25 -0500 | [diff] [blame] | 2435 | WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport)); |
| 2436 | |
Trond Myklebust | 99b1a4c | 2015-08-13 15:33:51 -0400 | [diff] [blame] | 2437 | if (transport->sock != NULL) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 2438 | dprintk("RPC: xs_connect delayed xprt %p for %lu " |
| 2439 | "seconds\n", |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 2440 | xprt, xprt->reestablish_timeout / HZ); |
Trond Myklebust | 99b1a4c | 2015-08-13 15:33:51 -0400 | [diff] [blame] | 2441 | |
| 2442 | /* Start by resetting any existing state */ |
| 2443 | xs_reset_transport(transport); |
| 2444 | |
Trond Myklebust | 0291017 | 2016-08-04 00:00:33 -0400 | [diff] [blame] | 2445 | delay = xs_reconnect_delay(xprt); |
Trond Myklebust | 3851f1c | 2016-08-04 00:08:45 -0400 | [diff] [blame] | 2446 | xs_reconnect_backoff(xprt); |
Trond Myklebust | 0291017 | 2016-08-04 00:00:33 -0400 | [diff] [blame] | 2447 | |
Trond Myklebust | 0291017 | 2016-08-04 00:00:33 -0400 | [diff] [blame] | 2448 | } else |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 2449 | dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); |
Trond Myklebust | 0291017 | 2016-08-04 00:00:33 -0400 | [diff] [blame] | 2450 | |
| 2451 | queue_delayed_work(xprtiod_workqueue, |
| 2452 | &transport->connect_worker, |
| 2453 | delay); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2454 | } |
| 2455 | |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 2456 | /** |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2457 | * xs_local_print_stats - display AF_LOCAL socket-specifc stats |
| 2458 | * @xprt: rpc_xprt struct containing statistics |
| 2459 | * @seq: output file |
| 2460 | * |
| 2461 | */ |
| 2462 | static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) |
| 2463 | { |
| 2464 | long idle_time = 0; |
| 2465 | |
| 2466 | if (xprt_connected(xprt)) |
| 2467 | idle_time = (long)(jiffies - xprt->last_used) / HZ; |
| 2468 | |
| 2469 | seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu " |
Andy Adamson | 15a4520 | 2012-02-14 16:19:18 -0500 | [diff] [blame] | 2470 | "%llu %llu %lu %llu %llu\n", |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2471 | xprt->stat.bind_count, |
| 2472 | xprt->stat.connect_count, |
Chuck Lever | 8440a88 | 2018-10-01 14:25:41 -0400 | [diff] [blame] | 2473 | xprt->stat.connect_time / HZ, |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2474 | idle_time, |
| 2475 | xprt->stat.sends, |
| 2476 | xprt->stat.recvs, |
| 2477 | xprt->stat.bad_xids, |
| 2478 | xprt->stat.req_u, |
Andy Adamson | 15a4520 | 2012-02-14 16:19:18 -0500 | [diff] [blame] | 2479 | xprt->stat.bklog_u, |
| 2480 | xprt->stat.max_slots, |
| 2481 | xprt->stat.sending_u, |
| 2482 | xprt->stat.pending_u); |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2483 | } |
| 2484 | |
| 2485 | /** |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 2486 | * xs_udp_print_stats - display UDP socket-specifc stats |
| 2487 | * @xprt: rpc_xprt struct containing statistics |
| 2488 | * @seq: output file |
| 2489 | * |
| 2490 | */ |
| 2491 | static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) |
| 2492 | { |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 2493 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 2494 | |
Andy Adamson | 15a4520 | 2012-02-14 16:19:18 -0500 | [diff] [blame] | 2495 | seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu " |
| 2496 | "%lu %llu %llu\n", |
Chuck Lever | fbfffbd | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 2497 | transport->srcport, |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 2498 | xprt->stat.bind_count, |
| 2499 | xprt->stat.sends, |
| 2500 | xprt->stat.recvs, |
| 2501 | xprt->stat.bad_xids, |
| 2502 | xprt->stat.req_u, |
Andy Adamson | 15a4520 | 2012-02-14 16:19:18 -0500 | [diff] [blame] | 2503 | xprt->stat.bklog_u, |
| 2504 | xprt->stat.max_slots, |
| 2505 | xprt->stat.sending_u, |
| 2506 | xprt->stat.pending_u); |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 2507 | } |
| 2508 | |
| 2509 | /** |
| 2510 | * xs_tcp_print_stats - display TCP socket-specifc stats |
| 2511 | * @xprt: rpc_xprt struct containing statistics |
| 2512 | * @seq: output file |
| 2513 | * |
| 2514 | */ |
| 2515 | static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) |
| 2516 | { |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 2517 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 2518 | long idle_time = 0; |
| 2519 | |
| 2520 | if (xprt_connected(xprt)) |
| 2521 | idle_time = (long)(jiffies - xprt->last_used) / HZ; |
| 2522 | |
Andy Adamson | 15a4520 | 2012-02-14 16:19:18 -0500 | [diff] [blame] | 2523 | seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu " |
| 2524 | "%llu %llu %lu %llu %llu\n", |
Chuck Lever | fbfffbd | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 2525 | transport->srcport, |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 2526 | xprt->stat.bind_count, |
| 2527 | xprt->stat.connect_count, |
Chuck Lever | 8440a88 | 2018-10-01 14:25:41 -0400 | [diff] [blame] | 2528 | xprt->stat.connect_time / HZ, |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 2529 | idle_time, |
| 2530 | xprt->stat.sends, |
| 2531 | xprt->stat.recvs, |
| 2532 | xprt->stat.bad_xids, |
| 2533 | xprt->stat.req_u, |
Andy Adamson | 15a4520 | 2012-02-14 16:19:18 -0500 | [diff] [blame] | 2534 | xprt->stat.bklog_u, |
| 2535 | xprt->stat.max_slots, |
| 2536 | xprt->stat.sending_u, |
| 2537 | xprt->stat.pending_u); |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 2538 | } |
| 2539 | |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2540 | /* |
| 2541 | * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason |
| 2542 | * we allocate pages instead doing a kmalloc like rpc_malloc is because we want |
| 2543 | * to use the server side send routines. |
| 2544 | */ |
Chuck Lever | 5fe6eaa | 2016-09-15 10:55:20 -0400 | [diff] [blame] | 2545 | static int bc_malloc(struct rpc_task *task) |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2546 | { |
Chuck Lever | 5fe6eaa | 2016-09-15 10:55:20 -0400 | [diff] [blame] | 2547 | struct rpc_rqst *rqst = task->tk_rqstp; |
| 2548 | size_t size = rqst->rq_callsize; |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2549 | struct page *page; |
| 2550 | struct rpc_buffer *buf; |
| 2551 | |
Chuck Lever | 5fe6eaa | 2016-09-15 10:55:20 -0400 | [diff] [blame] | 2552 | if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) { |
| 2553 | WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n", |
| 2554 | size); |
| 2555 | return -EINVAL; |
| 2556 | } |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2557 | |
Weston Andros Adamson | b8a13d0 | 2012-10-23 10:43:43 -0400 | [diff] [blame] | 2558 | page = alloc_page(GFP_KERNEL); |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2559 | if (!page) |
Chuck Lever | 5fe6eaa | 2016-09-15 10:55:20 -0400 | [diff] [blame] | 2560 | return -ENOMEM; |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2561 | |
| 2562 | buf = page_address(page); |
| 2563 | buf->len = PAGE_SIZE; |
| 2564 | |
Chuck Lever | 5fe6eaa | 2016-09-15 10:55:20 -0400 | [diff] [blame] | 2565 | rqst->rq_buffer = buf->data; |
Jeff Layton | 18e601d | 2016-10-24 20:33:23 -0400 | [diff] [blame] | 2566 | rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize; |
Chuck Lever | 5fe6eaa | 2016-09-15 10:55:20 -0400 | [diff] [blame] | 2567 | return 0; |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2568 | } |
| 2569 | |
| 2570 | /* |
| 2571 | * Free the space allocated in the bc_alloc routine |
| 2572 | */ |
Chuck Lever | 3435c74 | 2016-09-15 10:55:29 -0400 | [diff] [blame] | 2573 | static void bc_free(struct rpc_task *task) |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2574 | { |
Chuck Lever | 3435c74 | 2016-09-15 10:55:29 -0400 | [diff] [blame] | 2575 | void *buffer = task->tk_rqstp->rq_buffer; |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2576 | struct rpc_buffer *buf; |
| 2577 | |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2578 | buf = container_of(buffer, struct rpc_buffer, data); |
| 2579 | free_page((unsigned long)buf); |
| 2580 | } |
| 2581 | |
| 2582 | /* |
| 2583 | * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex |
| 2584 | * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request. |
| 2585 | */ |
| 2586 | static int bc_sendto(struct rpc_rqst *req) |
| 2587 | { |
| 2588 | int len; |
| 2589 | struct xdr_buf *xbufp = &req->rq_snd_buf; |
| 2590 | struct rpc_xprt *xprt = req->rq_xprt; |
| 2591 | struct sock_xprt *transport = |
| 2592 | container_of(xprt, struct sock_xprt, xprt); |
| 2593 | struct socket *sock = transport->sock; |
| 2594 | unsigned long headoff; |
| 2595 | unsigned long tailoff; |
| 2596 | |
Chuck Lever | 61677ee | 2011-05-09 15:22:34 -0400 | [diff] [blame] | 2597 | xs_encode_stream_record_marker(xbufp); |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2598 | |
| 2599 | tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK; |
| 2600 | headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK; |
| 2601 | len = svc_send_common(sock, xbufp, |
| 2602 | virt_to_page(xbufp->head[0].iov_base), headoff, |
| 2603 | xbufp->tail[0].iov_base, tailoff); |
| 2604 | |
| 2605 | if (len != xbufp->len) { |
| 2606 | printk(KERN_NOTICE "Error sending entire callback!\n"); |
| 2607 | len = -EAGAIN; |
| 2608 | } |
| 2609 | |
| 2610 | return len; |
| 2611 | } |
| 2612 | |
| 2613 | /* |
| 2614 | * The send routine. Borrows from svc_send |
| 2615 | */ |
Trond Myklebust | adfa714 | 2018-09-03 23:58:59 -0400 | [diff] [blame] | 2616 | static int bc_send_request(struct rpc_rqst *req) |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2617 | { |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2618 | struct svc_xprt *xprt; |
Andrzej Hajda | 7fc5613 | 2015-09-24 16:00:09 +0200 | [diff] [blame] | 2619 | int len; |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2620 | |
| 2621 | dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid)); |
| 2622 | /* |
| 2623 | * Get the server socket associated with this callback xprt |
| 2624 | */ |
| 2625 | xprt = req->rq_xprt->bc_xprt; |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2626 | |
| 2627 | /* |
| 2628 | * Grab the mutex to serialize data as the connection is shared |
| 2629 | * with the fore channel |
| 2630 | */ |
Trond Myklebust | c544577 | 2018-09-03 23:39:27 -0400 | [diff] [blame] | 2631 | mutex_lock(&xprt->xpt_mutex); |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2632 | if (test_bit(XPT_DEAD, &xprt->xpt_flags)) |
| 2633 | len = -ENOTCONN; |
| 2634 | else |
| 2635 | len = bc_sendto(req); |
| 2636 | mutex_unlock(&xprt->xpt_mutex); |
| 2637 | |
| 2638 | if (len > 0) |
| 2639 | len = 0; |
| 2640 | |
| 2641 | return len; |
| 2642 | } |
| 2643 | |
| 2644 | /* |
| 2645 | * The close routine. Since this is client initiated, we do nothing |
| 2646 | */ |
| 2647 | |
| 2648 | static void bc_close(struct rpc_xprt *xprt) |
| 2649 | { |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2650 | } |
| 2651 | |
| 2652 | /* |
| 2653 | * The xprt destroy routine. Again, because this connection is client |
| 2654 | * initiated, we do nothing |
| 2655 | */ |
| 2656 | |
| 2657 | static void bc_destroy(struct rpc_xprt *xprt) |
| 2658 | { |
Kinglong Mee | 47f72ef | 2014-03-24 11:58:16 +0800 | [diff] [blame] | 2659 | dprintk("RPC: bc_destroy xprt %p\n", xprt); |
| 2660 | |
| 2661 | xs_xprt_free(xprt); |
| 2662 | module_put(THIS_MODULE); |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2663 | } |
| 2664 | |
Chuck Lever | d31ae25 | 2017-08-01 12:00:39 -0400 | [diff] [blame] | 2665 | static const struct rpc_xprt_ops xs_local_ops = { |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2666 | .reserve_xprt = xprt_reserve_xprt, |
Trond Myklebust | 4cd34e7 | 2018-08-31 10:00:02 -0400 | [diff] [blame] | 2667 | .release_xprt = xprt_release_xprt, |
Trond Myklebust | f39c1bf | 2012-09-07 11:08:50 -0400 | [diff] [blame] | 2668 | .alloc_slot = xprt_alloc_slot, |
Chuck Lever | a9cde23 | 2018-05-04 15:34:59 -0400 | [diff] [blame] | 2669 | .free_slot = xprt_free_slot, |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2670 | .rpcbind = xs_local_rpcbind, |
| 2671 | .set_port = xs_local_set_port, |
J. Bruce Fields | dc10740 | 2013-02-20 17:52:19 -0500 | [diff] [blame] | 2672 | .connect = xs_local_connect, |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2673 | .buf_alloc = rpc_malloc, |
| 2674 | .buf_free = rpc_free, |
Trond Myklebust | 550aebf | 2018-09-14 14:32:45 -0400 | [diff] [blame] | 2675 | .prepare_request = xs_stream_prepare_request, |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2676 | .send_request = xs_local_send_request, |
| 2677 | .set_retrans_timeout = xprt_set_retrans_timeout_def, |
| 2678 | .close = xs_close, |
Trond Myklebust | a1311d8 | 2013-10-31 09:18:49 -0400 | [diff] [blame] | 2679 | .destroy = xs_destroy, |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2680 | .print_stats = xs_local_print_stats, |
Jeff Layton | d67fa4d | 2015-06-03 16:14:29 -0400 | [diff] [blame] | 2681 | .enable_swap = xs_enable_swap, |
| 2682 | .disable_swap = xs_disable_swap, |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2683 | }; |
| 2684 | |
Chuck Lever | d31ae25 | 2017-08-01 12:00:39 -0400 | [diff] [blame] | 2685 | static const struct rpc_xprt_ops xs_udp_ops = { |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 2686 | .set_buffer_size = xs_udp_set_buffer_size, |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 2687 | .reserve_xprt = xprt_reserve_xprt_cong, |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 2688 | .release_xprt = xprt_release_xprt_cong, |
Trond Myklebust | f39c1bf | 2012-09-07 11:08:50 -0400 | [diff] [blame] | 2689 | .alloc_slot = xprt_alloc_slot, |
Chuck Lever | a9cde23 | 2018-05-04 15:34:59 -0400 | [diff] [blame] | 2690 | .free_slot = xprt_free_slot, |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 2691 | .rpcbind = rpcb_getport_async, |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame] | 2692 | .set_port = xs_set_port, |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 2693 | .connect = xs_connect, |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 2694 | .buf_alloc = rpc_malloc, |
| 2695 | .buf_free = rpc_free, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 2696 | .send_request = xs_udp_send_request, |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 2697 | .set_retrans_timeout = xprt_set_retrans_timeout_rtt, |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 2698 | .timer = xs_udp_timer, |
Chuck Lever | a58dd39 | 2005-08-25 16:25:53 -0700 | [diff] [blame] | 2699 | .release_request = xprt_release_rqst_cong, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 2700 | .close = xs_close, |
| 2701 | .destroy = xs_destroy, |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 2702 | .print_stats = xs_udp_print_stats, |
Jeff Layton | d67fa4d | 2015-06-03 16:14:29 -0400 | [diff] [blame] | 2703 | .enable_swap = xs_enable_swap, |
| 2704 | .disable_swap = xs_disable_swap, |
Chuck Lever | 4a06825 | 2015-05-11 14:02:25 -0400 | [diff] [blame] | 2705 | .inject_disconnect = xs_inject_disconnect, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 2706 | }; |
| 2707 | |
Chuck Lever | d31ae25 | 2017-08-01 12:00:39 -0400 | [diff] [blame] | 2708 | static const struct rpc_xprt_ops xs_tcp_ops = { |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 2709 | .reserve_xprt = xprt_reserve_xprt, |
Trond Myklebust | 4cd34e7 | 2018-08-31 10:00:02 -0400 | [diff] [blame] | 2710 | .release_xprt = xprt_release_xprt, |
Trond Myklebust | 36bd7de | 2018-09-03 18:41:32 -0400 | [diff] [blame] | 2711 | .alloc_slot = xprt_alloc_slot, |
Chuck Lever | a9cde23 | 2018-05-04 15:34:59 -0400 | [diff] [blame] | 2712 | .free_slot = xprt_free_slot, |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 2713 | .rpcbind = rpcb_getport_async, |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame] | 2714 | .set_port = xs_set_port, |
Trond Myklebust | 0b9e794 | 2010-04-16 16:41:57 -0400 | [diff] [blame] | 2715 | .connect = xs_connect, |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 2716 | .buf_alloc = rpc_malloc, |
| 2717 | .buf_free = rpc_free, |
Trond Myklebust | 277e4ab | 2018-09-14 09:49:06 -0400 | [diff] [blame] | 2718 | .prepare_request = xs_stream_prepare_request, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 2719 | .send_request = xs_tcp_send_request, |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 2720 | .set_retrans_timeout = xprt_set_retrans_timeout_def, |
Trond Myklebust | c627d31 | 2015-02-10 11:06:04 -0500 | [diff] [blame] | 2721 | .close = xs_tcp_shutdown, |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 2722 | .destroy = xs_destroy, |
Trond Myklebust | 7196dbb | 2017-02-08 11:17:54 -0500 | [diff] [blame] | 2723 | .set_connect_timeout = xs_tcp_set_connect_timeout, |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 2724 | .print_stats = xs_tcp_print_stats, |
Jeff Layton | d67fa4d | 2015-06-03 16:14:29 -0400 | [diff] [blame] | 2725 | .enable_swap = xs_enable_swap, |
| 2726 | .disable_swap = xs_disable_swap, |
Chuck Lever | 4a06825 | 2015-05-11 14:02:25 -0400 | [diff] [blame] | 2727 | .inject_disconnect = xs_inject_disconnect, |
Chuck Lever | 42e5c3e | 2015-10-24 17:27:35 -0400 | [diff] [blame] | 2728 | #ifdef CONFIG_SUNRPC_BACKCHANNEL |
| 2729 | .bc_setup = xprt_setup_bc, |
Chuck Lever | 6b26cc8 | 2016-05-02 14:40:40 -0400 | [diff] [blame] | 2730 | .bc_maxpayload = xs_tcp_bc_maxpayload, |
Chuck Lever | 42e5c3e | 2015-10-24 17:27:35 -0400 | [diff] [blame] | 2731 | .bc_free_rqst = xprt_free_bc_rqst, |
| 2732 | .bc_destroy = xprt_destroy_bc, |
| 2733 | #endif |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2734 | }; |
| 2735 | |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2736 | /* |
| 2737 | * The rpc_xprt_ops for the server backchannel |
| 2738 | */ |
| 2739 | |
Chuck Lever | d31ae25 | 2017-08-01 12:00:39 -0400 | [diff] [blame] | 2740 | static const struct rpc_xprt_ops bc_tcp_ops = { |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2741 | .reserve_xprt = xprt_reserve_xprt, |
| 2742 | .release_xprt = xprt_release_xprt, |
Bryan Schumaker | 84e28a3 | 2012-09-24 13:39:01 -0400 | [diff] [blame] | 2743 | .alloc_slot = xprt_alloc_slot, |
Chuck Lever | a9cde23 | 2018-05-04 15:34:59 -0400 | [diff] [blame] | 2744 | .free_slot = xprt_free_slot, |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2745 | .buf_alloc = bc_malloc, |
| 2746 | .buf_free = bc_free, |
| 2747 | .send_request = bc_send_request, |
| 2748 | .set_retrans_timeout = xprt_set_retrans_timeout_def, |
| 2749 | .close = bc_close, |
| 2750 | .destroy = bc_destroy, |
| 2751 | .print_stats = xs_tcp_print_stats, |
Jeff Layton | d67fa4d | 2015-06-03 16:14:29 -0400 | [diff] [blame] | 2752 | .enable_swap = xs_enable_swap, |
| 2753 | .disable_swap = xs_disable_swap, |
Chuck Lever | 4a06825 | 2015-05-11 14:02:25 -0400 | [diff] [blame] | 2754 | .inject_disconnect = xs_inject_disconnect, |
Rahul Iyer | 4cfc7e6 | 2009-09-10 17:32:28 +0300 | [diff] [blame] | 2755 | }; |
| 2756 | |
Chuck Lever | 9247685 | 2010-10-20 11:53:01 -0400 | [diff] [blame] | 2757 | static int xs_init_anyaddr(const int family, struct sockaddr *sap) |
| 2758 | { |
| 2759 | static const struct sockaddr_in sin = { |
| 2760 | .sin_family = AF_INET, |
| 2761 | .sin_addr.s_addr = htonl(INADDR_ANY), |
| 2762 | }; |
| 2763 | static const struct sockaddr_in6 sin6 = { |
| 2764 | .sin6_family = AF_INET6, |
| 2765 | .sin6_addr = IN6ADDR_ANY_INIT, |
| 2766 | }; |
| 2767 | |
| 2768 | switch (family) { |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2769 | case AF_LOCAL: |
| 2770 | break; |
Chuck Lever | 9247685 | 2010-10-20 11:53:01 -0400 | [diff] [blame] | 2771 | case AF_INET: |
| 2772 | memcpy(sap, &sin, sizeof(sin)); |
| 2773 | break; |
| 2774 | case AF_INET6: |
| 2775 | memcpy(sap, &sin6, sizeof(sin6)); |
| 2776 | break; |
| 2777 | default: |
| 2778 | dprintk("RPC: %s: Bad address family\n", __func__); |
| 2779 | return -EAFNOSUPPORT; |
| 2780 | } |
| 2781 | return 0; |
| 2782 | } |
| 2783 | |
\"Talpey, Thomas\ | 3c341b0b | 2007-09-10 13:47:07 -0400 | [diff] [blame] | 2784 | static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 2785 | unsigned int slot_table_size, |
| 2786 | unsigned int max_slot_table_size) |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2787 | { |
| 2788 | struct rpc_xprt *xprt; |
Chuck Lever | ffc2e51 | 2006-12-05 16:35:11 -0500 | [diff] [blame] | 2789 | struct sock_xprt *new; |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2790 | |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 2791 | if (args->addrlen > sizeof(xprt->addr)) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 2792 | dprintk("RPC: xs_setup_xprt: address too large\n"); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2793 | return ERR_PTR(-EBADF); |
| 2794 | } |
| 2795 | |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 2796 | xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size, |
| 2797 | max_slot_table_size); |
Pavel Emelyanov | bd1722d | 2010-09-29 16:02:43 +0400 | [diff] [blame] | 2798 | if (xprt == NULL) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 2799 | dprintk("RPC: xs_setup_xprt: couldn't allocate " |
| 2800 | "rpc_xprt\n"); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2801 | return ERR_PTR(-ENOMEM); |
| 2802 | } |
| 2803 | |
Pavel Emelyanov | bd1722d | 2010-09-29 16:02:43 +0400 | [diff] [blame] | 2804 | new = container_of(xprt, struct sock_xprt, xprt); |
Trond Myklebust | edc1b01 | 2015-10-05 10:53:49 -0400 | [diff] [blame] | 2805 | mutex_init(&new->recv_mutex); |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 2806 | memcpy(&xprt->addr, args->dstaddr, args->addrlen); |
| 2807 | xprt->addrlen = args->addrlen; |
Frank van Maarseveen | d3bc9a1 | 2007-07-09 22:23:35 +0200 | [diff] [blame] | 2808 | if (args->srcaddr) |
Chuck Lever | fbfffbd | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 2809 | memcpy(&new->srcaddr, args->srcaddr, args->addrlen); |
Chuck Lever | 9247685 | 2010-10-20 11:53:01 -0400 | [diff] [blame] | 2810 | else { |
| 2811 | int err; |
| 2812 | err = xs_init_anyaddr(args->dstaddr->sa_family, |
| 2813 | (struct sockaddr *)&new->srcaddr); |
Stanislav Kinsbursky | 2aa1353 | 2011-11-10 14:33:23 +0300 | [diff] [blame] | 2814 | if (err != 0) { |
| 2815 | xprt_free(xprt); |
Chuck Lever | 9247685 | 2010-10-20 11:53:01 -0400 | [diff] [blame] | 2816 | return ERR_PTR(err); |
Stanislav Kinsbursky | 2aa1353 | 2011-11-10 14:33:23 +0300 | [diff] [blame] | 2817 | } |
Chuck Lever | 9247685 | 2010-10-20 11:53:01 -0400 | [diff] [blame] | 2818 | } |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2819 | |
| 2820 | return xprt; |
| 2821 | } |
| 2822 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2823 | static const struct rpc_timeout xs_local_default_timeout = { |
| 2824 | .to_initval = 10 * HZ, |
| 2825 | .to_maxval = 10 * HZ, |
| 2826 | .to_retries = 2, |
| 2827 | }; |
| 2828 | |
| 2829 | /** |
| 2830 | * xs_setup_local - Set up transport to use an AF_LOCAL socket |
| 2831 | * @args: rpc transport creation arguments |
| 2832 | * |
| 2833 | * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP |
| 2834 | */ |
| 2835 | static struct rpc_xprt *xs_setup_local(struct xprt_create *args) |
| 2836 | { |
| 2837 | struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr; |
| 2838 | struct sock_xprt *transport; |
| 2839 | struct rpc_xprt *xprt; |
| 2840 | struct rpc_xprt *ret; |
| 2841 | |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 2842 | xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, |
| 2843 | xprt_max_tcp_slot_table_entries); |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2844 | if (IS_ERR(xprt)) |
| 2845 | return xprt; |
| 2846 | transport = container_of(xprt, struct sock_xprt, xprt); |
| 2847 | |
| 2848 | xprt->prot = 0; |
| 2849 | xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); |
| 2850 | xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; |
| 2851 | |
| 2852 | xprt->bind_timeout = XS_BIND_TO; |
| 2853 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; |
| 2854 | xprt->idle_timeout = XS_IDLE_DISC_TO; |
| 2855 | |
| 2856 | xprt->ops = &xs_local_ops; |
| 2857 | xprt->timeout = &xs_local_default_timeout; |
| 2858 | |
Trond Myklebust | 550aebf | 2018-09-14 14:32:45 -0400 | [diff] [blame] | 2859 | INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); |
| 2860 | INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket); |
NeilBrown | 93dc41b | 2013-10-31 16:14:36 +1100 | [diff] [blame] | 2861 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2862 | switch (sun->sun_family) { |
| 2863 | case AF_LOCAL: |
| 2864 | if (sun->sun_path[0] != '/') { |
| 2865 | dprintk("RPC: bad AF_LOCAL address: %s\n", |
| 2866 | sun->sun_path); |
| 2867 | ret = ERR_PTR(-EINVAL); |
| 2868 | goto out_err; |
| 2869 | } |
| 2870 | xprt_set_bound(xprt); |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2871 | xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL); |
J. Bruce Fields | 7073ea8 | 2013-02-21 10:14:22 -0500 | [diff] [blame] | 2872 | ret = ERR_PTR(xs_local_setup_socket(transport)); |
| 2873 | if (ret) |
| 2874 | goto out_err; |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2875 | break; |
| 2876 | default: |
| 2877 | ret = ERR_PTR(-EAFNOSUPPORT); |
| 2878 | goto out_err; |
| 2879 | } |
| 2880 | |
| 2881 | dprintk("RPC: set up xprt to %s via AF_LOCAL\n", |
| 2882 | xprt->address_strings[RPC_DISPLAY_ADDR]); |
| 2883 | |
| 2884 | if (try_module_get(THIS_MODULE)) |
| 2885 | return xprt; |
| 2886 | ret = ERR_PTR(-EINVAL); |
| 2887 | out_err: |
Kinglong Mee | 315f381 | 2014-03-24 11:07:22 +0800 | [diff] [blame] | 2888 | xs_xprt_free(xprt); |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 2889 | return ret; |
| 2890 | } |
| 2891 | |
Trond Myklebust | 2881ae7 | 2007-12-20 16:03:54 -0500 | [diff] [blame] | 2892 | static const struct rpc_timeout xs_udp_default_timeout = { |
| 2893 | .to_initval = 5 * HZ, |
| 2894 | .to_maxval = 30 * HZ, |
| 2895 | .to_increment = 5 * HZ, |
| 2896 | .to_retries = 5, |
| 2897 | }; |
| 2898 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 2899 | /** |
| 2900 | * xs_setup_udp - Set up transport to use a UDP socket |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 2901 | * @args: rpc transport creation arguments |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 2902 | * |
| 2903 | */ |
Adrian Bunk | 483066d | 2007-10-24 18:24:02 +0200 | [diff] [blame] | 2904 | static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2905 | { |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 2906 | struct sockaddr *addr = args->dstaddr; |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2907 | struct rpc_xprt *xprt; |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 2908 | struct sock_xprt *transport; |
J. Bruce Fields | 0a68b0b | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 2909 | struct rpc_xprt *ret; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2910 | |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 2911 | xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries, |
| 2912 | xprt_udp_slot_table_entries); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2913 | if (IS_ERR(xprt)) |
| 2914 | return xprt; |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 2915 | transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2916 | |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 2917 | xprt->prot = IPPROTO_UDP; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 2918 | xprt->tsh_size = 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2919 | /* XXX: header size can vary due to auth type, IPv6, etc. */ |
| 2920 | xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); |
| 2921 | |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 2922 | xprt->bind_timeout = XS_BIND_TO; |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 2923 | xprt->reestablish_timeout = XS_UDP_REEST_TO; |
| 2924 | xprt->idle_timeout = XS_IDLE_DISC_TO; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2925 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 2926 | xprt->ops = &xs_udp_ops; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2927 | |
Trond Myklebust | ba7392b | 2007-12-20 16:03:55 -0500 | [diff] [blame] | 2928 | xprt->timeout = &xs_udp_default_timeout; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2929 | |
Trond Myklebust | f9b2ee7 | 2015-10-06 16:26:05 -0400 | [diff] [blame] | 2930 | INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn); |
Trond Myklebust | edc1b01 | 2015-10-05 10:53:49 -0400 | [diff] [blame] | 2931 | INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket); |
| 2932 | |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 2933 | switch (addr->sa_family) { |
| 2934 | case AF_INET: |
| 2935 | if (((struct sockaddr_in *)addr)->sin_port != htons(0)) |
| 2936 | xprt_set_bound(xprt); |
| 2937 | |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 2938 | xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 2939 | break; |
| 2940 | case AF_INET6: |
| 2941 | if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) |
| 2942 | xprt_set_bound(xprt); |
| 2943 | |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 2944 | xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 2945 | break; |
| 2946 | default: |
J. Bruce Fields | 0a68b0b | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 2947 | ret = ERR_PTR(-EAFNOSUPPORT); |
| 2948 | goto out_err; |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 2949 | } |
| 2950 | |
Chuck Lever | c740eff | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 2951 | if (xprt_bound(xprt)) |
| 2952 | dprintk("RPC: set up xprt to %s (port %s) via %s\n", |
| 2953 | xprt->address_strings[RPC_DISPLAY_ADDR], |
| 2954 | xprt->address_strings[RPC_DISPLAY_PORT], |
| 2955 | xprt->address_strings[RPC_DISPLAY_PROTO]); |
| 2956 | else |
| 2957 | dprintk("RPC: set up xprt to %s (autobind) via %s\n", |
| 2958 | xprt->address_strings[RPC_DISPLAY_ADDR], |
| 2959 | xprt->address_strings[RPC_DISPLAY_PROTO]); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 2960 | |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 2961 | if (try_module_get(THIS_MODULE)) |
| 2962 | return xprt; |
J. Bruce Fields | 0a68b0b | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 2963 | ret = ERR_PTR(-EINVAL); |
| 2964 | out_err: |
Kinglong Mee | 315f381 | 2014-03-24 11:07:22 +0800 | [diff] [blame] | 2965 | xs_xprt_free(xprt); |
J. Bruce Fields | 0a68b0b | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 2966 | return ret; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2967 | } |
| 2968 | |
Trond Myklebust | 2881ae7 | 2007-12-20 16:03:54 -0500 | [diff] [blame] | 2969 | static const struct rpc_timeout xs_tcp_default_timeout = { |
| 2970 | .to_initval = 60 * HZ, |
| 2971 | .to_maxval = 60 * HZ, |
| 2972 | .to_retries = 2, |
| 2973 | }; |
| 2974 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 2975 | /** |
| 2976 | * xs_setup_tcp - Set up transport to use a TCP socket |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 2977 | * @args: rpc transport creation arguments |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 2978 | * |
| 2979 | */ |
Adrian Bunk | 483066d | 2007-10-24 18:24:02 +0200 | [diff] [blame] | 2980 | static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2981 | { |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 2982 | struct sockaddr *addr = args->dstaddr; |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2983 | struct rpc_xprt *xprt; |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 2984 | struct sock_xprt *transport; |
J. Bruce Fields | 0a68b0b | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 2985 | struct rpc_xprt *ret; |
Trond Myklebust | b7993ce | 2013-04-14 11:42:00 -0400 | [diff] [blame] | 2986 | unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries; |
| 2987 | |
| 2988 | if (args->flags & XPRT_CREATE_INFINITE_SLOTS) |
| 2989 | max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2990 | |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 2991 | xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, |
Trond Myklebust | b7993ce | 2013-04-14 11:42:00 -0400 | [diff] [blame] | 2992 | max_slot_table_size); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 2993 | if (IS_ERR(xprt)) |
| 2994 | return xprt; |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 2995 | transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 2996 | |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 2997 | xprt->prot = IPPROTO_TCP; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 2998 | xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 2999 | xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 3000 | |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 3001 | xprt->bind_timeout = XS_BIND_TO; |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 3002 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; |
| 3003 | xprt->idle_timeout = XS_IDLE_DISC_TO; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 3004 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 3005 | xprt->ops = &xs_tcp_ops; |
Trond Myklebust | ba7392b | 2007-12-20 16:03:55 -0500 | [diff] [blame] | 3006 | xprt->timeout = &xs_tcp_default_timeout; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 3007 | |
Trond Myklebust | 3851f1c | 2016-08-04 00:08:45 -0400 | [diff] [blame] | 3008 | xprt->max_reconnect_timeout = xprt->timeout->to_maxval; |
Trond Myklebust | 7196dbb | 2017-02-08 11:17:54 -0500 | [diff] [blame] | 3009 | xprt->connect_timeout = xprt->timeout->to_initval * |
| 3010 | (xprt->timeout->to_retries + 1); |
Trond Myklebust | 3851f1c | 2016-08-04 00:08:45 -0400 | [diff] [blame] | 3011 | |
Trond Myklebust | c50b8ee | 2018-09-14 14:26:28 -0400 | [diff] [blame] | 3012 | INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); |
Trond Myklebust | edc1b01 | 2015-10-05 10:53:49 -0400 | [diff] [blame] | 3013 | INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket); |
| 3014 | |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 3015 | switch (addr->sa_family) { |
| 3016 | case AF_INET: |
| 3017 | if (((struct sockaddr_in *)addr)->sin_port != htons(0)) |
| 3018 | xprt_set_bound(xprt); |
| 3019 | |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 3020 | xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 3021 | break; |
| 3022 | case AF_INET6: |
| 3023 | if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) |
| 3024 | xprt_set_bound(xprt); |
| 3025 | |
Chuck Lever | 9dc3b09 | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 3026 | xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 3027 | break; |
| 3028 | default: |
J. Bruce Fields | 0a68b0b | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 3029 | ret = ERR_PTR(-EAFNOSUPPORT); |
| 3030 | goto out_err; |
Chuck Lever | 8f9d5b1 | 2007-08-06 11:57:53 -0400 | [diff] [blame] | 3031 | } |
| 3032 | |
Chuck Lever | c740eff | 2009-08-09 15:09:46 -0400 | [diff] [blame] | 3033 | if (xprt_bound(xprt)) |
| 3034 | dprintk("RPC: set up xprt to %s (port %s) via %s\n", |
| 3035 | xprt->address_strings[RPC_DISPLAY_ADDR], |
| 3036 | xprt->address_strings[RPC_DISPLAY_PORT], |
| 3037 | xprt->address_strings[RPC_DISPLAY_PROTO]); |
| 3038 | else |
| 3039 | dprintk("RPC: set up xprt to %s (autobind) via %s\n", |
| 3040 | xprt->address_strings[RPC_DISPLAY_ADDR], |
| 3041 | xprt->address_strings[RPC_DISPLAY_PROTO]); |
| 3042 | |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 3043 | if (try_module_get(THIS_MODULE)) |
| 3044 | return xprt; |
J. Bruce Fields | 0a68b0b | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 3045 | ret = ERR_PTR(-EINVAL); |
| 3046 | out_err: |
Kinglong Mee | 315f381 | 2014-03-24 11:07:22 +0800 | [diff] [blame] | 3047 | xs_xprt_free(xprt); |
J. Bruce Fields | 0a68b0b | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 3048 | return ret; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 3049 | } |
Chuck Lever | 282b32e | 2006-12-05 16:35:51 -0500 | [diff] [blame] | 3050 | |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 3051 | /** |
| 3052 | * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket |
| 3053 | * @args: rpc transport creation arguments |
| 3054 | * |
| 3055 | */ |
| 3056 | static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args) |
| 3057 | { |
| 3058 | struct sockaddr *addr = args->dstaddr; |
| 3059 | struct rpc_xprt *xprt; |
| 3060 | struct sock_xprt *transport; |
| 3061 | struct svc_sock *bc_sock; |
J. Bruce Fields | 0a68b0b | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 3062 | struct rpc_xprt *ret; |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 3063 | |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 3064 | xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, |
| 3065 | xprt_tcp_slot_table_entries); |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 3066 | if (IS_ERR(xprt)) |
| 3067 | return xprt; |
| 3068 | transport = container_of(xprt, struct sock_xprt, xprt); |
| 3069 | |
| 3070 | xprt->prot = IPPROTO_TCP; |
| 3071 | xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); |
| 3072 | xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; |
| 3073 | xprt->timeout = &xs_tcp_default_timeout; |
| 3074 | |
| 3075 | /* backchannel */ |
| 3076 | xprt_set_bound(xprt); |
| 3077 | xprt->bind_timeout = 0; |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 3078 | xprt->reestablish_timeout = 0; |
| 3079 | xprt->idle_timeout = 0; |
| 3080 | |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 3081 | xprt->ops = &bc_tcp_ops; |
| 3082 | |
| 3083 | switch (addr->sa_family) { |
| 3084 | case AF_INET: |
| 3085 | xs_format_peer_addresses(xprt, "tcp", |
| 3086 | RPCBIND_NETID_TCP); |
| 3087 | break; |
| 3088 | case AF_INET6: |
| 3089 | xs_format_peer_addresses(xprt, "tcp", |
| 3090 | RPCBIND_NETID_TCP6); |
| 3091 | break; |
| 3092 | default: |
J. Bruce Fields | 0a68b0b | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 3093 | ret = ERR_PTR(-EAFNOSUPPORT); |
| 3094 | goto out_err; |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 3095 | } |
| 3096 | |
Pavel Emelyanov | 50fa0d4 | 2010-10-05 20:49:35 +0400 | [diff] [blame] | 3097 | dprintk("RPC: set up xprt to %s (port %s) via %s\n", |
| 3098 | xprt->address_strings[RPC_DISPLAY_ADDR], |
| 3099 | xprt->address_strings[RPC_DISPLAY_PORT], |
| 3100 | xprt->address_strings[RPC_DISPLAY_PROTO]); |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 3101 | |
| 3102 | /* |
J. Bruce Fields | 99de8ea | 2010-12-08 12:45:44 -0500 | [diff] [blame] | 3103 | * Once we've associated a backchannel xprt with a connection, |
Weng Meiling | 28303ca | 2013-11-30 17:56:44 +0800 | [diff] [blame] | 3104 | * we want to keep it around as long as the connection lasts, |
| 3105 | * in case we need to start using it for a backchannel again; |
| 3106 | * this reference won't be dropped until bc_xprt is destroyed. |
J. Bruce Fields | 99de8ea | 2010-12-08 12:45:44 -0500 | [diff] [blame] | 3107 | */ |
| 3108 | xprt_get(xprt); |
| 3109 | args->bc_xprt->xpt_bc_xprt = xprt; |
| 3110 | xprt->bc_xprt = args->bc_xprt; |
| 3111 | bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt); |
| 3112 | transport->sock = bc_sock->sk_sock; |
| 3113 | transport->inet = bc_sock->sk_sk; |
| 3114 | |
| 3115 | /* |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 3116 | * Since we don't want connections for the backchannel, we set |
| 3117 | * the xprt status to connected |
| 3118 | */ |
| 3119 | xprt_set_connected(xprt); |
| 3120 | |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 3121 | if (try_module_get(THIS_MODULE)) |
| 3122 | return xprt; |
Kinglong Mee | 642aab5 | 2014-03-24 12:00:28 +0800 | [diff] [blame] | 3123 | |
| 3124 | args->bc_xprt->xpt_bc_xprt = NULL; |
J. Bruce Fields | 39a9bea | 2016-05-17 12:38:21 -0400 | [diff] [blame] | 3125 | args->bc_xprt->xpt_bc_xps = NULL; |
J. Bruce Fields | 99de8ea | 2010-12-08 12:45:44 -0500 | [diff] [blame] | 3126 | xprt_put(xprt); |
J. Bruce Fields | 0a68b0b | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 3127 | ret = ERR_PTR(-EINVAL); |
| 3128 | out_err: |
Kinglong Mee | 315f381 | 2014-03-24 11:07:22 +0800 | [diff] [blame] | 3129 | xs_xprt_free(xprt); |
J. Bruce Fields | 0a68b0b | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 3130 | return ret; |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 3131 | } |
| 3132 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 3133 | static struct xprt_class xs_local_transport = { |
| 3134 | .list = LIST_HEAD_INIT(xs_local_transport.list), |
| 3135 | .name = "named UNIX socket", |
| 3136 | .owner = THIS_MODULE, |
| 3137 | .ident = XPRT_TRANSPORT_LOCAL, |
| 3138 | .setup = xs_setup_local, |
| 3139 | }; |
| 3140 | |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 3141 | static struct xprt_class xs_udp_transport = { |
| 3142 | .list = LIST_HEAD_INIT(xs_udp_transport.list), |
| 3143 | .name = "udp", |
| 3144 | .owner = THIS_MODULE, |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 3145 | .ident = XPRT_TRANSPORT_UDP, |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 3146 | .setup = xs_setup_udp, |
| 3147 | }; |
| 3148 | |
| 3149 | static struct xprt_class xs_tcp_transport = { |
| 3150 | .list = LIST_HEAD_INIT(xs_tcp_transport.list), |
| 3151 | .name = "tcp", |
| 3152 | .owner = THIS_MODULE, |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 3153 | .ident = XPRT_TRANSPORT_TCP, |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 3154 | .setup = xs_setup_tcp, |
| 3155 | }; |
| 3156 | |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 3157 | static struct xprt_class xs_bc_tcp_transport = { |
| 3158 | .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list), |
| 3159 | .name = "tcp NFSv4.1 backchannel", |
| 3160 | .owner = THIS_MODULE, |
| 3161 | .ident = XPRT_TRANSPORT_BC_TCP, |
| 3162 | .setup = xs_setup_bc_tcp, |
| 3163 | }; |
| 3164 | |
Chuck Lever | 282b32e | 2006-12-05 16:35:51 -0500 | [diff] [blame] | 3165 | /** |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 3166 | * init_socket_xprt - set up xprtsock's sysctls, register with RPC client |
Chuck Lever | 282b32e | 2006-12-05 16:35:51 -0500 | [diff] [blame] | 3167 | * |
| 3168 | */ |
| 3169 | int init_socket_xprt(void) |
| 3170 | { |
Eric W. Biederman | 2b1bec5 | 2007-02-14 00:33:24 -0800 | [diff] [blame] | 3171 | if (!sunrpc_table_header) |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 3172 | sunrpc_table_header = register_sysctl_table(sunrpc_table); |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 3173 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 3174 | xprt_register_transport(&xs_local_transport); |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 3175 | xprt_register_transport(&xs_udp_transport); |
| 3176 | xprt_register_transport(&xs_tcp_transport); |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 3177 | xprt_register_transport(&xs_bc_tcp_transport); |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 3178 | |
Chuck Lever | 282b32e | 2006-12-05 16:35:51 -0500 | [diff] [blame] | 3179 | return 0; |
| 3180 | } |
| 3181 | |
| 3182 | /** |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 3183 | * cleanup_socket_xprt - remove xprtsock's sysctls, unregister |
Chuck Lever | 282b32e | 2006-12-05 16:35:51 -0500 | [diff] [blame] | 3184 | * |
| 3185 | */ |
| 3186 | void cleanup_socket_xprt(void) |
| 3187 | { |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 3188 | if (sunrpc_table_header) { |
| 3189 | unregister_sysctl_table(sunrpc_table_header); |
| 3190 | sunrpc_table_header = NULL; |
| 3191 | } |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 3192 | |
Chuck Lever | 176e21e | 2011-05-09 15:22:44 -0400 | [diff] [blame] | 3193 | xprt_unregister_transport(&xs_local_transport); |
\"Talpey, Thomas\ | bc25571 | 2007-09-10 13:46:39 -0400 | [diff] [blame] | 3194 | xprt_unregister_transport(&xs_udp_transport); |
| 3195 | xprt_unregister_transport(&xs_tcp_transport); |
Alexandros Batsakis | f300bab | 2009-09-10 17:33:30 +0300 | [diff] [blame] | 3196 | xprt_unregister_transport(&xs_bc_tcp_transport); |
Chuck Lever | 282b32e | 2006-12-05 16:35:51 -0500 | [diff] [blame] | 3197 | } |
Trond Myklebust | cbf1107 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 3198 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 3199 | static int param_set_uint_minmax(const char *val, |
| 3200 | const struct kernel_param *kp, |
Trond Myklebust | cbf1107 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 3201 | unsigned int min, unsigned int max) |
| 3202 | { |
Daniel Walter | 00cfaa9 | 2014-06-21 13:06:38 +0100 | [diff] [blame] | 3203 | unsigned int num; |
Trond Myklebust | cbf1107 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 3204 | int ret; |
| 3205 | |
| 3206 | if (!val) |
| 3207 | return -EINVAL; |
Daniel Walter | 00cfaa9 | 2014-06-21 13:06:38 +0100 | [diff] [blame] | 3208 | ret = kstrtouint(val, 0, &num); |
Dan Carpenter | 9761a24 | 2017-02-19 00:34:59 +0300 | [diff] [blame] | 3209 | if (ret) |
| 3210 | return ret; |
| 3211 | if (num < min || num > max) |
Trond Myklebust | cbf1107 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 3212 | return -EINVAL; |
| 3213 | *((unsigned int *)kp->arg) = num; |
| 3214 | return 0; |
| 3215 | } |
| 3216 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 3217 | static int param_set_portnr(const char *val, const struct kernel_param *kp) |
Trond Myklebust | cbf1107 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 3218 | { |
Frank Sorenson | ffb6ca3 | 2016-07-08 16:35:25 -0500 | [diff] [blame] | 3219 | return param_set_uint_minmax(val, kp, |
J. Bruce Fields | 826799e | 2018-10-18 15:27:02 -0400 | [diff] [blame] | 3220 | RPC_MIN_RESVPORT, |
Trond Myklebust | cbf1107 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 3221 | RPC_MAX_RESVPORT); |
| 3222 | } |
| 3223 | |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 3224 | static const struct kernel_param_ops param_ops_portnr = { |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 3225 | .set = param_set_portnr, |
| 3226 | .get = param_get_uint, |
| 3227 | }; |
| 3228 | |
Trond Myklebust | cbf1107 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 3229 | #define param_check_portnr(name, p) \ |
| 3230 | __param_check(name, p, unsigned int); |
| 3231 | |
| 3232 | module_param_named(min_resvport, xprt_min_resvport, portnr, 0644); |
| 3233 | module_param_named(max_resvport, xprt_max_resvport, portnr, 0644); |
| 3234 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 3235 | static int param_set_slot_table_size(const char *val, |
| 3236 | const struct kernel_param *kp) |
Trond Myklebust | cbf1107 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 3237 | { |
| 3238 | return param_set_uint_minmax(val, kp, |
| 3239 | RPC_MIN_SLOT_TABLE, |
| 3240 | RPC_MAX_SLOT_TABLE); |
| 3241 | } |
| 3242 | |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 3243 | static const struct kernel_param_ops param_ops_slot_table_size = { |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 3244 | .set = param_set_slot_table_size, |
| 3245 | .get = param_get_uint, |
| 3246 | }; |
| 3247 | |
Trond Myklebust | cbf1107 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 3248 | #define param_check_slot_table_size(name, p) \ |
| 3249 | __param_check(name, p, unsigned int); |
| 3250 | |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 3251 | static int param_set_max_slot_table_size(const char *val, |
| 3252 | const struct kernel_param *kp) |
| 3253 | { |
| 3254 | return param_set_uint_minmax(val, kp, |
| 3255 | RPC_MIN_SLOT_TABLE, |
| 3256 | RPC_MAX_SLOT_TABLE_LIMIT); |
| 3257 | } |
| 3258 | |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 3259 | static const struct kernel_param_ops param_ops_max_slot_table_size = { |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 3260 | .set = param_set_max_slot_table_size, |
| 3261 | .get = param_get_uint, |
| 3262 | }; |
| 3263 | |
| 3264 | #define param_check_max_slot_table_size(name, p) \ |
| 3265 | __param_check(name, p, unsigned int); |
| 3266 | |
Trond Myklebust | cbf1107 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 3267 | module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries, |
| 3268 | slot_table_size, 0644); |
Trond Myklebust | d9ba131 | 2011-07-17 18:11:30 -0400 | [diff] [blame] | 3269 | module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries, |
| 3270 | max_slot_table_size, 0644); |
Trond Myklebust | cbf1107 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 3271 | module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries, |
| 3272 | slot_table_size, 0644); |