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