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