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