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