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