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 | * |
| 6 | * TCP callback races fixes (C) 1998 Red Hat Software <alan@redhat.com> |
| 7 | * TCP send fixes (C) 1998 Red Hat Software <alan@redhat.com> |
| 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 | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <linux/types.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/capability.h> |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 21 | #include <linux/pagemap.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/socket.h> |
| 24 | #include <linux/in.h> |
| 25 | #include <linux/net.h> |
| 26 | #include <linux/mm.h> |
| 27 | #include <linux/udp.h> |
| 28 | #include <linux/tcp.h> |
| 29 | #include <linux/sunrpc/clnt.h> |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 30 | #include <linux/sunrpc/sched.h> |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 31 | #include <linux/file.h> |
| 32 | |
| 33 | #include <net/sock.h> |
| 34 | #include <net/checksum.h> |
| 35 | #include <net/udp.h> |
| 36 | #include <net/tcp.h> |
| 37 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 38 | /* |
Chuck Lever | c556b75 | 2005-11-01 12:24:48 -0500 | [diff] [blame] | 39 | * xprtsock tunables |
| 40 | */ |
| 41 | unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE; |
| 42 | unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE; |
| 43 | |
| 44 | unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT; |
| 45 | unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT; |
| 46 | |
| 47 | /* |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 48 | * We can register our own files under /proc/sys/sunrpc by |
| 49 | * calling register_sysctl_table() again. The files in that |
| 50 | * directory become the union of all files registered there. |
| 51 | * |
| 52 | * We simply need to make sure that we don't collide with |
| 53 | * someone else's file names! |
| 54 | */ |
| 55 | |
| 56 | #ifdef RPC_DEBUG |
| 57 | |
| 58 | static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE; |
| 59 | static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE; |
| 60 | static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT; |
| 61 | static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT; |
| 62 | |
| 63 | static struct ctl_table_header *sunrpc_table_header; |
| 64 | |
| 65 | /* |
| 66 | * FIXME: changing the UDP slot table size should also resize the UDP |
| 67 | * socket buffers for existing UDP transports |
| 68 | */ |
| 69 | static ctl_table xs_tunables_table[] = { |
| 70 | { |
| 71 | .ctl_name = CTL_SLOTTABLE_UDP, |
| 72 | .procname = "udp_slot_table_entries", |
| 73 | .data = &xprt_udp_slot_table_entries, |
| 74 | .maxlen = sizeof(unsigned int), |
| 75 | .mode = 0644, |
| 76 | .proc_handler = &proc_dointvec_minmax, |
| 77 | .strategy = &sysctl_intvec, |
| 78 | .extra1 = &min_slot_table_size, |
| 79 | .extra2 = &max_slot_table_size |
| 80 | }, |
| 81 | { |
| 82 | .ctl_name = CTL_SLOTTABLE_TCP, |
| 83 | .procname = "tcp_slot_table_entries", |
| 84 | .data = &xprt_tcp_slot_table_entries, |
| 85 | .maxlen = sizeof(unsigned int), |
| 86 | .mode = 0644, |
| 87 | .proc_handler = &proc_dointvec_minmax, |
| 88 | .strategy = &sysctl_intvec, |
| 89 | .extra1 = &min_slot_table_size, |
| 90 | .extra2 = &max_slot_table_size |
| 91 | }, |
| 92 | { |
| 93 | .ctl_name = CTL_MIN_RESVPORT, |
| 94 | .procname = "min_resvport", |
| 95 | .data = &xprt_min_resvport, |
| 96 | .maxlen = sizeof(unsigned int), |
| 97 | .mode = 0644, |
| 98 | .proc_handler = &proc_dointvec_minmax, |
| 99 | .strategy = &sysctl_intvec, |
| 100 | .extra1 = &xprt_min_resvport_limit, |
| 101 | .extra2 = &xprt_max_resvport_limit |
| 102 | }, |
| 103 | { |
| 104 | .ctl_name = CTL_MAX_RESVPORT, |
| 105 | .procname = "max_resvport", |
| 106 | .data = &xprt_max_resvport, |
| 107 | .maxlen = sizeof(unsigned int), |
| 108 | .mode = 0644, |
| 109 | .proc_handler = &proc_dointvec_minmax, |
| 110 | .strategy = &sysctl_intvec, |
| 111 | .extra1 = &xprt_min_resvport_limit, |
| 112 | .extra2 = &xprt_max_resvport_limit |
| 113 | }, |
| 114 | { |
| 115 | .ctl_name = 0, |
| 116 | }, |
| 117 | }; |
| 118 | |
| 119 | static ctl_table sunrpc_table[] = { |
| 120 | { |
| 121 | .ctl_name = CTL_SUNRPC, |
| 122 | .procname = "sunrpc", |
| 123 | .mode = 0555, |
| 124 | .child = xs_tunables_table |
| 125 | }, |
| 126 | { |
| 127 | .ctl_name = 0, |
| 128 | }, |
| 129 | }; |
| 130 | |
| 131 | #endif |
| 132 | |
| 133 | /* |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 134 | * How many times to try sending a request on a socket before waiting |
| 135 | * for the socket buffer to clear. |
| 136 | */ |
| 137 | #define XS_SENDMSG_RETRY (10U) |
| 138 | |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 139 | /* |
| 140 | * Time out for an RPC UDP socket connect. UDP socket connects are |
| 141 | * synchronous, but we set a timeout anyway in case of resource |
| 142 | * exhaustion on the local host. |
| 143 | */ |
| 144 | #define XS_UDP_CONN_TO (5U * HZ) |
| 145 | |
| 146 | /* |
| 147 | * Wait duration for an RPC TCP connection to be established. Solaris |
| 148 | * NFS over TCP uses 60 seconds, for example, which is in line with how |
| 149 | * long a server takes to reboot. |
| 150 | */ |
| 151 | #define XS_TCP_CONN_TO (60U * HZ) |
| 152 | |
| 153 | /* |
| 154 | * Wait duration for a reply from the RPC portmapper. |
| 155 | */ |
| 156 | #define XS_BIND_TO (60U * HZ) |
| 157 | |
| 158 | /* |
| 159 | * Delay if a UDP socket connect error occurs. This is most likely some |
| 160 | * kind of resource problem on the local host. |
| 161 | */ |
| 162 | #define XS_UDP_REEST_TO (2U * HZ) |
| 163 | |
| 164 | /* |
| 165 | * The reestablish timeout allows clients to delay for a bit before attempting |
| 166 | * to reconnect to a server that just dropped our connection. |
| 167 | * |
| 168 | * We implement an exponential backoff when trying to reestablish a TCP |
| 169 | * transport connection with the server. Some servers like to drop a TCP |
| 170 | * connection when they are overworked, so we start with a short timeout and |
| 171 | * increase over time if the server is down or not responding. |
| 172 | */ |
| 173 | #define XS_TCP_INIT_REEST_TO (3U * HZ) |
| 174 | #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ) |
| 175 | |
| 176 | /* |
| 177 | * TCP idle timeout; client drops the transport socket if it is idle |
| 178 | * for this long. Note that we also timeout UDP sockets to prevent |
| 179 | * holding port numbers when there is no RPC traffic. |
| 180 | */ |
| 181 | #define XS_IDLE_DISC_TO (5U * 60 * HZ) |
| 182 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 183 | #ifdef RPC_DEBUG |
| 184 | # undef RPC_DEBUG_DATA |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 185 | # define RPCDBG_FACILITY RPCDBG_TRANS |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 186 | #endif |
| 187 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 188 | #ifdef RPC_DEBUG_DATA |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 189 | static void xs_pktdump(char *msg, u32 *packet, unsigned int count) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 190 | { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 191 | u8 *buf = (u8 *) packet; |
| 192 | int j; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 193 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 194 | dprintk("RPC: %s\n", msg); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 195 | for (j = 0; j < count && j < 128; j += 4) { |
| 196 | if (!(j & 31)) { |
| 197 | if (j) |
| 198 | dprintk("\n"); |
| 199 | dprintk("0x%04x ", j); |
| 200 | } |
| 201 | dprintk("%02x%02x%02x%02x ", |
| 202 | buf[j], buf[j+1], buf[j+2], buf[j+3]); |
| 203 | } |
| 204 | dprintk("\n"); |
| 205 | } |
| 206 | #else |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 207 | static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 208 | { |
| 209 | /* NOP */ |
| 210 | } |
| 211 | #endif |
| 212 | |
Chuck Lever | ffc2e51 | 2006-12-05 16:35:11 -0500 | [diff] [blame] | 213 | struct sock_xprt { |
| 214 | struct rpc_xprt xprt; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 215 | |
| 216 | /* |
| 217 | * Network layer |
| 218 | */ |
| 219 | struct socket * sock; |
| 220 | struct sock * inet; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 221 | |
| 222 | /* |
| 223 | * State of TCP reply receive |
| 224 | */ |
| 225 | __be32 tcp_fraghdr, |
| 226 | tcp_xid; |
| 227 | |
| 228 | u32 tcp_offset, |
| 229 | tcp_reclen; |
| 230 | |
| 231 | unsigned long tcp_copied, |
| 232 | tcp_flags; |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 233 | |
| 234 | /* |
| 235 | * Connection of transports |
| 236 | */ |
Trond Myklebust | 34161db | 2006-12-07 15:48:15 -0500 | [diff] [blame] | 237 | struct delayed_work connect_worker; |
Frank van Maarseveen | d3bc9a1 | 2007-07-09 22:23:35 +0200 | [diff] [blame] | 238 | struct sockaddr_storage addr; |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 239 | unsigned short port; |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 240 | |
| 241 | /* |
| 242 | * UDP socket buffer size parameters |
| 243 | */ |
| 244 | size_t rcvsize, |
| 245 | sndsize; |
Chuck Lever | 314dfd7 | 2006-12-05 16:35:34 -0500 | [diff] [blame] | 246 | |
| 247 | /* |
| 248 | * Saved socket callback addresses |
| 249 | */ |
| 250 | void (*old_data_ready)(struct sock *, int); |
| 251 | void (*old_state_change)(struct sock *); |
| 252 | void (*old_write_space)(struct sock *); |
Chuck Lever | ffc2e51 | 2006-12-05 16:35:11 -0500 | [diff] [blame] | 253 | }; |
| 254 | |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 255 | /* |
| 256 | * TCP receive state flags |
| 257 | */ |
| 258 | #define TCP_RCV_LAST_FRAG (1UL << 0) |
| 259 | #define TCP_RCV_COPY_FRAGHDR (1UL << 1) |
| 260 | #define TCP_RCV_COPY_XID (1UL << 2) |
| 261 | #define TCP_RCV_COPY_DATA (1UL << 3) |
| 262 | |
Chuck Lever | ba10f2c | 2007-08-06 11:57:07 -0400 | [diff] [blame] | 263 | static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt) |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 264 | { |
| 265 | struct sockaddr_in *addr = (struct sockaddr_in *) &xprt->addr; |
| 266 | char *buf; |
| 267 | |
| 268 | buf = kzalloc(20, GFP_KERNEL); |
| 269 | if (buf) { |
Chuck Lever | bda243d | 2007-08-06 11:56:52 -0400 | [diff] [blame] | 270 | snprintf(buf, 20, NIPQUAD_FMT, |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 271 | NIPQUAD(addr->sin_addr.s_addr)); |
| 272 | } |
| 273 | xprt->address_strings[RPC_DISPLAY_ADDR] = buf; |
| 274 | |
| 275 | buf = kzalloc(8, GFP_KERNEL); |
| 276 | if (buf) { |
| 277 | snprintf(buf, 8, "%u", |
| 278 | ntohs(addr->sin_port)); |
| 279 | } |
| 280 | xprt->address_strings[RPC_DISPLAY_PORT] = buf; |
| 281 | |
Chuck Lever | 0c43b3d | 2007-08-06 11:56:57 -0400 | [diff] [blame] | 282 | buf = kzalloc(8, GFP_KERNEL); |
| 283 | if (buf) { |
| 284 | if (xprt->prot == IPPROTO_UDP) |
| 285 | snprintf(buf, 8, "udp"); |
| 286 | else |
| 287 | snprintf(buf, 8, "tcp"); |
| 288 | } |
| 289 | xprt->address_strings[RPC_DISPLAY_PROTO] = buf; |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 290 | |
| 291 | buf = kzalloc(48, GFP_KERNEL); |
| 292 | if (buf) { |
Chuck Lever | bda243d | 2007-08-06 11:56:52 -0400 | [diff] [blame] | 293 | snprintf(buf, 48, "addr="NIPQUAD_FMT" port=%u proto=%s", |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 294 | NIPQUAD(addr->sin_addr.s_addr), |
| 295 | ntohs(addr->sin_port), |
| 296 | xprt->prot == IPPROTO_UDP ? "udp" : "tcp"); |
| 297 | } |
| 298 | xprt->address_strings[RPC_DISPLAY_ALL] = buf; |
Chuck Lever | fbfe3cc | 2007-08-06 11:57:02 -0400 | [diff] [blame] | 299 | |
| 300 | buf = kzalloc(10, GFP_KERNEL); |
| 301 | if (buf) { |
| 302 | snprintf(buf, 10, "%02x%02x%02x%02x", |
| 303 | NIPQUAD(addr->sin_addr.s_addr)); |
| 304 | } |
| 305 | xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf; |
| 306 | |
| 307 | buf = kzalloc(8, GFP_KERNEL); |
| 308 | if (buf) { |
| 309 | snprintf(buf, 8, "%4hx", |
| 310 | ntohs(addr->sin_port)); |
| 311 | } |
| 312 | xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf; |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 313 | } |
| 314 | |
Chuck Lever | 4b6473f | 2007-08-06 11:57:12 -0400 | [diff] [blame] | 315 | static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt) |
| 316 | { |
| 317 | struct sockaddr_in6 *addr = (struct sockaddr_in6 *) &xprt->addr; |
| 318 | char *buf; |
| 319 | |
| 320 | buf = kzalloc(40, GFP_KERNEL); |
| 321 | if (buf) { |
| 322 | snprintf(buf, 40, NIP6_FMT, |
| 323 | NIP6(addr->sin6_addr)); |
| 324 | } |
| 325 | xprt->address_strings[RPC_DISPLAY_ADDR] = buf; |
| 326 | |
| 327 | buf = kzalloc(8, GFP_KERNEL); |
| 328 | if (buf) { |
| 329 | snprintf(buf, 8, "%u", |
| 330 | ntohs(addr->sin6_port)); |
| 331 | } |
| 332 | xprt->address_strings[RPC_DISPLAY_PORT] = buf; |
| 333 | |
| 334 | buf = kzalloc(8, GFP_KERNEL); |
| 335 | if (buf) { |
| 336 | if (xprt->prot == IPPROTO_UDP) |
| 337 | snprintf(buf, 8, "udp"); |
| 338 | else |
| 339 | snprintf(buf, 8, "tcp"); |
| 340 | } |
| 341 | xprt->address_strings[RPC_DISPLAY_PROTO] = buf; |
| 342 | |
| 343 | buf = kzalloc(64, GFP_KERNEL); |
| 344 | if (buf) { |
| 345 | snprintf(buf, 64, "addr="NIP6_FMT" port=%u proto=%s", |
| 346 | NIP6(addr->sin6_addr), |
| 347 | ntohs(addr->sin6_port), |
| 348 | xprt->prot == IPPROTO_UDP ? "udp" : "tcp"); |
| 349 | } |
| 350 | xprt->address_strings[RPC_DISPLAY_ALL] = buf; |
| 351 | |
| 352 | buf = kzalloc(36, GFP_KERNEL); |
| 353 | if (buf) { |
| 354 | snprintf(buf, 36, NIP6_SEQFMT, |
| 355 | NIP6(addr->sin6_addr)); |
| 356 | } |
| 357 | xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf; |
| 358 | |
| 359 | buf = kzalloc(8, GFP_KERNEL); |
| 360 | if (buf) { |
| 361 | snprintf(buf, 8, "%4hx", |
| 362 | ntohs(addr->sin6_port)); |
| 363 | } |
| 364 | xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf; |
| 365 | } |
| 366 | |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 367 | static void xs_free_peer_addresses(struct rpc_xprt *xprt) |
| 368 | { |
Chuck Lever | 0c43b3d | 2007-08-06 11:56:57 -0400 | [diff] [blame] | 369 | int i; |
| 370 | |
| 371 | for (i = 0; i < RPC_DISPLAY_MAX; i++) |
| 372 | kfree(xprt->address_strings[i]); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 373 | } |
| 374 | |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 375 | #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) |
| 376 | |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 377 | 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] | 378 | { |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 379 | struct msghdr msg = { |
| 380 | .msg_name = addr, |
| 381 | .msg_namelen = addrlen, |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 382 | .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0), |
| 383 | }; |
| 384 | struct kvec iov = { |
| 385 | .iov_base = vec->iov_base + base, |
| 386 | .iov_len = vec->iov_len - base, |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 387 | }; |
| 388 | |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 389 | if (iov.iov_len != 0) |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 390 | return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); |
| 391 | return kernel_sendmsg(sock, &msg, NULL, 0, 0); |
| 392 | } |
| 393 | |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 394 | 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] | 395 | { |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 396 | struct page **ppage; |
| 397 | unsigned int remainder; |
| 398 | int err, sent = 0; |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 399 | |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 400 | remainder = xdr->page_len - base; |
| 401 | base += xdr->page_base; |
| 402 | ppage = xdr->pages + (base >> PAGE_SHIFT); |
| 403 | base &= ~PAGE_MASK; |
| 404 | for(;;) { |
| 405 | unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder); |
| 406 | int flags = XS_SENDMSG_FLAGS; |
| 407 | |
| 408 | remainder -= len; |
| 409 | if (remainder != 0 || more) |
| 410 | flags |= MSG_MORE; |
| 411 | err = sock->ops->sendpage(sock, *ppage, base, len, flags); |
| 412 | if (remainder == 0 || err != len) |
| 413 | break; |
| 414 | sent += err; |
| 415 | ppage++; |
| 416 | base = 0; |
| 417 | } |
| 418 | if (sent == 0) |
| 419 | return err; |
| 420 | if (err > 0) |
| 421 | sent += err; |
| 422 | return sent; |
Chuck Lever | b4b5cc8 | 2005-08-11 16:25:29 -0400 | [diff] [blame] | 423 | } |
| 424 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 425 | /** |
| 426 | * xs_sendpages - write pages directly to a socket |
| 427 | * @sock: socket to send on |
| 428 | * @addr: UDP only -- address of destination |
| 429 | * @addrlen: UDP only -- length of destination address |
| 430 | * @xdr: buffer containing this request |
| 431 | * @base: starting position in the buffer |
| 432 | * |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 433 | */ |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 434 | 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] | 435 | { |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 436 | unsigned int remainder = xdr->len - base; |
| 437 | int err, sent = 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 438 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 439 | if (unlikely(!sock)) |
| 440 | return -ENOTCONN; |
| 441 | |
| 442 | clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags); |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 443 | if (base != 0) { |
| 444 | addr = NULL; |
| 445 | addrlen = 0; |
| 446 | } |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 447 | |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 448 | if (base < xdr->head[0].iov_len || addr != NULL) { |
| 449 | unsigned int len = xdr->head[0].iov_len - base; |
| 450 | remainder -= len; |
| 451 | err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0); |
| 452 | if (remainder == 0 || err != len) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 453 | goto out; |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 454 | sent += err; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 455 | base = 0; |
| 456 | } else |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 457 | base -= xdr->head[0].iov_len; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 458 | |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 459 | if (base < xdr->page_len) { |
| 460 | unsigned int len = xdr->page_len - base; |
| 461 | remainder -= len; |
| 462 | err = xs_send_pagedata(sock, xdr, base, remainder != 0); |
| 463 | if (remainder == 0 || err != len) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 464 | goto out; |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 465 | sent += err; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 466 | base = 0; |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 467 | } else |
| 468 | base -= xdr->page_len; |
| 469 | |
| 470 | if (base >= xdr->tail[0].iov_len) |
| 471 | return sent; |
| 472 | err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 473 | out: |
Trond Myklebust | 24c5684 | 2006-10-17 15:06:22 -0400 | [diff] [blame] | 474 | if (sent == 0) |
| 475 | return err; |
| 476 | if (err > 0) |
| 477 | sent += err; |
| 478 | return sent; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 479 | } |
| 480 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 481 | /** |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 482 | * xs_nospace - place task on wait queue if transmit was incomplete |
| 483 | * @task: task to put to sleep |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 484 | * |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 485 | */ |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 486 | static void xs_nospace(struct rpc_task *task) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 487 | { |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 488 | struct rpc_rqst *req = task->tk_rqstp; |
| 489 | struct rpc_xprt *xprt = req->rq_xprt; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 490 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 491 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 492 | dprintk("RPC: %5u xmit incomplete (%u left of %u)\n", |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 493 | task->tk_pid, req->rq_slen - req->rq_bytes_sent, |
| 494 | req->rq_slen); |
| 495 | |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 496 | if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) { |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 497 | /* Protect against races with write_space */ |
| 498 | spin_lock_bh(&xprt->transport_lock); |
| 499 | |
| 500 | /* Don't race with disconnect */ |
| 501 | if (!xprt_connected(xprt)) |
| 502 | task->tk_status = -ENOTCONN; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 503 | else if (test_bit(SOCK_NOSPACE, &transport->sock->flags)) |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 504 | xprt_wait_for_buffer_space(task); |
| 505 | |
| 506 | spin_unlock_bh(&xprt->transport_lock); |
| 507 | } else |
| 508 | /* Keep holding the socket if it is blocked */ |
| 509 | rpc_delay(task, HZ>>4); |
| 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 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 535 | req->rq_xtime = jiffies; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 536 | status = xs_sendpages(transport->sock, |
| 537 | (struct sockaddr *) &xprt->addr, |
| 538 | xprt->addrlen, xdr, |
| 539 | req->rq_bytes_sent); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 540 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 541 | dprintk("RPC: xs_udp_send_request(%u) = %d\n", |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 542 | xdr->len - req->rq_bytes_sent, status); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 543 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 544 | if (likely(status >= (int) req->rq_slen)) |
| 545 | return 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 546 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 547 | /* Still some bytes left; set up for a retry later. */ |
| 548 | if (status > 0) |
| 549 | status = -EAGAIN; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 550 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 551 | switch (status) { |
| 552 | case -ENETUNREACH: |
| 553 | case -EPIPE: |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 554 | case -ECONNREFUSED: |
| 555 | /* When the server has died, an ICMP port unreachable message |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 556 | * prompts ECONNREFUSED. */ |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 557 | break; |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 558 | case -EAGAIN: |
| 559 | xs_nospace(task); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 560 | break; |
| 561 | default: |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 562 | dprintk("RPC: sendmsg returned unrecognized error %d\n", |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 563 | -status); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 564 | break; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 565 | } |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 566 | |
| 567 | return status; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 568 | } |
| 569 | |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 570 | static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf) |
| 571 | { |
| 572 | u32 reclen = buf->len - sizeof(rpc_fraghdr); |
| 573 | rpc_fraghdr *base = buf->head[0].iov_base; |
| 574 | *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen); |
| 575 | } |
| 576 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 577 | /** |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 578 | * xs_tcp_send_request - write an RPC request to a TCP socket |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 579 | * @task: address of RPC task that manages the state of an RPC request |
| 580 | * |
| 581 | * Return values: |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 582 | * 0: The request has been sent |
| 583 | * EAGAIN: The socket was blocked, please call again later to |
| 584 | * complete the request |
| 585 | * ENOTCONN: Caller needs to invoke connect logic then call again |
| 586 | * other: Some other error occured, the request was not sent |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 587 | * |
| 588 | * XXX: In the case of soft timeouts, should we eventually give up |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 589 | * if sendmsg is not able to make progress? |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 590 | */ |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 591 | static int xs_tcp_send_request(struct rpc_task *task) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 592 | { |
| 593 | struct rpc_rqst *req = task->tk_rqstp; |
| 594 | struct rpc_xprt *xprt = req->rq_xprt; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 595 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 596 | struct xdr_buf *xdr = &req->rq_snd_buf; |
Chuck Lever | b595bb1 | 2007-08-06 11:56:42 -0400 | [diff] [blame] | 597 | int status; |
| 598 | unsigned int retry = 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 599 | |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 600 | xs_encode_tcp_record_marker(&req->rq_snd_buf); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 601 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 602 | xs_pktdump("packet data:", |
| 603 | req->rq_svec->iov_base, |
| 604 | req->rq_svec->iov_len); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 605 | |
| 606 | /* Continue transmitting the packet/record. We must be careful |
| 607 | * to cope with writespace callbacks arriving _after_ we have |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 608 | * called sendmsg(). */ |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 609 | while (1) { |
| 610 | req->rq_xtime = jiffies; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 611 | status = xs_sendpages(transport->sock, |
| 612 | NULL, 0, xdr, req->rq_bytes_sent); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 613 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 614 | dprintk("RPC: xs_tcp_send_request(%u) = %d\n", |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 615 | xdr->len - req->rq_bytes_sent, status); |
| 616 | |
| 617 | if (unlikely(status < 0)) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 618 | break; |
| 619 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 620 | /* If we've sent the entire packet, immediately |
| 621 | * reset the count of bytes sent. */ |
| 622 | req->rq_bytes_sent += status; |
Chuck Lever | ef759a2 | 2006-03-20 13:44:17 -0500 | [diff] [blame] | 623 | task->tk_bytes_sent += status; |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 624 | if (likely(req->rq_bytes_sent >= req->rq_slen)) { |
| 625 | req->rq_bytes_sent = 0; |
| 626 | return 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 627 | } |
| 628 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 629 | status = -EAGAIN; |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 630 | if (retry++ > XS_SENDMSG_RETRY) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 631 | break; |
| 632 | } |
| 633 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 634 | switch (status) { |
| 635 | case -EAGAIN: |
| 636 | xs_nospace(task); |
| 637 | break; |
| 638 | case -ECONNREFUSED: |
| 639 | case -ECONNRESET: |
| 640 | case -ENOTCONN: |
| 641 | case -EPIPE: |
| 642 | status = -ENOTCONN; |
| 643 | break; |
| 644 | default: |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 645 | dprintk("RPC: sendmsg returned unrecognized error %d\n", |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 646 | -status); |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 647 | xprt_disconnect(xprt); |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 648 | break; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 649 | } |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 650 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 651 | return status; |
| 652 | } |
| 653 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 654 | /** |
Trond Myklebust | e0ab53d | 2006-07-27 17:22:50 -0400 | [diff] [blame] | 655 | * xs_tcp_release_xprt - clean up after a tcp transmission |
| 656 | * @xprt: transport |
| 657 | * @task: rpc task |
| 658 | * |
| 659 | * This cleans up if an error causes us to abort the transmission of a request. |
| 660 | * In this case, the socket may need to be reset in order to avoid confusing |
| 661 | * the server. |
| 662 | */ |
| 663 | static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) |
| 664 | { |
| 665 | struct rpc_rqst *req; |
| 666 | |
| 667 | if (task != xprt->snd_task) |
| 668 | return; |
| 669 | if (task == NULL) |
| 670 | goto out_release; |
| 671 | req = task->tk_rqstp; |
| 672 | if (req->rq_bytes_sent == 0) |
| 673 | goto out_release; |
| 674 | if (req->rq_bytes_sent == req->rq_snd_buf.len) |
| 675 | goto out_release; |
| 676 | set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state); |
| 677 | out_release: |
| 678 | xprt_release_xprt(xprt, task); |
| 679 | } |
| 680 | |
| 681 | /** |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 682 | * xs_close - close a socket |
| 683 | * @xprt: transport |
| 684 | * |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 685 | * This is used when all requests are complete; ie, no DRC state remains |
| 686 | * on the server we want to save. |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 687 | */ |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 688 | static void xs_close(struct rpc_xprt *xprt) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 689 | { |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 690 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 691 | struct socket *sock = transport->sock; |
| 692 | struct sock *sk = transport->inet; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 693 | |
| 694 | if (!sk) |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 695 | goto clear_close_wait; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 696 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 697 | dprintk("RPC: xs_close xprt %p\n", xprt); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 698 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 699 | write_lock_bh(&sk->sk_callback_lock); |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 700 | transport->inet = NULL; |
| 701 | transport->sock = NULL; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 702 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 703 | sk->sk_user_data = NULL; |
Chuck Lever | 314dfd7 | 2006-12-05 16:35:34 -0500 | [diff] [blame] | 704 | sk->sk_data_ready = transport->old_data_ready; |
| 705 | sk->sk_state_change = transport->old_state_change; |
| 706 | sk->sk_write_space = transport->old_write_space; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 707 | write_unlock_bh(&sk->sk_callback_lock); |
| 708 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 709 | sk->sk_no_check = 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 710 | |
| 711 | sock_release(sock); |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 712 | clear_close_wait: |
| 713 | smp_mb__before_clear_bit(); |
| 714 | clear_bit(XPRT_CLOSE_WAIT, &xprt->state); |
| 715 | smp_mb__after_clear_bit(); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 716 | } |
| 717 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 718 | /** |
| 719 | * xs_destroy - prepare to shutdown a transport |
| 720 | * @xprt: doomed transport |
| 721 | * |
| 722 | */ |
| 723 | static void xs_destroy(struct rpc_xprt *xprt) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 724 | { |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 725 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 726 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 727 | dprintk("RPC: xs_destroy xprt %p\n", xprt); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 728 | |
Trond Myklebust | c1384c9 | 2007-06-14 18:00:42 -0400 | [diff] [blame] | 729 | cancel_rearming_delayed_work(&transport->connect_worker); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 730 | |
| 731 | xprt_disconnect(xprt); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 732 | xs_close(xprt); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 733 | xs_free_peer_addresses(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 734 | kfree(xprt->slot); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 735 | kfree(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 736 | } |
| 737 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 738 | static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 739 | { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 740 | return (struct rpc_xprt *) sk->sk_user_data; |
| 741 | } |
| 742 | |
| 743 | /** |
| 744 | * xs_udp_data_ready - "data ready" callback for UDP sockets |
| 745 | * @sk: socket with data to read |
| 746 | * @len: how much data to read |
| 747 | * |
| 748 | */ |
| 749 | static void xs_udp_data_ready(struct sock *sk, int len) |
| 750 | { |
| 751 | struct rpc_task *task; |
| 752 | struct rpc_xprt *xprt; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 753 | struct rpc_rqst *rovr; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 754 | struct sk_buff *skb; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 755 | int err, repsize, copied; |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 756 | u32 _xid; |
| 757 | __be32 *xp; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 758 | |
| 759 | read_lock(&sk->sk_callback_lock); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 760 | dprintk("RPC: xs_udp_data_ready...\n"); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 761 | if (!(xprt = xprt_from_sock(sk))) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 762 | goto out; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 763 | |
| 764 | if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) |
| 765 | goto out; |
| 766 | |
| 767 | if (xprt->shutdown) |
| 768 | goto dropit; |
| 769 | |
| 770 | repsize = skb->len - sizeof(struct udphdr); |
| 771 | if (repsize < 4) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 772 | dprintk("RPC: impossible RPC reply size %d!\n", repsize); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 773 | goto dropit; |
| 774 | } |
| 775 | |
| 776 | /* Copy the XID from the skb... */ |
| 777 | xp = skb_header_pointer(skb, sizeof(struct udphdr), |
| 778 | sizeof(_xid), &_xid); |
| 779 | if (xp == NULL) |
| 780 | goto dropit; |
| 781 | |
| 782 | /* Look up and lock the request corresponding to the given XID */ |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 783 | spin_lock(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 784 | rovr = xprt_lookup_rqst(xprt, *xp); |
| 785 | if (!rovr) |
| 786 | goto out_unlock; |
| 787 | task = rovr->rq_task; |
| 788 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 789 | if ((copied = rovr->rq_private_buf.buflen) > repsize) |
| 790 | copied = repsize; |
| 791 | |
| 792 | /* Suck it into the iovec, verify checksum if not done by hw. */ |
| 793 | if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) |
| 794 | goto out_unlock; |
| 795 | |
| 796 | /* Something worked... */ |
| 797 | dst_confirm(skb->dst); |
| 798 | |
Chuck Lever | 1570c1e | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 799 | xprt_adjust_cwnd(task, copied); |
| 800 | xprt_update_rtt(task); |
| 801 | xprt_complete_rqst(task, copied); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 802 | |
| 803 | out_unlock: |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 804 | spin_unlock(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 805 | dropit: |
| 806 | skb_free_datagram(sk, skb); |
| 807 | out: |
| 808 | read_unlock(&sk->sk_callback_lock); |
| 809 | } |
| 810 | |
Chuck Lever | dd45647 | 2006-12-05 16:35:44 -0500 | [diff] [blame] | 811 | 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] | 812 | { |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 813 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 814 | size_t len, used; |
| 815 | char *p; |
| 816 | |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 817 | p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset; |
| 818 | len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset; |
Chuck Lever | 9d29231 | 2006-12-05 16:35:41 -0500 | [diff] [blame] | 819 | used = xdr_skb_read_bits(desc, p, len); |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 820 | transport->tcp_offset += used; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 821 | if (used != len) |
| 822 | return; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 823 | |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 824 | transport->tcp_reclen = ntohl(transport->tcp_fraghdr); |
| 825 | if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT) |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 826 | transport->tcp_flags |= TCP_RCV_LAST_FRAG; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 827 | else |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 828 | transport->tcp_flags &= ~TCP_RCV_LAST_FRAG; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 829 | transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 830 | |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 831 | transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 832 | transport->tcp_offset = 0; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 833 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 834 | /* Sanity check of the record length */ |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 835 | if (unlikely(transport->tcp_reclen < 4)) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 836 | dprintk("RPC: invalid TCP record fragment length\n"); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 837 | xprt_disconnect(xprt); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 838 | return; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 839 | } |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 840 | dprintk("RPC: reading TCP record fragment of length %d\n", |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 841 | transport->tcp_reclen); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 842 | } |
| 843 | |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 844 | static void xs_tcp_check_fraghdr(struct sock_xprt *transport) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 845 | { |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 846 | if (transport->tcp_offset == transport->tcp_reclen) { |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 847 | transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 848 | transport->tcp_offset = 0; |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 849 | if (transport->tcp_flags & TCP_RCV_LAST_FRAG) { |
| 850 | transport->tcp_flags &= ~TCP_RCV_COPY_DATA; |
| 851 | transport->tcp_flags |= TCP_RCV_COPY_XID; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 852 | transport->tcp_copied = 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | } |
| 856 | |
Chuck Lever | dd45647 | 2006-12-05 16:35:44 -0500 | [diff] [blame] | 857 | 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] | 858 | { |
| 859 | size_t len, used; |
| 860 | char *p; |
| 861 | |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 862 | len = sizeof(transport->tcp_xid) - transport->tcp_offset; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 863 | dprintk("RPC: reading XID (%Zu bytes)\n", len); |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 864 | p = ((char *) &transport->tcp_xid) + transport->tcp_offset; |
Chuck Lever | 9d29231 | 2006-12-05 16:35:41 -0500 | [diff] [blame] | 865 | used = xdr_skb_read_bits(desc, p, len); |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 866 | transport->tcp_offset += used; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 867 | if (used != len) |
| 868 | return; |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 869 | transport->tcp_flags &= ~TCP_RCV_COPY_XID; |
| 870 | transport->tcp_flags |= TCP_RCV_COPY_DATA; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 871 | transport->tcp_copied = 4; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 872 | dprintk("RPC: reading reply for XID %08x\n", |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 873 | ntohl(transport->tcp_xid)); |
| 874 | xs_tcp_check_fraghdr(transport); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 875 | } |
| 876 | |
Chuck Lever | dd45647 | 2006-12-05 16:35:44 -0500 | [diff] [blame] | 877 | static inline void xs_tcp_read_request(struct rpc_xprt *xprt, struct xdr_skb_reader *desc) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 878 | { |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 879 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 880 | struct rpc_rqst *req; |
| 881 | struct xdr_buf *rcvbuf; |
| 882 | size_t len; |
| 883 | ssize_t r; |
| 884 | |
| 885 | /* Find and lock the request corresponding to this xid */ |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 886 | spin_lock(&xprt->transport_lock); |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 887 | req = xprt_lookup_rqst(xprt, transport->tcp_xid); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 888 | if (!req) { |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 889 | transport->tcp_flags &= ~TCP_RCV_COPY_DATA; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 890 | dprintk("RPC: XID %08x request not found!\n", |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 891 | ntohl(transport->tcp_xid)); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 892 | spin_unlock(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 893 | return; |
| 894 | } |
| 895 | |
| 896 | rcvbuf = &req->rq_private_buf; |
| 897 | len = desc->count; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 898 | if (len > transport->tcp_reclen - transport->tcp_offset) { |
Chuck Lever | dd45647 | 2006-12-05 16:35:44 -0500 | [diff] [blame] | 899 | struct xdr_skb_reader my_desc; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 900 | |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 901 | len = transport->tcp_reclen - transport->tcp_offset; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 902 | memcpy(&my_desc, desc, sizeof(my_desc)); |
| 903 | my_desc.count = len; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 904 | r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, |
Chuck Lever | 9d29231 | 2006-12-05 16:35:41 -0500 | [diff] [blame] | 905 | &my_desc, xdr_skb_read_bits); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 906 | desc->count -= r; |
| 907 | desc->offset += r; |
| 908 | } else |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 909 | r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied, |
Chuck Lever | 9d29231 | 2006-12-05 16:35:41 -0500 | [diff] [blame] | 910 | desc, xdr_skb_read_bits); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 911 | |
| 912 | if (r > 0) { |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 913 | transport->tcp_copied += r; |
| 914 | transport->tcp_offset += r; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 915 | } |
| 916 | if (r != len) { |
| 917 | /* Error when copying to the receive buffer, |
| 918 | * usually because we weren't able to allocate |
| 919 | * additional buffer pages. All we can do now |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 920 | * is turn off TCP_RCV_COPY_DATA, so the request |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 921 | * will not receive any additional updates, |
| 922 | * and time out. |
| 923 | * Any remaining data from this record will |
| 924 | * be discarded. |
| 925 | */ |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 926 | transport->tcp_flags &= ~TCP_RCV_COPY_DATA; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 927 | dprintk("RPC: XID %08x truncated request\n", |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 928 | ntohl(transport->tcp_xid)); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 929 | dprintk("RPC: xprt = %p, tcp_copied = %lu, " |
| 930 | "tcp_offset = %u, tcp_reclen = %u\n", |
| 931 | xprt, transport->tcp_copied, |
| 932 | transport->tcp_offset, transport->tcp_reclen); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 933 | goto out; |
| 934 | } |
| 935 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 936 | dprintk("RPC: XID %08x read %Zd bytes\n", |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 937 | ntohl(transport->tcp_xid), r); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 938 | dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, " |
| 939 | "tcp_reclen = %u\n", xprt, transport->tcp_copied, |
| 940 | transport->tcp_offset, transport->tcp_reclen); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 941 | |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 942 | if (transport->tcp_copied == req->rq_private_buf.buflen) |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 943 | transport->tcp_flags &= ~TCP_RCV_COPY_DATA; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 944 | else if (transport->tcp_offset == transport->tcp_reclen) { |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 945 | if (transport->tcp_flags & TCP_RCV_LAST_FRAG) |
| 946 | transport->tcp_flags &= ~TCP_RCV_COPY_DATA; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | out: |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 950 | if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 951 | xprt_complete_rqst(req->rq_task, transport->tcp_copied); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 952 | spin_unlock(&xprt->transport_lock); |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 953 | xs_tcp_check_fraghdr(transport); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 954 | } |
| 955 | |
Chuck Lever | dd45647 | 2006-12-05 16:35:44 -0500 | [diff] [blame] | 956 | 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] | 957 | { |
| 958 | size_t len; |
| 959 | |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 960 | len = transport->tcp_reclen - transport->tcp_offset; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 961 | if (len > desc->count) |
| 962 | len = desc->count; |
| 963 | desc->count -= len; |
| 964 | desc->offset += len; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 965 | transport->tcp_offset += len; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 966 | dprintk("RPC: discarded %Zu bytes\n", len); |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 967 | xs_tcp_check_fraghdr(transport); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 968 | } |
| 969 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 970 | 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] | 971 | { |
| 972 | struct rpc_xprt *xprt = rd_desc->arg.data; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 973 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | dd45647 | 2006-12-05 16:35:44 -0500 | [diff] [blame] | 974 | struct xdr_skb_reader desc = { |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 975 | .skb = skb, |
| 976 | .offset = offset, |
| 977 | .count = len, |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 978 | }; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 979 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 980 | dprintk("RPC: xs_tcp_data_recv started\n"); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 981 | do { |
| 982 | /* Read in a new fragment marker if necessary */ |
| 983 | /* Can we ever really expect to get completely empty fragments? */ |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 984 | if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 985 | xs_tcp_read_fraghdr(xprt, &desc); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 986 | continue; |
| 987 | } |
| 988 | /* Read in the xid if necessary */ |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 989 | if (transport->tcp_flags & TCP_RCV_COPY_XID) { |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 990 | xs_tcp_read_xid(transport, &desc); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 991 | continue; |
| 992 | } |
| 993 | /* Read in the request data */ |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 994 | if (transport->tcp_flags & TCP_RCV_COPY_DATA) { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 995 | xs_tcp_read_request(xprt, &desc); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 996 | continue; |
| 997 | } |
| 998 | /* Skip over any trailing bytes on short reads */ |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 999 | xs_tcp_read_discard(transport, &desc); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1000 | } while (desc.count); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1001 | dprintk("RPC: xs_tcp_data_recv done\n"); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1002 | return len - desc.count; |
| 1003 | } |
| 1004 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1005 | /** |
| 1006 | * xs_tcp_data_ready - "data ready" callback for TCP sockets |
| 1007 | * @sk: socket with data to read |
| 1008 | * @bytes: how much data to read |
| 1009 | * |
| 1010 | */ |
| 1011 | static void xs_tcp_data_ready(struct sock *sk, int bytes) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1012 | { |
| 1013 | struct rpc_xprt *xprt; |
| 1014 | read_descriptor_t rd_desc; |
| 1015 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1016 | dprintk("RPC: xs_tcp_data_ready...\n"); |
| 1017 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1018 | read_lock(&sk->sk_callback_lock); |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1019 | if (!(xprt = xprt_from_sock(sk))) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1020 | goto out; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1021 | if (xprt->shutdown) |
| 1022 | goto out; |
| 1023 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1024 | /* 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] | 1025 | rd_desc.arg.data = xprt; |
| 1026 | rd_desc.count = 65536; |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1027 | tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1028 | out: |
| 1029 | read_unlock(&sk->sk_callback_lock); |
| 1030 | } |
| 1031 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1032 | /** |
| 1033 | * xs_tcp_state_change - callback to handle TCP socket state changes |
| 1034 | * @sk: socket whose state has changed |
| 1035 | * |
| 1036 | */ |
| 1037 | static void xs_tcp_state_change(struct sock *sk) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1038 | { |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1039 | struct rpc_xprt *xprt; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1040 | |
| 1041 | read_lock(&sk->sk_callback_lock); |
| 1042 | if (!(xprt = xprt_from_sock(sk))) |
| 1043 | goto out; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1044 | dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); |
| 1045 | dprintk("RPC: state %x conn %d dead %d zapped %d\n", |
| 1046 | sk->sk_state, xprt_connected(xprt), |
| 1047 | sock_flag(sk, SOCK_DEAD), |
| 1048 | sock_flag(sk, SOCK_ZAPPED)); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1049 | |
| 1050 | switch (sk->sk_state) { |
| 1051 | case TCP_ESTABLISHED: |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 1052 | spin_lock_bh(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1053 | if (!xprt_test_and_set_connected(xprt)) { |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1054 | struct sock_xprt *transport = container_of(xprt, |
| 1055 | struct sock_xprt, xprt); |
| 1056 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1057 | /* Reset TCP record info */ |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1058 | transport->tcp_offset = 0; |
| 1059 | transport->tcp_reclen = 0; |
| 1060 | transport->tcp_copied = 0; |
Chuck Lever | e136d09 | 2006-12-05 16:35:23 -0500 | [diff] [blame] | 1061 | transport->tcp_flags = |
| 1062 | TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID; |
Chuck Lever | 5197113 | 2006-12-05 16:35:19 -0500 | [diff] [blame] | 1063 | |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1064 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; |
Chuck Lever | 44fbac2 | 2005-08-11 16:25:44 -0400 | [diff] [blame] | 1065 | xprt_wake_pending_tasks(xprt, 0); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1066 | } |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 1067 | spin_unlock_bh(&xprt->transport_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1068 | break; |
| 1069 | case TCP_SYN_SENT: |
| 1070 | case TCP_SYN_RECV: |
| 1071 | break; |
Trond Myklebust | 632e3bd | 2006-01-03 09:55:55 +0100 | [diff] [blame] | 1072 | case TCP_CLOSE_WAIT: |
| 1073 | /* Try to schedule an autoclose RPC calls */ |
| 1074 | set_bit(XPRT_CLOSE_WAIT, &xprt->state); |
| 1075 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) |
Trond Myklebust | c1384c9 | 2007-06-14 18:00:42 -0400 | [diff] [blame] | 1076 | queue_work(rpciod_workqueue, &xprt->task_cleanup); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1077 | default: |
| 1078 | xprt_disconnect(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1079 | } |
| 1080 | out: |
| 1081 | read_unlock(&sk->sk_callback_lock); |
| 1082 | } |
| 1083 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1084 | /** |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1085 | * xs_udp_write_space - callback invoked when socket buffer space |
| 1086 | * becomes available |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1087 | * @sk: socket whose state has changed |
| 1088 | * |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1089 | * Called when more output buffer space is available for this socket. |
| 1090 | * We try not to wake our writers until they can make "significant" |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1091 | * progress, otherwise we'll waste resources thrashing kernel_sendmsg |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1092 | * with a bunch of small requests. |
| 1093 | */ |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1094 | static void xs_udp_write_space(struct sock *sk) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1095 | { |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1096 | read_lock(&sk->sk_callback_lock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1097 | |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1098 | /* from net/core/sock.c:sock_def_write_space */ |
| 1099 | if (sock_writeable(sk)) { |
| 1100 | struct socket *sock; |
| 1101 | struct rpc_xprt *xprt; |
| 1102 | |
| 1103 | if (unlikely(!(sock = sk->sk_socket))) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1104 | goto out; |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1105 | if (unlikely(!(xprt = xprt_from_sock(sk)))) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1106 | goto out; |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1107 | if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags))) |
| 1108 | goto out; |
| 1109 | |
| 1110 | xprt_write_space(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1111 | } |
| 1112 | |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1113 | out: |
| 1114 | read_unlock(&sk->sk_callback_lock); |
| 1115 | } |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1116 | |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 1117 | /** |
| 1118 | * xs_tcp_write_space - callback invoked when socket buffer space |
| 1119 | * becomes available |
| 1120 | * @sk: socket whose state has changed |
| 1121 | * |
| 1122 | * Called when more output buffer space is available for this socket. |
| 1123 | * We try not to wake our writers until they can make "significant" |
| 1124 | * progress, otherwise we'll waste resources thrashing kernel_sendmsg |
| 1125 | * with a bunch of small requests. |
| 1126 | */ |
| 1127 | static void xs_tcp_write_space(struct sock *sk) |
| 1128 | { |
| 1129 | read_lock(&sk->sk_callback_lock); |
| 1130 | |
| 1131 | /* from net/core/stream.c:sk_stream_write_space */ |
| 1132 | if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { |
| 1133 | struct socket *sock; |
| 1134 | struct rpc_xprt *xprt; |
| 1135 | |
| 1136 | if (unlikely(!(sock = sk->sk_socket))) |
| 1137 | goto out; |
| 1138 | if (unlikely(!(xprt = xprt_from_sock(sk)))) |
| 1139 | goto out; |
| 1140 | if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags))) |
| 1141 | goto out; |
| 1142 | |
| 1143 | xprt_write_space(xprt); |
| 1144 | } |
| 1145 | |
| 1146 | out: |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1147 | read_unlock(&sk->sk_callback_lock); |
| 1148 | } |
| 1149 | |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1150 | static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1151 | { |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 1152 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 1153 | struct sock *sk = transport->inet; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1154 | |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1155 | if (transport->rcvsize) { |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1156 | sk->sk_userlocks |= SOCK_RCVBUF_LOCK; |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1157 | sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1158 | } |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1159 | if (transport->sndsize) { |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1160 | sk->sk_userlocks |= SOCK_SNDBUF_LOCK; |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1161 | sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1162 | sk->sk_write_space(sk); |
| 1163 | } |
| 1164 | } |
| 1165 | |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1166 | /** |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1167 | * xs_udp_set_buffer_size - set send and receive limits |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1168 | * @xprt: generic transport |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1169 | * @sndsize: requested size of send buffer, in bytes |
| 1170 | * @rcvsize: requested size of receive buffer, in bytes |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1171 | * |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1172 | * Set socket send and receive buffer size limits. |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1173 | */ |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1174 | 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] | 1175 | { |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1176 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 1177 | |
| 1178 | transport->sndsize = 0; |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1179 | if (sndsize) |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1180 | transport->sndsize = sndsize + 1024; |
| 1181 | transport->rcvsize = 0; |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1182 | if (rcvsize) |
Chuck Lever | 7c6e066 | 2006-12-05 16:35:30 -0500 | [diff] [blame] | 1183 | transport->rcvsize = rcvsize + 1024; |
Chuck Lever | 470056c | 2005-08-25 16:25:56 -0700 | [diff] [blame] | 1184 | |
| 1185 | xs_udp_do_set_buffer_size(xprt); |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 1188 | /** |
| 1189 | * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport |
| 1190 | * @task: task that timed out |
| 1191 | * |
| 1192 | * Adjust the congestion window after a retransmit timeout has occurred. |
| 1193 | */ |
| 1194 | static void xs_udp_timer(struct rpc_task *task) |
| 1195 | { |
| 1196 | xprt_adjust_cwnd(task, -ETIMEDOUT); |
| 1197 | } |
| 1198 | |
Chuck Lever | b85d880 | 2006-05-25 01:40:49 -0400 | [diff] [blame] | 1199 | static unsigned short xs_get_random_port(void) |
| 1200 | { |
| 1201 | unsigned short range = xprt_max_resvport - xprt_min_resvport; |
| 1202 | unsigned short rand = (unsigned short) net_random() % range; |
| 1203 | return rand + xprt_min_resvport; |
| 1204 | } |
| 1205 | |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame] | 1206 | /** |
| 1207 | * xs_set_port - reset the port number in the remote endpoint address |
| 1208 | * @xprt: generic transport |
| 1209 | * @port: new port number |
| 1210 | * |
| 1211 | */ |
| 1212 | static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) |
| 1213 | { |
Chuck Lever | 2061200 | 2007-08-06 11:57:23 -0400 | [diff] [blame] | 1214 | struct sockaddr *addr = (struct sockaddr *) &xprt->addr; |
Chuck Lever | c4efcb1 | 2006-08-22 20:06:19 -0400 | [diff] [blame] | 1215 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1216 | dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); |
Chuck Lever | c4efcb1 | 2006-08-22 20:06:19 -0400 | [diff] [blame] | 1217 | |
Chuck Lever | 2061200 | 2007-08-06 11:57:23 -0400 | [diff] [blame] | 1218 | switch (addr->sa_family) { |
| 1219 | case AF_INET: |
| 1220 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 1221 | break; |
| 1222 | case AF_INET6: |
| 1223 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
| 1224 | break; |
| 1225 | default: |
| 1226 | BUG(); |
| 1227 | } |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame] | 1228 | } |
| 1229 | |
Chuck Lever | 7dc753f | 2007-08-06 11:57:28 -0400 | [diff] [blame] | 1230 | static int xs_bind4(struct sock_xprt *transport, struct socket *sock) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1231 | { |
| 1232 | struct sockaddr_in myaddr = { |
| 1233 | .sin_family = AF_INET, |
| 1234 | }; |
Frank van Maarseveen | d3bc9a1 | 2007-07-09 22:23:35 +0200 | [diff] [blame] | 1235 | struct sockaddr_in *sa; |
Chuck Lever | 529b33c | 2005-08-25 16:25:54 -0700 | [diff] [blame] | 1236 | int err; |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 1237 | unsigned short port = transport->port; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1238 | |
Frank van Maarseveen | d3bc9a1 | 2007-07-09 22:23:35 +0200 | [diff] [blame] | 1239 | if (!transport->xprt.resvport) |
| 1240 | port = 0; |
| 1241 | sa = (struct sockaddr_in *)&transport->addr; |
| 1242 | myaddr.sin_addr = sa->sin_addr; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1243 | do { |
| 1244 | myaddr.sin_port = htons(port); |
Sridhar Samudrala | e6242e9 | 2006-08-07 20:58:01 -0700 | [diff] [blame] | 1245 | err = kernel_bind(sock, (struct sockaddr *) &myaddr, |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1246 | sizeof(myaddr)); |
Frank van Maarseveen | d3bc9a1 | 2007-07-09 22:23:35 +0200 | [diff] [blame] | 1247 | if (!transport->xprt.resvport) |
| 1248 | break; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1249 | if (err == 0) { |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 1250 | transport->port = port; |
Frank van Maarseveen | d3bc9a1 | 2007-07-09 22:23:35 +0200 | [diff] [blame] | 1251 | break; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1252 | } |
Chuck Lever | 529b33c | 2005-08-25 16:25:54 -0700 | [diff] [blame] | 1253 | if (port <= xprt_min_resvport) |
| 1254 | port = xprt_max_resvport; |
| 1255 | else |
| 1256 | port--; |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 1257 | } while (err == -EADDRINUSE && port != transport->port); |
Chuck Lever | 7dc753f | 2007-08-06 11:57:28 -0400 | [diff] [blame] | 1258 | dprintk("RPC: %s "NIPQUAD_FMT":%u: %s (%d)\n", |
| 1259 | __FUNCTION__, NIPQUAD(myaddr.sin_addr), |
| 1260 | port, err ? "failed" : "ok", err); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1261 | return err; |
| 1262 | } |
| 1263 | |
Chuck Lever | 90058d3 | 2007-08-06 11:57:33 -0400 | [diff] [blame] | 1264 | static int xs_bind6(struct sock_xprt *transport, struct socket *sock) |
| 1265 | { |
| 1266 | struct sockaddr_in6 myaddr = { |
| 1267 | .sin6_family = AF_INET6, |
| 1268 | }; |
| 1269 | struct sockaddr_in6 *sa; |
| 1270 | int err; |
| 1271 | unsigned short port = transport->port; |
| 1272 | |
| 1273 | if (!transport->xprt.resvport) |
| 1274 | port = 0; |
| 1275 | sa = (struct sockaddr_in6 *)&transport->addr; |
| 1276 | myaddr.sin6_addr = sa->sin6_addr; |
| 1277 | do { |
| 1278 | myaddr.sin6_port = htons(port); |
| 1279 | err = kernel_bind(sock, (struct sockaddr *) &myaddr, |
| 1280 | sizeof(myaddr)); |
| 1281 | if (!transport->xprt.resvport) |
| 1282 | break; |
| 1283 | if (err == 0) { |
| 1284 | transport->port = port; |
| 1285 | break; |
| 1286 | } |
| 1287 | if (port <= xprt_min_resvport) |
| 1288 | port = xprt_max_resvport; |
| 1289 | else |
| 1290 | port--; |
| 1291 | } while (err == -EADDRINUSE && port != transport->port); |
| 1292 | dprintk("RPC: xs_bind6 "NIP6_FMT":%u: %s (%d)\n", |
| 1293 | NIP6(myaddr.sin6_addr), port, err ? "failed" : "ok", err); |
| 1294 | return err; |
| 1295 | } |
| 1296 | |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1297 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 1298 | static struct lock_class_key xs_key[2]; |
| 1299 | static struct lock_class_key xs_slock_key[2]; |
| 1300 | |
| 1301 | static inline void xs_reclassify_socket(struct socket *sock) |
| 1302 | { |
| 1303 | struct sock *sk = sock->sk; |
| 1304 | BUG_ON(sk->sk_lock.owner != NULL); |
| 1305 | switch (sk->sk_family) { |
| 1306 | case AF_INET: |
| 1307 | sock_lock_init_class_and_name(sk, "slock-AF_INET-NFS", |
| 1308 | &xs_slock_key[0], "sk_lock-AF_INET-NFS", &xs_key[0]); |
| 1309 | break; |
| 1310 | |
| 1311 | case AF_INET6: |
| 1312 | sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFS", |
| 1313 | &xs_slock_key[1], "sk_lock-AF_INET6-NFS", &xs_key[1]); |
| 1314 | break; |
| 1315 | |
| 1316 | default: |
| 1317 | BUG(); |
| 1318 | } |
| 1319 | } |
| 1320 | #else |
| 1321 | static inline void xs_reclassify_socket(struct socket *sock) |
| 1322 | { |
| 1323 | } |
| 1324 | #endif |
| 1325 | |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1326 | static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) |
| 1327 | { |
| 1328 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 1329 | |
| 1330 | if (!transport->inet) { |
| 1331 | struct sock *sk = sock->sk; |
| 1332 | |
| 1333 | write_lock_bh(&sk->sk_callback_lock); |
| 1334 | |
| 1335 | sk->sk_user_data = xprt; |
| 1336 | transport->old_data_ready = sk->sk_data_ready; |
| 1337 | transport->old_state_change = sk->sk_state_change; |
| 1338 | transport->old_write_space = sk->sk_write_space; |
| 1339 | sk->sk_data_ready = xs_udp_data_ready; |
| 1340 | sk->sk_write_space = xs_udp_write_space; |
| 1341 | sk->sk_no_check = UDP_CSUM_NORCV; |
| 1342 | sk->sk_allocation = GFP_ATOMIC; |
| 1343 | |
| 1344 | xprt_set_connected(xprt); |
| 1345 | |
| 1346 | /* Reset to new socket */ |
| 1347 | transport->sock = sock; |
| 1348 | transport->inet = sk; |
| 1349 | |
| 1350 | write_unlock_bh(&sk->sk_callback_lock); |
| 1351 | } |
| 1352 | xs_udp_do_set_buffer_size(xprt); |
| 1353 | } |
| 1354 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1355 | /** |
Chuck Lever | 9c3d72d | 2007-08-06 11:57:43 -0400 | [diff] [blame] | 1356 | * xs_udp_connect_worker4 - set up a UDP socket |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 1357 | * @work: RPC transport to connect |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1358 | * |
| 1359 | * Invoked by a work queue tasklet. |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1360 | */ |
Chuck Lever | 9c3d72d | 2007-08-06 11:57:43 -0400 | [diff] [blame] | 1361 | static void xs_udp_connect_worker4(struct work_struct *work) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1362 | { |
Trond Myklebust | 34161db | 2006-12-07 15:48:15 -0500 | [diff] [blame] | 1363 | struct sock_xprt *transport = |
| 1364 | container_of(work, struct sock_xprt, connect_worker.work); |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 1365 | struct rpc_xprt *xprt = &transport->xprt; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 1366 | struct socket *sock = transport->sock; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1367 | int err, status = -EIO; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1368 | |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 1369 | if (xprt->shutdown || !xprt_bound(xprt)) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1370 | goto out; |
| 1371 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1372 | /* Start by resetting any existing state */ |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1373 | xs_close(xprt); |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1374 | |
| 1375 | if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1376 | dprintk("RPC: can't create UDP transport socket (%d).\n", -err); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1377 | goto out; |
| 1378 | } |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1379 | xs_reclassify_socket(sock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1380 | |
Chuck Lever | 7dc753f | 2007-08-06 11:57:28 -0400 | [diff] [blame] | 1381 | if (xs_bind4(transport, sock)) { |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1382 | sock_release(sock); |
| 1383 | goto out; |
| 1384 | } |
| 1385 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1386 | dprintk("RPC: worker connecting xprt %p to address: %s\n", |
Chuck Lever | 7559c7a | 2006-12-05 16:35:37 -0500 | [diff] [blame] | 1387 | xprt, xprt->address_strings[RPC_DISPLAY_ALL]); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 1388 | |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1389 | xs_udp_finish_connecting(xprt, sock); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1390 | status = 0; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1391 | out: |
| 1392 | xprt_wake_pending_tasks(xprt, status); |
| 1393 | xprt_clear_connecting(xprt); |
| 1394 | } |
| 1395 | |
Chuck Lever | 68e220b | 2007-08-06 11:57:48 -0400 | [diff] [blame^] | 1396 | /** |
| 1397 | * xs_udp_connect_worker6 - set up a UDP socket |
| 1398 | * @work: RPC transport to connect |
| 1399 | * |
| 1400 | * Invoked by a work queue tasklet. |
| 1401 | */ |
| 1402 | static void xs_udp_connect_worker6(struct work_struct *work) |
| 1403 | { |
| 1404 | struct sock_xprt *transport = |
| 1405 | container_of(work, struct sock_xprt, connect_worker.work); |
| 1406 | struct rpc_xprt *xprt = &transport->xprt; |
| 1407 | struct socket *sock = transport->sock; |
| 1408 | int err, status = -EIO; |
| 1409 | |
| 1410 | if (xprt->shutdown || !xprt_bound(xprt)) |
| 1411 | goto out; |
| 1412 | |
| 1413 | /* Start by resetting any existing state */ |
| 1414 | xs_close(xprt); |
| 1415 | |
| 1416 | if ((err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) { |
| 1417 | dprintk("RPC: can't create UDP transport socket (%d).\n", -err); |
| 1418 | goto out; |
| 1419 | } |
| 1420 | xs_reclassify_socket(sock); |
| 1421 | |
| 1422 | if (xs_bind6(transport, sock) < 0) { |
| 1423 | sock_release(sock); |
| 1424 | goto out; |
| 1425 | } |
| 1426 | |
| 1427 | dprintk("RPC: worker connecting xprt %p to address: %s\n", |
| 1428 | xprt, xprt->address_strings[RPC_DISPLAY_ALL]); |
| 1429 | |
| 1430 | xs_udp_finish_connecting(xprt, sock); |
| 1431 | status = 0; |
| 1432 | out: |
| 1433 | xprt_wake_pending_tasks(xprt, status); |
| 1434 | xprt_clear_connecting(xprt); |
| 1435 | } |
| 1436 | |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1437 | /* |
| 1438 | * We need to preserve the port number so the reply cache on the server can |
| 1439 | * find our cached RPC replies when we get around to reconnecting. |
| 1440 | */ |
| 1441 | static void xs_tcp_reuse_connection(struct rpc_xprt *xprt) |
| 1442 | { |
| 1443 | int result; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 1444 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1445 | struct sockaddr any; |
| 1446 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1447 | dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt); |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1448 | |
| 1449 | /* |
| 1450 | * Disconnect the transport socket by doing a connect operation |
| 1451 | * with AF_UNSPEC. This should return immediately... |
| 1452 | */ |
| 1453 | memset(&any, 0, sizeof(any)); |
| 1454 | any.sa_family = AF_UNSPEC; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 1455 | result = kernel_connect(transport->sock, &any, sizeof(any), 0); |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1456 | if (result) |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1457 | dprintk("RPC: AF_UNSPEC connect return code %d\n", |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1458 | result); |
| 1459 | } |
| 1460 | |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1461 | 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] | 1462 | { |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1463 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 1464 | |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 1465 | if (!transport->inet) { |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1466 | struct sock *sk = sock->sk; |
| 1467 | |
| 1468 | write_lock_bh(&sk->sk_callback_lock); |
| 1469 | |
| 1470 | sk->sk_user_data = xprt; |
Chuck Lever | 314dfd7 | 2006-12-05 16:35:34 -0500 | [diff] [blame] | 1471 | transport->old_data_ready = sk->sk_data_ready; |
| 1472 | transport->old_state_change = sk->sk_state_change; |
| 1473 | transport->old_write_space = sk->sk_write_space; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1474 | sk->sk_data_ready = xs_tcp_data_ready; |
| 1475 | sk->sk_state_change = xs_tcp_state_change; |
| 1476 | sk->sk_write_space = xs_tcp_write_space; |
Trond Myklebust | b079fa7b | 2005-12-13 16:13:52 -0500 | [diff] [blame] | 1477 | sk->sk_allocation = GFP_ATOMIC; |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1478 | |
| 1479 | /* socket options */ |
| 1480 | sk->sk_userlocks |= SOCK_BINDPORT_LOCK; |
| 1481 | sock_reset_flag(sk, SOCK_LINGER); |
| 1482 | tcp_sk(sk)->linger2 = 0; |
| 1483 | tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1484 | |
| 1485 | xprt_clear_connected(xprt); |
| 1486 | |
| 1487 | /* Reset to new socket */ |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 1488 | transport->sock = sock; |
| 1489 | transport->inet = sk; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1490 | |
| 1491 | write_unlock_bh(&sk->sk_callback_lock); |
| 1492 | } |
| 1493 | |
| 1494 | /* Tell the socket layer to start connecting... */ |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 1495 | xprt->stat.connect_count++; |
| 1496 | xprt->stat.connect_start = jiffies; |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1497 | return kernel_connect(sock, (struct sockaddr *) &xprt->addr, |
Chuck Lever | c4efcb1 | 2006-08-22 20:06:19 -0400 | [diff] [blame] | 1498 | xprt->addrlen, O_NONBLOCK); |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | /** |
Chuck Lever | 9c3d72d | 2007-08-06 11:57:43 -0400 | [diff] [blame] | 1502 | * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1503 | * @work: RPC transport to connect |
| 1504 | * |
| 1505 | * Invoked by a work queue tasklet. |
| 1506 | */ |
Chuck Lever | 9c3d72d | 2007-08-06 11:57:43 -0400 | [diff] [blame] | 1507 | static void xs_tcp_connect_worker4(struct work_struct *work) |
Chuck Lever | 16be2d2 | 2007-08-06 11:57:38 -0400 | [diff] [blame] | 1508 | { |
| 1509 | struct sock_xprt *transport = |
| 1510 | container_of(work, struct sock_xprt, connect_worker.work); |
| 1511 | struct rpc_xprt *xprt = &transport->xprt; |
| 1512 | struct socket *sock = transport->sock; |
| 1513 | int err, status = -EIO; |
| 1514 | |
| 1515 | if (xprt->shutdown || !xprt_bound(xprt)) |
| 1516 | goto out; |
| 1517 | |
| 1518 | if (!sock) { |
| 1519 | /* start from scratch */ |
| 1520 | if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) { |
| 1521 | dprintk("RPC: can't create TCP transport socket (%d).\n", -err); |
| 1522 | goto out; |
| 1523 | } |
| 1524 | xs_reclassify_socket(sock); |
| 1525 | |
| 1526 | if (xs_bind4(transport, sock) < 0) { |
| 1527 | sock_release(sock); |
| 1528 | goto out; |
| 1529 | } |
| 1530 | } else |
| 1531 | /* "close" the socket, preserving the local port */ |
| 1532 | xs_tcp_reuse_connection(xprt); |
| 1533 | |
| 1534 | dprintk("RPC: worker connecting xprt %p to address: %s\n", |
| 1535 | xprt, xprt->address_strings[RPC_DISPLAY_ALL]); |
| 1536 | |
| 1537 | status = xs_tcp_finish_connecting(xprt, sock); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1538 | dprintk("RPC: %p connect status %d connected %d sock state %d\n", |
| 1539 | xprt, -status, xprt_connected(xprt), |
| 1540 | sock->sk->sk_state); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1541 | if (status < 0) { |
| 1542 | switch (status) { |
| 1543 | case -EINPROGRESS: |
| 1544 | case -EALREADY: |
| 1545 | goto out_clear; |
Chuck Lever | 3167e12 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1546 | case -ECONNREFUSED: |
| 1547 | case -ECONNRESET: |
| 1548 | /* retry with existing socket, after a delay */ |
| 1549 | break; |
| 1550 | default: |
| 1551 | /* get rid of existing socket, and retry */ |
| 1552 | xs_close(xprt); |
| 1553 | break; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1554 | } |
| 1555 | } |
| 1556 | out: |
Chuck Lever | 44fbac2 | 2005-08-11 16:25:44 -0400 | [diff] [blame] | 1557 | xprt_wake_pending_tasks(xprt, status); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1558 | out_clear: |
Chuck Lever | 2226feb | 2005-08-11 16:25:38 -0400 | [diff] [blame] | 1559 | xprt_clear_connecting(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1560 | } |
| 1561 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1562 | /** |
Chuck Lever | 68e220b | 2007-08-06 11:57:48 -0400 | [diff] [blame^] | 1563 | * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint |
| 1564 | * @work: RPC transport to connect |
| 1565 | * |
| 1566 | * Invoked by a work queue tasklet. |
| 1567 | */ |
| 1568 | static void xs_tcp_connect_worker6(struct work_struct *work) |
| 1569 | { |
| 1570 | struct sock_xprt *transport = |
| 1571 | container_of(work, struct sock_xprt, connect_worker.work); |
| 1572 | struct rpc_xprt *xprt = &transport->xprt; |
| 1573 | struct socket *sock = transport->sock; |
| 1574 | int err, status = -EIO; |
| 1575 | |
| 1576 | if (xprt->shutdown || !xprt_bound(xprt)) |
| 1577 | goto out; |
| 1578 | |
| 1579 | if (!sock) { |
| 1580 | /* start from scratch */ |
| 1581 | if ((err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) { |
| 1582 | dprintk("RPC: can't create TCP transport socket (%d).\n", -err); |
| 1583 | goto out; |
| 1584 | } |
| 1585 | xs_reclassify_socket(sock); |
| 1586 | |
| 1587 | if (xs_bind6(transport, sock) < 0) { |
| 1588 | sock_release(sock); |
| 1589 | goto out; |
| 1590 | } |
| 1591 | } else |
| 1592 | /* "close" the socket, preserving the local port */ |
| 1593 | xs_tcp_reuse_connection(xprt); |
| 1594 | |
| 1595 | dprintk("RPC: worker connecting xprt %p to address: %s\n", |
| 1596 | xprt, xprt->address_strings[RPC_DISPLAY_ALL]); |
| 1597 | |
| 1598 | status = xs_tcp_finish_connecting(xprt, sock); |
| 1599 | dprintk("RPC: %p connect status %d connected %d sock state %d\n", |
| 1600 | xprt, -status, xprt_connected(xprt), sock->sk->sk_state); |
| 1601 | if (status < 0) { |
| 1602 | switch (status) { |
| 1603 | case -EINPROGRESS: |
| 1604 | case -EALREADY: |
| 1605 | goto out_clear; |
| 1606 | case -ECONNREFUSED: |
| 1607 | case -ECONNRESET: |
| 1608 | /* retry with existing socket, after a delay */ |
| 1609 | break; |
| 1610 | default: |
| 1611 | /* get rid of existing socket, and retry */ |
| 1612 | xs_close(xprt); |
| 1613 | break; |
| 1614 | } |
| 1615 | } |
| 1616 | out: |
| 1617 | xprt_wake_pending_tasks(xprt, status); |
| 1618 | out_clear: |
| 1619 | xprt_clear_connecting(xprt); |
| 1620 | } |
| 1621 | |
| 1622 | /** |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1623 | * xs_connect - connect a socket to a remote endpoint |
| 1624 | * @task: address of RPC task that manages state of connect request |
| 1625 | * |
| 1626 | * TCP: If the remote end dropped the connection, delay reconnecting. |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1627 | * |
| 1628 | * UDP socket connects are synchronous, but we use a work queue anyway |
| 1629 | * to guarantee that even unprivileged user processes can set up a |
| 1630 | * socket on a privileged port. |
| 1631 | * |
| 1632 | * If a UDP socket connect fails, the delay behavior here prevents |
| 1633 | * retry floods (hard mounts). |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1634 | */ |
| 1635 | static void xs_connect(struct rpc_task *task) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1636 | { |
| 1637 | struct rpc_xprt *xprt = task->tk_xprt; |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 1638 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1639 | |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1640 | if (xprt_test_and_set_connecting(xprt)) |
| 1641 | return; |
| 1642 | |
Chuck Lever | ee0ac0c | 2006-12-05 16:35:15 -0500 | [diff] [blame] | 1643 | if (transport->sock != NULL) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1644 | dprintk("RPC: xs_connect delayed xprt %p for %lu " |
| 1645 | "seconds\n", |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1646 | xprt, xprt->reestablish_timeout / HZ); |
Trond Myklebust | c1384c9 | 2007-06-14 18:00:42 -0400 | [diff] [blame] | 1647 | queue_delayed_work(rpciod_workqueue, |
| 1648 | &transport->connect_worker, |
| 1649 | xprt->reestablish_timeout); |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1650 | xprt->reestablish_timeout <<= 1; |
| 1651 | if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO) |
| 1652 | xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO; |
Chuck Lever | b0d93ad | 2005-08-11 16:25:53 -0400 | [diff] [blame] | 1653 | } else { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1654 | dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); |
Trond Myklebust | c1384c9 | 2007-06-14 18:00:42 -0400 | [diff] [blame] | 1655 | queue_delayed_work(rpciod_workqueue, |
| 1656 | &transport->connect_worker, 0); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1657 | } |
| 1658 | } |
| 1659 | |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 1660 | /** |
| 1661 | * xs_udp_print_stats - display UDP socket-specifc stats |
| 1662 | * @xprt: rpc_xprt struct containing statistics |
| 1663 | * @seq: output file |
| 1664 | * |
| 1665 | */ |
| 1666 | static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) |
| 1667 | { |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 1668 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
| 1669 | |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 1670 | seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n", |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 1671 | transport->port, |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 1672 | xprt->stat.bind_count, |
| 1673 | xprt->stat.sends, |
| 1674 | xprt->stat.recvs, |
| 1675 | xprt->stat.bad_xids, |
| 1676 | xprt->stat.req_u, |
| 1677 | xprt->stat.bklog_u); |
| 1678 | } |
| 1679 | |
| 1680 | /** |
| 1681 | * xs_tcp_print_stats - display TCP socket-specifc stats |
| 1682 | * @xprt: rpc_xprt struct containing statistics |
| 1683 | * @seq: output file |
| 1684 | * |
| 1685 | */ |
| 1686 | static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) |
| 1687 | { |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 1688 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 1689 | long idle_time = 0; |
| 1690 | |
| 1691 | if (xprt_connected(xprt)) |
| 1692 | idle_time = (long)(jiffies - xprt->last_used) / HZ; |
| 1693 | |
| 1694 | seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n", |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 1695 | transport->port, |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 1696 | xprt->stat.bind_count, |
| 1697 | xprt->stat.connect_count, |
| 1698 | xprt->stat.connect_time, |
| 1699 | idle_time, |
| 1700 | xprt->stat.sends, |
| 1701 | xprt->stat.recvs, |
| 1702 | xprt->stat.bad_xids, |
| 1703 | xprt->stat.req_u, |
| 1704 | xprt->stat.bklog_u); |
| 1705 | } |
| 1706 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1707 | static struct rpc_xprt_ops xs_udp_ops = { |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1708 | .set_buffer_size = xs_udp_set_buffer_size, |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 1709 | .reserve_xprt = xprt_reserve_xprt_cong, |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 1710 | .release_xprt = xprt_release_xprt_cong, |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 1711 | .rpcbind = rpcb_getport_async, |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame] | 1712 | .set_port = xs_set_port, |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1713 | .connect = xs_connect, |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 1714 | .buf_alloc = rpc_malloc, |
| 1715 | .buf_free = rpc_free, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1716 | .send_request = xs_udp_send_request, |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 1717 | .set_retrans_timeout = xprt_set_retrans_timeout_rtt, |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame] | 1718 | .timer = xs_udp_timer, |
Chuck Lever | a58dd39 | 2005-08-25 16:25:53 -0700 | [diff] [blame] | 1719 | .release_request = xprt_release_rqst_cong, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1720 | .close = xs_close, |
| 1721 | .destroy = xs_destroy, |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 1722 | .print_stats = xs_udp_print_stats, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1723 | }; |
| 1724 | |
| 1725 | static struct rpc_xprt_ops xs_tcp_ops = { |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 1726 | .reserve_xprt = xprt_reserve_xprt, |
Trond Myklebust | e0ab53d | 2006-07-27 17:22:50 -0400 | [diff] [blame] | 1727 | .release_xprt = xs_tcp_release_xprt, |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 1728 | .rpcbind = rpcb_getport_async, |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame] | 1729 | .set_port = xs_set_port, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1730 | .connect = xs_connect, |
Chuck Lever | 0210714 | 2006-01-03 09:55:49 +0100 | [diff] [blame] | 1731 | .buf_alloc = rpc_malloc, |
| 1732 | .buf_free = rpc_free, |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1733 | .send_request = xs_tcp_send_request, |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 1734 | .set_retrans_timeout = xprt_set_retrans_timeout_def, |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1735 | .close = xs_close, |
| 1736 | .destroy = xs_destroy, |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 1737 | .print_stats = xs_tcp_print_stats, |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1738 | }; |
| 1739 | |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 1740 | static struct rpc_xprt *xs_setup_xprt(struct rpc_xprtsock_create *args, unsigned int slot_table_size) |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 1741 | { |
| 1742 | struct rpc_xprt *xprt; |
Chuck Lever | ffc2e51 | 2006-12-05 16:35:11 -0500 | [diff] [blame] | 1743 | struct sock_xprt *new; |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 1744 | |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 1745 | if (args->addrlen > sizeof(xprt->addr)) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1746 | dprintk("RPC: xs_setup_xprt: address too large\n"); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 1747 | return ERR_PTR(-EBADF); |
| 1748 | } |
| 1749 | |
Chuck Lever | ffc2e51 | 2006-12-05 16:35:11 -0500 | [diff] [blame] | 1750 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
| 1751 | if (new == NULL) { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1752 | dprintk("RPC: xs_setup_xprt: couldn't allocate " |
| 1753 | "rpc_xprt\n"); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 1754 | return ERR_PTR(-ENOMEM); |
| 1755 | } |
Chuck Lever | ffc2e51 | 2006-12-05 16:35:11 -0500 | [diff] [blame] | 1756 | xprt = &new->xprt; |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 1757 | |
| 1758 | xprt->max_reqs = slot_table_size; |
| 1759 | xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL); |
| 1760 | if (xprt->slot == NULL) { |
| 1761 | kfree(xprt); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1762 | dprintk("RPC: xs_setup_xprt: couldn't allocate slot " |
| 1763 | "table\n"); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 1764 | return ERR_PTR(-ENOMEM); |
| 1765 | } |
| 1766 | |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 1767 | memcpy(&xprt->addr, args->dstaddr, args->addrlen); |
| 1768 | xprt->addrlen = args->addrlen; |
Frank van Maarseveen | d3bc9a1 | 2007-07-09 22:23:35 +0200 | [diff] [blame] | 1769 | if (args->srcaddr) |
| 1770 | memcpy(&new->addr, args->srcaddr, args->addrlen); |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 1771 | new->port = xs_get_random_port(); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 1772 | |
| 1773 | return xprt; |
| 1774 | } |
| 1775 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1776 | /** |
| 1777 | * xs_setup_udp - Set up transport to use a UDP socket |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 1778 | * @args: rpc transport creation arguments |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1779 | * |
| 1780 | */ |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 1781 | struct rpc_xprt *xs_setup_udp(struct rpc_xprtsock_create *args) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1782 | { |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 1783 | struct rpc_xprt *xprt; |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 1784 | struct sock_xprt *transport; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1785 | |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 1786 | xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 1787 | if (IS_ERR(xprt)) |
| 1788 | return xprt; |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 1789 | transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1790 | |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 1791 | if (ntohs(((struct sockaddr_in *)args->dstaddr)->sin_port) != 0) |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 1792 | xprt_set_bound(xprt); |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 1793 | |
| 1794 | xprt->prot = IPPROTO_UDP; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1795 | xprt->tsh_size = 0; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1796 | /* XXX: header size can vary due to auth type, IPv6, etc. */ |
| 1797 | xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); |
| 1798 | |
Chuck Lever | 9c3d72d | 2007-08-06 11:57:43 -0400 | [diff] [blame] | 1799 | INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_connect_worker4); |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1800 | xprt->bind_timeout = XS_BIND_TO; |
| 1801 | xprt->connect_timeout = XS_UDP_CONN_TO; |
| 1802 | xprt->reestablish_timeout = XS_UDP_REEST_TO; |
| 1803 | xprt->idle_timeout = XS_IDLE_DISC_TO; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1804 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1805 | xprt->ops = &xs_udp_ops; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1806 | |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 1807 | if (args->timeout) |
| 1808 | xprt->timeout = *args->timeout; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1809 | else |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1810 | xprt_set_timeout(&xprt->timeout, 5, 5 * HZ); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1811 | |
Chuck Lever | ba10f2c | 2007-08-06 11:57:07 -0400 | [diff] [blame] | 1812 | xs_format_ipv4_peer_addresses(xprt); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1813 | dprintk("RPC: set up transport to address %s\n", |
Chuck Lever | 7559c7a | 2006-12-05 16:35:37 -0500 | [diff] [blame] | 1814 | xprt->address_strings[RPC_DISPLAY_ALL]); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 1815 | |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 1816 | return xprt; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1817 | } |
| 1818 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1819 | /** |
| 1820 | * xs_setup_tcp - Set up transport to use a TCP socket |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 1821 | * @args: rpc transport creation arguments |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1822 | * |
| 1823 | */ |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 1824 | struct rpc_xprt *xs_setup_tcp(struct rpc_xprtsock_create *args) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1825 | { |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 1826 | struct rpc_xprt *xprt; |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 1827 | struct sock_xprt *transport; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1828 | |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 1829 | xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries); |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 1830 | if (IS_ERR(xprt)) |
| 1831 | return xprt; |
Chuck Lever | c847546 | 2006-12-05 16:35:26 -0500 | [diff] [blame] | 1832 | transport = container_of(xprt, struct sock_xprt, xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1833 | |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 1834 | if (ntohs(((struct sockaddr_in *)args->dstaddr)->sin_port) != 0) |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 1835 | xprt_set_bound(xprt); |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 1836 | |
| 1837 | xprt->prot = IPPROTO_TCP; |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1838 | xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32); |
Chuck Lever | 808012f | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 1839 | xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1840 | |
Chuck Lever | 9c3d72d | 2007-08-06 11:57:43 -0400 | [diff] [blame] | 1841 | INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4); |
Chuck Lever | 03bf4b7 | 2005-08-25 16:25:55 -0700 | [diff] [blame] | 1842 | xprt->bind_timeout = XS_BIND_TO; |
| 1843 | xprt->connect_timeout = XS_TCP_CONN_TO; |
| 1844 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; |
| 1845 | xprt->idle_timeout = XS_IDLE_DISC_TO; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1846 | |
Chuck Lever | 262965f | 2005-08-11 16:25:56 -0400 | [diff] [blame] | 1847 | xprt->ops = &xs_tcp_ops; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1848 | |
Frank van Maarseveen | 96802a0 | 2007-07-08 13:08:54 +0200 | [diff] [blame] | 1849 | if (args->timeout) |
| 1850 | xprt->timeout = *args->timeout; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1851 | else |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 1852 | xprt_set_timeout(&xprt->timeout, 2, 60 * HZ); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1853 | |
Chuck Lever | ba10f2c | 2007-08-06 11:57:07 -0400 | [diff] [blame] | 1854 | xs_format_ipv4_peer_addresses(xprt); |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 1855 | dprintk("RPC: set up transport to address %s\n", |
Chuck Lever | 7559c7a | 2006-12-05 16:35:37 -0500 | [diff] [blame] | 1856 | xprt->address_strings[RPC_DISPLAY_ALL]); |
Chuck Lever | edb267a | 2006-08-22 20:06:18 -0400 | [diff] [blame] | 1857 | |
Chuck Lever | c8541ec | 2006-10-17 14:44:27 -0400 | [diff] [blame] | 1858 | return xprt; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 1859 | } |
Chuck Lever | 282b32e | 2006-12-05 16:35:51 -0500 | [diff] [blame] | 1860 | |
| 1861 | /** |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 1862 | * init_socket_xprt - set up xprtsock's sysctls |
Chuck Lever | 282b32e | 2006-12-05 16:35:51 -0500 | [diff] [blame] | 1863 | * |
| 1864 | */ |
| 1865 | int init_socket_xprt(void) |
| 1866 | { |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 1867 | #ifdef RPC_DEBUG |
Eric W. Biederman | 2b1bec5 | 2007-02-14 00:33:24 -0800 | [diff] [blame] | 1868 | if (!sunrpc_table_header) |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 1869 | sunrpc_table_header = register_sysctl_table(sunrpc_table); |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 1870 | #endif |
| 1871 | |
Chuck Lever | 282b32e | 2006-12-05 16:35:51 -0500 | [diff] [blame] | 1872 | return 0; |
| 1873 | } |
| 1874 | |
| 1875 | /** |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 1876 | * cleanup_socket_xprt - remove xprtsock's sysctls |
Chuck Lever | 282b32e | 2006-12-05 16:35:51 -0500 | [diff] [blame] | 1877 | * |
| 1878 | */ |
| 1879 | void cleanup_socket_xprt(void) |
| 1880 | { |
Chuck Lever | fbf7668 | 2006-12-05 16:35:54 -0500 | [diff] [blame] | 1881 | #ifdef RPC_DEBUG |
| 1882 | if (sunrpc_table_header) { |
| 1883 | unregister_sysctl_table(sunrpc_table_header); |
| 1884 | sunrpc_table_header = NULL; |
| 1885 | } |
| 1886 | #endif |
Chuck Lever | 282b32e | 2006-12-05 16:35:51 -0500 | [diff] [blame] | 1887 | } |