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