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