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