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