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