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