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