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