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