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